For modern web developers, a data science model is only valuable if it can be consumed over standard HTTP/REST protocols. FastAPI is the premier modern Python web framework for serving high-performance, asynchronous machine learning microservices.
Figure 41.1: Advance Web Dev Architecture — Async FastAPI Endpoints, DuckDB-Wasm & Dockerized Serving.
# main.py - Run with: uvicorn main:app --reload --port 8000
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import joblib
import numpy as np
from contextlib import asynccontextmanager
# In-memory model cache
ml_models = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
# Load model artifact once on server startup
print("Loading serialized ML model artifact...")
# Simulating loaded pre-trained model: dummy weights
ml_models["churn_model"] = lambda x: 1 / (1 + np.exp(-(x[0]*0.2 - x[1]*0.01)))
yield
# Clean up on shutdown
ml_models.clear()
app = FastAPI(
title="Veewom Customer Churn Inference Microservice",
description="Real-time predictive scoring API for web developers.",
version="2.0.0",
lifespan=lifespan
)
# Pydantic schema for strict incoming JSON validation
class CustomerFeatures(BaseModel):
support_tickets: int = Field(..., ge=0, le=50, description="Count of support tickets opened")
tenure_months: int = Field(..., ge=0, le=120, description="Account age in months")
monthly_spend: float = Field(..., ge=0.0, description="Monthly recurring billing")
class Config:
json_schema_extra = {
"example": {"support_tickets": 3, "tenure_months": 14, "monthly_spend": 79.99}
}
class PredictionResponse(BaseModel):
churn_probability: float
churn_risk_tier: str
action_recommended: str
@app.post("/predict", response_model=PredictionResponse)
async def predict_churn(customer: CustomerFeatures):
try:
model = ml_models.get("churn_model")
if not model:
raise HTTPException(status_code=503, detail="Model artifact unavailable.")
features = np.array([customer.support_tickets, customer.tenure_months])
prob = float(model(features))
tier = "High Risk" if prob >= 0.60 else ("Medium Risk" if prob >= 0.30 else "Low Risk")
action = "Send 20% Retention Offer" if prob >= 0.60 else "Standard Nurture Campaign"
return PredictionResponse(
churn_probability=round(prob, 4),
churn_risk_tier=tier,
action_recommended=action
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Once your FastAPI server starts, visit http://localhost:8000/docs in your browser. FastAPI automatically generates an interactive Swagger UI where web developers can test JSON requests and review schemas in real-time!
Broaden your statistical analysis, Python data wrangling, and production engineering expertise:
Combine backend FastAPI endpoints with ultra-fast in-browser computing.
Build semantic search and similarity APIs with FastAPI and Postgres.
Package FastAPI apps into multi-stage Docker containers with Gunicorn.
Integrate FastAPI endpoints with interactive frontend dashboard applications.