Rachel7 commited on
Commit
de2f27e
·
verified ·
1 Parent(s): 9dd2e06

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
2
+ model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
3
+ def answer_question(gallery):
4
+ answer_list = []
5
+ question_list = ['What color is in the image?',
6
+ 'What style is the image?',
7
+ 'What vibe is the image?',
8
+ 'What kind of product is the image?',
9
+ 'Is the product in the image personalizable?',
10
+ 'What material is the product in the image?',
11
+ 'What event is the product in the image made for?',
12
+ 'What font is in the image?']
13
+ for i,c in gallery:
14
+ for q in question_list:
15
+ encoding = processor(i, q, return_tensors="pt")
16
+ # forward pass
17
+ with torch.no_grad():
18
+ outputs = model(**encoding)
19
+
20
+ logits = outputs.logits
21
+ idx = logits.argmax(-1).item()
22
+ predicted_answer = model.config.id2label[idx]
23
+ answer_list.append(predicted_answer)
24
+ df = list_to_dataframe(answer_list)
25
+ return df
26
+ #now try it with gradio interface and upload gallery
27
+ with gr.Blocks() as demo:
28
+
29
+ gallery = gr.Gallery(label="Gallery",type='pil')
30
+ #question = gr.Textbox(label="question")
31
+
32
+ #name buttons to display
33
+ image_button = gr.Button("Submit")
34
+
35
+ answer = gr.Textbox(label='Predicted answer')
36
+ image_button.click(fn=answer_question, inputs=[gallery], outputs=[answer])
37
+
38
+ demo.launch()