AI Hints
Home›Machine Learning Tutorial›Shipping Your Model
Machine Learning · Lesson 59 of 85

Serving with an API

Twenty lines to production.

⏱ 2 min read▶ Video at 2:05:32🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

An A P I is like a waiter in a restaurant. The customer doesn't walk into the kitchen. They give the waiter an order, and the waiter brings back the food. Your website or mobile app sends a request with a house, and your A P I sends back a price. The kitchen is your model.

Serving with an API
From the video: Serving with an API
  • An API is a waiter
  • Orders in, food out
  • The kitchen is your model

A model behind an API

Fast A P I is the modern way to do this. We load the model one time, when the server starts. The House class defines what a request must contain, with a type for every field. This is called validation, and it protects your model from bad data. Then one function handles the predict route. It turns the request into a one row table, asks the model, and returns the price as J S O N. That's a real web service, in about twenty lines.

api.pypython
import joblib
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd

app = FastAPI(title="HomeWorth API")
model = joblib.load("homeworth.joblib")

class House(BaseModel):
    MedInc: float
    HouseAge: float
    AveRooms: float
    AveBedrms: float
    Population: float
    AveOccup: float
    Latitude: float
    Longitude: float

@app.post("/predict")
def predict(house: House):
    row = pd.DataFrame([house.model_dump()])
    return {"price": round(float(model.predict(row)[0]) * 100000)}

Testing the API

Let's call it. We use Fast A P I's test client, which behaves exactly like a real caller. We send a house as J S O N. Run it. We get status two hundred, which means success, and a price. Now look at the last line. We send bad data, with text instead of a number. The server responds with an error code of four twenty two, and our model is never even touched. That's validation doing its job.

call_api.pypython
import json
from fastapi.testclient import TestClient
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import HistGradientBoostingRegressor
import joblib

data = fetch_california_housing(as_frame=True)
joblib.dump(HistGradientBoostingRegressor(random_state=0).fit(data.data, data.target),
            "homeworth.joblib")

from api import app
client = TestClient(app)

house = data.data.iloc[0].to_dict()
reply = client.post("/predict", json=house)

print("Status:", reply.status_code)
print("Reply: ", json.dumps(reply.json()))
bad = client.post("/predict", json={"MedInc": "lots"})
print("Bad input status:", bad.status_code)
Output
Status: 200
Reply:  {"price": 415527}
Bad input status: 422
Also uses api.py
import joblib
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd

app = FastAPI(title="HomeWorth API")
model = joblib.load("homeworth.joblib")

class House(BaseModel):
    MedInc: float
    HouseAge: float
    AveRooms: float
    AveBedrms: float
    Population: float
    AveOccup: float
    Latitude: float
    Longitude: float

@app.post("/predict")
def predict(house: House):
    row = pd.DataFrame([house.model_dump()])
    return {"price": round(float(model.predict(row)[0]) * 100000)}

Running it for real

To run this for real, you use a server called uvicorn. Point it at the file and the app, and your model is live on your machine. Fast A P I even gives you an interactive documentation page for free, at slash docs, where anyone can try it. Next, you would wrap all of this in a Docker container, and deploy it to the cloud. We cover that fully in our Docker, FastAPI, and M L Ops courses.

Terminal
uvicorn api:app --port 8000
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Summary

Twenty lines of code, and your model can serve the whole world. Your developers are happy. But the sales team can't use an A P I, they want something to click on. Next, you'll build a real interactive app, using nothing but Python.

Key takeaways
  • An API wraps your model
  • Validation blocks bad data
  • uvicorn runs it for real
Next up: A Demo App in Minutes — Streamlit for machine learning