sabber commited on
Commit
9fd67db
Β·
verified Β·
1 Parent(s): c5f05c3

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +475 -0
README.md ADDED
@@ -0,0 +1,475 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ license: apache-2.0
5
+ library_name: peft
6
+ tags:
7
+ - medical
8
+ - healthcare
9
+ - question-answering
10
+ - conversational-ai
11
+ - medical-qa
12
+ - clinical-nlp
13
+ - lora
14
+ - medphi
15
+ - patient-education
16
+ base_model: microsoft/MediPhi-Instruct
17
+ datasets:
18
+ - private
19
+ pipeline_tag: text-generation
20
+ model-index:
21
+ - name: medphi-medical-qa-adapter
22
+ results:
23
+ - task:
24
+ type: question-answering
25
+ name: Medical Question Answering
26
+ dataset:
27
+ name: Medical Screening Dataset
28
+ type: custom
29
+ metrics:
30
+ - name: Training Loss
31
+ type: loss
32
+ value: 0.6441
33
+ - name: Validation Loss
34
+ type: loss
35
+ value: 0.6446
36
+ ---
37
+
38
+ # MediPhi Medical QA Adapter
39
+
40
+ This is a LoRA adapter fine-tuned on Microsoft's [MediPhi-Instruct](https://huggingface.co/microsoft/MediPhi-Instruct) for medical question-answering. The model is designed to provide comprehensive, accurate answers to questions about medical diseases, conditions, and health-related topics.
41
+
42
+ ## Model Description
43
+
44
+ - **Model Type:** LoRA Adapter for Causal Language Model
45
+ - **Base Model:** microsoft/MediPhi-Instruct (3.8B parameters)
46
+ - **Trainable Parameters:** 0.328% (12.5M parameters via LoRA)
47
+ - **Language:** English
48
+ - **Domain:** Medical/Healthcare
49
+ - **Task:** Question Answering, Conversational AI
50
+ - **License:** Apache 2.0
51
+
52
+ ### Model Purpose
53
+
54
+ This model serves as a medical assistant chatbot capable of answering user queries about medical conditions, diseases, symptoms, treatments, and genetic disorders. It has been fine-tuned on 16,406 medical Q&A pairs covering a wide range of health topics including rare genetic disorders and common medical conditions.
55
+
56
+ ## Key Features
57
+
58
+ - **Medical Domain Expertise:** Trained on diverse medical Q&A covering diseases and conditions
59
+ - **Comprehensive Responses:** Generates detailed explanations including definitions, causes, symptoms, and treatments
60
+ - **Step-by-Step Reasoning:** Employs structured thinking for medical information delivery
61
+ - **Efficient Fine-tuning:** Uses 4-bit quantization with LoRA for memory efficiency
62
+ - **Patient Education Focus:** Optimized for explaining complex medical concepts clearly
63
+
64
+ ## Training Data
65
+
66
+ ### Dataset Statistics
67
+
68
+ - **Total Q&A Pairs:** 16,406 medical question-answer pairs
69
+ - **Dataset Size:** 21 MB
70
+ - **Data Splits:**
71
+ - Train: 12,304 samples (75%)
72
+ - Validation: 2,051 samples (12.5%)
73
+ - Test: 2,051 samples (12.5%)
74
+
75
+ ### Data Coverage
76
+
77
+ The dataset covers a wide range of medical topics including:
78
+ - **Rare Genetic Disorders:** Tourette syndrome, Denys-Drash syndrome, etc.
79
+ - **Common Conditions:** Dry eye syndrome, immunodeficiency disorders
80
+ - **Medical Concepts:** Genetic inheritance patterns, diagnostic methods
81
+ - **Treatment Information:** Management strategies, preventive care
82
+
83
+ ### Data Format
84
+
85
+ ```python
86
+ {
87
+ "messages": [
88
+ {
89
+ "role": "system",
90
+ "content": "You are a knowledgeable medical assistant. Provide accurate information about medical conditions and diseases. Always think step by step."
91
+ },
92
+ {
93
+ "role": "user",
94
+ "content": "What is [medical condition]?"
95
+ },
96
+ {
97
+ "role": "assistant",
98
+ "content": "[Comprehensive medical explanation]"
99
+ }
100
+ ]
101
+ }
102
+ ```
103
+
104
+ ## Training Details
105
+
106
+ ### Training Configuration
107
+
108
+ - **Framework:** PyTorch with Hugging Face Transformers
109
+ - **Fine-tuning Method:** LoRA (Low-Rank Adaptation) with SFT (Supervised Fine-Tuning)
110
+ - **Quantization:** 4-bit NF4 with double quantization
111
+ - **Compute:** Single RTX 5090 GPU (16 vCPU, 141 GB RAM)
112
+ - **Training Time:** ~140 steps to convergence
113
+
114
+ ### LoRA Hyperparameters
115
+
116
+ ```python
117
+ {
118
+ "r": 8,
119
+ "lora_alpha": 32,
120
+ "target_modules": ["o_proj", "qkv_proj", "gate_up_proj", "down_proj"],
121
+ "lora_dropout": 0.05,
122
+ "bias": "none",
123
+ "task_type": "CAUSAL_LM"
124
+ }
125
+ ```
126
+
127
+ ### Training Hyperparameters
128
+
129
+ ```python
130
+ {
131
+ "num_train_epochs": 3,
132
+ "per_device_train_batch_size": 4,
133
+ "gradient_accumulation_steps": 8,
134
+ "learning_rate": 2e-4,
135
+ "lr_scheduler_type": "cosine",
136
+ "max_seq_length": 1024,
137
+ "optim": "adamw_torch",
138
+ "gradient_checkpointing": True,
139
+ "packing": True
140
+ }
141
+ ```
142
+
143
+ ### Quantization Configuration
144
+
145
+ ```python
146
+ {
147
+ "load_in_4bit": True,
148
+ "bnb_4bit_quant_type": "nf4",
149
+ "bnb_4bit_use_double_quant": True,
150
+ "bnb_4bit_compute_dtype": "bfloat16"
151
+ }
152
+ ```
153
+
154
+ ## Performance
155
+
156
+ ### Training Convergence
157
+
158
+ | Step | Training Loss | Validation Loss |
159
+ |------|---------------|-----------------|
160
+ | 20 | 1.1799 | 0.8300 |
161
+ | 40 | 0.7834 | 0.7168 |
162
+ | 60 | 0.7185 | 0.6892 |
163
+ | 80 | 0.6838 | 0.6710 |
164
+ | 100 | 0.6641 | 0.6592 |
165
+ | 120 | 0.6638 | 0.6515 |
166
+ | 140 | 0.6441 | 0.6446 |
167
+
168
+ **Key Observations:**
169
+ - Rapid convergence within 140 training steps
170
+ - Training and validation loss converged, indicating good generalization
171
+ - No significant overfitting observed
172
+
173
+ ### Qualitative Improvements
174
+
175
+ **Example 1 - Dry Eye Condition**
176
+
177
+ *Original Dataset Response:* Citations and contact information only
178
+
179
+ *Fine-tuned Model Response:* Comprehensive explanation covering:
180
+ - Definition and mechanism
181
+ - Environmental, aging, and medication-related causes
182
+ - Symptoms (gritty sensation, redness, blurred vision, light sensitivity)
183
+ - Treatment options (artificial tears, lifestyle modifications, medical interventions)
184
+
185
+ **Example 2 - Genetic Disorders**
186
+
187
+ *Original Dataset Response:* Basic definition of 3 types
188
+
189
+ *Fine-tuned Model Response:* Expanded information including:
190
+ - Inheritance patterns (autosomal dominant/recessive, X-linked, mitochondrial)
191
+ - Specific examples (cystic fibrosis, sickle cell disease, Huntington's disease, Down syndrome)
192
+ - Diagnostic methods and genetic testing
193
+ - Management strategies and treatment approaches
194
+ - Prevention through genetic counseling
195
+
196
+ ## Usage
197
+
198
+ ### Installation
199
+
200
+ ```bash
201
+ pip install torch transformers peft bitsandbytes accelerate
202
+ ```
203
+
204
+ ### Basic Usage
205
+
206
+ ```python
207
+ from peft import AutoPeftModelForCausalLM
208
+ from transformers import AutoTokenizer
209
+
210
+ # Load model and tokenizer
211
+ model = AutoPeftModelForCausalLM.from_pretrained(
212
+ "sabber/medphi-medical-qa-adapter",
213
+ torch_dtype="auto",
214
+ device_map="auto"
215
+ )
216
+ tokenizer = AutoTokenizer.from_pretrained("sabber/medphi-medical-qa-adapter")
217
+
218
+ # Prepare conversation
219
+ messages = [
220
+ {
221
+ "role": "system",
222
+ "content": "You are a knowledgeable medical assistant. Provide accurate information about medical conditions and diseases. Always think step by step."
223
+ },
224
+ {
225
+ "role": "user",
226
+ "content": "What is Type 2 Diabetes and what are its main symptoms?"
227
+ }
228
+ ]
229
+
230
+ # Tokenize and generate
231
+ inputs = tokenizer.apply_chat_template(
232
+ messages,
233
+ tokenize=True,
234
+ add_generation_prompt=True,
235
+ return_tensors="pt"
236
+ ).to(model.device)
237
+
238
+ outputs = model.generate(
239
+ inputs,
240
+ max_new_tokens=512,
241
+ temperature=0.7,
242
+ top_p=0.9,
243
+ do_sample=True,
244
+ pad_token_id=tokenizer.eos_token_id
245
+ )
246
+
247
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
248
+ print(response)
249
+ ```
250
+
251
+ ### Pipeline Usage
252
+
253
+ ```python
254
+ from transformers import pipeline
255
+
256
+ # Create conversational pipeline
257
+ pipe = pipeline(
258
+ "text-generation",
259
+ model=model,
260
+ tokenizer=tokenizer,
261
+ max_new_tokens=512,
262
+ temperature=0.7,
263
+ top_p=0.9,
264
+ do_sample=True
265
+ )
266
+
267
+ # Ask medical question
268
+ messages = [
269
+ {"role": "system", "content": "You are a knowledgeable medical assistant. Provide accurate information about medical conditions and diseases. Always think step by step."},
270
+ {"role": "user", "content": "What causes high blood pressure?"}
271
+ ]
272
+
273
+ result = pipe(messages)
274
+ print(result[0]['generated_text'][-1]['content'])
275
+ ```
276
+
277
+ ### Multi-Turn Conversation
278
+
279
+ ```python
280
+ conversation_history = [
281
+ {
282
+ "role": "system",
283
+ "content": "You are a knowledgeable medical assistant. Provide accurate information about medical conditions and diseases. Always think step by step."
284
+ }
285
+ ]
286
+
287
+ # First question
288
+ conversation_history.append({"role": "user", "content": "What is asthma?"})
289
+ response = generate_response(conversation_history)
290
+ conversation_history.append({"role": "assistant", "content": response})
291
+
292
+ # Follow-up question
293
+ conversation_history.append({"role": "user", "content": "What triggers asthma attacks?"})
294
+ response = generate_response(conversation_history)
295
+ print(response)
296
+ ```
297
+
298
+ ### Merging Adapter with Base Model
299
+
300
+ ```python
301
+ from peft import AutoPeftModelForCausalLM
302
+
303
+ # Load and merge
304
+ model = AutoPeftModelForCausalLM.from_pretrained(
305
+ "sabber/medphi-medical-qa-adapter",
306
+ torch_dtype="auto",
307
+ device_map="auto"
308
+ )
309
+ merged_model = model.merge_and_unload()
310
+
311
+ # Save merged model
312
+ merged_model.save_pretrained("medphi-medical-qa-merged")
313
+ tokenizer.save_pretrained("medphi-medical-qa-merged")
314
+ ```
315
+
316
+ ## System Prompt
317
+
318
+ The model uses the following system prompt for optimal performance:
319
+
320
+ ```
321
+ You are a knowledgeable medical assistant. Provide accurate information about
322
+ medical conditions and diseases. Always think step by step.
323
+ ```
324
+
325
+ This prompt encourages:
326
+ - **Structured reasoning:** Step-by-step explanations
327
+ - **Accuracy focus:** Emphasis on providing correct medical information
328
+ - **Comprehensive coverage:** Detailed responses covering multiple aspects
329
+
330
+ ## Limitations and Bias
331
+
332
+ ### Limitations
333
+
334
+ 1. **Training Data Scope:** Model trained on 16,406 Q&A pairs; may not cover all medical conditions
335
+ 2. **Not a Medical Professional:** Cannot replace professional medical advice or diagnosis
336
+ 3. **Language:** English only
337
+ 4. **Clinical Validation:** Outputs should be reviewed by healthcare professionals before clinical application
338
+ 5. **Rare Conditions:** Performance may vary for extremely rare or newly discovered conditions
339
+ 6. **Quantization Effects:** 4-bit quantization may affect precision in certain edge cases
340
+
341
+ ### Bias Considerations
342
+
343
+ - **Dataset Bias:** Training data may reflect biases present in medical literature
344
+ - **Language Bias:** Trained exclusively on English medical content
345
+ - **Regional Bias:** May reflect medical practices and terminology from specific regions
346
+ - **Completeness:** May provide more detailed responses for well-documented conditions
347
+
348
+ ### Ethical Considerations
349
+
350
+ - **Not for Diagnosis:** This model should NOT be used for self-diagnosis or medical decision-making
351
+ - **Professional Review Required:** All outputs must be reviewed by qualified healthcare professionals
352
+ - **Patient Safety:** Users should always consult with licensed medical professionals for health concerns
353
+ - **Transparency:** Users should be informed when AI-generated medical content is provided
354
+ - **Privacy:** Do not share personally identifiable health information when using this model
355
+
356
+ ## Intended Use
357
+
358
+ ### Primary Use Cases
359
+
360
+ βœ… **Medical Education:** Teaching medical concepts and terminology
361
+ βœ… **Patient Information:** Providing general information about conditions and diseases
362
+ βœ… **Research Assistant:** Helping researchers understand medical concepts
363
+ βœ… **Content Generation:** Creating draft content for medical education materials
364
+ βœ… **Conversational AI:** Building medical information chatbots and assistants
365
+
366
+ ### Out-of-Scope Use
367
+
368
+ ❌ **Clinical Diagnosis:** Not validated for diagnostic purposes
369
+ ❌ **Treatment Planning:** Not suitable for creating treatment plans
370
+ ❌ **Emergency Response:** Not appropriate for emergency medical situations
371
+ ❌ **Prescription Decisions:** Cannot be used for medication recommendations
372
+ ❌ **Mental Health Crisis:** Not designed for crisis intervention or counseling
373
+ ❌ **Legal/Medical Records:** Not validated for official medical documentation
374
+
375
+ ## Evaluation Benchmarks
376
+
377
+ The model has been prepared for evaluation on standard medical benchmarks:
378
+
379
+ - **MEDQA:** Medical Question Answering benchmark
380
+ - **MEDMCQA:** Multiple Choice Medical Questions
381
+ - **PubMedQA:** Biomedical literature question answering
382
+ - **MMLU Medical Subsets:**
383
+ - Anatomy
384
+ - Clinical Knowledge
385
+ - College Medicine
386
+ - Medical Genetics
387
+ - Professional Medicine
388
+
389
+ *Note: Comprehensive benchmark results will be added as evaluation completes.*
390
+
391
+ ## Future Improvements
392
+
393
+ Suggested enhancements based on current limitations:
394
+
395
+ 1. **Increase LoRA Rank:** Higher rank for greater model capacity
396
+ 2. **Full Precision Training:** Use FP32 or FP16 instead of 4-bit quantization
397
+ 3. **Data Augmentation:** Expand training data with more diverse medical sources
398
+ 4. **Error Analysis:** Systematic analysis of model failure cases
399
+ 5. **Benchmark Evaluation:** Complete evaluation on medical QA benchmarks
400
+ 6. **Multi-lingual Support:** Extend to support multiple languages
401
+ 7. **Clinical Validation:** Formal evaluation by medical professionals
402
+
403
+ ## Model Architecture
404
+
405
+ ### Base Model: MediPhi-Instruct (Phi-3.5-mini-instruct)
406
+
407
+ **Key Components:**
408
+ - **Parameters:** 3.8 billion
409
+ - **Vocabulary Size:** 32,064 tokens
410
+ - **Hidden Dimension:** 3,072
411
+ - **Layers:** 32 Phi3DecoderLayers
412
+ - **Attention:** Multi-head self-attention with rotary positional embeddings
413
+ - **Activation:** SiLU (Swish) activation function
414
+ - **Normalization:** RMSNorm layer normalization
415
+
416
+ **LoRA Target Modules:**
417
+ - `o_proj` - Output projection in attention
418
+ - `qkv_proj` - Query-Key-Value projection in attention
419
+ - `gate_up_proj` - Gate and up projection in MLP
420
+ - `down_proj` - Down projection in MLP
421
+
422
+ ## Citation
423
+
424
+ If you use this model in your research, please cite:
425
+
426
+ ```bibtex
427
+ @misc{medphi-medical-qa-adapter,
428
+ author = {Sabber Ahamed},
429
+ title = {MediPhi Medical QA Adapter: LoRA Fine-tuning for Medical Question Answering},
430
+ year = {2025},
431
+ publisher = {Hugging Face},
432
+ howpublished = {\url{https://huggingface.co/sabber/medphi-medical-qa-adapter}},
433
+ note = {Fine-tuned on 16,406 medical Q&A pairs for patient education and medical information retrieval}
434
+ }
435
+ ```
436
+
437
+ Please also cite the base MediPhi model:
438
+
439
+ ```bibtex
440
+ @article{medphi2024,
441
+ title={MediPhi: A Medical Language Model},
442
+ author={Microsoft Research},
443
+ journal={arXiv preprint},
444
+ year={2024}
445
+ }
446
+ ```
447
+
448
+ ## Model Card Authors
449
+
450
+ Sabber Ahamed
451
+
452
+ ## Model Card Contact
453
+
454
+ For questions, issues, or feedback, please:
455
+ - Open an issue on the [model repository](https://huggingface.co/sabber/medphi-medical-qa-adapter/discussions)
456
+ - Contact via Hugging Face profile
457
+
458
+ ## Acknowledgments
459
+
460
+ - **Base Model:** Microsoft MediPhi-Instruct team
461
+ - **Framework:** Hugging Face Transformers, PEFT, and TRL libraries
462
+ - **Compute:** GPU infrastructure for model training
463
+ - **Community:** Open-source ML and medical NLP communities
464
+
465
+ ## Additional Resources
466
+
467
+ - **Training Code:** Available in project repository
468
+ - **Evaluation Scripts:** Provided for reproducibility
469
+ - **Documentation:** Comprehensive README with implementation details
470
+
471
+ ---
472
+
473
+ **Medical Disclaimer:** This model is provided for educational and research purposes only. It is NOT approved for clinical use, medical diagnosis, or treatment planning. All medical information should be verified by qualified healthcare professionals. In case of medical emergencies, contact emergency services immediately. Always consult with licensed medical professionals for health concerns and treatment decisions.
474
+
475
+ **Technical Disclaimer:** This model may generate incorrect or incomplete information. Users should verify all outputs and use appropriate safeguards when deploying in production environments. The model's responses should be reviewed and validated before any public-facing use.