Spaces:
Runtime error
Runtime error
Commit
•
229b554
1
Parent(s):
c97e6f9
Update app.py (#1)
Browse files- Update app.py (cf714c5126d7d6975fc8bb3f8d615099b4a20a97)
Co-authored-by: Jim Pang <jim33282007@users.noreply.huggingface.co>
app.py
CHANGED
@@ -20,7 +20,7 @@ def load_image_from_url(url):
|
|
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"])
|
@@ -33,11 +33,19 @@ elif input_type == "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 |
-
|
|
|
|
|
|
|
|
|
41 |
else:
|
42 |
with st.spinner("Generating caption..."):
|
43 |
# Process the image and generate caption
|
@@ -49,8 +57,8 @@ if st.button('Generate Caption'):
|
|
49 |
elif input_type == "Image URL" and image_url:
|
50 |
result = image_to_text(image_url)
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
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", "Text"))
|
24 |
|
25 |
if input_type == "Upload Image":
|
26 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
33 |
image = load_image_from_url(image_url)
|
34 |
if image:
|
35 |
st.image(image, caption='Image from URL', use_column_width=True)
|
36 |
+
elif input_type == "Text":
|
37 |
+
text_input = st.text_input("Enter text here:", "")
|
38 |
+
if text_input:
|
39 |
+
st.image(image, caption='Image from URL', use_column_width=True)
|
40 |
|
41 |
# Generate caption button
|
42 |
if st.button('Generate Caption'):
|
43 |
if not image:
|
44 |
+
if not text_input:
|
45 |
+
st.warning("Please upload an image, enter an image URL or input text")
|
46 |
+
else:
|
47 |
+
result = text_input
|
48 |
+
|
49 |
else:
|
50 |
with st.spinner("Generating caption..."):
|
51 |
# Process the image and generate caption
|
|
|
57 |
elif input_type == "Image URL" and image_url:
|
58 |
result = image_to_text(image_url)
|
59 |
|
60 |
+
if result:
|
61 |
+
generated_text = result[0]['generated_text']
|
62 |
+
st.success(f'Generated Caption: {generated_text}')
|
63 |
+
else:
|
64 |
+
st.error("Failed to generate caption.")
|