Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
10 |
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
11 |
|
12 |
-
|
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 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
st.
|
44 |
-
|
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 |
+
|
|
|
|
|
|
|
|
|
|
|
|