Serve a model as a web endpoint
In this task you'll put the champion model behind a web address that apps can call one request at a time.
Online serving answers one request at a time, in milliseconds, for example when a customer is at the checkout. mlflow models serve starts a small web server for any registered model: /ping for health checks and /invocations for predictions, taking JSON in a fixed shape.
Serving the alias (models:/delivery-late@champion) means a restart picks up a newly promoted model. In production the same model would run in a container behind a load balancer, with several copies, health probes and a timeout.
Example. SS Kaiser's checkout shows a delivery warning for risky baskets. The shop calls the model's /invocations address with the basket details and gets an answer in under 50 milliseconds, while the nightly batch handles everything else.
Before you start
You need: Python 3.10 or newer, and about 500 MB of disk for MLflow and scikit-learn. No cloud account. Time: 20 minutes.
Where to look
- mlops folder
- mlflow models serve
- http://127.0.0.1:5001/invocations
Steps
- Point MLflow at the local database for this terminal (see Commands for bash and PowerShell).
- Run
mlflow models serve -m "models:/delivery-late@champion" -p 5001 --env-manager local. Wait for the line saying it's running on 127.0.0.1:5001.--env-manager local uses your current Python environment instead of building a new one. - In a second terminal, check health:
curl -i http://127.0.0.1:5001/pingshowsHTTP/1.1 200 OK. - Send two orders to /invocations with the curl line in Commands. The answer is
{"predictions": 0, 1}: the short weekday order on time, the long weekend evening order late. - Stop the server with Ctrl+C.
Commands
# Point MLflow at the local database for this terminal
export MLFLOW_TRACKING_URI=sqlite:///mlflow.db # bash
$env:MLFLOW_TRACKING_URI = "sqlite:///mlflow.db" # PowerShell
mlflow models serve -m "models:/delivery-late@champion" -p 5001 --env-manager local
# In a second terminal
curl -i http://127.0.0.1:5001/ping
curl -X POST http://127.0.0.1:5001/invocations -H "Content-Type: application/json" -d '{"dataframe_split":{"columns":["distance_km","items","hour","weekend","warehouse"],"data":[[4.2,2,10,0,"north"],[31.5,9,19,1,"south"]]}}'
# PowerShell: use curl.exe instead of curl, and put the JSON in a file: curl.exe ... -d "@order.json"
Watch out for
- Cost
- Free on your computer. In the cloud an endpoint costs money every hour it runs, even with no requests.
- Security
- This server has no sign-in and listens on your computer only; never expose it to a network as it is.
- Common mistakes
- Serving a fixed version so promotions never arrive, and no timeout on the app's calls to the model.
Clean up
Keep the mlops folder for the next tasks in this section; delete it (and its .venv) at the end.
Words to know
- Online serving
- Answering prediction requests one at a time as they arrive.
- Endpoint
- The web address a model answers on.
- Health check
- A quick request (here /ping) that shows the service is up.
- Latency
- How long one request takes to answer.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On