Views
No views yet
"<image>Task: Analyze this dermatological image. Is this Leprosy? Answer 'Yes' or 'No'."1import torch
2from PIL import Image
3from transformers import AutoProcessor, AutoModelForImageTextToText
4from peft import PeftModel
5
6# 1. Load Model and Processor
7model_id = "google/paligemma-3b-mix-224"
8adapter_id = "barqawiz/paligemma-leprosy-detector-v1"
9
10processor = AutoProcessor.from_pretrained(model_id)
11base_model = AutoModelForImageTextToText.from_pretrained(
12 model_id,
13 torch_dtype=torch.float16,
14 device_map="auto",
15 quantization_config={"load_in_4bit": True}
16)
17model = PeftModel.from_pretrained(base_model, adapter_id)
18
19# 2. Prepare Input
20image = Image.open("lesion_sample.jpg").convert("RGB")
21prompt = "<image>Task: Analyze this dermatological image. Is this Leprosy? Answer 'Yes' or 'No'."
22
23# Note: PaliGemma processor uses the 'suffix' parameter for training,
24# but for inference, we pass the text as 'text'
25inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
26
27# 3. Generate
28with torch.no_grad():
29 output = model.generate(**inputs, max_new_tokens=15)
30 result = processor.decode(output[0], skip_special_tokens=True)
31 # Strip prompt to get answer
32 clean_result = result.replace(prompt.replace("<image>", ""), "").strip()
33 print(f"Is it Leprosy? {clean_result}")