Capstone: Serve
Container and API.
Step four. We have a champion model in the registry. Now we package it, with a small web service, into a container, and prove that it answers. The service and the container are the ones you have already seen. The only new idea is where the model file comes from, the pipeline's output.

- The champion model is ready
- Package it with a web service
- Prove it answers
The service and its container
Here is the service for this project. It loads the model from the models folder, once, when it starts. It describes the four input fields. It has a health endpoint, for the platform to check. And a predict endpoint, which returns the churn probability, and the model version. The container recipe, which you will see in the next step, copies this file, and the model, onto a slim Python image, with the pinned libraries.
import joblib
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel
model = joblib.load("models/model.joblib")
app = FastAPI(title="Churn API")
class Customer(BaseModel):
tenure: int
charges: float
calls: int
contract: int
@app.get("/health")
def health():
return {"status": "ok"}
@app.post("/predict")
def predict(customer: Customer):
row = pd.DataFrame([customer.model_dump()])
probability = float(model.predict_proba(row)[0][1])
return {"churn_probability": round(probability, 3), "model_version": "1"}Build, run, and call it
Now we build the image, and list it. We start a container, and map a port. A moment later, the health check answers ok. Now we ask for a prediction, for a new customer, three months of tenure, a high bill, four support calls, and a monthly contract. The service answers with a churn probability, and the model version. The model that went through the pipeline, the tests, and the gate, is now answering requests, from inside a container.
docker build -q -t churn-api:1 . sha256:8062645299b3b9c5c1d073532ff7e1ce1878d13ad824c67ea24c65f1bfa5eaf7 docker images churn-api --format '{{.Repository}}:{{.Tag}} {{.Size}}' churn-api:1 729MB docker run -d --name churn -p 8100:8000 churn-api:1 | cut -c1-12 dc7ce3123e00 curl -s localhost:8100/health {"status":"ok"} curl -s -X POST localhost:8100/predict -H 'Content-Type: application/json' -d '{"tenure":3,"charges":95,"calls":4,"contract":0}' {"churn_probability":0.94,"model_version":"1"}
Summary
Excellent. The champion model is packaged into a container, with a health check, and answers predictions. Next, step five, describing the deployment on Kubernetes, and automating the whole path with a pipeline.
Key takeaways- Service loads the model once
- Health and predict endpoints
- Container answers real requests