DEPLOYING ML PROJECT ON HEROKU WITH FLASK

ML projects on Heroku with Flask

Hello everyone !! Welcome to the most awaited tutorial on deploying  AI ML project on Heroku with flask. It was during my College minor project. Our team decided to create a project on the ML domain. We installed a Jupyter notebook on our device, completed the project there itself. 

Going through lots of online free hosting platforms, we came through Heroku. First thing we had to do was to convert our python ipynb files into flask files. It was kinda difficult and time consuming to go through all the tutorials on the Internet but they weren’t enough. After some weeks of research we completed the project. 

Today, we are creating tutorials on how we created  and deployed our first ML project on Heroku using flask. So let’s begin:

TABLE OF CONTENT

  • 1. CREATING ML PROJECT
  • 2. SETTING UP FLASK & FIRST APPLICATION
  • 3. CONVERTING IPYNB TO FLASK APP
  • 4. SETTING UP HEROKU VIA CLI
  • 5. DEPLOYING ML PROJECT ON HEROKU

This might be a long tutorial but we will try to make it short and easy to understand. Before going through the tutorial let’s first create ML project.

1. CREATING ML PROJECT ON JUPYTER NOTEBOOK

We will not go through a detailed coding tutorial to create Ml project in this tutorial. If you are new to AI then you can check our previous tutorials  on AI and also visit our free AI codes sections where you can get detailed tutorials on making your first py AI projects along with the project code.

For this tutorial, we will use code from our previous ML project code tutorial “ Diabetes Prediction using K-means” 

2. SETTING UP FLASK & FIRST APPLICATION 

In this section, we will teach you to create a flask application and run it locally. Initially, create a folder name “flaskdemo”. Go to the folder via terminal using “ cd path/flaskdemo”

Install virtual environment inside the project folder, create virtual environment and activate it using the following terminal code.

Then you need to install flask on the environment using the following command :

The above command installs Flask version 1.1.2. This is the version you’ll use throughout this tutorial. Next, you need to create a requirements.txt file listing the project’s dependencies. You can use the python3 -m pip freeze command for this task:

Similarly you can install gunicorn as our server with command pip install gunicorn

CREATING FIRST FLASK APPLICATION

You can see the app.py folder in the project folder, replace the file with following codes.. Otherwise, create an app.py file with the following set of codes. 

RUNNING FILE LOCALLY 

You can find various ways to run flask applications locally on the internet. One of them is the “flask run” command. By default, Flask will run the application you defined in app.py on port 5000. While the application is running, go to http://localhost:5000 using your web browser. You’ll see a web page containing the message Hello World! 

During development, you normally want to reload your application automatically whenever you make a change to it. When you set FLASK_ENV=development, Flask will monitor changes to app files and reload the server when there’s a change. Otherwise, you had to stop and restart the flask application using the “flask run” command.

RENDERING HTML CONTENTS

We can display the html contents in the browser with the help of the render command. First we need to create a templates folder inside the project folder where we store HTML files. If we need to display any images then it can be stored inside a static folder. 

Now we need to render the html file from app.py flask application file:

Run the Flask application

http://127.0.0.1:5000/

3. CONVERTING IPYNB TO FLASK APP

Let’s go through the code of the project we have made on step 1. “Diabetes Prediction using K-means”. Here, we will remove the visualization and analysis part from the original code. We will only be filling the missing data, training and testing part. Hence our final code will be:

Since the code needs to be deployed on Flask, we need to set up the home page as “home.html” where we need UI to enter BMI value, Glucose level and Age. All these three pieces of information will be entered by the user and this will be transferred to the main program “app.py” where the actual process is carried out. The final prediction will then be displayed on our screen by rendering “display.html”.

Here we have set up all the coding parts. Now we need to install all the packages to run the code in flask. 

1. Pip install pandas

2. Pip install numpy

3. Pip install scikit-learn

Once installing the required packages, run the following shell command in terminal so that all the required packages details will be written in requirements.txt file.

After completing all these steps, execute the command “flask run” on the terminal. You can run your flask application by typing http://127.0.0.1:5000/ on your browser.

4. SETTING UP HEROKU VIA CLI

Throughout this section we will learn to host our ML project on Heroku. By the end of this tutorial you can access your project under a url over the internet.  

  1. Set up your Heroku account. Sign up here
  2. Fill all the required information and verify it using the email address.

To deploy our application via Command Line Interface (CLI), we need to install Heroku CLI locally. You can get the installation guide on Heroku Developer’s Documentation. Generally, for linux users we use the following shell command to download and execute it.

Now we are ready to set up Heroku CLI. Next step is to login to Heroku using the login credentials we had entered earlier. 

We will be redirected to the browser and the following screen will be rendered. Click Log In to complete the authentication process and start using the Heroku CLI:

ML project on Heroku with flask

FINAL PROCESS OF APPLICATION DEPLOYMENT

We assume you have the basic knowledge about Git as we are using Git & Heroku CLI to deploy our application. First, we need to define how the application runs, and this can be done by generating procfile.

Make sure to install Gunicorn and update the requirements.txt file using pip: 

You can find the codes here for deployment

5. DEPLOYING ML PROJECT ON HEROKU

ML project on Heroku with flask

This is one of the ways of creating Heroku applications. We can also create app from CLI but let’s continue with the manual way.

ML project on Heroku with flask

To upload our code, first we need to create a git repository which can be done by the following command. Then we need to add our files and commit. 

If you have created the Heroku application manually then you need to add our repository to the remote one:

If you haven’t, then you can create from terminal command :

And with that done, let’s upload the project by pushing it to Heroku:

Congratulations, your first ML project on Heroku with flask is live. You can test your app by running https://{your-project-name}.herokuapp.com/ on your browser. Or, you can execute the command “heroku open” on your terminal. 

CONCLUSION

Hooray !! We have deployed our first ML project on Heroku using the Flask framework. This is our first step. You can customize your app with more features and more UI designs. Feel free to reach us via our instagram channel (@aihub_). If you face any problems, we are always available to guide you. You can leave us a comment, we will respond as soon as possible. Till then, Stay Safe, Stay Healthy. Keep Learning !!! 

Codes of this tutorial can be on our github profile.

Search Keys:

deploy machine learning model flask
how to deploy machine learning models
deploy machine learning model on website
flask machine learning github
flask machine learning web app
deploy machine learning model for free
flask machine learning api
how to deploy flask app on heroku

deploy machine learning model flask, heroku
how to deploy machine learning models
deploy ml model on heroku
deploy machine learning model for free
deploy machine learning model on website
deploying machine learning models with flask for beginners free download
deploy machine learning model python
tensorflow on heroku

About Diwas Pandey

Highly motivated, strong drive with excellent interpersonal, communication, and team-building skills. Motivated to learn, grow and excel in Data Science, Artificial Intelligence, SEO & Digital Marketing

View all posts by Diwas Pandey →

Leave a Reply

Your email address will not be published.