File size: 1,234 Bytes
e2e136b
 
 
3f05b71
c09b2b3
e2e136b
3f05b71
e2e136b
 
 
 
 
 
 
 
 
 
 
 
3f05b71
e2e136b
 
 
c09b2b3
1a8bb86
e2e136b
 
 
1fdcfa0
e2e136b
 
 
 
 
6b4fe24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gradio as gr
from transformers import ViltProcessor, ViltForQuestionAnswering
import torch

torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg', 'astronaut.jpg')

def getAnswer(query, image):
    encoding = processor(image, text, return_tensors="pt")
    
    # forward pass
    with torch.no_grad():
     outputs = model(**encoding)
     
    logits = outputs.logits
    idx = logits.argmax(-1).item()
    predicted_answer = model.config.id2label[idx]
   
    return predicted_answer

image = gr.inputs.Image(type="pil")
question = gr.inputs.Textbox(label="Question about the image")
answer = gr.outputs.Textbox(label="Predicted answer")
examples = [["cats.jpg", "How many cats are there?"], ["astronaut.jpg", "What's the astronaut riding on?"]]


title="Visual question and answering"

iface = gr.Interface(fn=getAnswer, 
                         inputs=[image, question], 
                         outputs=answer, 
                         examples=examples, 
                         title=title,
                         enable_queue=True)
iface.launch(debug=True )