Skip to content

Deploying ML Models to Production — A Complete Guide

DodaTech 1 min read

In this tutorial, you'll learn about Deploying ML Models to Production. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

What You'll Learn

Take a trained ML model and deploy it as a production REST API using FastAPI, Docker, and cloud hosting — from saved model to live endpoint.

Why It Matters

A model in a Jupyter notebook has zero value. A model behind an API creates real value. Deployment is the most underrated ML skill.

Real-World Use

Serving a fraud detection model as an API endpoint, hosting an image classification service, or exposing a sentiment analysis model to your application.

Step 1: Save Your Trained Model

import joblib
from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier()
model.fit(X_train, y_train)

# Save to file
joblib.dump(model, "model.pkl")

For TensorFlow/PyTorch:

# TensorFlow
model.save("tf_model.keras")

# PyTorch
torch.save(model.state_dict(), "model.pt")

Step 2: Create a FastAPI App

# app.py
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import numpy as np

app = FastAPI()
model = joblib.load("model.pkl")

class PredictionInput(BaseModel):
    features: list[float]

class PredictionOutput(BaseModel):
    prediction: int
    probability: float

@app.post("/predict", response_model=PredictionOutput)
def predict(data: PredictionInput):
    X = np.array(data.features).reshape(1, -1)
    pred = model.predict(X)[0]
    proba = max(model.predict_proba(X)[0])
    return PredictionOutput(prediction=int(pred), probability=float(proba))

Step 3: Run Locally

pip install fastapi uvicorn
uvicorn app:app --reload

Test with:

curl -X POST http://localhost:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"features": [5.1, 3.5, 1.4, 0.2]}'

Step 4: Containerize with Docker

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
docker build -t model-api .
docker run -p 8000:8000 model-api

Step 5: Deploy

Platform Method
Railway Connect GitHub repo
Hugging Face Spaces Create Space → upload
AWS Lambda Container image → Lambda
Google Cloud Run gcloud run deploy

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro