File size: 5,675 Bytes
5345aa5 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 ac4efc7 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 2c47f90 c1bc804 | 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | ---
title: UPDRS API
emoji: π§
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
---
# Parkinson's Disease Motor UPDRS Prediction API
A production-style FastAPI service that predicts **motor UPDRS score** from a voice sample and patient metadata.
The service:
- accepts audio uploads (`wav`, `mp3`, `ogg`, `webm`),
- converts them to WAV when needed,
- extracts clinical voice biomarkers using Praat,
- loads a trained ensemble model from Hugging Face,
- returns a numeric UPDRS motor prediction.
---
## 1) What this API does
Given:
- `age`
- `sex` (`male` or `female`)
- `test_time`
- `audio_file`
The API returns:
- `prediction` (float): estimated motor UPDRS score.
---
## 2) Project structure
```text
UPDRS_API/
βββ main.py
βββ requirements.txt
βββ Dockerfile
βββ README.md
βββ routers/
β βββ analyze_router.py
βββ services/
β βββ voice_analyze_service.py
βββ ml/
β βββ model_predictor.py
βββ utils/
β βββ file_handler.py
β βββ voice_data_extraction.py
βββ schema/
βββ patient_inputs.py
```
### Responsibilities
- **main.py**: app startup, CORS, lifespan hook, route registration.
- **routers/analyze_router.py**: request validation and endpoint definitions.
- **services/voice_analyze_service.py**: end-to-end orchestration.
- **utils/file_handler.py**: upload persistence + ffmpeg conversion.
- **utils/voice_data_extraction.py**: Praat/parselmouth feature extraction.
- **ml/model_predictor.py**: model download/cache and inference.
---
## 3) API endpoints
| Method | Path | Purpose |
| ------ | ---------------- | -------------------------------------------- |
| GET | `/` | Basic welcome/status message |
| GET | `/health` | Health + model cache state |
| POST | `/analyze/test` | Echo/debug endpoint for multipart form input |
| POST | `/analyze/voice` | Main prediction endpoint |
### `GET /health` response
```json
{
"status": "ok",
"models_loaded": true
}
```
---
## 4) Main prediction endpoint
### `POST /analyze/voice`
Content type: `multipart/form-data`
| Field | Type | Validation |
| ------------ | ------ | ------------------ |
| `age` | int | `10 < age < 120` |
| `sex` | string | `male` or `female` |
| `test_time` | float | `> 0` |
| `audio_file` | file | required |
### Example response
```json
{
"prediction": 18.42
}
```
---
## 5) How prediction works (pipeline)
1. Request arrives at `POST /analyze/voice`.
2. Audio file is written to a temp file.
3. Non-WAV formats are converted to WAV via `ffmpeg`.
4. Praat/parselmouth extracts acoustic and nonlinear features.
5. `sex` is encoded (`male=1`, `female=0`).
6. Features are combined with `age` and `test_time`.
7. Features are ordered/scaled according to `feature_names.pkl` and `scaler.pkl`.
8. Ensemble model predicts UPDRS.
9. Temp file is always cleaned up.
---
## 6) Extracted voice features
The service extracts these features:
- **Jitter**
- `Jitter(%)`
- `Jitter(Abs)`
- `Jitter:RAP`
- `Jitter:PPQ5`
- `Jitter:DDP`
- **Shimmer**
- `Shimmer`
- `Shimmer(dB)`
- `Shimmer:APQ3`
- `Shimmer:APQ5`
- `Shimmer:APQ11`
- `Shimmer:DDA`
- **Noise/Harmonics**
- `NHR`
- `HNR`
- **Nonlinear dynamics**
- `RPDE`
- `DFA`
- `PPE`
---
## 7) Model details
- Model repo: https://huggingface.co/xplorers/parkinsons-updrs-model
- Artifacts downloaded on startup:
- `ensemble_model.pkl`
- `feature_names.pkl`
- `scaler.pkl`
- Models are cached in-memory (`_cache`) after first load.
---
## 8) Environment variables
Create a `.env` file in the project root (optional but recommended):
```env
HF_TOKEN=your_huggingface_access_token
```
`main.py` calls `load_dotenv()`, so `.env` is loaded automatically.
---
## 9) Local development setup
### Prerequisites
- Python 3.11+ (recommended for this codebase)
- `ffmpeg` installed and available in PATH
### Install
```bash
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
### Run
```bash
uvicorn main:app --reload --host 0.0.0.0 --port 8000
```
Open:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
---
## 10) Docker usage
Build image:
```bash
docker build -t updrs-api .
```
Run container:
```bash
docker run --rm -p 7860:7860 --env HF_TOKEN=$HF_TOKEN updrs-api
```
Container entrypoint uses:
```bash
uvicorn main:app --host 0.0.0.0 --port 7860
```
---
## 11) cURL examples
### Health check
```bash
curl http://127.0.0.1:8000/health
```
### Voice prediction
```bash
curl -X POST "http://127.0.0.1:8000/analyze/voice" \
-F "age=63" \
-F "sex=male" \
-F "test_time=12.5" \
-F "audio_file=@./sample.wav"
```
---
## 12) Error handling and troubleshooting
### 401 Unauthorized from Hugging Face
Cause: gated/private model repository.
Fix:
1. Ensure your account has access to the model page.
2. Set `HF_TOKEN` in environment or `.env`.
3. Restart the API.
### `ffmpeg` conversion failure
Cause: `ffmpeg` missing or unsupported input file.
Fix:
- Install `ffmpeg` (system package).
- Test source file manually with `ffmpeg -i <file>`.
### Models not loaded
`predict_parkinson()` raises runtime error if startup download failed.
Fix:
- Check server startup logs.
- Verify internet access and token permissions.
---
## 13) License
This project includes a `LICENSE` file at the repository root. Review it before distribution or production use.
|