How to deploy your ML model on the web
We are using Flask API and HeroKu to deploy the trained machine learning model on the web.
For beginners to Data Science, it must be challenging how to productionize your ML model. We will be using Flask Api to wrap the model and then we will deploy the API to Heroku for wider use.
First Building a Flask API
Flask is a popular micro framework for building web applications. Since it is a micro-framework, it is very easy to use. Flask is a web framework for Python, meaning that it provides functionality for building web applications, including managing HTTP requests and rendering templates.
We will run the following commands on the terminal: -
Create a virtual environmentpython3 -m venv vir_env
Activate the virtual environmentsource vir_env/bin/activate
Install all the packages we’ll needpip3 install flask gunicorn sklearn
Save a list of packages and versions that production will need to installpip3 freeze > requirements.txt
Now inside the same directory create new files with the same name
app.py
wsgi.py
procfile (has no extension)
Open the directory on your code editor and then open app.py and load the pickled model and make changes according to the parameters required by the model. (So, it can take those parameters from the user) Here is an example of the app.py I have trained a model that gives the present value of the car after giving certain values as input
This article is not about how to use Flask Api so I will keep this simple. We need to import json and pickle, you can also add templates to the app.py so it can make the web app look colorful e.g. index.html and use CSS to style it.
Add the following to wsgi.py
from app import app
if __name__ == "__main__":
app.run()
Add “web: gunicorn app:app” to procfile
Run the Flask API locally on the system by simply running the python app.py on the command line
Copy the address and paste it on your local browser and you will see the working API with your model inside it
Note that all the html templates, css and model.pkl should be in the same directory and properly linked in app.py.
Deploy to Heroku
Define Your Process Types
The next step is to create a file called a Procfile. This will define the process types that tell Heroku how to deploy your application. You can create a Procfile using this command:
touch Procfile
What you type in here will vary depending on the type of application you are deploying. For instance, you may use web: flask run
if you are deploying a Python Flask application.
Create an application
Once you’ve set up a Procfile, you are ready to create an app. You can do so by running this command:
heroku create app-name-here
Substitute app-name-here
for the name of your application. Once this command has run, you’ll see an output that looks something like this:
Substitute "app-name-here" for the name of your application. Once this command has run, you'll see an output that looks something like this:Creating app... done, ⬢ app-name-here
If you navigate to the domain for your application, you’ll see a page that says no app exists yet. This is because we haven’t deployed our application to Heroku yet. That’s what we are going to do in the next section.
Set Up Git and Deploy Your App
You need to use the Git command in order to deploy your application to Heroku. To do so, we’ll need to do some more setup.
First, initialize a repository in your project folder and commit your code:
git initgit add *git commit -m "Push code"
This will create a Git repository on our local computer for our project and add all of our code to a commit with the message Push code
. Once we’ve run these commands, we are ready to deploy our application to Heroku.
Run this command to deploy your app to Heroku:
git push heroku master
It may take a few minutes between running this command and your application being available on the Internet for people to use.
This is because Heroku will need to install all the dependencies for your project. The more dependencies you have, the longer this process will take. However, after you deploy your project for the first time, you’ll notice this process typically speeds up.
Once our application has deployed, we can navigate to the URL of our application and see it live on the Internet:
While deploying you’ll see the URL given to your app from Heroku. This is where you’ll send your API request. In this case
Car Price (capricepredict.herokuapp.com)
That’s it! If you’re running an application that requires additional configuration, you may need to use the heroku run
command to configure your app. For instance, you may need to use heroku run
to migrate the database for your project, if you are using one.
There are a lot more things one can add while deploying their model on the internet like security, authentication, tools to manage and update database etc. But this should do for beginners. ;)