peft safetensors medicalQA question-answering en dataset:bigbio/pubmed_qa base_model:unsloth/phi-4-unsloth-bnb-4bit base_model:adapter:unsloth/phi-4-unsloth-bnb-4bit license:mit region:us
---
base_model: unsloth/phi-4-unsloth-bnb-4bit
datasets:
- bigbio/pubmed_qa
language:
- en
library_name: peft
license: mit
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 when combined with a RAG system.

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},
}