Deploying Machine Learning Model Using Heroku
The main aim of this article is to learn how to make an end-to-end project with any of your machine learning problems.
About the Project
This is a complete machine learning project which aims to predict the selling price of the used car, based on certain features.
This project can be divided into 4 parts:
1. Building machine learning model
2. Frontend development
3. Backend development
These are the files which are required for our complete project:
- Model.pkl – This file is our machine learning model.
- Index.html – This file contains the frontend code for our web app.
- App.py – This file contains the backend code for our web app.
- Requirements.txt – This file contains all the required libraries and packages for this project.
- Procfile – This file contains the command for deployment.
- Anaconda navigator should be installed on your systems(If you haven’t then referred to this doc)
- You must have basic knowledge about using a jupyter notebook and visual studio code.
- You must have a GitHub account(But if you don’t have then create one with this link.
Before we start, this is my github repository link for this project, use it to understand the file structures in case of any doubt.
Set up the environment:
- Open Anaconda Powershell prompt(for windows you can easily search this by typing “Anaconda powershell prompt”)
- Create a virtual environment by typing the command in powershell:
conda create -n your_environment_name python=3.8 anaconda
Press “y” to complete the installation.
Activate this virtual environment by typing:
conda activate your_environment_name
Note: For every step of this project we need to use this virtual environment. So always make sure that you are in the right environment.
- Install these packages by typing this command :
pip install package_name
Where packages to be installed are :
- Now our environment is ready to use for the project. Meanwhile create a folder for our project manually, at the location where you want it. (do not close the Anaconda Powershell ).
- Open this empty folder, Copy the path of this folder. (by manual ctrl+l & ctrl+c). In the powershell paste this path as :
Below screenshot shows that how we can activate virtual environment and get inside the particular folder
- Now open the jupyter notebook from this particular path under this particular virtual environment.
To install jupyter notebook, type the command in powershell:
pip install jupyter
Note: Since this is a new virtual environment, we needed to install the jupyter notebook. We will not need to install it anytime after this. Open jupyter notebook by typing the below command in powershell :
You will notice that the jupyter notebook is opened in the browser. This jupyter notebook is launched at the very location of your project folder.
Since I have already created these files before writing this blog, so we can see these files here. But you will get the empty folder.
- Create a new file by clicking a new button on the top right side.
- This was all for setup. Let’s start with our machine learning model.
Building Machine Learning Model
Machine learning Life cycle:
- Problem definition
- Gathering data
- Data preparation and Exploratory data analysis
- Feature engineering and feature selection
- Model training and model evaluation
- Hyperparameter tuning and validation
The Below picture will make you understand the life cycle easily.
Image by author
Here in this blog, we are using a car dataset, which can be found here. With this dataset, we need to train our model to predict the selling price of the car based on certain features.
The complete ml model can be found here. Please refer to this notebook link. This notebook contains all the above steps of the machine learning life cycle. Every step is explained with the comments.
In case the notebook does not open in GitHub, then refer to this google colab link, this is the same notebook.
So, the first step i.e Building the ml model is almost completed.
But the twist is: How can we use this ml model to build an end-to-end project?
To answer this question, If you could see the notebook, there is an additional step at the very end of it. The step is ‘Saving the ml model’. This can be done by dumping our ml model into a pickle file and then loading this pickle file to wherever we require to.
In this way, we have created our model.pkl file
The next step is to create frontend code for our project i.e. index.html
We are doing the Front End part before the backend part in order to take the rough idea of this web app’s design structure that is the UI Part. Frontend development is all about outer beauty and looks. To be more precise, our frontend code will indicate how our web app is going to be displayed on the screen. The Frontend is the face of our model(or any project). We can make it as creative as we can. But since this is our first end to end project, we will use minimum styles in it.
The project aims to create a web app that can predict the selling price of old cars based on certain features. We have already built the model to do such predictions. But how can the user ask our web app to make predictions by giving his car’s details?
This can be done by asking the user to enter the details of the car into a html form. So, here we aim to create a html form that can take data from users. As we will be using minimum styles for this, so I have used internal CSS with the Html. To be precise, I have used the <style> tag to provide styles to my web page/web app or simply my Html form.
Steps to be followed:
- Go back to the Anaconda Powershell prompt.
- Type the command: code
This command will open vs code from the same folder location.
- Press ctrl+shift+p; a dialogue box will appear.
- Type in that search bar : Python: Select interpreter.
- A List of virtual environments will be displayed. Select our current virtual environment from that list, if it’s not selected.(my virtual environment for this project is car_price_prediction)
- Now, create a folder named “templates”, under this folder create a new file “Index.html” (by right-clicking on the folder in the file explorer section which is on the left side of the page.)
- Refer to this index.html file to get the frontend code. I have used a background image that can be found in the static/IMG folder, if you want you can add that image too.
- After completing the code, right-click on the index.html file from the file explorer section, click on the “copy path” option, open the browser and paste that path into it.
- In this way, you can test the frontend code. Make corrections in styles if you want any. Make sure that your HTML form is displayed correctly on the screen. As of now, you would not be able to predict anything, it would be just a simple webpage.
Since we have created the frontend part, the next step is to create the backend part.
Now we have built our ml model and also designed the structure for our web app. But these two exist independently now, so to connect them together we need the backend service.
There are many backend frameworks available that can be used here. For this project, we are going to use “Flask”, as it is a very light framework and it requires minimum coding to get started. So here we will create our main file i.e. “app.py”.
We will be using GET and POST methods to fetch the data.
Code for app.py
Since we are done with the model building, frontend, and also backend part, we can now test our model on our local server.
Run the command :
Note : You can run either in your vs code terminal or in the anaconda powershell. But just make sure that you are in the right folder and in the right environment. If you get confused about this thing, please read the starting part again, where I mentioned how to activate a particular environment and get into the particular location.
After running the command you will see the local server address displayed on the screen like “http://127.0.0.1:5000/”.
Just copy this address and paste it into the browser. Now we can test our model by filling up the values in the form and predicting the result.
Make sure everything is working fine. Now, it is time for deployment.
Since we have completed our project and also tested it on a local server, we can’t access this complete project on some other device or share it with others. So, to sort this problem we will deploy our project on a cloud platform.
For deployment, we are using Heroku. Heroku is a cloud-based platform where we can build and deploy our web apps. The advantage of Heroku is that unlike other cloud platforms it doesn’t charge bills. It provides a free service to get started. It is very simple and easy to use for beginners. So, in a few minutes with a few steps, you can get your app deployed. Other details on Heroku can be found here. Make sure you check this page out before moving further.
As we can deploy on Heroku in many ways but I prefer to connect to the Github repository and then deploy.
So, let’s get started.
Before deployment, we now need additional files. They are requirements.txt and Procfile.
What is requirements.txt and why do we need it?
If you remember when we have created a virtual environment at the very beginning of this project, we installed some required packages. The same thing will happen when we deploy our project on Heroku. It also needs to load all these required packages to run the project.
So, the requirements.txt file is nothing but the list of required packages.
How to create requirements.txt?
- Go back to your anaconda powershell within the same environment, and same project folder.
- Type this command in the powershell :
pip freeze > requirements.txt
- Notice that after running this file you get a new file named “requirements” in the project folder
Now, the next is to create a Procfile.
What is Procfile and why do we need it?
Do you remember how we have run our complete project on the local server? This was done by typing a simple command in the terminal and that simple command was :
But only we knew that this is our main file, when we will run the project through Heroku it also needs to run this same file. So, how can Heroku run this specific file?
This requirement is fulfilled by creating a Procfile. Procfile contains the list of commands which are required to run in order to start our project.
How to create Procfile?
- Create a simple file named “Procfile” (without a file extension) in the same project folder.
- Type the below command in this file :
web: gunicorn app:app
This will let the Heroku run app.py.
Uploading the code on Github:
- Open your Github account.( I am assuming you have a github account and a basic knowledge of it but if you are new to this please refer to this documentation.)
- Now create a repository on github and name it. You can refer to this doc.
- Push all the files into this repository(we will be connecting this repository to the heroku account in the next step). Let the branch be “main”. You can either use git bash or manually upload the files that I am leaving on to you. You can refer to this doc.
- Now our code is uploaded to the server.
- Click here to open the Heroku website.
- Log in if you already have an account. Otherwise, click on the ‘sign up’ button on the top right of the page.
- Fill the required details in the form and hit the “Create a free account” button.
- Set the password and login.
- Now you would have reached the “Welcome to heroku” page. From there click on the “Create a new app” button.
- On the new redirected page, type the app name(of your choice) and hit “create app” button.
- On the new redirected page, you can see the “deployment method” section. Click on the “connect to github” option.
- As soon as you click this button, an authorization page will appear. Click on the “authorize heroku” button. Now your Heroku account is connected to your Github.
- In the “Connect to github” section, type your repository name(the repository which contains this project) and click on the “Deploy branch” button.
- You will notice that some files will start installing and loading. After the whole installation is processed, you can see the message “ Your app was successfully deployed”.
- Click on the “view” button below this message to see the deployed web app.
- Copy the URL and share it wherever you want.
You can visit my web-app by clicking on this link.
Congratulations! We have created our first end-to-end machine learning project. Try creating more end to projects with creative UI designs or you can even reuse the code. You can find more datasets for machine learning problems at Kaggle. So this was all for this blog. As always I tried to explain everything from scratch. I hope you enjoyed learning with practical implementation.
Do upvote and share with your friends, if you find it useful. Let me know your feedback in the comment section.
Thanks for reading!