File size: 7,246 Bytes
a555984
 
 
bddc701
 
 
 
 
 
 
 
a555984
 
 
 
58d8272
a555984
 
 
 
 
4995b64
 
 
 
 
 
 
 
570407e
4995b64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a555984
74446e5
9291534
74446e5
 
a555984
74446e5
 
 
a555984
74446e5
 
a555984
74446e5
 
a555984
74446e5
 
a555984
74446e5
60853e0
74446e5
 
 
 
 
 
 
a555984
74446e5
 
a555984
74446e5
 
 
 
 
 
 
 
 
a555984
74446e5
 
 
 
 
 
a555984
74446e5
 
a555984
74446e5
 
a555984
74446e5
 
9291534
a555984
7b627b6
 
 
b5d88b0
7b627b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da5928d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b627b6
a555984
 
1c7bb7b
8fa17aa
 
1c7bb7b
e0311f6
8fa17aa
 
1c7bb7b
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---
base_model: unsloth/phi-4-unsloth-bnb-4bit
library_name: peft
license: mit
datasets:
- bigbio/pubmed_qa
language:
- en
pipeline_tag: question-answering
tags:
- medicalQA
---

# Model Card for Model ID

The Phi-4 Medical QA Model is a fine-tuned version of the "unsloth/phi-4" language model, optimized for answering medical questions with clarity and detail. Leveraging the unsloth's advanced quantization techniques, this model provides accurate, context-aware responses tailored to the healthcare domain. It is particularly suited for applications like patient assistance, medical education, and evidence-based healthcare support.



## Model Details

The Phi-4 Medical QA Model builds upon the robust foundation provided by the "unsloth/phi-4" pre-trained language model. Fine-tuned on the PubMedQA dataset, it is specifically designed to answer complex medical questions by integrating domain-specific knowledge and language understanding capabilities.

The model employs several advanced techniques:

LoRA Fine-Tuning: Low-Rank Adaptation (LoRA) enhances parameter efficiency, allowing domain adaptation with minimal compute.

4-Bit Quantization: Memory usage is significantly reduced, making the model deployable on resource-constrained systems.

Gradient Checkpointing: Further optimizations for handling long sequences and reducing GPU memory usage.

The model is trained using the SFTTrainer library from the trl package, with parameters optimized for accuracy and resource efficiency.

Model Architecture

Base Model: "unsloth/phi-4"

Tokenization: Custom tokenizer from the unsloth framework

Fine-Tuning Techniques:

Targeted modules: q_proj, k_proj, v_proj, and others

LoRA Rank: 16

LoRA Alpha: 16

Dropout: 0 (optimized for this use case)

Training Dataset: PubMedQA (labeled fold0 source)

Hardware Used: NVIDIA A100 GPUs


## How to Use
```python
# Install required libraries
!pip install unsloth peft bitsandbytes accelerate transformers

# Import necessary modules
from transformers import AutoTokenizer
from unsloth import FastLanguageModel

# Define the MedQA prompt
medqa_prompt = """You are a medical QA system. Answer the following medical question clearly and in detail with complete sentences.

### Question:
{}

### Answer:
"""

# Load the model and tokenizer using unsloth
model_name = "Vijayendra/Phi4-MedQA" 
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name=model_name,
    max_seq_length=2048,
    dtype=None,  # Use default precision
    load_in_4bit=True,  # Enable 4-bit quantization
    device_map="auto"  # Automatically map model to available devices
)

# Enable faster inference
FastLanguageModel.for_inference(model)

# Prepare the medical question
medical_question = "What are the common symptoms of diabetes?"  # Replace with your medical question
inputs = tokenizer(
    [medqa_prompt.format(medical_question)],
    return_tensors="pt",
    padding=True,
    truncation=True,
    max_length=1024
).to("cuda")  # Ensure inputs are on the GPU

# Generate the output
outputs = model.generate(
    **inputs,
    max_new_tokens=512,  # Allow for detailed responses
    use_cache=True  # Speeds up generation
)

# Decode and clean the response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)

# Extract and print the generated answer
answer_text = response.split("### Answer:")[1].strip() if "### Answer:" in response else response.strip()

print(f"Question: {medical_question}")
print(f"Answer: {answer_text}")
```

### Model Output:

Question: What are the common symptoms of diabetes?

Answer: Diabetes is a chronic disease that affects the way the body processes blood sugar (glucose). There are two main types of diabetes: type 1 and type 2. Type 1 diabetes is an autoimmune disease in which the body's immune system attacks and destroys the insulin-producing cells in the pancreas. Type 2 diabetes is a metabolic disorder in which the body does not produce enough insulin or the body's cells do not respond properly to insulin. Both types of diabetes can lead to serious health complications if not properly managed.

The most common symptoms of diabetes include:

1. Increased thirst: People with diabetes may feel thirsty all the time, even after drinking plenty of fluids. This is because the body is trying to flush out excess sugar in the blood.

2. Frequent urination: People with diabetes may need to urinate more often than usual, even during the night. This is because the body is trying to get rid of excess sugar in the blood.

3. Increased hunger: People with diabetes may feel hungry all the time, even after eating a full meal. This is because the body is not able to use the sugar in the blood for energy.

4. Fatigue: People with diabetes may feel tired all the time, even after getting a good night's sleep. This is because the body is not able to use the sugar in the blood for energy.

5. Blurred vision: People with diabetes may have trouble seeing clearly, especially at night. This is because high blood sugar levels can damage the blood vessels in the eyes.

6. Slow healing of wounds: People with diabetes may have trouble healing from cuts, scrapes, and other wounds. This is because high blood sugar levels can damage the blood vessels and nerves in the skin.

7. Tingling or numbness in the hands or feet: People with diabetes may experience tingling or numbness in the hands or feet. This is because high blood sugar levels can damage the nerves in the body.

8. Unintended weight loss: People with diabetes may lose weight without trying. This is because the body is not able to use the sugar in the blood for energy and is instead breaking down muscle and fat for energy.

It is important to note that not everyone with diabetes will experience all of these symptoms. Some people may not experience any symptoms at all, especially in the early stages of the disease. If you are experiencing any of these symptoms, it is important to see a doctor for a proper diagnosis and treatment. Early diagnosis and treatment can help prevent serious health complications associated

### Intended Use
This model is intended for:

Answering medical and healthcare-related questions.

Supporting healthcare professionals and students with evidence-based insights.

Enhancing patient care via interactive QA systems.

### Limitations

Domain Restriction: The model performs best on medical questions and may not generalize well to other domains.

Bias and Fairness: The model inherits biases from the PubMedQA dataset.

Hallucination Risks: As with all large language models, responses should be validated by professionals before application in critical scenarios.

### Framework versions

- PEFT 0.14.0
- 
### Credit and Citation

This model is built upon and extends the "unsloth/phi-4" model. Special thanks to the UnsLOTH team for enabling efficient training and inference.

For more information about the "unsloth/phi-4" model:
@article{unsloth_phi4,
  title={Phi-4: A Scalable Model for Long-Sequence Tasks},
  author={UnsLOTH Team and Microsoft},
  year={2025},
  publisher={UnsLOTH},
}

@article{vijayendra_phi4_finetune,
  title={Phi-4 Fine-Tuned for Medical QA},
  author={Vijayendra Dwari},
  year={2025},
  publisher={Hugging Face},
}