Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,35 @@
|
|
|
|
|
|
|
|
1 |
|
2 |
-
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import ViltProcessor, ViltForQuestionAnswering
|
3 |
+
import torch
|
4 |
|
5 |
+
torch.hub.download_url_to_file('https://cocodataset.org/#explore?id=531313', 'dog.jpg')
|
6 |
+
torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg', 'astronaut.jpg')
|
7 |
|
8 |
+
def getAnswer(query, image):
|
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 about the image")
|
23 |
+
answer = gr.outputs.Textbox(label="Predicted answer")
|
24 |
+
examples = [["dog.jpg", "Where is the dog lying?"], ["astronaut.jpg", "What's the astronaut riding on?"]]
|
25 |
|
26 |
+
|
27 |
+
title="Visual question and answering"
|
28 |
+
|
29 |
+
interface = gr.Interface(fn=getAnswer,
|
30 |
+
inputs=[image, question],
|
31 |
+
outputs=answer,
|
32 |
+
examples=examples,
|
33 |
+
title=title,
|
34 |
+
enable_queue=True)
|
35 |
+
iface.launch()
|