Anon4445 commited on
Commit
6b714ed
1 Parent(s): dc6d2d7

add app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import ViltProcessor, ViltForQuestionAnswering
3
+ from PIL import Image
4
+
5
+ def load_model():
6
+ processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
7
+ model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
8
+ return processor, model
9
+
10
+ def predict(image, text, processor, model):
11
+ encoding = processor(image, text, return_tensors="pt")
12
+ outputs = model(**encoding)
13
+ logits = outputs.logits
14
+ idx = logits.argmax(-1).item()
15
+ return model.config.id2label[idx]
16
+
17
+ def main():
18
+ st.title("VQA")
19
+ st.write("Upload an image and input a question to get an answer.")
20
+
21
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
22
+
23
+ if uploaded_image is not None:
24
+ image = Image.open(uploaded_image)
25
+ question = st.text_input("Question about the image:")
26
+
27
+ if question:
28
+ processor, model = load_model()
29
+ answer = predict(image, question, processor, model)
30
+
31
+ col1, col2 = st.columns(2)
32
+ with col1:
33
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
34
+ with col2:
35
+ st.write(f"**Question:** {question}")
36
+ st.write(f"**Answer:** {answer}")
37
+
38
+ if __name__ == "__main__":
39
+ main()