Chapter 1: REST APIs for ML
FastAPI for ML Serving
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import mlflow
import numpy as np
app = FastAPI(title="ML Model API", version="1.0.0")
# Load model at startup
model = None
@app.on_event("startup")
async def load_model():
global model
model = mlflow.sklearn.load_model("models:/churn-model/Production")
class PredictionRequest(BaseModel):
features: list[float] = Field(..., min_length=10, max_length=10)
customer_id: str
class PredictionResponse(BaseModel):
customer_id: str
prediction: int
probability: float
model_version: str
@app.post("/predict", response_model=PredictionResponse)
async def predict(request: PredictionRequest):
try:
features = np.array(request.features).reshape(1, -1)
prediction = model.predict(features)[0]
probability = model.predict_proba(features)[0][1]
return PredictionResponse(
customer_id=request.customer_id,
prediction=int(prediction),
probability=float(probability),
model_version="2.1.0"
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health():
return {"status": "healthy", "model_loaded": model is not None}
Key Takeaways
Summary
- Use Pydantic for request/response validation
- Load models at startup not per-request
- Implement health checks for orchestration
- Use async for better throughput