File size: 1,313 Bytes
815c641
f9ccb9d
d81d39e
 
 
db282a4
b39cd40
d81d39e
 
2dc4350
d81d39e
4f0110a
7349566
4f0110a
5604e74
4f0110a
 
 
5604e74
4f0110a
 
7349566
4f0110a
5604e74
4f0110a
db282a4
0c5ba4f
d81d39e
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoProcessor, AutoModelForCausalLM
from huggingface_hub import hf_hub_download
from PIL import Image
from datasets import load_dataset
from torch.utils.data import DataLoader

processor = AutoProcessor.from_pretrained("microsoft/git-base-vqav2")
model = AutoModelForCausalLM.from_pretrained("microsoft/git-base-vqav2")

dataset=load_dataset("Multimodal-Fatima/OK-VQA_train")
Dataset({
    features:['image', 'answers','question'],
    num_rows: 200
})

{'image':Image(decode=True,id=None),
'answers':Sequence(feature=Value(dtype='int64',id=None),length=-1,id=None),
'question':Value(dtype='string',id=None)}

Dataset({
    features:['input_ids','attention_mask','pixel_values','pixel_mask','labels'],
    num_rows:200
})

file_path = hf_hub_download(repo_id=Dataset, repo_type="dataset")
image = Image.open(file_path).convert("RGB")

pixel_values = processor(images=image, return_tensors="pt").pixel_values

input_ids = processor(text=question, add_special_tokens=False).input_ids
input_ids = [processor.tokenizer.cls_token_id] + input_ids
input_ids = torch.tensor(input_ids).unsqueeze(0)

generated_ids = model.generate(pixel_values=pixel_values, input_ids=input_ids, max_length=50)
print(processor.batch_decode(generated_ids, skip_special_tokens=True))