Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,28 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Initialize the
|
| 5 |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 6 |
|
| 7 |
# Streamlit app title
|
| 8 |
-
st.title("Image
|
| 9 |
|
| 10 |
-
# Input for
|
| 11 |
-
image_url = st.text_input("Enter the URL of
|
| 12 |
|
| 13 |
-
#
|
| 14 |
if image_url:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
if st.button("Generate Caption"):
|
| 19 |
-
with st.spinner("Generating caption..."):
|
| 20 |
-
caption = captioner(image_url)
|
| 21 |
-
st.write("**Caption:**", caption[0]['generated_text'])
|
| 22 |
-
|
| 23 |
-
# Add some information about the app
|
| 24 |
-
st.write("""
|
| 25 |
-
This app uses a pre-trained model from the Hugging Face Transformers library to generate captions for images.
|
| 26 |
-
Enter an image URL above and click "Generate Caption" to see the result.
|
| 27 |
-
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Initialize the image captioning pipeline
|
| 5 |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 6 |
|
| 7 |
# Streamlit app title
|
| 8 |
+
st.title("Image to Text Captioning")
|
| 9 |
|
| 10 |
+
# Input for image URL
|
| 11 |
+
image_url = st.text_input("Enter the URL of the image:")
|
| 12 |
|
| 13 |
+
# If an image URL is provided
|
| 14 |
if image_url:
|
| 15 |
+
try:
|
| 16 |
+
# Display the image
|
| 17 |
+
st.image(image_url, caption="Provided Image", use_column_width=True)
|
| 18 |
+
|
| 19 |
+
# Generate the caption
|
| 20 |
+
caption = captioner(image_url)
|
| 21 |
+
|
| 22 |
+
# Display the caption
|
| 23 |
+
st.write("**Generated Caption:**")
|
| 24 |
+
st.write(caption[0]['generated_text'])
|
| 25 |
+
except Exception as e:
|
| 26 |
+
st.error(f"An error occurred: {e}")
|
| 27 |
|
| 28 |
+
# To run this app, save this code to a file (e.g., `app.py`) and run `streamlit run app.py` in your terminal.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|