Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import ViltProcessor, ViltForVisualQuestionAnswering
|
3 |
+
import torch
|
4 |
+
|
5 |
+
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
6 |
+
model = ViltForVisualQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
7 |
+
|
8 |
+
def answer_question(image, text):
|
9 |
+
encoding = processor(image, text, return_tensors="pt")
|
10 |
+
|
11 |
+
# forward pass
|
12 |
+
with torch.no_grad():
|
13 |
+
outputs = model(**encoding)
|
14 |
+
|
15 |
+
logits = outputs.logits
|
16 |
+
idx = logits.argmax(-1).item()
|
17 |
+
predicted_answer = model.config.id2label[idx])
|
18 |
+
|
19 |
+
return predicted_answer
|
20 |
+
|
21 |
+
image = gr.inputs.Image(type="pil")
|
22 |
+
question = gr.inputs.Textbox(label="Question")
|
23 |
+
answer = gr.outputs.Textbox(label="Predicted answer")
|
24 |
+
gr.Interface(fn=classify_image, inputs=[image, question], outputs=answer, enable_queue=True).launch(debug=True)
|