Pranav4datasc commited on
Commit
a90d43c
1 Parent(s): 568e07f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +56 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import requests
4
+ from io import BytesIO
5
+ from transformers import ViltProcessor, ViltForQuestionAnswering
6
+
7
+ st.set_page_config(layout='wide',page_title='VQA')
8
+
9
+ #Vilt model
10
+ processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
11
+ model =ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
12
+
13
+ def get_answer(image,text):
14
+ try:
15
+ #load and process the image
16
+ img = Image.open(BytesIO(image)).convert('RGB')
17
+ encoding = processor(img,text,return_tensors="pt")
18
+
19
+ #forward pass
20
+ outputs = model(**encoding)
21
+ logits = outputs.logits
22
+ idx = logits.argmax(-1).item()
23
+ answer = model.config.id2label[idx]
24
+
25
+ return answer
26
+ except Exception as e:
27
+ return str(e)
28
+
29
+ st.title("Visual Question Answering App")
30
+ st.write("Update an image and enter qustion to get and answer")
31
+
32
+
33
+ col1,col2 = st.columns(2)
34
+
35
+ with col1:
36
+ uploaded_file = st.file_uploader("Upload your own image",type=['jpg','png','jpeg'])
37
+ st.image(uploaded_file,use_column_width=True)
38
+
39
+ with col2:
40
+ question = st.text_input("Question")
41
+
42
+ if uploaded_file and question is not None:
43
+ if st.button("Ask Question"):
44
+ image = Image.open(uploaded_file)
45
+ image_byte_array = BytesIO()
46
+ image.save(image_byte_array,format="JPEG")
47
+ image_bytes = image_byte_array.getvalue()
48
+
49
+
50
+ answer = get_answer(image_bytes,question)
51
+ #st.show(answer)
52
+ st.info("Your Question:" + question)
53
+ st.success("Answer:" + answer)
54
+
55
+
56
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ requests
4
+ pillow
5
+ fastapi
6
+ uvicorn
7
+ streamlit
8
+ python-multipart