Deployment of a Machine Learning model into Localhost

INSAID
5 min readJan 13, 2022

by Pronay Ghosh and Hiren Rupchandani

  • 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.

By deployment, we can say that the ML model is running on a system or any server and is able to work for real-time data. i.e, it can take raw input and process all of the operations it has been trained for and give the predicted output results.

In order to deploy a machine learning model into the Amazon EC2 first we will have to go through the following steps:

  • 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.

Follow us for more upcoming future articles related to Data Science, Machine Learning, and Artificial Intelligence.

Also, Do give us a Clap👏 if you find this article useful as your encouragement catalyzes inspiration for and helps to create more cool stuff like this.

--

--

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!