Fast machine learning model deployment

Yannawut Kimnaruk
5 min readMay 29, 2022
Image by KEHN HERMANO: https://www.pexels.com/th-th/photo/3881034/

❓ Why does model deployment matter?

After finishing machine learning model training, it is time to illustrate how the model work. You can send your code or a model pickle file for others to run but it might not be convenient, especially for your client who is not familiar with coding. Waiting for software developers to create a website/application and connect to the model API could take so much time and effort.

One of the fastest ways to showcase your machine learning model is to create a simple website to deploy your model on your own. Sound troublesome but try reading this article and you may realize that it is not that difficult with the use of the Streamlit.

🌎 Result

After finishing this article, this is what I hope that you could do.

An interactive website where users can input the values and the result from the model will simultaneously show corresponding to the inputted values.

💽Dataset and Model

In this article, I use the dataset in the below article to train the model.

I created a data pipeline to train the model. Then, I save the pipeline as a joblib file as described in step 8 of the link below.

You can understand this article without reading the above articles. Just knowing that this is a classification task when you input data to the model, will return the probability of the person changing to a data science job.

📤 Deployment

I will deploy the model using Streamlit.

1. Install Streamlit

If you use Anaconda, open Anaconda Prompt (You can use Window search).
Then, type the following line.

pip install streamlit

Test that the installation worked

streamlit hello

You can visit this website for more detail

2. Create a new Python file (.py)

I created a web.py in the same folder as the model joblib file (you can create a pickle file instead if you would like).

This is the entire code in web.py.

Let’s go through the code step by step

1. import the libraries

import streamlit as st
import pandas as pd
import joblib

2. Load trained model pipeline

clf_pipeline = joblib.load("pipe.joblib")

Use joblib.load to load a saved joblib file and save it as clf_pipeline.

If your joblib or pickle file is in another folder, specify the location of that file.

3. Steamlit part

The advantage of streamlit is that you can make a beautiful (enough) website without any UX/UI knowledge since it is like a block template.

I use only 4 streamlit commands in this code

  • streamlit.write: Printing text on the screen
  • streamlit.sidebar.header: Title of the sidebar
  • streamlit.sidebar.slider: Slider for numerical features. Input min_value, max_value, value, and step.
  • streamlit.sidebar.select_slider: Slider for categorical features. Input a list of options.
st.write("Change job prediction")# Sidebar - Collects user input features to perform prediction
with st.sidebar.header('Input your data'):
city_development_index = st.sidebar.slider('city_development_index', 0.0, 1.0, 0.0, 0.01)
relevent_experience = st.sidebar.select_slider('relevent_experience', options=[0, 1])
experience = st.sidebar.slider('experience', min_value=0, max_value=21, step=1)
last_new_job = st.sidebar.slider('last_new_job', min_value=0, max_value=5, step=1)
training_hours = st.sidebar.slider('training_hours', min_value=0, max_value=300, step=1)
gender = st.sidebar.select_slider('gender', options=['Male', 'Female', 'Other'])
enrolled_university = st.sidebar.select_slider('enrolled_university', options=['no_enrollment', 'Full time course', 'Part time course'])
education_level = st.sidebar.select_slider('education_level', options=['Graduate', 'Masters', 'High School', 'Phd', 'Primary School'])
major_discipline = st.sidebar.select_slider('major_discipline', options=['STEM', 'Business Degree', 'Arts', 'Humanities', 'No Major', 'Other'])
company_size = st.sidebar.select_slider('company_size', options=['50-99', '<10', '10000+', '5000-9999', '1000-4999', '10/49', '100-500', '500-999'])
company_type = st.sidebar.select_slider('company_type', options=['Pvt Ltd', 'Funded Startup', 'Early Stage Startup', 'Other', 'Public Sector'])

Note: This is simplified code to make it easier to understand Streamlit. Many adjustments could be done to enhance the user experience.

The result of this code will be something like this. (Read how to run in step no. 3)

Users can input values to the model by changing the slider on the sidebar. Then, the inputted values will be saved in the variables to input into the model.

4. Prediction

The format of input data for my pipeline is a dataframe, so user input will be changed to a dataframe before feeding to the model.

num_cols = ['city_development_index','relevent_experience', 'experience','last_new_job', 'training_hours']
cat_cols = ['gender', 'enrolled_university', 'education_level', 'major_discipline', 'company_size', 'company_type']
prediction_proba = clf_pipeline.predict_proba(pd.DataFrame\
(data=[[city_development_index,\
relevent_experience, experience,\
last_new_job, training_hours,\
gender, enrolled_university,\
education_level, major_discipline,\
company_size, company_type]]\
,columns=num_cols+cat_cols))

clf_pipeline.predict_proba is used to predict the probability of this person changing to a data science job.

5. Print the prediction result

Print acquired probability from step 4

st.write(f"Probability of this person changing to a data science job is {round(prediction_proba[0,1]*100,2)}%")

3. Run streamlit

It’s showtime!!

Open Anaconda Prompt and type each command before pressing Enter.

cd your_folder_location

streamlit run your_file_name

cd C:\Project\Job_Change_of_Data Scientists
streamlit run web.py

You will see 2 website URLs:
Local URL can only be opened from your computer.
Network URL can be accessed by other devices.

Local URL: http://localhost:8501
Network URL: http://192.168.1.104:8501

The browser will automatically open your Streamlit website. If not, you can type the URL by yourself.

When changing the value on the sidebar, the probability will also change.

Done!!!

Note: If your model requires a long-running time, it will show a running icon on the top right corner. You have to wait until the program finishes running before you see the result.

Conclusion

To quickly show the machine learning model result, you can use Streamlit to create a demo website where users can input values and observe the prediction from the model.

--

--