0% found this document useful (0 votes)
26 views

How To Deploy Machine Learning Models - by Gurami Keretchashvili - Towards Data Science

The document discusses how to deploy machine learning models on the web using Streamlit. It introduces Streamlit and describes the 7 steps to build and deploy an ML project using Streamlit, including building and saving an ML model, testing the model, creating a main file to run the web app, uploading the project to GitHub, and creating a Streamlit account. The steps are then demonstrated through an example of deploying an XGBoost model for fish weight prediction as a web app.

Uploaded by

OnlyBy Myself
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

How To Deploy Machine Learning Models - by Gurami Keretchashvili - Towards Data Science

The document discusses how to deploy machine learning models on the web using Streamlit. It introduces Streamlit and describes the 7 steps to build and deploy an ML project using Streamlit, including building and saving an ML model, testing the model, creating a main file to run the web app, uploading the project to GitHub, and creating a Streamlit account. The steps are then demonstrated through an example of deploying an XGBoost model for fish weight prediction as a web app.

Uploaded by

OnlyBy Myself
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

Open in app Get started

Published in Towards Data Science

You have 2 free member-only stories left this month.


Sign up for Medium and get an extra one

Gurami Keretchashvili Follow

Dec 29, 2021 · 5 min read · · Listen

Save

How to Deploy Machine Learning Models


The easiest way to deploy machine learning models on the web

Introduction
I will introduce the easiest way to deploy machine learning applications on the web.
In the previous notebooks, I have built machine learning models using linear and
tree-based models. It turned out that hyper tuned XGboost model performed best.
That is why today we will build a Fish Weight Prediction web application using
XGboost. In general, there are different options to deploy ML models, such as Flask,
Django, Streamlit, etc. Today I will use Streamlit because it is the easiest and faster
way to do it and it does not require any web development knowledge.

179 1

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 1/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

ML model deployment demo(Photo by the author)


Open in app Get started

∘ Introduction
∘ Fish Weight Prediction Application
∘ How was it done?
∘ Conclusion:

Fish Weight Prediction Application


You can see the Demo HERE.

You can see GitHub code HERE.

How was it done?


There are three main python files to build the application.

Photo by the author

1. model.py — Build ML model and Save it.

2. prediction.py — Test saved ML model.

3. main.py — the main file to run the web app.

Here are the 7 steps to follow in order to build and deploy the ML project by
yourself.

Step 1: Create a new virtual environment using Pycharm IDE.

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 2/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

Open in app Get started

Pycharm IDE, Photo by the author

Step 2: Install necessary libraries.

Here in requirements.txt, I created all necessary libraries to install. You can


download the file and install it using:

pip install -r /path/to/requirements.txt

or you can install each library one by one using pip.

Step 3: Build the best machine learning model and Save it.

model.py in this file, I created a machine learning model and saved it as a JSON file
best_model.json.

best_xgboost_model.save_model("best_model.json")

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 3/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

You can see the detailed explanation of how ML models are built in my previous
Open in app Get started
article.

Step 4: Test the loaded model.

prediction.py in this file, I test the loaded model and saved label-encoder classes as
a classes.npy file using.

np.save('classes.npy', label_encoder.classes_)

Step 5: Create main.py file

Step 5.1 Import libraries and dataset

import streamlit as st
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import xgboost as xgb
import numpy as np

st.header("Fish Weight Prediction App")


st.text_input("Enter your Name: ", key="name")
data = pd.read_csv("fish.csv")

Step 5.2: Load saved label encoder classes

encoder = LabelEncoder()
encoder.classes_ = np.load('classes.npy',allow_pickle=True)

Step 5.3: Load saved the best model

# load model
best_xgboost_model = xgb.XGBRegressor()
best_xgboost_model.load_model("best_model.json")

Step 5.4: Show dataframe on the web

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 4/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

If the checkbox is checked, display training data


Open in app Get started

if st.checkbox('Show dataframe'):
data

Photo by the author

Step 5.5: Select Fish Species

select the name of the fish out of distinct values of all the fishes.

st.subheader("Please select relevant features of your fish!")


left_column, right_column = st.columns(2)
with left_column:
inp_species = st.radio(
'Name of the fish:',
np.unique(data['Species']))

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 5/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

Open in app Get started

Photo by the author

Step 5.6: Select each value of features using a slider window

input_Length1 = st.slider('Vertical length(cm)', 0.0,


max(data["Length1"]), 1.0)
input_Length2 = st.slider('Diagonal length(cm)', 0.0,
max(data["Length2"]), 1.0)
input_Length3 = st.slider('Cross length(cm)', 0.0,
max(data["Length3"]), 1.0)
input_Height = st.slider('Height(cm)', 0.0, max(data["Height"]),
1.0)
input_Width = st.slider('Diagonal width(cm)', 0.0,
max(data["Width"]), 1.0)

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 6/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

Open in app Get started

Photo by the author

Step 5.7: Make prediction button

Once a user clicks the button, the species input is transformed from text into a
relevant number, then all the input features are concatenated with a specific shape,
and model prediction is done!

if st.button('Make Prediction'):
input_species = encoder.transform(np.expand_dims(inp_species,
-1))
inputs = np.expand_dims(
[int(input_species), input_Length1, input_Length2,
input_Length3, input_Height, input_Width], 0)
prediction = best_xgboost_model.predict(inputs)
print("final pred", np.squeeze(prediction, -1))
st.write(f"Your fish weight is: {np.squeeze(prediction, -1)}
Gram")

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 7/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

Open in app Get started

Photo by the author

Step 6: Upload local project to Github

You can use git to upload your local project or you can just drag and drop the folder
to an empty repository on GitHub.

Step 7: Create an account on Streamlit

The final step is to create an account and connect your GitHub repository by
clicking “From existing repo”

Streamlit, Photo by the author

Conclusion:
YES, it is free and easy. In general building, machine learning application consists
of mainly two parts. The first is building a model and the second is to deploy and
monitor it. Today we have done the deployment part using Streamlit. This simple
application just illustrates how the model is deployed and it can be a sample for
other big projects. In the following article, I will also try to deploy ML models using
another method as well.

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 8/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

If you would like to learn more about applied data science here is my new YouTube
Open in app Get started
channel — AI Academy with Friends

AI Academy with Friends


Share your videos with friends, family, and the world
www.youtube.com

I hope you enjoyed it and now can start creating beautiful apps yourself.

If you like my articles and want to see upcoming stories follow me on medium.

Gurami Keretchashvili - Medium


a Data Scientist is not the one who knows python, Sklearn,
TensorFlow, etc. But who is who knows how to play with the…
medium.com

Sign up for The Variable


By Towards Data Science

Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-
edge research to original features you don't want to miss. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review
our Privacy Policy for more information about our privacy practices.

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 9/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science

Get this newsletter Get started


Open in app

About Help Terms Privacy

Get the Medium app

https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 10/10

You might also like