Update README.md
Browse files
README.md
CHANGED
|
@@ -25,109 +25,59 @@ It was trained using **2,000 medical instruction–response pairs** to enhance B
|
|
| 25 |
| **Frameworks** | 🤗 Transformers, PEFT, PyTorch |
|
| 26 |
| **Hardware** | Trained on a single NVIDIA GPU (e.g., T4 or A100) |
|
| 27 |
|
| 28 |
-
---
|
| 29 |
-
|
| 30 |
-
### ⚙️ Training Configuration
|
| 31 |
-
|
| 32 |
-
| Parameter | Value |
|
| 33 |
-
| ------------------------- | ------------------------------ |
|
| 34 |
-
| `learning_rate` | 2e-4 |
|
| 35 |
-
| `batch_size` | 4 (with gradient accumulation) |
|
| 36 |
-
| `num_train_epochs` | 3 |
|
| 37 |
-
| `optimizer` | AdamW |
|
| 38 |
-
|
| 39 |
-
---
|
| 40 |
-
|
| 41 |
-
### 🧩 Fine-tuning Workflow
|
| 42 |
-
|
| 43 |
-
1. **Loaded BioGPT base model**
|
| 44 |
-
|
| 45 |
-
```python
|
| 46 |
-
model = AutoModelForCausalLM.from_pretrained("microsoft/biogpt")
|
| 47 |
-
tokenizer = AutoTokenizer.from_pretrained("microsoft/biogpt")
|
| 48 |
-
```
|
| 49 |
-
|
| 50 |
-
2. **Applied LoRA configuration**
|
| 51 |
-
|
| 52 |
-
```python
|
| 53 |
-
LoraConfig(
|
| 54 |
-
r=8,
|
| 55 |
-
lora_alpha=16,
|
| 56 |
-
target_modules=["c_attn", "c_proj", "q_proj", "v_proj"],
|
| 57 |
-
lora_dropout=0.1,
|
| 58 |
-
bias="none",
|
| 59 |
-
task_type="CAUSAL_LM"
|
| 60 |
-
)
|
| 61 |
-
```
|
| 62 |
-
|
| 63 |
-
3. **Trained using Hugging Face `Trainer` with EarlyStoppingCallback**
|
| 64 |
-
|
| 65 |
-
* Evaluation after each epoch
|
| 66 |
-
* Best model automatically saved
|
| 67 |
-
|
| 68 |
-
4. **Merged LoRA adapter into base BioGPT**
|
| 69 |
-
|
| 70 |
-
```python
|
| 71 |
-
merged_model = model.merge_and_unload()
|
| 72 |
-
merged_model.save_pretrained("./biogpt-lora-merged")
|
| 73 |
-
```
|
| 74 |
-
|
| 75 |
-
5. **Pushed merged model to Hugging Face Hub**
|
| 76 |
|
| 77 |
---
|
| 78 |
|
| 79 |
### 💬 Example Usage
|
| 80 |
|
| 81 |
```python
|
| 82 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 83 |
import torch
|
|
|
|
| 84 |
|
|
|
|
| 85 |
model_name = "alanjoshua2005/biogpt-instruct"
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
```
|
| 105 |
-
|
| 106 |
-
---
|
| 107 |
-
|
| 108 |
-
### 📊 Example Output
|
| 109 |
-
|
| 110 |
-
```
|
| 111 |
-
Instruction: Explain what COVID-19 is in simple terms.
|
| 112 |
-
Answer: COVID-19 is a viral disease caused by SARS-CoV-2.
|
| 113 |
-
It mainly affects the lungs and can cause fever, cough, and tiredness.
|
| 114 |
-
It spreads through droplets when an infected person coughs or sneezes.
|
| 115 |
-
```
|
| 116 |
-
|
| 117 |
-
---
|
| 118 |
-
|
| 119 |
-
### ⚠️ Disclaimer
|
| 120 |
-
|
| 121 |
-
This model is **for research and educational use only**.
|
| 122 |
-
It is **not a substitute for professional medical advice or diagnosis**.
|
| 123 |
-
Always consult qualified medical professionals for real-world medical questions.
|
| 124 |
-
|
| 125 |
-
---
|
| 126 |
-
|
| 127 |
-
### 🤝 Acknowledgements
|
| 128 |
-
|
| 129 |
-
* [Microsoft Research](https://huggingface.co/microsoft) for releasing **BioGPT**
|
| 130 |
-
* [FreedomIntelligence](https://huggingface.co/FreedomIntelligence) for the **medical reasoning dataset**
|
| 131 |
-
* [Hugging Face](https://huggingface.co) and [PEFT](https://github.com/huggingface/peft) for fine-tuning utilities
|
| 132 |
-
|
| 133 |
-
---
|
|
|
|
| 25 |
| **Frameworks** | 🤗 Transformers, PEFT, PyTorch |
|
| 26 |
| **Hardware** | Trained on a single NVIDIA GPU (e.g., T4 or A100) |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
---
|
| 30 |
|
| 31 |
### 💬 Example Usage
|
| 32 |
|
| 33 |
```python
|
|
|
|
| 34 |
import torch
|
| 35 |
+
from transformers import BioGptTokenizer, BioGptForCausalLM, set_seed
|
| 36 |
|
| 37 |
+
# Load fine-tuned model
|
| 38 |
model_name = "alanjoshua2005/biogpt-instruct"
|
| 39 |
+
tokenizer = BioGptTokenizer.from_pretrained(model_name)
|
| 40 |
+
model = BioGptForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16).to("cuda")
|
| 41 |
+
|
| 42 |
+
# Function to get a clean model response
|
| 43 |
+
def generate_response(instruction):
|
| 44 |
+
# Format the instruction properly
|
| 45 |
+
prompt = f"### Instruction: {instruction}\n### Response:"
|
| 46 |
+
|
| 47 |
+
# Tokenize
|
| 48 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 49 |
+
|
| 50 |
+
# Reproducibility
|
| 51 |
+
set_seed(42)
|
| 52 |
+
|
| 53 |
+
# Generate
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
outputs = model.generate(
|
| 56 |
+
**inputs,
|
| 57 |
+
min_length=100,
|
| 58 |
+
max_length=1024,
|
| 59 |
+
temperature=0.5, # lower = more factual, less hallucination
|
| 60 |
+
top_p=0.9,
|
| 61 |
+
do_sample=True,
|
| 62 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Decode and clean output
|
| 66 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 67 |
+
if "### Response:" in text:
|
| 68 |
+
text = text.split("### Response:")[-1].strip()
|
| 69 |
+
if "### Instruction:" in text:
|
| 70 |
+
text = text.split("### Instruction:")[0].strip()
|
| 71 |
+
text = text.replace(instruction, "").strip()
|
| 72 |
+
|
| 73 |
+
return text
|
| 74 |
+
|
| 75 |
+
# 🧍♂️ User Input
|
| 76 |
+
print("🧠 BioGPT Instruct — Medical Query Assistant\n")
|
| 77 |
+
user_query = input("Enter your medical question or instruction:\n> ")
|
| 78 |
+
|
| 79 |
+
# Get and display the response
|
| 80 |
+
response = generate_response(user_query)
|
| 81 |
+
print("\n🧠 Model Response:\n")
|
| 82 |
+
print(response)
|
| 83 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|