Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load the image-to-text model pipeline
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_pipeline():
|
| 8 |
+
return pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
| 9 |
+
|
| 10 |
+
pipe = load_pipeline()
|
| 11 |
+
|
| 12 |
+
# Streamlit app title
|
| 13 |
+
st.title("🖼️ Image Caption Generator using AI")
|
| 14 |
+
st.write("Upload an image, and the model will generate a descriptive caption.")
|
| 15 |
+
|
| 16 |
+
# File upload section
|
| 17 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 18 |
+
|
| 19 |
+
if uploaded_file:
|
| 20 |
+
# Display the uploaded image
|
| 21 |
+
image = Image.open(uploaded_file)
|
| 22 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 23 |
+
|
| 24 |
+
# Generate a caption for the image
|
| 25 |
+
st.write("Generating caption...")
|
| 26 |
+
caption = pipe(image)
|
| 27 |
+
|
| 28 |
+
# Display the generated caption
|
| 29 |
+
if caption:
|
| 30 |
+
st.subheader("Generated Caption:")
|
| 31 |
+
st.write(caption[0]['generated_text'])
|
| 32 |
+
else:
|
| 33 |
+
st.write("Couldn't generate a caption. Please try a different image.")
|
| 34 |
+
|
| 35 |
+
# Footer information
|
| 36 |
+
st.markdown("---")
|
| 37 |
+
st.write("Created with 🤗 Transformers and Streamlit")
|