Anniek commited on
Commit
e2e136b
·
1 Parent(s): 1a49186

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -1,8 +1,35 @@
 
 
 
1
 
2
- from transformers import pipeline
 
3
 
4
- image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- image_to_text("https://ankur3107.github.io/assets/images/image-captioning-example.png")
 
 
 
7
 
8
- # [{'generated_text': 'a soccer game with a player jumping to catch the ball '}]
 
 
 
 
 
 
 
 
 
 
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()