Upload app.py
#13
by Neryvert - opened
app.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import ViltProcessor, ViltForQuestionAnswering
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# prepare image + question
|
| 6 |
+
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 7 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 8 |
+
text = "How many cats are there?"
|
| 9 |
+
|
| 10 |
+
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 11 |
+
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 12 |
+
|
| 13 |
+
# prepare inputs
|
| 14 |
+
encoding = processor(image, text, return_tensors="pt")
|
| 15 |
+
|
| 16 |
+
# forward pass
|
| 17 |
+
outputs = model(**encoding)
|
| 18 |
+
logits = outputs.logits
|
| 19 |
+
idx = logits.argmax(-1).item()
|
| 20 |
+
print("Predicted answer:", model.config.id2label[idx])
|