Deployment Of Machine Learning Models

Introduction

When we talk about working on Machine Learning(ML) projects, our focus primarily lies on data cleaning, Exploratory Data Analysis(EDA), model selection followed by training and testing. However, we often tend to let slide the Deployment part of the model. Here, in this article, we’ll learn about model deployment, its significance, and how to deploy a basic ML model.

What is Model Deployment?

Exactly! Every machine learning enthusiast must know ‘What is Model Deployment?’ Deployment simply means the implementation of something. Model deployment refers to the integration of the model into a production environment  to derive powerful business insights based on given data.

Why is Model Deployment important?

An organization can only benefit from a machine learning model when the model’s insights are well received by the end-user. Imagine working on a ‘Loan Prediction Model’ for months and building a fairly accurate model.

now our model will be useful to the target audience (in this case, the bank) when it’s deploy into production. Hence, the bank gets an output of whether a customer should be granted a loan or not based on the input provided. 

How to deploy an ML model?

After gaining familiarity with model deployment and its importance, the obvious question that comes to our mind is, ‘How to put machine learning models into production?’.

Flask:  It is a small web application framework in python. We can use the flask framework to develop and deploy web apps. The image below depicts how this powerful tool can be used for connecting the webpage with the model. 

Django:  It is yet another Python-based web development framework. We can use this framework to deploy our ML models as it is a nice way of presenting the model wrap up in a web app but this will require comparatively more time and understanding of the framework.

Streamlit: It is an open-source web application framework that is very helpful to Machine Learning and Data Science teams as it is the quickest way of creating data apps. however, It’s a python-based framework and comes with various features for deploying models.

Model Deployment using Streamlit

Here, we are going to learn how to deploy a machine learning model onto a webpage using the Streamlit library. This particular library makes it very easy to integrate python codes into a web application that predicts the response of the target variable when the required inputs are provided.

The following steps need to be followed:
  • Create a machine learning model and train it.
  • Now take the model and dump it into a pickle file.
  • Once we have the pickle file that contains the machine learning model, we will build the frontend of the ml model using the Streamlit library.
  • We will also take inputs from the web application and supply it to the pickle file (that contains the model), get the outputs back, and display it onto the webpage.

The following command shows how to dump a trained machine learning model into a pickle file. Here, we have dumped a logistic regression model into a model.pkl file. ‘wb’ basically means we are going this in write mode, i.e. we can write on the pickle file, model.pkl.

After this, we move to the main program, where we import the streamlit library and then fetch the pickle file using pickle.load() function. We do this in the ‘read mode’.

After loading the pickle file, we can continue with the functions required for the model to work, followed by the HTML commands for creating the frontend of the webpage.

Finally, to run this, we can use the command streamlit run app.py on the terminal (the main program is named  ‘app.py’). This will create a local URL on the host, which redirects us to the webpage.

Deployment of ML web app on Cloud platform

The web app created using Streamlit is only accessible on the host server. So, to make it available to the other users, we need to deploy the model on some cloud platforms such as – Amazon SageMaker, Heroku, Azure, Google Cloud, etc.

Let’s discuss how to deploy a model on the Heroku platform.

Heroku is a cloud platform as a service (PaaS). It allows us to build, run, and operate applications on the cloud.

The three simple steps for deploying the ml model on Heroku are:
  • Upload all the python files used in creating the web app into a Github repository.
  • The three specific files that we need to have in the Github repository are: 
  1. requirements.txt: This file contains all the necessary packages and libraries (along with the version) used in the python coding.
  2. setup.sh:  This file allows Heroku to understand how to create a virtual environment for your python file.
  3. procfile:  It gives instructions as to which python file to run. It contains a single statement web: sh setup.sh && streamlit run app.py
  • Create an account on Heroku, link the Github repository to it, and click on the deploy button.

Customer-Friendly Deployment

Our job as a Data Scientist is not limited to building/developing models but we also need to have the perspective of a Software Developer. Every model that we create and deploy should be done with the intention to satisfy the end-user. One should always focus on keeping the webpage clear and precise. Nobody is interested in filling lengthy forms to get the desired output. 

Hence, we should always try and improve our models so that they meet the growing production demands.

written by: Jagriti Prakash

Reviewed by: Rushikesh Lavate

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *