REST API Serving
A model is just a math equation. To actually let users interact with it, you must wrap it in a standard web server so that mobile apps and websites can send it data over the internet and receive predictions back.
Why Does This Exist?
You have trained a brilliant computer vision model that detects diseases in plant leaves. You serialized it to ONNX, and you wrote a Python script that loads the model and successfully analyzes a local image file.
But how does a farmer standing in a field in Iowa use it? They can't run your Python script on their iPhone. The industry standard way to connect software applications across the internet is via a REST API (Representational State Transfer Application Programming Interface).
By wrapping your model inside a REST API, you create a dedicated URL (an "endpoint") on your server. Any mobile app, website, or other software can send data to this URL and receive an instant prediction back, regardless of what programming language they are written in.
Think of It Like This
Think of It Like This
Imagine you are an incredibly smart chef who makes the best pizza in the world (the ML Model). However, you only speak Italian, and you are locked in the kitchen.
To serve customers, you need a waiter (the REST API). The waiter takes the customer's order in English, walks into the kitchen, translates it into Italian for you, waits for you to cook the pizza, and then delivers it back to the customer. The waiter is the standardized interface that bridges the outside world to the kitchen.
How It Works
A typical ML REST API is built using lightweight web frameworks like FastAPI (Python), Flask (Python), or Express (Node.js).
When a user wants a prediction, their device sends an HTTP POST Request to your server. The request contains a payload of data formatted in JSON (JavaScript Object Notation), which is a universal text format that all programming languages understand.
The REST API performs three jobs:
- Validation: It checks the incoming JSON. Did the user actually send an image, or did they accidentally send a PDF? If it's invalid, it immediately rejects it with a
400 Bad Requesterror. - Execution: It hands the validated data to the ML model and waits for the inference to finish.
- Response: It takes the mathematical output of the model (e.g.,
[0.1, 0.9]), formats it into a human-readable JSON response ({"disease": "blight", "confidence": 0.9}), and sends it back over the internet with a200 OKsuccess code.
Show Me the Code
Here is how you wrap a pre-trained PyTorch model into a production-ready REST API using FastAPI.
from fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModelimport torch
# 1. Initialize the Web Serverapp = FastAPI(title="Plant Disease API")
# 2. Load the model once into memory when the server startsmodel = load_my_pytorch_model()model.eval()
# 3. Define the exact JSON shape we expect from the userclass PredictionRequest(BaseModel): pixel_data: list[float] crop_type: str
# 4. Create the Endpoint URL (/predict)@app.post("/predict")def get_prediction(request: PredictionRequest): try: # Extract data from the JSON request input_tensor = torch.tensor(request.pixel_data) # Run the ML model (The actual math) with torch.no_grad(): output = model(input_tensor) # Format the result back into JSON return { "disease_detected": True if output > 0.5 else False, "confidence": float(output) } except Exception as e: # If anything breaks, tell the user gracefully raise HTTPException(status_code=500, detail="Inference Failed")Watch Out For
Watch Out For
The Global Interpreter Lock (GIL) Bottleneck.
In Python, the GIL prevents multiple threads from executing Python code at the exact same time. If your web server receives 50 requests simultaneously, and your model takes 1 second to run, the 50th user will have to wait 50 seconds to get their response! Python REST APIs (like FastAPI) are great for routing requests, but they are terrible at concurrent heavy math. For high-traffic production, you must use a dedicated Inference Engine (like Triton or vLLM) behind your API, or rely on asynchronous dynamic-batching.
The Quick Version
- A REST API acts as a bridge, allowing users across the internet to query your ML model.
- Users send data (like text or images) via an HTTP POST request formatted as JSON.
- The API validates the input, runs the model, and returns a JSON response.
- FastAPI is the modern standard for writing ML APIs in Python because it automatically validates the incoming JSON data using Pydantic.
- Wrapping a model in a Python API is easy, but scaling it to handle thousands of simultaneous users requires advanced serving architectures to overcome Python's concurrency limitations.
What to Read Next
batch-vs-realtime-inference— Sometimes you don't need a REST API at all. You can just process data offline overnight.dynamic-batching— How a REST API can group 50 simultaneous user requests into a single batch to speed up GPU execution.