Spaces:
Runtime error
Runtime error
Upload 4 files
Browse filesFiles changed (4)
- DockerfileAdded+13
- README.md+23-10
- app.pyAdded+82
- requirements.txtAdded+7
12345678910111213FROM python:3.11-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc libgomp1 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY . .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 7860
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]123456789101234567891011121314151617181920212223---title: NeuroHealthemoji: 🏃colorFrom: bluecolorTo: yellowsdk: dockerpinned: false---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# NeuroHealth Alzheimer MRI Space
This space exposes Alzheimer's MRI models saved in `backend/saved_models`.
## Endpoints
- `GET /health` — model loading status
- `GET /models` — available Alzheimer's MRI models
- `POST /predict/image?model_key=<key>` — MRI image prediction
## Supported model keys
- `ad_dn121`
- `ad_dn169`
- `ad_dn201`
- `ad_homogeneous`
## Run locally with Docker
```bash
docker build -t neurohealth-ad-mri .
docker run --rm -p 7860:7860 neurohealth-ad-mri
```12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
BACKEND_PATH = str(REPO_ROOT / "backend")
if BACKEND_PATH not in sys.path:
sys.path.insert(0, BACKEND_PATH)
from fastapi import FastAPI, UploadFile, File, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from app.models.model_manager import get_model_manager, MODEL_CONFIGS, ENSEMBLE_CONFIGS
app = FastAPI(
title="NeuroHealth Alzheimer MRI Space",
version="1.0.0",
description="FastAPI wrapper for Alzheimer's MRI models stored in backend/saved_models.",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
model_manager = get_model_manager()
AD_KEYS = {"ad_dn121", "ad_dn169", "ad_dn201", "ad_homogeneous"}
@app.get("/")
async def root():
return {
"message": "NeuroHealth Alzheimer MRI Space",
"supported_endpoints": ["/health", "/models", "/predict/image"],
}
@app.get("/health")
async def health():
statuses = model_manager.get_model_status()
return {
"status": "ok",
"model_status": {k: v for k, v in statuses.items() if k in AD_KEYS},
"available_models": [m["key"] for m in model_manager.get_available_models("alzheimers")],
}
@app.get("/models")
async def get_models():
models = model_manager.get_available_models("alzheimers")
models.append({
"key": "ad_homogeneous",
"name": "Alzheimer's MRI Homogeneous Ensemble (DenseNet 121+169+201)",
"condition": "alzheimers",
"imaging_type": "mri",
"type": "ensemble",
})
return {"models": models}
@app.post("/predict/image")
async def predict_image(
model_key: str = Query(..., description="Model key such as ad_dn121 or ad_homogeneous"),
file: UploadFile = File(...),
):
if model_key not in AD_KEYS:
raise HTTPException(status_code=400, detail=f"Unknown Alzheimer MRI model key: {model_key}")
image_bytes = await file.read()
filename = file.filename or ""
config = MODEL_CONFIGS.get(model_key, {})
if model_key == "ad_homogeneous":
result = model_manager.predict_ensemble(model_key, image_bytes, filename)
else:
result = model_manager.predict_image(model_key, image_bytes, filename)
if "error" in result:
raise HTTPException(status_code=400, detail=result["error"])
return result1234567fastapi
uvicorn[standard]
torch
torchvision
pillow
numpy
pydanticDockerfileAdded+13
README.md+23-10
app.pyAdded+82
requirements.txtAdded+7