python 24 lines · 1 tab

Serving scikit-learn models behind a FastAPI prediction API

1 tab
import joblib
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title='Churn Prediction API')
model = joblib.load('artifacts/churn_pipeline.joblib')

class PredictionRequest(BaseModel):
    age: int
    income: float
    country: str
    plan_tier: str
    days_since_last_login: int

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

@app.post('/predict')
def predict(payload: PredictionRequest):
    frame = pd.DataFrame([payload.model_dump()])
    probability = float(model.predict_proba(frame)[:, 1][0])
    return {'churn_probability': probability, 'model_version': '2026-04-07'}
1 file · python Explain with highlit

Deployment should not rewrite the feature logic from scratch. I expose trained pipelines behind FastAPI so the exact preprocessing and estimator objects travel together. Strong request schemas and explicit model versioning keep this boring in the right way.

Share this code

Here's the card — post it anywhere.

Serving scikit-learn models behind a FastAPI prediction API — share card
Link copied