Views
No views yet
1import torch
2from transformers import AutoProcessor, AutoModelForCausalLM
3import os
4
5# 1. Define the path to your new, fully merged model directory
6model_directory = "medgemma-4b-it-merged"
7
8# --- Verification ---
9if not os.path.isdir(model_directory):
10 print(f"❌ Error: The directory '{model_directory}' was not found.")
11else:
12 # 2. Load both the model AND the processor from the same directory
13 print("Loading model and processor from the same self-contained directory...")
14 model = AutoModelForCausalLM.from_pretrained(
15 model_directory,
16 torch_dtype=torch.bfloat16,
17 device_map="auto"
18 )
19 processor = AutoProcessor.from_pretrained(model_directory)
20 processor.tokenizer.padding_side = "right"
21 print("✅ Model and processor loaded successfully.")
22
23 # 3. Prepare data for inference (no changes here)
24 patient_age = "58"
25 patient_sex = "female"
26 new_results = {
27 "WBC": "18.9", "RBC": "3.8", "HGB": "105", "HCT": "33", "PLT": "420",
28 "MCV": "87", "MCH": "28", "MPV": "11.0", "Ne %": "78", "LYM": "1.5",
29 "MON": "0.6", "EO": "0.3", "BA": "0.1", "İMM": "0.5", "ATL": "0",
30 "ESR": "55", "HGB/RBC": "27.6"
31 }
32
33 results_str = "\n".join([f"- {key}: {value}" for key, value in new_results.items()])
34 user_prompt = (
35 "Zəhmət olmasa, aşağıdakı pasiyent məlumatlarına və qan analizi nəticələrinə əsasən klinik rəy bildir.\n\n"
36 "### Pasiyent məlumatları\n"
37 f"- Pasiyentin yaşı: {patient_age}\n"
38 f"- Pasiyentin cinsi: {patient_sex}\n\n"
39 "### Qan Analizi nəticələri\n"
40 f"{results_str}"
41 )
42 messages = [{"role": "user", "content": [{"type": "text", "text": user_prompt}]}]
43 prompt = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
44
45 # 4. Run Inference (no changes here)
46 inputs = processor(text=prompt, return_tensors="pt").to(model.device)
47 generation_kwargs = {"max_new_tokens": 512, "do_sample": False}
48
49 print("\nGenerating feedback...")
50 with torch.no_grad():
51 outputs = model.generate(**inputs, **generation_kwargs)
52
53 response = processor.batch_decode(outputs, skip_special_tokens=True)
54 final_response = response[0].strip().split('<|assistant|>')[-1]
55
56 print("\n--- Generated Clinical Feedback ---")
57 print(final_response.strip())
58