Skip to content
KH.Portfolio
All notes

Online Notes

17. FastAPI for ML Inference

Basic API:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"message": "API running"}

Prediction route:

@app.post("/predict")
def predict(customer: CustomerInput):
    ...

Pydantic request schema

from pydantic import BaseModel

class CustomerInput(BaseModel):
    age: int
    monthly_spend: float
    months_subscribed: int
    support_tickets: int
    plan: str

This is conceptually similar to a DTO in NestJS.

Validation

from pydantic import Field

age: int = Field(ge=18, le=100)
monthly_spend: float = Field(ge=0)

Restrict values:

from typing import Literal

plan: Literal["Basic", "Standard", "Premium"]

Response model

class PredictionResponse(BaseModel):
    prediction: Literal["stay", "churn"]
    churn_probability: float

Route:

@app.post("/predict", response_model=PredictionResponse)

Health endpoint

@app.get("/health")
def health():
    return {"status": "ok"}

Useful for deployment/service monitoring.

Safe model path

from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
MODEL_PATH = BASE_DIR / "models" / "churn_model.pkl"

Avoid importing training code just to get paths.

Important Python architecture note

Top-level code runs when a module is imported.

Use:

def train_model():
    ...

if __name__ == "__main__":
    train_model()

to prevent training from accidentally executing when another module imports train.py.