How to deploy and host Machine Learning model

Kaustuv
6 min readOct 26, 2019

--

One of the important phase in Data Science project is model deployment & hosting. Most Data Science tutorials and educational contents focus on model creation and increasing its efficiency and very few emphasize on model deployment & hosting. Many a time best model is not production deployeble due to its inherit complexity. A famous example is “The Netflix Prize” competition when Netflix found out that the best awarded model is too complex to be deployed into production.

Model deployment means showcasing your model to end user through web based REST services. In this tutorial we will perform end to end development of a Data Science project from problem specification till deployment & hosting for a simple yet practical use case.

Problem Statement : HR of an XYZ company needs your help in deciding how much salary she should offer to a new hire of the sales team. She has observed new hire is offered higher salary compare to existing employee with same or more experience. This, she feels, is an incorrect practice. As a Data Scientist your job is to help her standardize salary across sales department through your salary prediction model. For starting up, she has shared data set consisting of current employees experience and their corresponding salary

Solution : We will break our work into 3 tasks. First, create a supervised regression model for salary prediction. Second, develop a web application using flask and third, host flask application using some platform as a service (PAS) portal.

The complete source code is available on GitHub

1. Create supervised regression model

Our problem is of type supervised regression learning. We will train a linear regression model. Our data set is a plane text .csv file with two columns, one for experience level and another for salary. You can view it here. Model is build inside model.py file as shown below.


# model.py
# Building a regression model
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle
import joblib
# Importing the data set
dataset = pd.read_csv('dataset/Sales_Salary_Data.csv')

# separate feature & target
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

# Splitting the data set into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)


# Fitting Simple Linear Regression to the Training set
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# we can use either pickle or dump to save the model
#pickle.dump(regressor, open('model.pkl','wb'))
joblib.dump(regressor, 'model.pkl')

If you have created any python ML model previously then this code will be very easy and self explanatory. Note the last part where we have saved model as model.pkl file . There are two ways of saving the model one using pickle and other using joblib library. Once you execute model.py, model.pkl file will get created. Try opening this and you will see unreadable characters because of its serialized nature.

Next we will build a flask app and deploy our model on localhost.

2. Build flask application

Flask is a micro web framework written in Python. Micro-framework because it does not require particular tool or library. However it supports extension that can add application features. Flask is light weight and particularly suitable for small and mid size model deployment.

app.py

# import flask
from flask import Flask, render_template, request, redirect, url_for
from sklearn.externals import joblib
app = Flask(__name__)
loaded_model = joblib.load('model.pkl')
@app.route("/")
def root():
return render_template("index.html")
@app.route("/predict", methods=['POST'])
def make_prediction():

if request.method == 'POST':
exp = request.form['exp']
X = [[float(exp)]]
[prediction] = loaded_model.predict(X)
salary = round(prediction, 2)
msg = "Standard salary for provided experience of " + str(exp) + " years, would be: ₹ " + str(salary) + "/-- "

return render_template("index.html", prediction_text= msg)
if __name__ == '__main__':
app.run( debug=True)

Above app.py is our flask app. First, we have defined flask constructor name and loaded our serialized model , then routed home page to index.html, index page is html form, from where user will submit experience value.This will further posted and fetched inside make_prediction function, make_prediction will predict salary using our regression model.

Below is our index.html. It is basically a html form to enter candidate experience(in years) and posts to ‘/predict’ URL. Flask follow a standard project structure. All .html are inside templates folder.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Base ML model</title>
<style>
body {
background-color: #E6E6FA
}
</style>
</head>
<body>
<h1> Predict standard salary from candidate experience </h1>
<form action = {{ url_for('make_prediction')}} method = "post">
<p>Enter candidate experience (in yrs):</p>
<p><input type="float" name = "exp" pattern="[+-]?([0-9]*[.])?[0-9]+" oninvalid="setCustomValidity('Plz enter a valid experience value')"
onchange="try{setCustomValidity('')}catch(e){}" /></p>
<p><input type = "submit" value="predict"/> </p>
</form>

<br></br>
<h3>{{ prediction_text }} </h3>

</body>
</html>

By default flask executes at port 5000 on local host. When you execute this flask app and open URL http://localhost:5000 in browser, you will see web app running.

Congrats! You have created web framework over you model and can now play with it in you laptop but how others will use it ? Remember you are building this for HR. How will she use it? For this you need to host it.

3. Host your application on pythonwnywhere.com.

Large organizations have in-house clusters for their hosting services. Startups mostly use public clouds such as AWS and GCP. There are also Platform as a Service (PAS) portals that provisions hosting of applications.They require minimal setup. Few portals even allow us to host applications free. Specially small applications such as the one we have build. In this tutorial we will host our flask application using one such portal pythonanywhere.com. It is customized for python based framework deployment such as Django and Flask and is very easy & quick to deploy.

Below are steps to host our flask app on pythonanywhere.com. You can also refer this video for the whole setup

1 . Sign up & login : First sign up and login into https://www.pythonanywhere.com/

2. Setup virtual environment : Setup virtual environment and install required dependencies. Navigate to console tab, start new bash console and execute below commands in terminal (I have tested with python version 3.7, edit with your repective python version).

# make virtual environment 
mkvirtualenv --python=/usr/bin/python3.7 my-virtualenv
# install required dependencies
pip install sklearn
pip install flask

3. Initiate web app. Navigate to web tab and click on `Add new web app`. You will be asked to select domain name (select default), framework name (select manual configuration) and python version(select version same as of virtual environment).

4. Customizing web app for flask application : Open WSGI configuration file(under web > code) , uncomment flask portion, set project directory path and update your flask app name. Now upload flask application file(app.py), saved model file (model.pkl) and template directory with index.html inside specified project directory path. Below is the extract of WSGI configuration file for our flask application setup.

import sys
## code to underneath the home directory. So if you just ran
## "git clone git@github.com/myusername/myproject.git"
## ...or uploaded files to the directory "myproject", then you should
## specify "/home/datasciencemodel/myproject"
path = '/home/datasciencemodel/'
if path not in sys.path:
sys.path.append(path)
from app_file import app as application

In above example project directory is set as ‘/home/datasciencemodel/’ and flask app name is app. Customize in accordance with your user name and app name.

5. Reload website : Navigate to web tab and reload your website.

Web app is hosted and running at`username.pythonanywhere.com.` Play with this and share with other.

Thank for reading, if you found this article useful, please like. Happy Coding…:)

--

--