Blog · November 25, 2026
How to deploy a ML model to the cloud
I have been using jupyter notebook and colab for training machine learning models and testing the predictions in the notebook itself. I have never understood how these models can be used outside the notebook, or rather I did not try to learn them as I assumed deploying a model was very hard. But I couldn’t be more wrong. In this article I will show you how you could deploy your first machine learning model in less than 20 minutes. So get ready to go beyond the notebook.
I will be using the following libraries and tools apart from the general ML libraries like scikit-learn:
- uv: for environment management
- joblib: for exporting the ml model
- Docker: for containerization
- FastAPI: to create the API for interacting with the model
NOTE The code snippets I share are from my own project, modify them to satisfy your use case before executing them.
Let’s start!!!!
As this article is about deployment I am going to assume you have already experimented and trained various models and have decided the best model for your use case.
- The first step is to save the model the you wish to deploy using the
jobliblibrary.
joblib.dump(best_model, "best_model.joblib")
- Create a predict.py file where we define the api to access the predictions of the model. I have use FastAPI for creating the api. It should. contain a post method for sending the inputs to the model and getting the predictions. Here is an example predict file that I used for a phishing url detection project.
from fastapi import FastAPI # for creating the api
from pydantic import BaseModel
import joblib # for importing the ml model
import pandas as pd
import uvicorn
# a helper function that extracts the required features from the input
from feature_extraction import extract_url_features
app = FastAPI(title="Phishing Detection")
model = joblib.load('best_model.joblib')
class URLRequest(BaseModel):
url: str
@app.get("/")
def home():
return {"message": "Phishing Detection API is running"}
@app.post("/predict")
def predict(request: URLRequest):
features = extract_url_features(request.url)
features_df = pd.DataFrame([features])
prediction = int(model.predict(features_df)[0])
probabilities = model.predict_proba(features_df)[0]
return {
"url": request.url,
"prediction": "legitimate" if prediction == 1 else "phishing",
"phishing_probability": round(float(probabilities[0]), 6),
"legitimate_probability": round(float(probabilities[1]), 6),
}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=9696)
Setting up the virtual environment
-
using uv
uv init # to initailize the project # to add dependencies to the project (you might have more) uv add fastapi uvicorn joblib pandas scikit-learn # resolve dependencies and generates or updates the lockfile uv lock # installs dependencies specified by project and the lockfile into the environment uv sync -
docker
- create a docker file
Dockerfile - add these contents to it
- create a docker file
# use this version of python (base image)
FROM python:3.11-slim
# make /app as the working directory, this is where the application will start
WORKDIR /app
# copy our config files to the directory
COPY pyproject.toml uv.lock ./
# execute this command to install the dependencies
RUN pip install --no-cache-dir uv && uv sync --frozen --no-dev
# copy the files required to run our service
COPY feature_extraction.py predict.py best_model.joblib ./
# the port the containerized application listens on
EXPOSE 9696
# run this command
CMD ["sh", "-c", "uv run --frozen --no-sync uvicorn predict:app --host 0.0.0.0 --port ${PORT:-9696}"]
- create .dockerignore file and add those folders you dont wish to have in the docker.
- eg:
.venv/
__pycache__/
.git/
.ipynb_checkpoints/
*.pyc
notebook.ipynb
experiments/
datasets
- build the docker image from the directory containing the Dockerfile.
docker build -t phishing-detection .
# docker build: builds an image
# -t phishing-detection: assigns a name to the image
# .: specifies our current directory as the build context
- run the docker container
docker run -d -p 9696:9696 --name phishing-api phishing-detection
# -d: runs container in background
# -p 9696:9696: maps host port to container port
# --name: assigns a name to the container
# phishing-detection: image to run
- the api should now be accessible at:
http://localhost:9696
Deploying on cloud
- We can deploy our model on the cloud for free using render.
- First push the project to github.
- Then on render dashboard create a new webservice and connect your github account.
- Select the model repository and use the following settings
| Setting | Value |
|---|---|
| Branch | main |
| Language / Runtime | Docker |
| Dockerfile Path | ./Dockerfile |
| Docker Context Directory | . |
| Region | Choose a region close to your users |
| Instance Type | Free, if available and suitable |
- The finally click Deploy Web Service
Exercise
- define the api using flask instead of FastAPI