Spaces:
Build error
Build error
Commit
·
f5a4803
1
Parent(s):
08458f0
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,38 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
3 |
from PIL import Image
|
4 |
-
import requests
|
5 |
-
from io import BytesIO
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
model =
|
11 |
|
|
|
12 |
def generate_caption(image):
|
13 |
-
inputs =
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoFeatureExtractor, AutoModelForSequenceClassification, AutoTokenizer
|
4 |
from PIL import Image
|
|
|
|
|
5 |
|
6 |
+
# Load the pretrained model and tokenizer
|
7 |
+
model_name = "nlpconnect/vit-gpt2-image-captioning"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
|
11 |
+
# Define a function to generate captions from an image
|
12 |
def generate_caption(image):
|
13 |
+
inputs = tokenizer(image, return_tensors="pt")
|
14 |
+
with torch.no_grad():
|
15 |
+
logits = model(**inputs).logits
|
16 |
+
caption = tokenizer.decode(logits.argmax(1)[0], skip_special_tokens=True)
|
17 |
+
return caption
|
18 |
|
19 |
+
def main():
|
20 |
+
st.title("Image to Text Captioning")
|
21 |
|
22 |
+
with st.form("my_form"):
|
23 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
|
|
24 |
|
25 |
+
if uploaded_file is not None:
|
26 |
+
# Display the uploaded image
|
27 |
+
image = Image.open(uploaded_file)
|
28 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
29 |
+
|
30 |
+
clicked = st.form_submit_button("Generate Caption")
|
31 |
+
if clicked:
|
32 |
+
if "image" in locals():
|
33 |
+
caption = generate_caption(image)
|
34 |
+
st.subheader("Generated Caption:")
|
35 |
+
st.write(caption)
|
36 |
|
37 |
+
if __name__ == "__main__":
|
38 |
+
main()
|