Deployment of a Machine Learning model into Localhost

  • In the previous article, we saw what are the different types of AWS Services.
  • Now is the time to deploy a machine learning model into Amazon EC2.
  • However, in order to do so, we will first have to deploy our machine learning model into the localhost.
  • Hence, in this article, we will deploy a trained machine learning model into the local host.
  • Drop the model into a pickle file
  • Creating the web app template
  • Deploy the web app into the localhost

Drop a model into a pickle file:

  • In order to drop a model into a pickle format at first, we will have to make sure that the preprocessing of the model has been done properly.
  • Next, we will have to check how the model is performing on unseen data.
  • After finalizing the best fit we will have to drop the model in a pickle format.
  • In order to learn more, you can refer here.
Image made by carbon

Creating the Web App template:

  • Now after creating the pickle file we will have to make a web application template for the same.
  • This template should be able to take a user input from the end-user, run the model and then display the predicted output.
  • This should be named templates.html and we need to place this HTML file into the templates folder.
  • The following is the UI that you’ll be able to see.

Deploy the model into the localhost:

  • In order to deploy the web application into the local host, we will first have to install a few applications such as pycharm or Spyder as the IDE(Integrated Development Environment) for the project.
  • First, we will have to make an app.py file. Here, we will first have to call the necessary libraries such as Flask, render_template, and request.
  • Followed by this we will have to call other operational libraries such as NumPy and pandas.
  • Then, we will have to create a function that can render the templates.
  • This function will call the templates.html file and then route it with the POST method in the localhost.
  • After routing is done we will have to build the predict function.
  • The objective of making this function is that whenever the end-user clicks on the predict button the form is supposed to shoot the request to the backend.
  • From there as a response, the model is supposed to return the predicted output.
  • After this, we will load the pickle file.
  • We will have to keep a note that the pickle file has all the values of the model’s train and validation data.
  • Hence, at first, we will have to feed the data properly.
  • We will have to make sure that there is no index mismatch and each and every feature should be ordered as per the pickle file.
  • Hence, to avoid this issue it is always advised to have a cross-verification of the code with the notebook file.
  • Likewise one can use data.columns (say the name of the data frame is data) and use that order to drop the pickle and finally feed that list into the columns list.
  • Finally, we will have to make the predictions through the loaded pickle file.
  • If the response is returning 0 then probably there is an error you might want to check otherwise if the exit status is 1, then we will return the rendered template and the call to action will return the model’s prediction through the predict button.
  • Now we would have to run the code from pycharm IDE and we will receive one link in the console which includes the port number.
  • You can refer to the below image and once you click on that link you can see that the model is running on your localhost.

Conclusion:

  • So far in this article, we covered a high-level overview of how to deploy a machine learning model into the local host.
  • In the next article, we will see how to deploy a machine learning model into Amazon EC2.

--

--

--

One of India’s leading institutions providing world-class Data Science & AI programs for working professionals with a mission to groom Data leaders of tomorrow!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Face Recognition using Transfer Learning

Similarities in machine learning — Dynamic Time Warping example

“Speech Recognition” Science-Research, February 2022, Week 4 — summary from Astrophysics Data…

Human Learning Journal: Scrapping the web

Audio files to dataset by feature extraction with librosa

einsum - an underestimated function

Machine Learning Web Application deployment in 5 steps

Machine learning is much more than a good model

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
INSAID

INSAID

One of India’s leading institutions providing world-class Data Science & AI programs for working professionals with a mission to groom Data leaders of tomorrow!

More from Medium

BTS of Amazon S3

Access to data from ML model training

Exploring Amazon Athena

An exciting start on AWS SageMaker