Views
No views yet
Disclaimer: This model is for educational and research purposes only. Do not use it as a substitute for professional medical advice, diagnosis, or treatment. Always consult a qualified healthcare professional.
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5# Load base model with 4-bit quantization
6bnb_config = BitsAndBytesConfig(
7 load_in_4bit=True,
8 bnb_4bit_quant_type="nf4",
9 bnb_4bit_compute_dtype=torch.bfloat16,
10 bnb_4bit_use_double_quant=True,
11)
12
13base_model = AutoModelForCausalLM.from_pretrained(
14 "Qwen/Qwen2.5-1.5B-Instruct",
15 quantization_config=bnb_config,
16 device_map="auto",
17)
18
19# Load fine-tuned adapter
20model = PeftModel.from_pretrained(base_model, "Coddieharsh/ClinicalQwen-1.5B-Medical")
21tokenizer = AutoTokenizer.from_pretrained("Coddieharsh/ClinicalQwen-1.5B-Medical")
22
23# Generate response
24def ask(question):
25 prompt = (
26 "<|im_start|>system\n"
27 "You are a knowledgeable medical assistant. Provide accurate, evidence-based "
28 "medical information. Always recommend consulting a healthcare professional "
29 "for personal medical advice.<|im_end|>\n"
30 f"<|im_start|>user\n{question}<|im_end|>\n"
31 "<|im_start|>assistant\n"
32 )
33 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
34 with torch.no_grad():
35 outputs = model.generate(
36 **inputs,
37 max_new_tokens=512,
38 temperature=0.7,
39 do_sample=True,
40 top_p=0.9,
41 repetition_penalty=1.1,
42 pad_token_id=tokenizer.eos_token_id,
43 )
44 response = tokenizer.decode(outputs[0], skip_special_tokens=False)
45 return response.split("<|im_start|>assistant")[-1].split("<|im_end|>")[0].strip()
46
47print(ask("What are the symptoms of Type 2 diabetes?"))| Parameter | Value |
|---|---|
| Base Model | Qwen/Qwen2.5-1.5B-Instruct |
| Fine-tuning Method | QLoRA (4-bit NF4) |
| Dataset | lavita/medical-qa-datasets |
| Training Samples | 5,000 |
| Epochs | 1 |
| Final Loss | 1.8991 |
| Training Time | ~2.4 hours |
| Hardware | Google Colab T4 (16GB) |
| LoRA Rank (r) | 8 |
| LoRA Alpha | 16 |
| Learning Rate | 2e-4 |
| Batch Size (effective) | 16 (2 × 8 grad accum) |
| Optimizer | paged_adamw_8bit |
| Scheduler | Cosine |
| Max Sequence Length | 512 |
| Trainable Parameters | 9.2M / 1,552M (< 1%) |
q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj| Metric | Score | Note |
|---|---|---|
| BERTScore F1 | 0.5913 | Semantic similarity (primary metric) |
| Perplexity | 3.14 | Fluency — lower is better |
| Keyword Coverage | 86.0% | Medical term coverage |
| ROUGE-L | 0.1219 | Surface overlap (low = paraphrasing, expected) |
| BLEU | 0.0099 | N-gram precision (low = paraphrasing, expected) |