Deploying ML model on Heroku using Heroku CLI — Part 1
If you are not familiar with Flask web app and and Flask API for Machine Learning model, go through following tutorials :
- Building Flask web application — Part 1
- Building Flask web application — Part 2
- Flask API for Machine Learning Model from scratch
In this article, we will focus on deployment specifically on Heroku platform. To know about model deployment challenges you can go through following blog :
https://neptune.ai/blog/model-deployment-challenges-lessons-from-ml-engineers
Step 1 :
Visit heroku, create your account and log in.
Create new app.
Step 2:
In the Flask API tutorial for ML model, we have created the folder. We will add few more files in it. So right now our folder structure looks like this :
Open VS code in this folder as shown below :
Step 3 :
Now that we are going to deploy our model on Heroku platform, it must know what are the required libraries our Machine Learning model requires to run. So we created requirements.txt file where all the required dependencies are enlisted.
I have given you this list. You can create your own too by creating virtual environment and running the app in this environment. Then run following command in terminal
pip freeze >requirements.txt
This file will be automatically created in the same folder. This is how I created it. But you don’t have to worry since I have given the list already.
Copy following code in requirements.txt file.
certifi==2021.10.8click==8.1.3Flask==2.1.2gunicorn==20.1.0importlib-metadata==4.11.3itsdangerous==2.1.2Jinja2==3.1.2MarkupSafe==2.1.1numpy==1.21.6typing_extensions==4.2.0Werkzeug==2.1.2zipp==3.8.0scikit-learn==0.24.2
Step 4 :
In VS code, create runtime.txt file and write following code in it. This is just to let Heroku know the Python version our app needs to get executed.
python-3.9.7
Step 5 :
Deploying a Python web application needs three things:
- A web server — it accepts requests and handles https connections.
- A WSGI application server (like Gunicorn) — Out of these, requests meant for application are passed on to the application server and respective application.
- Web application — The application code processes single request.
Python Web Server Gateway Interface (WSGI) is a way to make sure that web servers and python web applications can interact with each other. Gunicorn is a WSGI server.
In VS code, create wsgi.py file and write following code :
from main import appif __name__ =="__main__": app.run()
Step 6 :
In VS code create Procfile, it tells heroku environment to start execution from wsgi.py file and app is flask name in wsgi:app as written below :
web: gunicorn wsgi:app
Now that we are ready with all the required files, we will push this files to heroku platform.
Remaining part of deployment is given in next tutorial.