Serverless ML Deployment: From Notebook to API
.pkl or .h5 file sitting in a Jupyter notebook, you can bypass the entire MLOps nightmare by using serverless containers. This approach removes the need to manage OS patches or scale clusters manually.The Infrastructure Gap
Traditional deployment is a slog: you provision a VM, fight with dependency versions, write a wrapper API, containerize it, and then set up a load balancer. Serverless flips this. You provide the image, and the cloud provider handles the scaling and execution.
- Deployment Speed: Minutes vs. days.
- Cost Efficiency: Pay-per-request means zero cost for idle models.
- Scalability: Automatic scaling during traffic spikes without manual intervention.
- Maintenance: No OS updates or hardware monitoring.
Real-World Deployment Workflow
To move from a notebook to a live endpoint in under 10 minutes, you need your model file, a basic FastAPI wrapper, and a cloud CLI (like gcloud for Google Cloud Run) configured.
1. Environment Setup
Keep your model file (e.g.,
my_model.pkl) in a dedicated project folder. Create a requirements.txt to lock your versions, as mismatching scikit-learn versions between training and deployment is the #1 cause of crashes.fastapi
uvicorn
scikit-learn==1.3.0
pandas2. The API Wrapper
You need a lightweight entry point. FastAPI is the standard here because of its speed and automatic Swagger documentation.
from fastapi import FastAPI
import joblib
import pandas as pd
app = FastAPI()
model = joblib.load("my_model.pkl")
@app.post("/predict")
def predict(data: dict):
df = pd.DataFrame([data])
prediction = model.predict(df)
return {"prediction": prediction.tolist()}3. Containerization & Deployment
Since serverless platforms like Google Cloud Run or AWS App Runner require a container, you'll need a simple Dockerfile.
FROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]Once the image is built, a single CLI command pushes it to the cloud and gives you a public HTTPS URL. This is the most practical tutorial for anyone who wants to focus on prompt engineering or model tuning rather than managing Kubernetes clusters. For a deep dive into optimizing these containers for faster cold starts, check out the documentation at promptcube3.com.