Views
No views yet
pip install transformers peft pillow torch1from transformers import AutoModelForImageTextToText, AutoProcessor
2from peft import PeftModel
3from PIL import Image
4
5# Load base model
6base_model_id = "google/medgemma-4b-it"
7model = AutoModelForImageTextToText.from_pretrained(
8 base_model_id,
9 torch_dtype="auto",
10 device_map="auto"
11)
12
13# Load LoRA adapter
14model = PeftModel.from_pretrained(
15 model,
16 "convaiinnovations/medgemma-4b-ecginstruct-lora"
17)
18
19# Load processor
20processor = AutoProcessor.from_pretrained(base_model_id)1# Load ECG image
2image = Image.open("ecg_image.png").convert("RGB")
3
4# Prepare prompt
5messages = [
6 {
7 "role": "user",
8 "content": [
9 {"type": "image"},
10 {"type": "text", "text": "Analyze this ECG and provide a detailed interpretation."}
11 ]
12 }
13]
14
15# Process input
16text = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
17inputs = processor(text=[text], images=[[image]], return_tensors="pt", padding=True)
18inputs = {k: v.to(model.device) for k, v in inputs.items()}
19
20# Generate
21outputs = model.generate(
22 **inputs,
23 max_new_tokens=512,
24 do_sample=False,
25 temperature=None,
26 top_p=None
27)
28
29# Decode
30response = processor.decode(outputs[0], skip_special_tokens=True)
31print(response)1@misc{medgemma-ecginstruct-lora,
2 author = {convaiinnovations},
3 title = {MedGemma-4B ECGInstruct LoRA},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/convaiinnovations/medgemma-4b-ecginstruct-lora}}
7}