Views
No views yet
pip install transformers accelerate bitsandbytes>0.37.01from transformers import AutoProcessor, AutoModelForVision2Seq
2from huggingface_hub import hf_hub_download
3import torch
4
5device = "cuda" if torch.cuda.is_available() else "cpu"
6
7
8model = AutoModelForVision2Seq.from_pretrained('hassenhamdi/granite-vision-3.1-2b-preview-8bit', trust_remote_code=True).to(device)
9tokenizer = AutoProcessor.from_pretrained('ibm-granite/granite-vision-3.1-2b-preview')
10
11
12# prepare image and text prompt, using the appropriate prompt template
13
14img_path = hf_hub_download(repo_id=model_path, filename='example.png')
15
16conversation = [
17 {
18 "role": "user",
19 "content": [
20 {"type": "image", "url": img_path},
21 {"type": "text", "text": "What is the highest scoring model on ChartQA and what is its score?"},
22 ],
23 },
24]
25inputs = processor.apply_chat_template(
26 conversation,
27 add_generation_prompt=True,
28 tokenize=True,
29 return_dict=True,
30 return_tensors="pt"
31).to(device)
32
33
34# autoregressively complete prompt
35output = model.generate(**inputs, max_new_tokens=100)
36print(processor.decode(output[0], skip_special_tokens=True))