Spaces:
Sleeping
Sleeping
Create model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import ViltProcessor, ViltForQuestionAnswering
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
# 470MB
|
| 5 |
+
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 6 |
+
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def model_pipeline(text: str, image: Image):
|
| 10 |
+
# prepare inputs
|
| 11 |
+
encoding = processor(image, text, return_tensors="pt")
|
| 12 |
+
|
| 13 |
+
# forward pass
|
| 14 |
+
outputs = model(**encoding)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
idx = logits.argmax(-1).item()
|
| 17 |
+
|
| 18 |
+
return model.config.id2label[idx]
|