Update app.py
Browse files
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 |
-
|
| 14 |
-
|
|
|
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
# forward pass
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 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 |
|