Once a model is saved, the most common way to make its predictions available to other applications is wrapping it in a small web API — a service other programs can send data to and receive predictions from.
# app.py — a minimal prediction API using FastAPI
from fastapi import FastAPI
import joblib
import pandas as pd
app = FastAPI()
model = joblib.load("full_pipeline.joblib") # load ONCE when the app starts, not per-request
@app.post("/predict")
def predict(features: dict):
df = pd.DataFrame([features]) # wrap the single input as a one-row DataFrame
prediction = model.predict(df)[0]
probability = model.predict_proba(df)[0].max()
return {"prediction": str(prediction), "confidence": float(probability)}
# Run with: uvicorn app:app --reload
# Then: POST http://localhost:8000/predict with JSON body {"feature1": 5, "feature2": "A"}
FastAPI is a modern, popular Python framework for building this kind of API — a decorated function (@app.post("/predict")) becomes an HTTP endpoint that accepts input (here, as JSON) and returns a prediction, callable from any other application, website, or mobile app capable of making an HTTP request, regardless of what language that caller is written in.
Loading the model once at startup, outside the request-handling function, rather than inside it, is essential for performance — reloading a potentially large model file from disk on every single incoming request would be needlessly slow, while loading it once and keeping it in memory lets each request run in milliseconds.
Beyond the code shown, production deployment involves further concerns this notebook only touches on: input validation (rejecting malformed requests before they reach the model), logging predictions for later monitoring, handling concurrent requests safely, and tracking model performance over time to detect drift — the gradual degradation that happens as the real-world data a deployed model sees increasingly diverges from the data it was originally trained on. These operational concerns, collectively known as MLOps, are a substantial field in their own right, but understanding this basic save-and-serve pattern is the essential first step toward making any trained model useful outside a notebook.