tensorturnBETA
Home › Machine Learning Tasks

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

OptionSetup effortScalingCost when idleBest for
Self-hosted Flask or FastAPIHigh: you write the service, Dockerfile, auth and hostingManual, or via your own container platformYou pay for the server even with no trafficFull 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 configurationAutoscaling, with tuningDepends on configuration; always-on instances bill continuouslyOrganisations already on that cloud, with compliance and networking needs
Dedicated serving frameworks (TorchServe, Triton, BentoML)Medium to highStrong, especially for GPU batchingYou run the infrastructureHigh throughput or many models on shared GPUs
TensorTurn /predict endpointLow: save best weights from a run and deployManaged, scales to zero when idleScales to zero when unusedGetting 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.

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

How to deploy a model on TensorTurn

Start building free

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.