Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,32 +4,53 @@ from PIL import Image
|
|
4 |
import requests
|
5 |
from io import BytesIO
|
6 |
|
7 |
-
#
|
8 |
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
9 |
|
10 |
-
st.title('
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
image = Image.open(uploaded_file)
|
18 |
-
st.image(image, caption='Uploaded Image', use_column_width=True)
|
19 |
-
|
20 |
-
# Save image to a buffer
|
21 |
-
buf = BytesIO()
|
22 |
-
image.save(buf, format='JPEG')
|
23 |
-
byte_img = buf.getvalue()
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import requests
|
5 |
from io import BytesIO
|
6 |
|
7 |
+
# Initialize the pipeline
|
8 |
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
9 |
|
10 |
+
st.title('Image Captioning Application')
|
11 |
|
12 |
+
# Function to load images from URL
|
13 |
+
def load_image_from_url(url):
|
14 |
+
try:
|
15 |
+
response = requests.get(url)
|
16 |
+
img = Image.open(BytesIO(response.content))
|
17 |
+
return img
|
18 |
+
except Exception as e:
|
19 |
+
st.error(f"Error loading image from URL: {e}")
|
20 |
+
return None
|
21 |
|
22 |
+
# User option to select input type: Upload or URL
|
23 |
+
input_type = st.radio("Select input type:", ("Upload Image", "Image URL"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
if input_type == "Upload Image":
|
26 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
27 |
+
if uploaded_file is not None:
|
28 |
+
image = Image.open(uploaded_file)
|
29 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
30 |
+
elif input_type == "Image URL":
|
31 |
+
image_url = st.text_input("Enter the image URL here:", "")
|
32 |
+
if image_url:
|
33 |
+
image = load_image_from_url(image_url)
|
34 |
+
if image:
|
35 |
+
st.image(image, caption='Image from URL', use_column_width=True)
|
36 |
|
37 |
+
# Generate caption button
|
38 |
+
if st.button('Generate Caption'):
|
39 |
+
if not image:
|
40 |
+
st.warning("Please upload an image or enter an image URL.")
|
41 |
+
else:
|
42 |
+
with st.spinner("Generating caption..."):
|
43 |
+
# Process the image and generate caption
|
44 |
+
if input_type == "Upload Image":
|
45 |
+
# Save the uploaded image to a temporary file to pass its path to the model
|
46 |
+
with open("temp_image.jpg", "wb") as f:
|
47 |
+
f.write(uploaded_file.getbuffer())
|
48 |
+
result = image_to_text("temp_image.jpg")
|
49 |
+
elif input_type == "Image URL" and image_url:
|
50 |
+
result = image_to_text(image_url)
|
51 |
+
|
52 |
+
if result:
|
53 |
+
generated_text = result[0]['generated_text']
|
54 |
+
st.success(f'Generated Caption: {generated_text}')
|
55 |
+
else:
|
56 |
+
st.error("Failed to generate caption.")
|