AidenYan commited on
Commit
b5ee4bd
·
verified ·
1 Parent(s): c5f357c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -1,17 +1,18 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
  from diffusers import DiffusionPipeline
 
 
4
  from PIL import Image
5
  import requests
6
  from io import BytesIO
7
 
 
8
 
9
- # Load the image-to-text pipeline
10
  image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
11
 
12
- # Load the text-to-image model
13
- text_to_image = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
14
 
 
15
  def load_image_from_url(url):
16
  try:
17
  response = requests.get(url)
@@ -21,30 +22,39 @@ def load_image_from_url(url):
21
  st.error(f"Error loading image from URL: {e}")
22
  return None
23
 
24
- def main():
25
- st.title("Image to Story to Image Converter")
26
-
27
- # User input for image URL
28
- image_url = st.text_input("Enter the image URL:")
29
-
30
- # Process the image and display the generated image
31
- if st.button("Generate Story and Image"):
32
- if image_url:
33
- try:
34
- image = load_image_from_url(image_url)
35
- # Generate caption from image
36
- image_text = image_to_text(image)
37
- rephrased_text = "I want to buy " + image_text + " and [MASK] for my children"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Generate image from rephrased text
40
- generated_image = text_to_image(rephrased_text)
41
-
42
- # Display the generated image
43
- st.image(generated_image, caption='Generated Image')
44
- except Exception as e:
45
- st.error(f"Error processing image: {e}")
46
- else:
47
- st.warning("Please enter an image URL.")
48
-
49
- if __name__ == "__main__":
50
- main()
 
 
 
1
  from diffusers import DiffusionPipeline
2
+ from transformers import pipeline
3
+ import streamlit as st
4
  from PIL import Image
5
  import requests
6
  from io import BytesIO
7
 
8
+ text_to_image = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
9
 
10
+ # Initialize the pipeline
11
  image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
12
 
13
+ st.title('Image Captioning Application')
 
14
 
15
+ # Function to load images from URL
16
  def load_image_from_url(url):
17
  try:
18
  response = requests.get(url)
 
22
  st.error(f"Error loading image from URL: {e}")
23
  return None
24
 
25
+ # User option to select input type: Upload or URL
26
+ input_type = st.radio("Select input type:", ("Upload Image", "Image URL"))
27
+
28
+ if input_type == "Upload Image":
29
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
30
+ if uploaded_file is not None:
31
+ image = Image.open(uploaded_file)
32
+ st.image(image, caption='Uploaded Image', use_column_width=True)
33
+ elif input_type == "Image URL":
34
+ image_url = st.text_input("Enter the image URL here:", "")
35
+ if image_url:
36
+ image = load_image_from_url(image_url)
37
+ if image:
38
+ st.image(image, caption='Image from URL', use_column_width=True)
39
+
40
+ # Generate caption button
41
+ if st.button('Generate Caption'):
42
+ if not image:
43
+ st.warning("Please upload an image or enter an image URL.")
44
+ else:
45
+ with st.spinner("Generating caption..."):
46
+ # Process the image and generate caption
47
+ if input_type == "Upload Image":
48
+ # Save the uploaded image to a temporary file to pass its path to the model
49
+ with open("temp_image.jpg", "wb") as f:
50
+ f.write(uploaded_file.getbuffer())
51
+ result = image_to_text("temp_image.jpg")
52
+ elif input_type == "Image URL" and image_url:
53
+ result = image_to_text(image_url)
54
 
55
+ if result:
56
+ generated_text = result[0]['generated_text']
57
+ st.success(f'Generated Caption: {generated_text}')
58
+ else:
59
+ st.error("Failed to generate caption.")
60
+