Spaces:
Build error
Build error
Commit
·
5a546be
1
Parent(s):
b1d6ac3
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageCaptioning
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
st.title("Image Captioning App")
|
8 |
+
|
9 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
10 |
+
model = AutoModelForImageCaptioning.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
11 |
+
|
12 |
+
def generate_caption(image):
|
13 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
14 |
+
caption = model.generate(**inputs)
|
15 |
+
return caption[0]
|
16 |
+
|
17 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
18 |
+
|
19 |
+
if uploaded_image:
|
20 |
+
image = Image.open(uploaded_image)
|
21 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
22 |
+
|
23 |
+
caption = generate_caption(image)
|
24 |
+
|
25 |
+
st.subheader("Generated Caption:")
|
26 |
+
st.write(caption)
|