Views
No views yet
1# you will need to adjust code if you didnt use peft
2
3from PIL import Image
4from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor
5import torch
6import requests
7from peft import PeftModel
8
9base_model_id = BASE_MODEL_ID
10peft_model_id = THIS_MODEL_ID
11max_new_tokens = 100
12text = "Whats on the flower?"
13img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/bee.JPG?download=true"
14image = Image.open(requests.get(img_url, stream=True).raw)
15
16device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17base_model = PaliGemmaForConditionalGeneration.from_pretrained(base_model_id)
18processor = PaliGemmaProcessor.from_pretrained(base_model_id)
19
20model = PeftModel.from_pretrained(base_model, peft_model_id)
21model.merge_and_unload()
22
23model = model.eval().to(device)
24
25inputs = processor(text=text, images=image, return_tensors="pt").to(device)
26with torch.inference_mode():
27 generated_ids = model.generate(
28 **inputs,
29 max_new_tokens=max_new_tokens,
30 do_sample=False,
31 )
32result = processor.batch_decode(generated_ids, skip_special_tokens=True)
33print(result)