Spaces:
Sleeping
Sleeping
Commit
•
21e804e
1
Parent(s):
84800d6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
from transformers import GPT2TokenizerFast, ViTImageProcessor, VisionEncoderDecoderModel
|
5 |
+
|
6 |
+
# load image captioning model and corresponding tokenizer and image processor
|
7 |
+
model = VisionEncoderDecoderModel.from_pretrained("jojo-ai-mst/image-vision-cap")
|
8 |
+
tokenizer = GPT2TokenizerFast.from_pretrained("jojo-ai-mst/image-vision-cap")
|
9 |
+
image_processor = ViTImageProcessor.from_pretrained("jojo-ai-mst/image-vision-cap")
|
10 |
+
|
11 |
+
|
12 |
+
def generate_caption(image):
|
13 |
+
image = Image.open(image)
|
14 |
+
pixel_values = image_processor(image, return_tensors="pt").pixel_values
|
15 |
+
|
16 |
+
# autoregressively generate caption (uses greedy decoding by default)
|
17 |
+
generated_ids = model.generate(pixel_values)
|
18 |
+
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
19 |
+
print(generated_text)
|
20 |
+
|
21 |
+
return generated_text
|
22 |
+
|
23 |
+
st.header("Welcome to Vision Caption Prototype",divider="rainbow")
|
24 |
+
|
25 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
26 |
+
|
27 |
+
st.markdown("""
|
28 |
+
<style>
|
29 |
+
.stButton button {
|
30 |
+
background-color: green;
|
31 |
+
color: white;
|
32 |
+
}
|
33 |
+
</style>
|
34 |
+
""", unsafe_allow_html=True)
|
35 |
+
|
36 |
+
st.divider()
|
37 |
+
|
38 |
+
if st.button("Get Answer"):
|
39 |
+
if uploaded_file is not None:
|
40 |
+
st.header("Result")
|
41 |
+
st.image(uploaded_file, caption=uploaded_file.name, use_column_width=True)
|
42 |
+
|
43 |
+
#answer = "The answer will be generated by AI"
|
44 |
+
|
45 |
+
caption = generate_caption(uploaded_file)
|
46 |
+
|
47 |
+
st.subheader(caption)
|
48 |
+
else:
|
49 |
+
st.write("Please upload an image and type a question.")
|