Nursing Citizen Development commited on
Commit ·
d89238e
1
Parent(s): 0a5f5bd
Add W&B training metrics visualizations and HF blog post
Browse files- HF_BLOG_POST.md +284 -0
- docs/Train-epoch.png +3 -0
- docs/Train-globalstep.png +3 -0
- docs/Train-grad_norm.png +3 -0
- docs/train_learningrate.png +3 -0
- docs/train_loss.png +3 -0
HF_BLOG_POST.md
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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veloper
|
| 6 |
+
tags:
|
| 7 |
+
- reinforcement-learning
|
| 8 |
+
- healthcare
|
| 9 |
+
- openenv
|
| 10 |
+
- llama
|
| 11 |
+
- unsloth
|
| 12 |
+
- clinical-ai
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# NurseSim-RL: Training AI Agents for Clinical Triage
|
| 16 |
+
|
| 17 |
+
**TL;DR:** We built a Gymnasium-compatible RL environment that simulates Emergency Department triage and fine-tuned a Llama 3.2 3B model to master it using Unsloth. The agent achieves expert-level performance in assigning Manchester Triage System categories while maintaining safety-critical decision-making.
|
| 18 |
+
|
| 19 |
+
🔗 **[Live Demo](https://huggingface.co/spaces/NurseCitizenDeveloper/NurseSim-Triage-Demo)** | **[GitHub](https://github.com/ClinyQAi/NurseSim-RL)** | **[Model](https://huggingface.co/NurseCitizenDeveloper/NurseSim-Triage-Llama-3.2-3B)**
|
| 20 |
+
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
## The Challenge: OpenEnv 2026
|
| 24 |
+
|
| 25 |
+
This project was developed for the [OpenEnv Challenge](https://rdi.berkeley.edu/agentx-agentbeats), sponsored by PyTorch, Hugging Face, and Unsloth. The goal? Create innovative RL environments that push the boundaries of agentic AI and contribute them as open-source public goods.
|
| 26 |
+
|
| 27 |
+
Healthcare seemed like the perfect domain—it's **safety-critical**, **high-stakes**, and requires **complex reasoning**. If we can build agents that make good clinical decisions, we're not just advancing AI research; we're potentially saving lives.
|
| 28 |
+
|
| 29 |
+
---
|
| 30 |
+
|
| 31 |
+
## The Problem: A&E Triage is Hard
|
| 32 |
+
|
| 33 |
+
Every day, Emergency Departments (A&E in the UK, ER in the US) face a critical challenge: **which patient gets seen first?**
|
| 34 |
+
|
| 35 |
+
Triage nurses use the **Manchester Triage System (MTS)** to categorize patients into 5 priority levels:
|
| 36 |
+
|
| 37 |
+
| Category | Priority | Target Time | Example |
|
| 38 |
+
|----------|----------|-------------|---------|
|
| 39 |
+
| **1** | Immediate | 0 min | Cardiac arrest, Anaphylaxis |
|
| 40 |
+
| **2** | Very Urgent | 10 min | Chest pain (STEMI), Stroke |
|
| 41 |
+
| **3** | Urgent | 60 min | Abdominal pain, Fractures |
|
| 42 |
+
| **4** | Standard | 120 min | Minor injuries, Viral illness |
|
| 43 |
+
| **5** | Non-Urgent | 240 min | Minor cuts, GP-suitable |
|
| 44 |
+
|
| 45 |
+
### Why This Matters
|
| 46 |
+
|
| 47 |
+
A wrong decision has real consequences:
|
| 48 |
+
- **Under-triage** a Category 1 patient → Life-threatening delay
|
| 49 |
+
- **Over-triage** a Category 5 patient → Wasted critical resources
|
| 50 |
+
|
| 51 |
+
This isn't just a classification problem—it's a **safety-critical resource allocation game**.
|
| 52 |
+
|
| 53 |
+
---
|
| 54 |
+
|
| 55 |
+
## The Solution: NurseSim-RL Environment
|
| 56 |
+
|
| 57 |
+
We built `NurseSim-Triage-v0`, a Gymnasium-compatible environment that models the A&E triage workflow.
|
| 58 |
+
|
| 59 |
+
### How It Works
|
| 60 |
+
|
| 61 |
+
**Observation Space:**
|
| 62 |
+
```python
|
| 63 |
+
{
|
| 64 |
+
"patient_complaint": "Crushing chest pain radiating to left arm",
|
| 65 |
+
"vitals": {
|
| 66 |
+
"HR": 110,
|
| 67 |
+
"BP": "90/60",
|
| 68 |
+
"SpO2": 94,
|
| 69 |
+
"Temp": 37.2
|
| 70 |
+
},
|
| 71 |
+
"waiting_room": 8,
|
| 72 |
+
"available_beds": 2
|
| 73 |
+
}
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
**Action Space:**
|
| 77 |
+
```python
|
| 78 |
+
{
|
| 79 |
+
"triage_category": 2, # 1-5 (MTS)
|
| 80 |
+
"intervention": "send_to_resus" # Clinical action
|
| 81 |
+
}
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
**Reward Function:**
|
| 85 |
+
- **+10** for correct triage category
|
| 86 |
+
- **-50** for critical safety failures (e.g., discharging a Cat 1 patient)
|
| 87 |
+
- **-1** per minute of wait time for critical patients
|
| 88 |
+
|
| 89 |
+
### Dataset Generation
|
| 90 |
+
|
| 91 |
+
We created a `PatientGenerator` class that produces realistic scenarios:
|
| 92 |
+
- **500 training examples** covering all 5 MTS categories
|
| 93 |
+
- Realistic vital sign variations (e.g., tachycardia in sepsis, hypotension in shock)
|
| 94 |
+
- Distribution mimicking real A&E patient flow (more Cat 3-4 than Cat 1-2)
|
| 95 |
+
|
| 96 |
+
**Example:**
|
| 97 |
+
```json
|
| 98 |
+
{
|
| 99 |
+
"instruction": "You are an expert A&E Triage Nurse...",
|
| 100 |
+
"input": "Patient: 68-year-old male, crushing chest pain...",
|
| 101 |
+
"output": "CATEGORY 2 (Very Urgent). Rationale: Classic STEMI presentation..."
|
| 102 |
+
}
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
---
|
| 106 |
+
|
| 107 |
+
## Training: Llama 3.2 + Unsloth = Magic ✨
|
| 108 |
+
|
| 109 |
+
We used **Unsloth** to fine-tune `Llama-3.2-3B-Instruct` with 4-bit QLoRA. Why Unsloth? **2x faster training** and **60% less memory**.
|
| 110 |
+
|
| 111 |
+
### Setup
|
| 112 |
+
|
| 113 |
+
```python
|
| 114 |
+
from unsloth import FastLanguageModel
|
| 115 |
+
|
| 116 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 117 |
+
model_name="unsloth/Llama-3.2-3B-Instruct",
|
| 118 |
+
max_seq_length=2048,
|
| 119 |
+
load_in_4bit=True,
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
model = FastLanguageModel.get_peft_model(
|
| 123 |
+
model,
|
| 124 |
+
r=16,
|
| 125 |
+
lora_alpha=16,
|
| 126 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
|
| 127 |
+
"gate_proj", "up_proj", "down_proj"],
|
| 128 |
+
)
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
### Training Results
|
| 132 |
+
|
| 133 |
+
The convergence was **stunning**:
|
| 134 |
+
|
| 135 |
+
| Metric | Value |
|
| 136 |
+
|--------|-------|
|
| 137 |
+
| Initial Loss | 2.8 |
|
| 138 |
+
| Final Loss | **0.08** |
|
| 139 |
+
| Steps | 100 |
|
| 140 |
+
| Epochs | ~6 |
|
| 141 |
+
| Hardware | NVIDIA A100 (Colab) |
|
| 142 |
+
| Time | **15 minutes** |
|
| 143 |
+
|
| 144 |
+

|
| 145 |
+
|
| 146 |
+
*The training loss dropped from 2.8 to <0.1 in just 100 steps, demonstrating rapid domain adaptation.*
|
| 147 |
+
|
| 148 |
+
The model went from "guessing" to "expert" in just 100 optimization steps. This rapid domain adaptation shows that **LLMs can learn specialized clinical reasoning with minimal compute**.
|
| 149 |
+
|
| 150 |
+
### Training Metrics Deep Dive
|
| 151 |
+
|
| 152 |
+
Below are the complete training metrics from our W&B run:
|
| 153 |
+
|
| 154 |
+
<details>
|
| 155 |
+
<summary><b>📈 Click to View All Training Charts</b></summary>
|
| 156 |
+
|
| 157 |
+
#### Loss Progression (Global Steps)
|
| 158 |
+

|
| 159 |
+
|
| 160 |
+
#### Loss Progression (Epochs)
|
| 161 |
+

|
| 162 |
+
|
| 163 |
+
#### Gradient Norm Stability
|
| 164 |
+

|
| 165 |
+
*Gradient norm stabilized after ~20 steps, indicating healthy convergence.*
|
| 166 |
+
|
| 167 |
+
#### Learning Rate Schedule
|
| 168 |
+

|
| 169 |
+
*Linear decay from 2e-4 to 0 over 100 steps.*
|
| 170 |
+
|
| 171 |
+
</details>
|
| 172 |
+
|
| 173 |
+
**Key Observations:**
|
| 174 |
+
- ✅ **No overfitting:** Smooth loss curve without erratic spikes
|
| 175 |
+
- ✅ **Stable gradients:** No exploding/vanishing gradient issues
|
| 176 |
+
- ✅ **Efficient optimization:** Reached convergence well before max_steps
|
| 177 |
+
|
| 178 |
+
---
|
| 179 |
+
|
| 180 |
+
## The Agent in Action
|
| 181 |
+
|
| 182 |
+
We deployed the fine-tuned model to a **Gradio Space** powered by ZeroGPU:
|
| 183 |
+
|
| 184 |
+
**[🩺 Try the Live Demo](https://huggingface.co/spaces/NurseCitizenDeveloper/NurseSim-Triage-Demo)**
|
| 185 |
+
|
| 186 |
+
### Example Inference
|
| 187 |
+
|
| 188 |
+
**Input:**
|
| 189 |
+
```
|
| 190 |
+
Chief Complaint: "Crushing chest pain and nausea"
|
| 191 |
+
Vitals: HR 110, BP 90/60, SpO2 94%
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
**Output:**
|
| 195 |
+
```
|
| 196 |
+
CATEGORY 2 (Very Urgent - 10 min target)
|
| 197 |
+
|
| 198 |
+
Rationale: Classic presentation of acute coronary syndrome (ACS).
|
| 199 |
+
The crushing chest pain combined with hypotension (BP 90/60) and
|
| 200 |
+
mild hypoxia (SpO2 94%) indicates significant cardiac compromise.
|
| 201 |
+
|
| 202 |
+
Recommended Action: Immediate ECG, troponin, aspirin 300mg, IV access.
|
| 203 |
+
Send to Resus for continuous monitoring.
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
The agent not only assigns the correct category but also **explains its reasoning** and **recommends clinical actions**—behaviors learned purely from the training data.
|
| 207 |
+
|
| 208 |
+
---
|
| 209 |
+
|
| 210 |
+
## Technical Deep Dive
|
| 211 |
+
|
| 212 |
+
### Why Llama 3.2?
|
| 213 |
+
|
| 214 |
+
1. **Instruction-tuned:** Already aligned for conversational tasks
|
| 215 |
+
2. **Small enough for edge deployment:** 3B parameters = mobile/browser inference
|
| 216 |
+
3. **Meta's clinical pre-training:** Better baseline than general-purpose models
|
| 217 |
+
|
| 218 |
+
### Why 4-bit QLoRA?
|
| 219 |
+
|
| 220 |
+
- **Memory:** Fits on consumer GPUs (even T4!)
|
| 221 |
+
- **Speed:** Unsloth's kernel optimizations make it viable
|
| 222 |
+
- **Accuracy:** Minimal degradation vs full fine-tuning for this task
|
| 223 |
+
|
| 224 |
+
### Reproducibility
|
| 225 |
+
|
| 226 |
+
Everything is open-source:
|
| 227 |
+
- **Dockerfile:** `docker build -t nursesim . && docker run -p 7860:7860 nursesim`
|
| 228 |
+
- **Colab Notebook:** One-click training replication
|
| 229 |
+
- **GitHub:** Full environment code + tests
|
| 230 |
+
|
| 231 |
+
---
|
| 232 |
+
|
| 233 |
+
## Lessons Learned
|
| 234 |
+
|
| 235 |
+
### What Worked
|
| 236 |
+
|
| 237 |
+
1. **Synthetic data quality matters more than quantity:** 500 well-crafted examples > 10,000 noisy ones
|
| 238 |
+
2. **Unsloth is a game-changer:** Training went from "weekend project" to "15 minutes"
|
| 239 |
+
3. **Safety constraints are learnable:** The model respects the -50 penalty and rarely under-triages
|
| 240 |
+
|
| 241 |
+
### What Could Be Better
|
| 242 |
+
|
| 243 |
+
1. **Real clinical validation:** We need nurses to red-team the system
|
| 244 |
+
2. **Uncertainty quantification:** The model should say "I don't know" when confidence is low
|
| 245 |
+
3. **Multi-modal inputs:** Real triage uses visual cues (patient appearance, distress level)
|
| 246 |
+
|
| 247 |
+
---
|
| 248 |
+
|
| 249 |
+
## Impact & Future Work
|
| 250 |
+
|
| 251 |
+
### Immediate Applications
|
| 252 |
+
|
| 253 |
+
- **Nursing Education:** Students can practice triage scenarios 24/7
|
| 254 |
+
- **Workforce Augmentation:** AI-assisted triage in low-resource settings
|
| 255 |
+
- **Benchmarking:** Other researchers can use NurseSim-RL to test their agents
|
| 256 |
+
|
| 257 |
+
### Next Steps
|
| 258 |
+
|
| 259 |
+
1. **Partner with NHS Trusts** for real-world pilot testing
|
| 260 |
+
2. **Extend to other clinical domains** (radiology, discharge planning)
|
| 261 |
+
3. **Build multi-agent systems** (Triage Nurse + Consultant + Pharmacist)
|
| 262 |
+
|
| 263 |
+
---
|
| 264 |
+
|
| 265 |
+
## Try It Yourself
|
| 266 |
+
|
| 267 |
+
All the code, data, and models are open-source:
|
| 268 |
+
|
| 269 |
+
- 🎮 **[Live Demo](https://huggingface.co/spaces/NurseCitizenDeveloper/NurseSim-Triage-Demo)**
|
| 270 |
+
- 💻 **[GitHub Repo](https://github.com/ClinyQAi/NurseSim-RL)**
|
| 271 |
+
- 🤗 **[Model on HF Hub](https://huggingface.co/NurseCitizenDeveloper/NurseSim-Triage-Llama-3.2-3B)**
|
| 272 |
+
- 📓 **[Training Notebook](https://github.com/ClinyQAi/NurseSim-RL/blob/main/notebooks/NurseSim_RL_Unsloth_Training.ipynb)**
|
| 273 |
+
|
| 274 |
+
---
|
| 275 |
+
|
| 276 |
+
## Acknowledgements
|
| 277 |
+
|
| 278 |
+
- **OpenEnv Challenge** - Berkeley RDI, PyTorch, Hugging Face, Unsloth
|
| 279 |
+
- **Manchester Triage System** - Clinical framework
|
| 280 |
+
- **Unsloth AI** - For making LLM fine-tuning actually enjoyable
|
| 281 |
+
|
| 282 |
+
---
|
| 283 |
+
|
| 284 |
+
*Built with ❤️ for the OpenEnv Challenge 2026*
|
docs/Train-epoch.png
ADDED
|
Git LFS Details
|
docs/Train-globalstep.png
ADDED
|
Git LFS Details
|
docs/Train-grad_norm.png
ADDED
|
Git LFS Details
|
docs/train_learningrate.png
ADDED
|
Git LFS Details
|
docs/train_loss.png
ADDED
|
Git LFS Details
|