Rachel7 commited on
Commit
1c274c4
·
verified ·
1 Parent(s): 8b674bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -10,8 +10,9 @@ import pandas as pd
10
  processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
11
  model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
12
  def answer_question(gallery):
13
- answer_list = []
14
- question_list = ['What color is in the image?',
 
15
  'What style is the image?',
16
  'What vibe is the image?',
17
  'What kind of product is the image?',
@@ -19,19 +20,21 @@ def answer_question(gallery):
19
  'What material is the product in the image?',
20
  'What event is the product in the image made for?',
21
  'What font is in the image?']
22
- for i,c in gallery:
23
- for q in question_list:
24
- encoding = processor(i, q, return_tensors="pt")
25
  # forward pass
26
- with torch.no_grad():
27
- outputs = model(**encoding)
28
 
29
- logits = outputs.logits
30
- idx = logits.argmax(-1).item()
31
- predicted_answer = model.config.id2label[idx]
32
- answer_list.append(predicted_answer)
33
- df = list_to_dataframe(answer_list)
34
- return df
 
 
35
  #now try it with gradio interface and upload gallery
36
  with gr.Blocks() as demo:
37
 
 
10
  processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
11
  model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
12
  def answer_question(gallery):
13
+ try:
14
+ answer_list = []
15
+ question_list = ['What color is in the image?',
16
  'What style is the image?',
17
  'What vibe is the image?',
18
  'What kind of product is the image?',
 
20
  'What material is the product in the image?',
21
  'What event is the product in the image made for?',
22
  'What font is in the image?']
23
+ for i,c in gallery:
24
+ for q in question_list:
25
+ encoding = processor(i, q, return_tensors="pt")
26
  # forward pass
27
+ with torch.no_grad():
28
+ outputs = model(**encoding)
29
 
30
+ logits = outputs.logits
31
+ idx = logits.argmax(-1).item()
32
+ predicted_answer = model.config.id2label[idx]
33
+ answer_list.append(predicted_answer)
34
+ df = list_to_dataframe(answer_list)
35
+ return df
36
+ except Exception as e:
37
+ return f"An error occurred: {e}"
38
  #now try it with gradio interface and upload gallery
39
  with gr.Blocks() as demo:
40