ML Model Deployment with Webhosting frameworks

asha Last Updated : 23 Feb, 2021
5 min read

This article was published as a part of the Data Science Blogathon.

Introduction

With the motivation of award-winning from Analytics Vidhya Blogathon3 continuing this series for my previous blog Machine Learning Model  – server less Deployment.

In Machine Learning problems we usually start by defining start by defining the problem statement followed by data collection and preparation, understanding of the data, and model building but, in the end, we want our model to be available for the end-users so that they can make use of it.

ML Model Deployment with Webhosting frameworks

Figure: From my previous article

We have many blogs till now for ML model deployment but from case to case its different, few may choose Webhosting frameworks or server less compute or cloud platform-specific frameworks or Webhosting frameworks can deploy some PASS services like Heroku, or the same Webhosting framework can deploy using GCP compute engine, Azure VM services or AWS Ec2, etc…

There are many ways, from that, I have chosen one option to demonstrate in my blog i.e. ML model deployment using flask framework Heroku

Here, we cover the following topics :

  1. Major types of model deployments
  2. Save model and Create a web app using Flask and commit code in GitHub
  3. Create an account in Heroku
  4. Connect GitHub repository in Heroku
  5. Deploy the model

Let’s discuss this in detailed

1. Major types of model deployment

  •       Web Hosting Framework
  •       Server less Compute
  •       Cloud Platform specific frameworks

In my previous blog discussed “Server less Compute” now going to discuss “Machine Learning Model Deployment” using Web hosting Framework Flask

Let’s see a brief about flask :

The previous blog in Blogathon3 discussed server less compute  given link in the reference, now focusing on machine learning model deployment using Flask

Flask – a web services framework in python

Now, you might think what is a web service? Web service is a form of API only that assumes that an API is hosted over a server and can be consumed. Web API, Web Service Flask is a web service development framework in Python.

Flask Installation in python through pip

pip install flask

Flask framework comes with an inbuilt light-weighted web server that needs minimal configuration, and it can be controlled from our Python code.

Here, the code is used to create a simple Web-API which upon receiving a particular URL produces a specific output.

From flask import Flask

app = Flask(__name__)

@app.route(“/”)

def hello():

Return(“welcome to ML API”)

If__name__ == ‘__main__’:

App.run(debug=True)

We created an instance of the Flask class and passed in the “name” variable. This variable will be “main” if this file is being directly run through Python as a script. If you imported the file instead, the value of “name” would be the name of the file which you imported.

The hello() method is responsible for producing an output. Whenever your API is properly hit (or consumed). In this case, hitting a web-browser with localhost:5000/ will produce the intended output

Steps to be followed for Model deployment using Flask web framework

  • Serializing trained model parameters to disk

  • Serializing ‘scikit learn’ models using JSON, Pickel, and ‘joblib’

  • Including pre-processing steps as a part of model serialization

  • Building a text preprocessing and classification pipeline in scikit learn

  • Deploying the classification model to a Flask web application.

Serializing scikit-learn model – parameter serialization /deserialization

Choose json/ packle /joblib to serialize

JSON – require knowledge of what parameters to serialize because we use many models with many parameters

Pickle – Can work with any Python object not just with model estimator objects

Joblib – Setup pipeline object – all preprocessing steps and final state of the pipeline is our model that we apply to data. Sequence Apply to, series entire pipeline rather than serializing only parameters, pipeline used for test data also.

It can work great with a large multidimensional array, training and prediction data should be processed using the model parameters.

2. Save model and Create a web app using Flask and save code in Github

Steps to start model deployment

My GitHub repositions consist of “heroku_Flask_sal_deployment“, “DeployMLmodelUsing_Flask” from that I have chosen salary prediction based on experience, test_score, and interview_score regression model.

  1. Explaining about Data, code, and models

  2. Complete code details given in my GitHub README main branch  code can find in my master branch

This project has four major parts :

  1. model.py – This contains code for our Machine Learning model to predict employee salaries observed on training data in the ‘hiring.csv’ file.

  2. app.py – This contains Flask APIs that receive employee details through GUI or API calls, compute the precited value based on our model, and returns it.

  3. request.py – This uses the requests module to call APIs already defined in app.py and display the returned value.

  4. templates – This folder contains the HTML template to allow the user to enter employee detail and displays the predicted employee salary.

Run below coming in your command prompt

Using the Linear Regression algorithm create the model and serialize the model using pickle, reload and test the model using simple inputs.

python model.py 
<script src="https://gist.github.com/Asha-ai/8a94f5ef6a62faed06c95d9522f2e5ae.js"></script>

Finally, This would create a serialized version of our model into a file model .pkl

Start Flask API using the command

python app.py

<script src="https://gist.github.com/Asha-ai/81cd520b7f1716cd1db70c4a08899713.js"></script>

By default, the flask will run on port 5000.

Navigate to URL http://localhost:5000

Run the flask app:

flask Webhosting frameworks

3. Create an account in Heroku

What is Heroku :

Heroku is a cloud platform as a service supporting several programming languages. One of the first cloud platforms, it supports many programming languages, but now supports Ruby, Java, Node.js, Scala, Clojure, Python, PHP, and Go.

Create a Heroku account:

heruko webhosting framework

Connect GitHub repository:

 

Connect GitHub repository: webhosting framework

Choose master branch : Upload all code files in maser branch

Heroku generates URL: https://sal-prediction-flask.herokuapp.com/

Code References:

My GitHub Repository:

Readme in the main branch

Code files at the master branch

please make sure your code is in the master branch before connecting with HEROKU

Conclusion

Become a Full-Stack Data Scientist by learning various ML Model deployments and the reason behind my detailed explanation with screenshots is at the initial days I struggle a lot for learning ML Model deployment, So I decided my blogs should useful to data science enthusiasts.

Thanks for reading hope It helps you! and will meet you with my next blog: Deploy ML Model using “Cloud platform  Services“.

Please share your Comments and Suggestions!

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.

Have 4+ years of experience in data science field, working in telecom domain With a strong foundation in Python, machine learning, deep learning, natural language processing (NLP), and artificial intelligence (AI).

Responses From Readers

Clear

Muhammad Abdullah
Muhammad Abdullah

Can you please write a article on how to deploy ML models with FAST api on AWS cloud using Lambda functions or EC2 or Sage maker, any of them.

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details