Spaces:
Build error
Build error
Commit
·
b913cef
1
Parent(s):
49b52c9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
model_name = "nlpconnect/vit-gpt2-image-captioning"
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
|
11 |
+
def generate_caption(image):
|
12 |
+
image = image.convert("RGB")
|
13 |
+
|
14 |
+
image = image.resize((224, 224))
|
15 |
+
|
16 |
+
inputs = tokenizer("Image caption: ", return_tensors="pt", max_length=30, truncation=True)
|
17 |
+
|
18 |
+
with st.spinner("Generating caption..."):
|
19 |
+
caption_ids = model.generate(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"])
|
20 |
+
|
21 |
+
generated_caption = tokenizer.decode(caption_ids[0], skip_special_tokens=True)
|
22 |
+
|
23 |
+
return generated_caption
|
24 |
+
|
25 |
+
def main():
|
26 |
+
st.title("Image Captioning App")
|
27 |
+
|
28 |
+
with st.form("my_form"):
|
29 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
30 |
+
|
31 |
+
if uploaded_file is not None:
|
32 |
+
image = Image.open(uploaded_file)
|
33 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
34 |
+
|
35 |
+
clicked = st.form_submit_button("Generate Caption")
|
36 |
+
|
37 |
+
if clicked and uploaded_file is not None:
|
38 |
+
caption = generate_caption(image)
|
39 |
+
st.success("Generated Caption:")
|
40 |
+
st.write(caption)
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
main()
|