File size: 3,312 Bytes
12e3308
 
 
 
 
 
 
 
2306d9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bbf49a
2306d9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bbf49a
 
2306d9c
 
 
 
 
 
12e3308
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
---
license: mit
title: Radiology Report Generation System
sdk: docker
emoji: 🏒
colorFrom: blue
colorTo: green
---
# Radiology AI Report Generation System v2.0

AI-powered radiology report generation using BLIP-2 vision + Phi-2 LLM fine-tuned on real radiology reports.

## What it does
- Upload any radiology image (X-ray, CT, MRI, Ultrasound, PET, Mammography, etc.)
- BLIP-2 reads the actual image and describes what it sees
- Phi-2 (fine-tuned on 227,835 real radiology reports) generates a structured report
- Outputs: FINDINGS, IMPRESSION, RECOMMENDATIONS + UMLS/ICD-10/SNOMED CT codes

## Requirements
- Windows 10/11
- Python 3.10
- NVIDIA GPU with 8GB+ VRAM (RTX 4060 or better)
- CUDA 12.8
- 20GB free disk space (for models)

---

## Quick Start

### Step 1 β€” Setup (run once)
```
Double-click: setup.bat
```
This creates venv, installs all dependencies, verifies GPU.

### Step 2 β€” Fine-tune (run once, ~35 minutes)
```
Double-click: run_training.bat
```
Downloads unsloth/Radiology_mini (227,835 real reports) and fine-tunes Phi-2.

### Step 3 β€” Start the system
Terminal 1:
```
run_backend.bat
```
Terminal 2:
```
run_frontend.bat
```

### Step 4 β€” Use the system
Open browser: http://localhost:7860

---

## Manual commands (if .bat files don't work)

```bash
cd D:\radiology_ai_system
venv\Scripts\activate

# Backend
set USE_4BIT=true
set ENABLE_VISION_CAPTION=true
uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000

# Frontend (new terminal)
venv\Scripts\activate
python frontend/ui.py

# Fine-tuning (Git Bash)
python training/train_unsloth.py --max_steps 300
```

---

## Project Structure
```
radiology_ai_system/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ main.py              # FastAPI endpoints
β”‚   β”œβ”€β”€ model_loader.py      # BLIP-2 + Phi-2 loading
β”‚   β”œβ”€β”€ preprocessing.py     # Image preprocessing (9 modalities)
β”‚   β”œβ”€β”€ report_generator.py  # Report generation + parsing
β”‚   β”œβ”€β”€ validation_module.py # Report quality scoring
β”‚   └── medical_coding.py    # UMLS/ICD-10/SNOMED CT
β”œβ”€β”€ frontend/
β”‚   └── ui.py                # Gradio UI
β”œβ”€β”€ training/
β”‚   β”œβ”€β”€ train_unsloth.py     # Fine-tuning on Radiology_mini
β”‚   β”œβ”€β”€ prepare_dataset.py   # Dataset preparation
β”‚   └── evaluate_model.py    # Model evaluation
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ slm_model/           # Phi-2 cached here
β”‚   β”‚   └── lora_adapter/    # Fine-tuned LoRA adapter saved here
β”‚   └── vision_encoder/      # BLIP-2 cached here
β”œβ”€β”€ setup.bat                # One-time setup
β”œβ”€β”€ run_backend.bat          # Start backend
β”œβ”€β”€ run_frontend.bat         # Start frontend
β”œβ”€β”€ run_training.bat         # Fine-tune model
└── requirements.txt
```

---

## VRAM Usage
| Component     | VRAM   |
|---------------|--------|
| Phi-2 (4-bit) | ~2.0GB |
| BLIP-2        | ~3.5GB |
| Total         | ~5.5GB |
| Available     | 8.6GB  βœ… |

---

## API Endpoints
- POST /api/generate-report  β€” Generate report from image
- GET  /api/report/{id}      β€” Get stored report
- GET  /api/export/pdf/{id}  β€” Download PDF
- GET  /api/export/json/{id} β€” Download JSON
- GET  /api/health           β€” System health
- GET  /docs                 β€” Swagger UI (http://localhost:8000/docs)