Deploying Your Machine Learning Model

Learn how to deploy your machine learning model using FastAPI, enabling your models to serve predictions through a simple API for real-world applications.

🔰 beginner
⏱️ 30 minutes
👤 SuperML Team

· Machine Learning · 2 min read

📋 Prerequisites

  • Basic Python knowledge
  • Trained machine learning model
  • Familiarity with scikit-learn

🎯 What You'll Learn

  • Understand the basics of model deployment
  • Create a FastAPI application to serve your model
  • Send HTTP requests to get predictions from your model
  • Understand considerations for real-world model serving

Introduction

After training your machine learning model, the next step is to deploy it so others can use it in real-world applications. Deployment involves serving your model as an API so it can receive input data and return predictions.


Why Deploy a Model?

✅ Allow users and applications to use your model easily.
✅ Integrate machine learning into production systems.
✅ Automate predictions for workflows and products.


Tools for Deployment

Popular options include:

  • FastAPI (lightweight, Python-based, easy to use)
  • Flask
  • Docker (for containerization)
  • Cloud services (AWS, GCP, Azure)

In this tutorial, we’ll use FastAPI for simplicity.


Example: Deploying a Scikit-Learn Model with FastAPI

1️⃣ Install Dependencies

In your project environment, install FastAPI and Uvicorn:

pip install fastapi uvicorn scikit-learn pandas

2️⃣ Train and Save Your Model

First, train and save your model using scikit-learn:

# train_model.py
import pandas as pd
from sklearn.linear_model import LinearRegression
import joblib

data = {
    'Hours': [1, 2, 3, 4, 5],
    'Scores': [10, 20, 30, 40, 50]
}
df = pd.DataFrame(data)

X = df[['Hours']]
y = df['Scores']

model = LinearRegression()
model.fit(X, y)

# Save the model
joblib.dump(model, 'linear_model.pkl')

Run this script to save linear_model.pkl.


3️⃣ Create the FastAPI App

# app.py
from fastapi import FastAPI
from pydantic import BaseModel
import joblib

app = FastAPI()

# Load the trained model
model = joblib.load('linear_model.pkl')

# Request schema
class InputData(BaseModel):
    Hours: float

@app.post("/predict")
def predict(data: InputData):
    prediction = model.predict([[data.Hours]])
    return {"prediction": prediction[0]}

4️⃣ Run the FastAPI App

Run the server using Uvicorn:

uvicorn app:app --reload

Your API will be available at http://127.0.0.1:8000.


5️⃣ Test Your API

You can test your deployment using:

  • The interactive docs at http://127.0.0.1:8000/docs
  • Sending a POST request using curl or Postman:
curl -X POST "http://127.0.0.1:8000/predict" -H "Content-Type: application/json" -d '{"Hours": 6}'

Conclusion

🎉 You have successfully:

✅ Trained and saved a machine learning model.
✅ Created a FastAPI application to serve your model.
✅ Sent requests to get predictions from your deployed model.


What’s Next?

  • Explore Docker to containerize your FastAPI app for easier deployment.
  • Deploy to cloud platforms (AWS, GCP, Azure) for public access.
  • Learn about model monitoring to track performance in production.

Join our SuperML Community to share your deployment projects and get feedback!

Back to Tutorials

Related Tutorials

🔰beginner ⏱️ 50 minutes

Dimensionality Reduction

Learn what dimensionality reduction is, why it matters in machine learning, and how techniques like PCA, t-SNE, and UMAP help simplify high-dimensional data for effective analysis.

Machine Learning2 min read
machine learningdimensionality reductiondata preprocessing +1
🔰beginner ⏱️ 50 minutes

Genetic Algorithms

Learn what genetic algorithms are, how they mimic natural selection to solve optimization problems, and how they are used in machine learning.

Machine Learning2 min read
machine learninggenetic algorithmsoptimization +1
🔰beginner ⏱️ 40 minutes

Introduction to Natural Language Processing (NLP)

A clear, beginner-friendly introduction to NLP, explaining what it is, why it matters, and its key tasks with practical examples.

Machine Learning2 min read
nlpmachine learningdeep learning +1
🔰beginner ⏱️ 45 minutes

Limitations of Machine Learning

Understand the key limitations and fundamental limits of machine learning to set realistic expectations while building and using ML models.

Machine Learning2 min read
machine learninglimitationsbeginner