Step-by-step guide to deploying an ML model using Heroku
Machine learning is a continuous process of collecting, processing data, extracting meaningful features, and finally modeling that refined data. After completing the cumbersome procedure, the life cycle did not stop there. To have user interaction on this model, we need to employ various deployment strategies on edge cloud-based, IoT devices, such as mobile phones. The deployment platform basically depends on the use case of the application. In this article, we will discuss cloud-based deployment using Heroku cloud service. Below are the main points listed which we will discuss.
- What is Heroku?
- Our ML model
- Build a web app
- Step-by-step deployment with Heroku
Let’s start by understanding what Heroku is.
What is Heroku?
Heroku is a Platform as a Service (PaaS) that allows developers to host serverless programs. This means that scripts can be written to serve one or the other for certain purposes. AWS (Amazon Web Services) hosts the Heroku platform, which is an infrastructure-as-a-service solution.
Heroku is a free platform with an uptime constraint of 500 hours. Applications are hosted on a dyno, which goes into sleep mode after 30 minutes of inactivity. This ensures that your app doesn’t eat up all of your free time when you’re not using it. Ruby, Java, PHP, Python, Node, Go, and Scala are all supported on the platform. Most data science beginners use this platform to gain experience in running and deploying models in the real world.
Our ML model
The ML model we are going to deploy aims to predict whether or not a certain patient will suffer from cardiac arrest based on 10 input features. The complete model building procedure, from pre-processing to evaluation, is well documented in this notebook.
Basically, while building a web application, we need to manage the front-end logic, back-end logic, and cloud service that will serve our application. The front-end logic is that part that we often see when we visit a website and give or mention our responses and that response is then processed by the back-end logic.
Here in our case, using the frontend, we will take the values from our model for which it will predict the results and in the backend, we will process these values with the model method. .to predict() to get a prediction. And with this prediction obtained, we will move further to the frontend.
In the next section we will see how we can prepare our model for the web interface, the minimum HTML file configuration and web framework configuration.
Build a web app
As stated earlier, we must have prepared our ML so that it can be easily interfaced with web frameworks and this process is usually referred to as model serialization and deserialization.
Various model formats are commonly used, such as Pickle, a task library from sklearn that transforms the Python object model into a binary stream in export format and allows the model to be integrated into technologies such as Java. We will use the Pickle module to export the model. As you can see from the notebook, the serialization and deserialization API is simple.
import pickle # serializing pickle.dump(model,open('Healtcare.pkl','wb')) # deserializing pic=pickle.load(open('Healthcare.pkl','rb'))
Now, to ensure the interaction link between this model and the user, we have taken advantage of web development frameworks such as the famous FLASK. It’s a simple framework that lets you build a server with just a few lines of code. Many features, such as authentication and RESTful nature, are not explicitly supported because it is a small package.
Before we go any further, let’s understand the directory structure of our project. I did all this web related development in VS Code and my working directory structure is as follows,
The static and templates folders are used by the flask’s render_template module which essentially makes our HTML page simple. The template folder contains our main HTML file and the static folder contains images which are used in the HTML page.
Virtual contains all the packages we have installed for this model. Later we have app.py which contains the main application logic. Next to it we have a pickle file that we created earlier.
Next is Profile. A Procfile is included with Heroku apps and provides the commands the app will run on startup. A Procfile can be used to declare a wide range of process types, including your application’s web server.
The last one is the requirements.txt file which contains information about all the packages we used in this project and Heroku needs to install all these packages in order to run the application. This file can be easily created using pip as,
pip freeze > requirements.txt
Now let’s look at the app.py file where we wrote code for web applications.
# import dependencies from flask import Flask,render_template,request import numpy as np import pickle model=pickle.load(open('Healthcare.pkl','rb')) app = Flask(__name__) @app.route('/',methods=['POST','GET']) def new(): return render_template('new.html') @app.route('/predict', methods=['POST'] ) def predict(): # getting data data1=float(request.form['a']) data2=float(request.form['b']) data3=float(request.form['c']) data4=float(request.form['d']) data5=float(request.form['e']) data6=float(request.form['f']) data7=float(request.form['g']) data8=float(request.form['h']) data9=float(request.form['i']) data10=float(request.form['j']) # preparing for the prediction features=np.array([data1,data2,data3,data4,data5,data6,data7,data8,data9,data10]) pred = model.predict([features]) def statement(): if pred == 0: return 'Result:- The model has predicted that you will not suffer from any cardic arresst but you should take care of your self.' else: return 'Result:- You should consult with doctor, The model has predicted that you will suffer form cardic arrest.' return render_template('new.html',statement=statement()) if __name__=='__main__': app.run(debug=True)
Our web application will contain a single page, called the default or main page. the ‘/’ route is the home URL. This should, by default, take you to the index.html file, which serves as the main page.
When a user lands on the main page, we want to do a specific action. We built a function that would respond to user queries based on POST requests.
When the information is sent back to the web server, a user makes a POST request. We give the user the opportunity to fulfill these ten features. We get this information from an HTML file one by one using the application form method by mentioning the key for each value.
We now turn the feature list into a NumPy array and save it to the feature list. Then, using the trained model, use the prediction model function to make predictions. Finally, the expected result is displayed on the web page.
Now to test this app just run the app.py file from your IDE terminal window and it will host your app locally as below.
Now, in order to facilitate this interface globally, we will deploy it to the cloud using Heroku, a free platform to deploy any web application.
Step-by-step deployment with Heroku
Heroku offers three different ways to deploy templates: Heroku git, GitHub, and the container registry. The Git technique is used in this article to deploy the model. the GIT and Heroku Command Line Interface (You can create and administer Heroku applications directly from the terminal using the Heroku Command Line Interface (CLI).) must be installed in our system and the installation procedure is similar to any installation procedure. software installation, just follow the default configuration settings.
Before going further, make sure you have created an account on Heroku, otherwise create it here.
Now, let’s start the deployment procedure. We will do it step by step.
Step 1: type heroku login in the terminal window of VS Code or any IDE you are working on or you can even use your operating system’s terminal window. After running this command, you will get to the Heroku login page where you need to login. After successful login, you will see the account details in a terminal window.
2nd step: After login, we need to initialize the current directory as a working git repository and add all the files in the directory. For this, you need to run the commands below sequentially one after the other.
git commit -m ‘First release’
Step 3: Then after creating the git repository, we need to tell Heroku to create our application by mentioning the application name, below is the command. Sometimes the name you choose may not be available in this case, just change it to another one.
Heroku Cardiac Arrest Prediction
Upon successful deployment of this command, Heroku will give the URL where our application will be hosted. For this deployment, the URL is
Step 4: Now we just need to push our repository to the server, this will create our application with all the dependencies from the requirements.txt file and you can also follow this procedure in your terminal window. The command to push the application to the web is as follows,
git heroku push master
After successful execution of this command, our application will be live at the URL mentioned above.
In this article, we covered how to deploy a machine learning model as an application on the web. We deployed our model directly from the terminal window, as mentioned before, the deployment can be done via Git-Hub also for this you need to create a master branch for the git-hub repository and upload all the files there and from the Heroku website, you can easily deploy the template.
All the files for this deployment are in this deposit.