0% found this document useful (0 votes)
69 views20 pages

Model Deployment GL

Uploaded by

ARCHANA R
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)
69 views20 pages

Model Deployment GL

Uploaded by

ARCHANA R
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/ 20

radha.diff@gmail.

com
FMEH37ZJ05

Model Deployment

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
What is Model Deployment

 Model Deployment is the application of a model for making


predictions on new data/ by taking user input

 Generally, building a model in Python or any other language


is not the last step
[email protected]
FMEH37ZJ05

 Whenever the final model is ready, it’s intelligence is used to


predict on newer data by creating a graphical user interface
where users can enter the data and get the predictions

 Common examples: Chat Bots, Auto-generated Text


suggestions, and Facebook’s auto tag suggestion, etc.
Proprietary content. ©Great Learning. AllThisRights
file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Key Features of Model Deployment Architecture
 Modularity: Specifically for Pre-Processing steps.

 Reproducibility: Provision for Backward and Forward compatibility.

 Scalability: Minimal impact on the response time when users are


increased.
[email protected]
FMEH37ZJ05

 Extensibility: Easy to modify for future tasks.

 Testing: the ability to test variation between model versions.

 Automation: eliminating manual steps wherever possible to reduce


error chances.
Proprietary content. ©Great Learning. AllThisRights
file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Architecture Comparison

[email protected]
FMEH37ZJ05

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Model Deployment Options

 REST (Representational State Transfer) API – API interface


required to integrate with modern enterprise applications.

 On-demand analysis via GUI- Drag-and-drop prediction interface


which allows users to get predictions when they need them.
[email protected]
FMEH37ZJ05

 Scoring code export - Model is available as an executable .jar file or


as Java source code, and can be deployed anywhere Java runs.

 Standalone scoring engine - Separate staging and production


environments so that models can be tested and implemented in a stable,
isolated environment. The Standalone Engine has the capability to run
imported models without ever touching the development server from which
they were exported.
Proprietary content. ©Great Learning. AllThisRights
file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Model Deployment Options

Deployment on Cloud and /or Big Data environment

Cloud Deployment – Different type of clouds. So choice is first made on the


type of cloud and then one of the model deployment technique discussed
in the previous foil.
[email protected]
FMEH37ZJ05

Big Data Environment – Key requirement is that model should be deployed


so that model is able to run both on the train and test environment without
changing the storage location in the Hadoop network.

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Model Deployment Platforms

 There are many platforms like Amazon Web Services, R Shiny,


Heroku Platform, Google Cloud, etc

 All these platforms provide free services to beginners for limited time
period with certain restrictions
[email protected]
FMEH37ZJ05

 In this session, we are going to use FLASK for Model Deployment on


Local Host.

 Also we will extend this FLASK model on a Heroku Platform

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Case Study I – Predict Salary

 WHO is a specialized agency of the UN which is concerned with the world population health. Based
upon the various parameters, WHO allocates budget for various areas to conduct various
campaigns/initiatives to improve healthcare. Annual salary is an important variable which is considered
to decide budget to be allocated for an area. We have a data which contains information about 32561
samples and 15 continuous and categorical variables. Extraction of data was done from 1994 Census
dataset. The goal here is to build a binary model to predict whether the salary is >50K or <50K

Data Dictionary
age: age
[email protected]
FMEH37ZJ05
workclass: workclass
fnlwgt: samplting weight
education: highest education
education-no. of years: number of years of education in total
marrital status: marrital status
occupation: occupation
relationship: relationship
race: race
sex: sex
capital gain: income from investment sources other than salary/wages
capital loss: income from investment sources other than salary/wages
working hours: nummber of working hours per week
native-country: native country
salary: salary
Proprietary content. ©Great Learning. AllThisRights
file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
What is FLASK

 Flask is a web framework that provides libraries to build


lightweight web applications in python.

 It is based on WSGI toolkit and jinja2 template engine.


 WSGI is an acronym for web server gateway interface which is a standard for
python web application development.
 Jinja2 is a web template engine to render the dynamic web pages.
[email protected]
FMEH37ZJ05

 Flask requires python 2.7 or higher

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application using Flask

 Create a “pickle” of the model (i.e. file with an extension as


.pkl)
 “Pickling” is the process whereby a Python object hierarchy is converted into a
[email protected]
FMEH37ZJ05
byte stream

# save the model to disk


Import pickle
pickle_out = open("adult_flask.pkl","wb")
pickle.dump(model, pickle_out)
loaded_model = pickle.load(open("adult_flask.pkl", "rb"))
result = loaded_model.score(X_test, y_test)
print(result)

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application using Flask

 Create an HTML page/file with or without CSS which would


be the index page
[email protected]
FMEH37ZJ05
 One Page could be created both to receive the user input and display the result or
separate page could be created to receive input and then to display result.

 These files should have an extension as “.html” and these files should be stored
in the folder “templates”

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application using Flask

 Create an file with an extension “.py” using python editor like


jupyter notebook.

[email protected]
# Import the library files
FMEH37ZJ05
import numpy as np
import pandas as pd
from flask import Flask, request,render_template
import pickle

# Initialise the flask


app= Flask(__name__)

# Define html file to get user input


@app.route('/')
def home():
return render_template('Adult.html')
Proprietary content. ©Great Learning. AllThisRights
file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application using Flask

# Prediction Function
def ValuePredictor(to_predict_list):
to_predict = np.array(to_predict_list).reshape(1, 12)
loaded_model = pickle.load(open("adult_flask.pkl", "rb"))
result = loaded_model.predict(to_predict)
return result[0]

# Output page and logic


@app.route('/result', methods = ['POST'])
[email protected]
FMEH37ZJ05 def result():
if request.method == 'POST':
to_predict_list = request.form.to_dict()
to_predict_list = list(to_predict_list.values())
to_predict_list = list(map(int, to_predict_list))
result = ValuePredictor(to_predict_list)
if int(result)== 1:
prediction ='Income more than 50K'
else:
prediction ='Income less that 50K'
return render_template("result.html", prediction = prediction)

# Main function
if __name__ == "__main__":
app.run(debug=True)
app.config['TEMPLATES_AUTO_RELOAD'] = True

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application using Flask

Flask
Templates
Adult.html
result.html
adult_flask.pkl
app.py
[email protected]
FMEH37ZJ05

@ Anconda prompt change the directory to where you have all the files like in our case,
folder is “Flask”. Once you are at the required directory type,
python app.py

In the output you will get


http://127.0.0.1:5000/

This is the URL of the application on your local machine, copy this in the
browser and run the application
Proprietary content. ©Great Learning. AllThisRights
file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
What is Heroku

 Heroku is a platform as a service (PaaS) that enables


developers to build, run, and operate applications entirely
in the cloud.
[email protected]
FMEH37ZJ05

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application on Heroku

 Create a login in Heroku Platform

 Create a new Application, follow the platform instructions


[email protected]
FMEH37ZJ05

 Now Application can be deployed in two different ways


 Create a repository on GitHub
 Use Heroku CLI to deploy application through a command line. For this download
Heroku CLI available on the Heroku home page and download another package
by the name “Git” .. (You can search this on google and download as per your
OS)

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application on Heroku

 Create a file “Procfile” without any extension (“P” is capital in


the name of the file) with the following content

[email protected]
FMEH37ZJ05
web:gunicorn app:app

Name of the .py file Name which is assigned to initialise flask

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application on Heroku

 Create a file “requirements.txt” which will have all the python


libraries with their version which are required for your
application. Basically Heroku platform will install these
[email protected]
FMEH37ZJ05
libraries as part of setup

Flask==1.1.1
gunicorn ==20.0.4
scikit.learn== 0.22.2
matplotlib == 3.2.1
seaborn==0.9.0
requests==0.11.1
numpy >=1.18.2
pandas >=0.25.1

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
Steps to Deploy Application on Heroku

@ Anconda prompt change the directory to where you have all the files like in our case,
folder is “Flask”. Once you are at the required directory type the following commands in the
same sequence. At the end it will create a link which can be used by anybody.
https://adultsalaryprediction.herokuapp.com/

[email protected]
FMEH37ZJ05

Proprietary content. ©Great Learning. AllThisRights


file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
FMEH37ZJ05

HAPPY LEARNING
Proprietary content. ©Great Learning. AllThisRights
file is meant Reserved. Unauthorized
for personal use by [email protected] only. use or distribution prohibited.
Sharing or publishing the contents in part or full is liable for legal action.

You might also like