Spaces:
Runtime error
Runtime error
Commit
·
a7f5ca7
1
Parent(s):
3843517
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio
|
| 2 |
+
from transformers import ViltProcessor, ViltForQuestionAnswering
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 6 |
+
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def predict_answer(image, question):
|
| 10 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 11 |
+
encoding = processor(image, question, return_tensors="pt")
|
| 12 |
+
outputs = model(**encoding)
|
| 13 |
+
logits = outputs.logits
|
| 14 |
+
probs = logits.softmax(dim=-1)
|
| 15 |
+
sorted_probs, sorted_indices = probs[0].sort(descending=True)
|
| 16 |
+
answer_list = []
|
| 17 |
+
for i in range(5):
|
| 18 |
+
prob = sorted_probs[i].item()
|
| 19 |
+
if prob > 0.00:
|
| 20 |
+
idx = sorted_indices[i].item()
|
| 21 |
+
answer = model.config.id2label[idx]
|
| 22 |
+
answer_list.append(f"{answer}: {prob:.2%}")
|
| 23 |
+
return answer_list
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
inputs = [
|
| 27 |
+
gradio.components.Image(label="Image"),
|
| 28 |
+
gradio.components.Textbox(label="Question", placeholder="Enter your question here.")
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
outputs = [
|
| 32 |
+
gradio.components.Textbox(label="Answer 1"),
|
| 33 |
+
gradio.components.Textbox(label="Answer 2"),
|
| 34 |
+
gradio.components.Textbox(label="Answer 3"),
|
| 35 |
+
gradio.components.Textbox(label="Answer 4"),
|
| 36 |
+
gradio.components.Textbox(label="Answer 5")
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
title = "Visual Question Answering (vilt-b32-finetuned-vqa)"
|
| 40 |
+
|
| 41 |
+
gradio.Interface(fn=predict_answer, inputs=inputs, outputs=outputs, title=title, allow_flagging="never",
|
| 42 |
+
css="footer{display:none !important}").launch(share=True)
|