Blog

    Home » Uncategorized » MLOps for Beginners: Deploying Your First Machine Learning Model with Python
Featured Image
Tricky Talks (or TWS Blog)

Stay Sharp with Fresh Ideas, How-Tos, and Tech Insights

MLOps for Beginners: Deploying Your First Machine Learning Model with Python

  • Posted By: trickywebsolutions
  • Calendar June 13, 2025
Blog Image

Model deployment refers to converting a trained machine learning (ML) model into a functional tool that can be used by other systems or actual users. For instance, a fraud detection model may be deployed to observe real-time transactions and instantly flag suspicious behavior. Putting ML models into production environments is crucial as it makes their predictions accessible and applicable in real-world scenarios. However, creating machine learning models is exciting. But making them accessible to others is even more rewarding. In this blog, we’ll explore what is MLOps and how to develop and deploy machine learning model with Python.

What is MLOps? 

Machine Learning Operations is a set of technologies that make building, releasing, monitoring, and managing data science models easy in a complex enterprise. MLOps is a cross-functional discipline, usually involving the cooperation of multiple team members. It covers:

  • Model development
  • Version control
  • Model deployment
  • Monitoring and retraining
  • Infrastructure automation

Here is what it does

  • Move through the complete data science lifecycle to handle, build, launch, and oversee essential machine learning models more efficiently.
  • Expand enterprise models with the necessary security, governance, compliance, reproducibility, and audit capabilities needed to perform this reliably and widely.
  • Maintain peak model performance with ongoing model retraining.

What are Machine Learning Models?

Machine learning models are nothing but a mathematical structures that use computational algorithms to identify patterns, relationships, and representations within data. These ML models allow systems to perform some specific operations such as classification, prediction, and decision making. Huge datasets are fed to ML models to train them so they can generate accurate results. 

Popular Deployment Strategies

Primarily, we need to concentrate on these strategies:

Shadow Deployment: In this we need to run the new model along with current model without impacting production traffic. It enables performance comparison in a real-world environment and ensures the new model meets the desired metrics before full deployment.

Canary Deployment: This method slowly releases the new model to a small group of users while the rest stick with the current model. It provides the opportunity to observe the behavior of the new model and pinpoint problems before making it universally available.

A/B Testing: This displays different model versions to separate user groups and compares their effectiveness. It helps determine the better version before rolling it out universally.

MLOps Lifecycle Stages

Businesses that want to implement data science operations into their workflows are not only required to enable enterprise-level development but also have to comply with the approaches used by data scientists. Here are the MLOps lifecycle stages you need to follow to efficiency deploy machine learning model: 

Manage Stage: This stage focus on identifying and understanding the requirements of the projects in detail. Afterwords, based on the severity, it prioritise the tasks.   

Development Stage: This stage is responsible for developing the ML models with various modeling techniques depending on diverse factors.      

Deployment Stage: In this stage we deploy machine learning model into the business environment to make sure its working seamlessly. 

Monitor Stage: This stage involves monitoring the performance of the models after it is deployed on business environment, ensuring its delivering expected results. 

What We’ll Build in This Project

You’ll build and deploy a simple ML model using Python. Here is what we will implement in this project: 

  • How to train and save an ML model
  • How to create a basic API using FastAPI
  • How to test the deployed model locally
  • How to containerize the application using Docker
  • How to deploy it to a cloud platform

Tools Required:

  • Python: Core programming language
  • scikit-learn: For training the ML model
  • FastAPI: To expose the model via API
  • Uvicorn: ASGI server to run FastAPI
  • Docker: For containerization
  • GitHub: Version control
  • Cloud Provider (AWS/GCP): For production deployment

Step 1: Train and Save Your ML Model

Let’s start by training a simple classification model using scikit-learn.


# train_model.py
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pickle
# Load dataset
iris = load_iris()
X, y = iris.data, iris.target
# Train model
model = RandomForestClassifier()
model.fit(X, y)
# Save model to disk
with open("iris_model.pkl", "wb") as f:
    pickle.dump(model, f)

Run this script to save your trained model as iris_model.pkl.

Step 2: Create an API with FastAPI

Install the necessary libraries:


pip install fastapi uvicorn

Now, create an API using FastAPI.


# app.py
from fastapi import FastAPI
from pydantic import BaseModel
import numpy as np
import pickle
# Load model
with open("iris_model.pkl", "rb") as f:
    model = pickle.load(f)
# Define request schema
class IrisInput(BaseModel):
    sepal_length: float
    sepal_width: float
    petal_length: float
    petal_width: float
# Initialize app
app = FastAPI()

@app.post("/predict")
def predict(data: IrisInput):
    features = np.array([[data.sepal_length, data.sepal_width, data.petal_length, data.petal_width]])
    prediction = model.predict(features)
    return {"predicted_class": int(prediction[0])}

Step 3: Run the API Server

Start your FastAPI server using Uvicorn:


uvicorn app:app --reload

Your API is now running at http://localhost:8000. You can test it by sending a POST request to /predict with JSON input.

 Example API Call

Using a tool like Postman or curl, you can test the API:



POST http://localhost:8000/predict

{

  "sepal_length": 5.1,

  "sepal_width": 3.5,

  "petal_length": 1.4,

  "petal_width": 0.2

}

Response:


{
  "predicted_class": 0
}

Step 4: Containerize with Docker

Create a Dockerfile:


FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install fastapi uvicorn scikit-learn numpy pydantic
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

Build and run the Docker image:


docker build -t iris-api .
docker run -p 8000:8000 iris-api

Step 5: Deploy to Cloud 

You can now deploy your container to a cloud platform. Here’s an outline using Google Cloud Run:

1. Push your Docker image to Google Container Registry:


docker tag iris-api gcr.io/YOUR_PROJECT_ID/iris-api
docker push gcr.io/YOUR_PROJECT_ID/iris-api

2. Deploy it with Cloud Run:


gcloud run deploy iris-api \
  --image gcr.io/YOUR_PROJECT_ID/iris-api \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

Alternatively, use AWS ECS, Azure App Services, or Heroku.

Beginner MLOps Concepts You Just Practiced

  • Model Packaging: Saved your model using pickle.
  • API Serving: Exposed model with FastAPI.
  • Containerization: Used Docker to standardize environment.
  • Version Control: Use GitHub to manage your code.
  • Cloud Deployment: Deployed using GCP/AWS/other cloud tools.
  • Testing: Manually tested predictions; automate in production.

Conclusion

Deploying a machine learning model doesn’t have to be complex. With Python, FastAPI, Docker, and a cloud platform, you can serve your first model and take your first steps in MLOps. As you become comfortable, explore automated pipelines, cloud-native tools, and production monitoring.