How to Deploy a Machine Learning Model as an API
To deploy a machine learning model as an API, you package the trained weights with the preprocessing code, wrap them in a web service that accepts input and returns predictions, host that service, and monitor its latency and errors. You can build this yourself with Flask or FastAPI, use a managed cloud ML endpoint, or on TensorTurn save the best weights from a run and get an authenticated /predict endpoint that scales to zero and logs every call with latency and status.
What deploying a model as an API means
A trained model on its own is just a file. Deployment turns it into a service other software can call: a client sends an HTTP request with input data (a row of features, a text string or an image), the server runs the same preprocessing used in training, calls the model, and returns a prediction as JSON. This is called online or real-time inference. The alternative is batch inference, where you score a whole file on a schedule; it is simpler and cheaper when nobody needs an answer within seconds.
Model serving options compared
| Option | Setup effort | Scaling | Cost when idle | Best for |
|---|---|---|---|---|
| Self-hosted Flask or FastAPI | High: you write the service, Dockerfile, auth and hosting | Manual, or via your own container platform | You pay for the server even with no traffic | Full control, custom pipelines, teams with DevOps capacity |
| Managed cloud ML endpoints (for example AWS SageMaker, Google Vertex AI, Azure ML) | Medium: model packaging plus cloud configuration | Autoscaling, with tuning | Depends on configuration; always-on instances bill continuously | Organisations already on that cloud, with compliance and networking needs |
| Dedicated serving frameworks (TorchServe, Triton, BentoML) | Medium to high | Strong, especially for GPU batching | You run the infrastructure | High throughput or many models on shared GPUs |
| TensorTurn /predict endpoint | Low: save best weights from a run and deploy | Managed, scales to zero when idle | Scales to zero when unused | Getting a model trained on TensorTurn behind an API quickly |
The self-hosted and cloud options are stronger when you need private networking, custom request handling, A/B traffic splitting or very high throughput. A hosted /predict endpoint is strongest when you want the shortest path from a trained model to a working, authenticated API without writing serving code.
Scale to zero and cold starts
Scale to zero means the service shuts down its compute when no requests arrive and starts again on the next call. You stop paying for idle servers, which suits internal tools, prototypes and models called a few times an hour. The trade-off is the cold start: the first request after an idle period waits while the model loads. If you need consistently low latency for user-facing traffic, keep a warm instance, send periodic warm-up requests, or use a smaller model that loads faster.
Why export to ONNX
ONNX (Open Neural Network Exchange) is an open format for trained models. Exporting to ONNX decouples the model from the framework that trained it, so a PyTorch or scikit-learn model can run in ONNX Runtime on servers, browsers or edge devices, often with faster CPU inference and a smaller dependency footprint. Check that predictions from the exported model match the original on a sample of inputs before you ship it, since unsupported operators or preprocessing differences can change outputs.
Monitoring a deployed model
A model that worked on launch day can degrade silently. Monitor two layers: the service and the predictions.
- Latency: track typical and tail latency (p50 and p95), not just the average, since a few slow calls are what users notice.
- Status and error rate: a jump in 4xx responses usually means clients send malformed input; 5xx means the model or service failed.
- Input drift: compare the distribution of recent inputs with the training data. PSI below 0.1 is generally treated as stable and above 0.25 as a major shift.
- Prediction drift: watch the share of each predicted class or the mean predicted value over time.
- Ground truth: when real outcomes arrive, recompute the metrics you trained on and retrain when they slip.
TensorTurn logs every /predict call with its latency and status, which covers the service layer. For data drift, you can upload a recent sample of inputs as a dataset and use the profiling and datetime drift checks to compare it with your training data.
Common deployment mistakes
- Training-serving skew: preprocessing in the API differs from preprocessing in training, such as a different category encoding or missing scaler. Save the whole pipeline, not just the model.
- Loading untrusted pickle files. Unpickling can execute arbitrary code; prefer formats such as ONNX, framework-native weights or safer serialisers.
- No authentication on a public endpoint.
- Not pinning library versions, so a dependency update changes predictions.
- Never retraining. Data changes; schedule a review of live metrics.
How to deploy a model on TensorTurn
- Train a model in a TensorTurn notebook with scikit-learn, XGBoost, LightGBM, PyTorch, Keras or YOLO.
- Review the reported metrics and save the best weights from the run.
- Deploy to get a live, authenticated /predict API endpoint. It serves YOLO, scikit-learn, PyTorch, ONNX and Keras models and scales to zero when idle.
- Call the endpoint from your application with your API key.
- Watch the call log for latency and status, and retrain and redeploy when your data changes.
Frequently asked questions
How do I deploy a machine learning model as a REST API?
Wrap the trained model and its preprocessing in a web service such as FastAPI or Flask, containerise it, host it, add authentication and monitor it. On TensorTurn, you save the best weights from a training run and get an authenticated /predict endpoint without writing that serving code.
What does scale to zero mean for a model API?
The endpoint releases its compute when there is no traffic and starts again when a request arrives, so idle time costs nothing. The first request after an idle period is slower because the model must load, which is called a cold start.
Should I export my model to ONNX?
Export to ONNX when you want to run the model outside its training framework, for example on CPU servers, edge devices or in other languages. Verify that ONNX outputs match the original model on sample inputs before deploying.
What should I monitor after deploying a model?
Monitor latency, error rates and status codes for the service, and input drift, prediction distribution and real-world accuracy for the model. Retrain when drift is large or measured accuracy falls.
Which model types can TensorTurn deploy?
The /predict endpoint serves YOLO, scikit-learn, PyTorch, ONNX and Keras models trained in TensorTurn. Each call is logged with its latency and status.