File size: 4,634 Bytes
742cd9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FastAPI application entry point.

Wires together:
- Application lifecycle (model loading at startup)
- Route registration
- Middleware (CORS, logging)
- Health check endpoint
- Interactive API documentation

Run with:
    uvicorn src.api.main:app --reload --port 8000
"""

import logging
import time
from contextlib import asynccontextmanager

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

from src.api.models import HealthResponse
from src.api.predictor import predictor
from src.api.routers import predict

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s β€” %(name)s β€” %(levelname)s β€” %(message)s",
)
logger = logging.getLogger(__name__)


# --- Lifespan: load model at startup, clean up at shutdown ---
@asynccontextmanager
async def lifespan(app: FastAPI):
    """
    FastAPI lifespan event handler.

    Runs at startup: loads model artifacts into memory.
    Runs at shutdown: logs graceful shutdown.

    Using lifespan instead of @app.on_event is the modern
    FastAPI pattern β€” on_event is deprecated as of FastAPI 0.93.
    """
    # Startup
    logger.info("Starting Readmission Risk API...")
    logger.info("Loading model artifacts β€” this takes ~5 seconds...")

    try:
        predictor.load()
        logger.info("API ready to serve predictions")
    except Exception as e:
        logger.error(f"Failed to load model at startup: {e}")
        logger.error("API will start but /predict endpoints will return 503")

    yield  # API is running here

    # Shutdown
    logger.info("Shutting down Readmission Risk API...")


# --- Application ---
app = FastAPI(
    title="30-Day Readmission Risk API",
    description="""
## Clinical Decision Support β€” Readmission Risk Prediction

Predicts 30-day hospital readmission risk at the point of patient discharge
using an XGBoost model trained on synthetic clinical data (Synthea).

### Model Performance
- **AUC-ROC:** 0.891 (exceeds 0.75 clinical benchmark)
- **Recall:** 77% (catches 4 in 5 readmissions)
- **Fairness:** Audited with IBM AIF360 β€” PASS for gender and age

### Endpoints
- `POST /predict/features` β€” predict from engineered features
- `GET /predict/example` β€” sample high-risk patient
- `GET /health` β€” API health check

### Clinical Disclaimer
This tool is a **clinical decision support system**.
It does not replace clinical judgement. All predictions
should be interpreted in the context of the full clinical picture.

### Model Version
`xgboost_v1_auc0.891` β€” trained June 2026
    """,
    version="1.0.0",
    contact={
        "name": "Ibrahim β€” Health Tech Engineer",
        "url": "https://drkryptomed.github.io",
    },
    lifespan=lifespan,
)


# --- CORS Middleware ---
# Required for browser-based frontends to call the API
# In production: replace ["*"] with specific frontend domain
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# --- Request logging middleware ---
@app.middleware("http")
async def log_requests(request: Request, call_next):
    """Log every request with timing information."""
    start_time = time.time()
    response = await call_next(request)
    duration = (time.time() - start_time) * 1000

    logger.info(
        f"{request.method} {request.url.path} β€” "
        f"{response.status_code} β€” "
        f"{duration:.1f}ms"
    )
    return response


# --- Health check ---
@app.get(
    "/health",
    response_model=HealthResponse,
    tags=["System"],
    summary="API health check",
    description="Returns API status and model loading state. "
                "Use this to verify the service is ready before sending predictions.",
)
async def health_check() -> HealthResponse:
    """
    Health check endpoint.

    Returns 200 if API is running.
    Check model_loaded field to confirm predictions are available.
    """
    return HealthResponse(
        status="healthy" if predictor.is_loaded else "degraded",
        model_loaded=predictor.is_loaded,
        model_version=predictor.model_version,
        api_version="1.0.0",
    )


# --- Root endpoint ---
@app.get(
    "/",
    tags=["System"],
    summary="API root",
)
async def root():
    """Redirect information for API root."""
    return {
        "message": "30-Day Readmission Risk API",
        "version": "1.0.0",
        "docs": "/docs",
        "health": "/health",
        "predict": "/predict/features",
    }


# --- Register routers ---
app.include_router(predict.router)