A Practical guide on Deploying Machine Learning Project 101

Pawan Trivedi
Geek Culture
Published in
4 min readMay 27, 2021

A practical student guide on How to deploy your first ML project on Heroku.

In this article, you will be learning the approach for How to build a machine learning model, pack it flask, and deploy it on the cloud.

This article is divided into two parts —

  1. Building ML Model
  2. Deploying it on cloud

So, before starting, let’s see the flow of the project with the problem that we’re going to tackle and build an application to solve that and following is the technology stack for the project:-

Building Machine Learning Model

I am not going to dig deeper into the process of preprocessing, feature engineering, building a generalized model, and other fine tuning steps, rather I’ll take the data that I had used in my project and build a model onto that. So that I can focus on the main task.

So we built a reliable machine learning model to predict CO2 Emissions in different type of Cars and the dateset was taken from Kaggle[1].

For building the model we used Linear Regression algorithm, we trained the model on the data which we have preprocessed and fine-tuned as needed for the model.

You can download the data from Kaggle and make it ready for training the model once the data set is ready you can go through the following code: —

Using the above steps you trained a model and pack it into the pickle file which we’ll be using for the deployment. Now we’ll move to the deployment part as our model is ready.

Deploying it on cloud

So, now model is trained and ready in the pickle file and now Let’s start…

First, we create a new virtual environment using conda distribution, and it’s always a good idea to have a separate virtual environment for every python project and for more deep dive into refer this article[2].

By running the following command you can create a virtual environment C02 and by executing `python app.py` it will start a local server where web application will load using that you can make the prediction.

And to run this setup on your system you can download code from this repository and execute it in the same manner.

Now, that you have tested your machine learning model in the local system and as it is working fine it’s time to put it into the cloud so that everyone can access it.

Once your model is packed into the pickle file, you have to create a flask application, you can start creating flask app from scratch[3] or can use my template and modify it as per your requirement.

In C02 project, there are total 10 feature in which 6 were numerical such as (Engine Size, Cylinders, Fuel Consumption city, Hwy, etc.) and 4 were categorical such as(Fuel Types, Transmission type, Make, Vehicle Class). So for the numerical feature, I created box field for taking the input and for the categorical I created the drop-down menu to select from. For better a understanding of how I developed the front end check this HTML file.

By pressing Calculate co2 button model will return the predicted c02 once all the fields are filled and values are selected from drop down menus.

So to deploy it on Heroku, first push the code onto GitHub and following files are necessary for deploying the application on Heroku.

wsgi.py
requirements.txt
Procfile

Go to the Heroku and once you are done with the login process, the home screen will appear and now follow these steps: —

  • Click on create new app
  • Fill App name and click on create app
  • No need to choose a pipeline as of now
  • Select deployment method as GitHub and connect to GitHub account
  • Search the repository name where you hosted the code and click on connect
  • Choose branch(main) and Enable automatic deploys by clicking same the button(so whenever you’ll make a change in the code(GitHub) that will reflect in the application automatically)and click on deploy branch.
  • Now the application will build and once it is done you’ll get a URL to access that application or by clicking on view button you can see your application.

You can use the following graphics to have a better understanding of how to deploy an application on Heroku.

My application link: https://coemission.herokuapp.com/

--

--