Â
Having the machine learning model developed is only half the job. The model is still not useful until it is put into production and provides business value.
Knowing how to deploy our model has become an essential skill for any data scientist, and many employers already expect us to be able to do that. So, it’s beneficial for any data scientist from any level to learn about model deployment into production.
This article will discuss how to deploy the machine learning model into production.
Without further ado, let’s get into it.
Â
Machine Learning Model Preparation
Â
We will start the guide by preparing the model we deploy into production. First, we will set the virtual environment for the whole tutorial. You can do that by using the following code in your terminal.
python -m venv myvirtualenv
Â
After you have installed and activated the virtual environment, you will need to install the required packages. Create the requirements.txt
file and fill it out with the following library list.
pandas
scikit-learn
fastapi
pydantic
uvicorn
streamlit
Â
After the requirements.txt
ready, we must install them using the following code.
pip install -r requirements.txt
Â
Once everything is ready, we will start developing our machine learning model. For this tutorial, we will use the diabetes data from Kaggle. Put the data in the data folder.
Then, create a file called train_model.py in the app folder. Within train_model.py
, we will train the machine learning model using the code below.
import pandas as pd
import joblib
from sklearn.linear_model import LogisticRegression
data = pd.read_csv("data\\diabetes.csv")
X = data.drop('Outcome', axis =1)
y = data['Outcome']
model = LogisticRegression()
model.fit(X, y)
joblib.dump(model, 'models\\logreg_model.joblib')
Â
You can change the location of the dataset and model path to your liking. I will put the model into the model’s folder.
We will skip all the data preparation and the model evaluation, as our aim in this article is to deploy the model into production. When our model is ready, we will prepare to deploy our model.
Â
Model Deployment
Â
In this section, we will create API for our model prediction and deploy them with Docker while testing them with the Streamlit front end.
First, ensure you already have a docker desktop installed, as we will test them locally.
Next, create a file called main.py
in the app folder and fill it with the following code to generate the API.
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
# Load the logistic regression model
model = joblib.load('../models/logreg_model.joblib')
# Define the input data model
class DiabetesData(BaseModel):
Pregnancies: int
Glucose: int
BloodPressure: int
SkinThickness: int
Insulin: int
BMI: float
DiabetesPedigreeFunction: float
Age: int
app = FastAPI()
# Define prediction endpoint
@app.post("/predict")
def predict(data: DiabetesData):
input_data =
'Pregnancies': [data.Pregnancies],
'Glucose': [data.Glucose],
'BloodPressure': [data.BloodPressure],
'SkinThickness': [data.SkinThickness],
'Insulin': [data.Insulin],
'BMI': [data.BMI],
'DiabetesPedigreeFunction': [data.DiabetesPedigreeFunction],
'Age': [data.Age]
input_df = pd.DataFrame(input_data)
# Make a prediction
prediction = model.predict(input_df)
result = "Diabetes" if prediction[0] == 1 else "Not Diabetes"
return "prediction": result
Â
Additionally, we will have a frontend web to try the API model we deployed. To do that, create a file called frontend.py in the app folder. Then, fill them with the following code.
import streamlit as st
import requests
import json
API_URL = "http://localhost:8000/predict"
st.title("Diabetes Prediction App")
st.write("Enter the details below to make a prediction.")
pregnancies = st.number_input("Pregnancies", min_value=0, step=1)
glucose = st.number_input("Glucose", min_value=0, step=1)
blood_pressure = st.number_input("Blood Pressure", min_value=0, step=1)
skin_thickness = st.number_input("Skin Thickness", min_value=0, step=1)
insulin = st.number_input("Insulin", min_value=0, step=1)
bmi = st.number_input("BMI", min_value=0.0, step=0.1)
diabetes_pedigree_function = st.number_input("Diabetes Pedigree Function", min_value=0.0, step=0.1)
age = st.number_input("Age", min_value=0, step=1)
if st.button("Predict"):
input_data =
"Pregnancies": pregnancies,
"Glucose": glucose,
"BloodPressure": blood_pressure,
"SkinThickness": skin_thickness,
"Insulin": insulin,
"BMI": bmi,
"DiabetesPedigreeFunction": diabetes_pedigree_function,
"Age": age
response = requests.post(API_URL, data=json.dumps(input_data), headers="Content-Type": "application/json")
if response.status_code == 200:
prediction = response.json().get("prediction", "No prediction")
st.success(f"Prediction: prediction")
else:
st.error("Error in making prediction. Please check your input data and try again.")
Â
When everything is ready, we will create the Docker file as the basis for our model deployment. You should fill in the code below in the file.
FROM python:3.9-slim
WORKDIR /app
COPY app /app
COPY models /models
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
EXPOSE 8000 8501
CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port 8000 & streamlit run frontend.py --server.port=8501 --server.enableCORS=false"]
Â
We will create the image with the Docker file ready and then deploy the model via container. To do that, run the following code in the terminal to build the image.
docker build -t diabetes-prediction-app .
Â
The code above creates the Docker image for our model container. Then, we will use the following code to make the API for model deployment.
docker run -d -p 8000:8000 -p 8501:8501 --name diabetes-prediction-container diabetes-prediction-app
Â
With everything ready, ensure the container runs and access the front end with the address below.
Â
You should have the front-end look like the image below.
Â
Â
If everything works well, congratulations! You just deployed your machine learning model to production.
Â
Conclusion
Â
In this article, we have gone through the simple way to deploy our model into production using the FastAPI and Docker.
Of course, there are still many things to learn from maintaining the model and monitoring it in production. Deploying it into the cloud system will require a different tutorial article, so stay tuned for the others.
I hope this has helped!
Â
Â
Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and data tips via social media and writing media. Cornellius writes on a variety of AI and machine learning topics.