Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,56 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
import streamlit as st
|
|
|
|
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
-
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
|
|
|
|
|
8 |
|
|
|
9 |
text_to_image = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
|
10 |
-
# Initialize the pipeline
|
11 |
-
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
st.
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
if
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
generated_text = result[0]['generated_text']
|
57 |
-
st.success(f'Generated Caption: {generated_text}')
|
58 |
-
else:
|
59 |
-
st.error("Failed to generate caption.")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
from PIL import Image
|
5 |
import requests
|
6 |
+
import io
|
|
|
7 |
|
8 |
+
# Load the image-to-text pipeline
|
9 |
+
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
10 |
|
11 |
+
# Load the text-to-image model
|
12 |
text_to_image = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
|
|
|
|
|
13 |
|
14 |
+
def main():
|
15 |
+
st.title("Image to Story to Image Converter")
|
16 |
+
|
17 |
+
# User input for text or URL
|
18 |
+
input_option = st.radio("Select input option:", ("Text", "URL"))
|
19 |
+
|
20 |
+
# Input text
|
21 |
+
if input_option == "Text":
|
22 |
+
text_input = st.text_input("Enter the text:")
|
23 |
+
if st.button("Generate Story and Image") and text_input:
|
24 |
+
generate_image(text_input)
|
25 |
+
|
26 |
+
# Input URL
|
27 |
+
elif input_option == "URL":
|
28 |
+
uploaded_file = st.file_uploader("Upload an image file:", type=["jpg", "jpeg", "png"])
|
29 |
+
if uploaded_file is not None:
|
30 |
+
image = Image.open(uploaded_file)
|
31 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
32 |
+
if st.button("Generate Story and Image"):
|
33 |
+
image_text = image_to_text_from_file(uploaded_file)
|
34 |
+
generate_image(image_text)
|
35 |
+
else:
|
36 |
+
image_url = st.text_input("Enter the image URL:")
|
37 |
+
if st.button("Generate Story and Image") and image_url:
|
38 |
+
image_text = image_to_text_from_url(image_url)
|
39 |
+
generate_image(image_text)
|
40 |
+
|
41 |
+
def image_to_text_from_file(uploaded_file):
|
42 |
+
image_bytes = io.BytesIO(uploaded_file.read())
|
43 |
+
return image_to_text(image_bytes)[0]['generated_text']
|
44 |
+
|
45 |
+
def image_to_text_from_url(image_url):
|
46 |
+
response = requests.get(image_url)
|
47 |
+
image_bytes = io.BytesIO(response.content)
|
48 |
+
return image_to_text(image_bytes)[0]['generated_text']
|
49 |
+
|
50 |
+
def generate_image(text):
|
51 |
+
rephrased_text = "I want to buy " + text + " and [MASK] for my children"
|
52 |
+
generated_image = text_to_image(rephrased_text)
|
53 |
+
st.image(generated_image, caption="Generated Image", use_column_width=True)
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
main()
|
|
|
|
|
|
|
|