-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_starter.py
42 lines (30 loc) · 1.31 KB
/
model_starter.py
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
37
38
39
40
41
42
from transformers import ViltProcessor, ViltForQuestionAnswering
import requests
from PIL import Image
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
def model_pipeline(text: str, image: Image):
# prepare inputs
encoding = processor(image, text, return_tensors="pt")
# forward pass
outputs = model(**encoding)
logits = outputs.logits
idx = logits.argmax(-1).item()
return model.config.id2label[idx]
# this part is just printing the result to the console
# from transformers import ViltProcessor, ViltForQuestionAnswering
# import requests
# from PIL import Image
# # prepare image + question
# url = "https://unsplash.com/photos/gray-and-black-mallard-ducks-flying-during-day-time-WPmPsdX2ySw"
# image = Image.open(requests.get(url, stream=True).raw)
# text = "What's the animal doing?"
# processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
# model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
# # prepare inputs
# encoding = processor(image, text, return_tensors="pt")
# # forward pass
# outputs = model(**encoding)
# logits = outputs.logits
# idx = logits.argmax(-1).item()
# print("Predicted answer:", model.config.id2label[idx])