Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +13 -0
- README.md +23 -10
- app.py +82 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
RUN apt-get update \
|
| 4 |
+
&& apt-get install -y --no-install-recommends gcc libgomp1 \
|
| 5 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
COPY . .
|
| 9 |
+
RUN pip install --upgrade pip
|
| 10 |
+
RUN pip install -r requirements.txt
|
| 11 |
+
|
| 12 |
+
EXPOSE 7860
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# NeuroHealth Alzheimer MRI Space
|
| 2 |
+
|
| 3 |
+
This space exposes Alzheimer's MRI models saved in `backend/saved_models`.
|
| 4 |
+
|
| 5 |
+
## Endpoints
|
| 6 |
+
|
| 7 |
+
- `GET /health` — model loading status
|
| 8 |
+
- `GET /models` — available Alzheimer's MRI models
|
| 9 |
+
- `POST /predict/image?model_key=<key>` — MRI image prediction
|
| 10 |
+
|
| 11 |
+
## Supported model keys
|
| 12 |
+
|
| 13 |
+
- `ad_dn121`
|
| 14 |
+
- `ad_dn169`
|
| 15 |
+
- `ad_dn201`
|
| 16 |
+
- `ad_homogeneous`
|
| 17 |
+
|
| 18 |
+
## Run locally with Docker
|
| 19 |
+
|
| 20 |
+
```bash
|
| 21 |
+
docker build -t neurohealth-ad-mri .
|
| 22 |
+
docker run --rm -p 7860:7860 neurohealth-ad-mri
|
| 23 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
REPO_ROOT = Path(__file__).resolve().parent.parent
|
| 5 |
+
BACKEND_PATH = str(REPO_ROOT / "backend")
|
| 6 |
+
if BACKEND_PATH not in sys.path:
|
| 7 |
+
sys.path.insert(0, BACKEND_PATH)
|
| 8 |
+
|
| 9 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Query
|
| 10 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
+
from app.models.model_manager import get_model_manager, MODEL_CONFIGS, ENSEMBLE_CONFIGS
|
| 12 |
+
|
| 13 |
+
app = FastAPI(
|
| 14 |
+
title="NeuroHealth Alzheimer MRI Space",
|
| 15 |
+
version="1.0.0",
|
| 16 |
+
description="FastAPI wrapper for Alzheimer's MRI models stored in backend/saved_models.",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
app.add_middleware(
|
| 20 |
+
CORSMiddleware,
|
| 21 |
+
allow_origins=["*"],
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"],
|
| 24 |
+
allow_headers=["*"],
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
model_manager = get_model_manager()
|
| 28 |
+
AD_KEYS = {"ad_dn121", "ad_dn169", "ad_dn201", "ad_homogeneous"}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@app.get("/")
|
| 32 |
+
async def root():
|
| 33 |
+
return {
|
| 34 |
+
"message": "NeuroHealth Alzheimer MRI Space",
|
| 35 |
+
"supported_endpoints": ["/health", "/models", "/predict/image"],
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@app.get("/health")
|
| 40 |
+
async def health():
|
| 41 |
+
statuses = model_manager.get_model_status()
|
| 42 |
+
return {
|
| 43 |
+
"status": "ok",
|
| 44 |
+
"model_status": {k: v for k, v in statuses.items() if k in AD_KEYS},
|
| 45 |
+
"available_models": [m["key"] for m in model_manager.get_available_models("alzheimers")],
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.get("/models")
|
| 50 |
+
async def get_models():
|
| 51 |
+
models = model_manager.get_available_models("alzheimers")
|
| 52 |
+
models.append({
|
| 53 |
+
"key": "ad_homogeneous",
|
| 54 |
+
"name": "Alzheimer's MRI Homogeneous Ensemble (DenseNet 121+169+201)",
|
| 55 |
+
"condition": "alzheimers",
|
| 56 |
+
"imaging_type": "mri",
|
| 57 |
+
"type": "ensemble",
|
| 58 |
+
})
|
| 59 |
+
return {"models": models}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.post("/predict/image")
|
| 63 |
+
async def predict_image(
|
| 64 |
+
model_key: str = Query(..., description="Model key such as ad_dn121 or ad_homogeneous"),
|
| 65 |
+
file: UploadFile = File(...),
|
| 66 |
+
):
|
| 67 |
+
if model_key not in AD_KEYS:
|
| 68 |
+
raise HTTPException(status_code=400, detail=f"Unknown Alzheimer MRI model key: {model_key}")
|
| 69 |
+
|
| 70 |
+
image_bytes = await file.read()
|
| 71 |
+
filename = file.filename or ""
|
| 72 |
+
config = MODEL_CONFIGS.get(model_key, {})
|
| 73 |
+
|
| 74 |
+
if model_key == "ad_homogeneous":
|
| 75 |
+
result = model_manager.predict_ensemble(model_key, image_bytes, filename)
|
| 76 |
+
else:
|
| 77 |
+
result = model_manager.predict_image(model_key, image_bytes, filename)
|
| 78 |
+
|
| 79 |
+
if "error" in result:
|
| 80 |
+
raise HTTPException(status_code=400, detail=result["error"])
|
| 81 |
+
|
| 82 |
+
return result
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
pillow
|
| 6 |
+
numpy
|
| 7 |
+
pydantic
|