6 Simple Steps to Deploy NLP based Flask Application on Heroku

Aakash Goel
4 min readMar 26, 2020

--

Image 1: NLP Application hosted on Heroku Server

1. Objective

Deploy NLP (Natural Language Processing) based Flask Application on Heroku Server.

2. Assumption

  • Git is installed on your system
  • Your NLP based Flask Application is already ready to use and you are able to run on your local system (localhost — Refer URL bar in screen-shot: 127.0.0.1). Something like below:
Image 2: Flask application on local system
  • You have Linux System and for your reference, all screen-shots/experiments shown in this article are performed on machine with following specification
Image 3: System specification

3. Step by Step process

Lets start and see in simple steps how to deploy Flask application on Heroku Server.

A) Account on Heroku App

Follow 3 steps to have account on heroku

i) Create Account: Visit Link https://signup.heroku.com/ and fill all details

Image 4: Creating account on heroku

ii) Activate Account: Check your mail and click on the activation link received from noreply@heroku.com

Image 5: Account Activation Mail from Heroku

iii) Log In: Visit Link https://www.heroku.com/ and you should receive following screen after successful Log In

Image 6: Successful Log In to Heroku

B) Install Heroku CLI (command line interface)

Follow below commands

sudo apt install snapd
sudo snap install --classic heroku
Image 7: Heroku Installation

Assuming, your current working directory in terminal is root directory of flask application, execute following command for login to heroku via CLI.

heroku login -i
## If you get issue, heroku: command not found, Follow below commands
sudo vi /etc/environment
## If you not able to find :/snap/bin in /etc/environment file, add it, save;close;execute below command and finally should look like Image 8
## Saving and activate environment
source /etc/environment && export PATH
Image 8: Setting up environment for Heroku
## Again try for heroku login via CLI
heroku login -i
## If it asks for login credentials (Image 9), it means heroku CLI installation is successful
Image 9: Heroku Login via CLI successful

C) Create heroku application

## Current working directory in terminal should be root directory of flask application
source /etc/environment && export PATH
## create application with app name: "aakash-word-booster" - Image 10heroku create aakash-word-booster
Image 10: Application creation

D) Setup files required to deploy NLP based Flask Application

i) Procfile: Create file with name “procfile” at root directory location of your flask app. Write below line in file and here api is name of python file contain flask application with flask name app (app = Flask(__name__)).

web: gunicorn api:app

ii) requirements.txt: It contains all libraries with version required to run your flask application on heroku server. You can expand list based on your application.

gunicorn==19.10.0
flask==0.12
nltk==3.3

iii) nltk.txt: To download nltk_data on heroku, need to create nltk.txt at same root directory location of flask application and write nltk package name based on your requirement.

wordnet 

E) Deploy Flask Application on Heroku

## Initialization of git
git init
## attaching git to heroku application name - Image 11
heroku git:remote -a aakash-word-booster
## Use below command to validate above attaching command, it should display exactly - Image 12
git remote -v
## git adding complete flask application to it
git add .
## commit your flask application - Image 13
git commit -m "first heroku commit"
## Push your application to heroku - Image 14
git push heroku master
Image 11: Attacing git to heroku application
Image 12: Validation of git attach to heroku
Image 13: Output after git commit
Image 14: Output after git push

F) Test heroku application

Go to your browser and paste link (deployed to Heroku) mentioned in Image 14 i.e. visit link: https://aakash-word-booster.herokuapp.com/.

4. References

  1. https://aakash-word-booster.herokuapp.com/
  2. https://stackoverflow.com/questions/13965823/resource-corpora-wordnet-not-found-on-heroku
  3. https://help.heroku.com/4NOFQ22L/how-do-i-use-nltk-with-python-on-heroku
  4. https://medium.com/@gitaumoses4/deploying-a-flask-application-on-heroku-e509e5c76524
  5. https://devcenter.heroku.com/articles/heroku-cli#download-and-install
  6. https://stackoverflow.com/questions/12795498/heroku-command-not-found
  7. https://stackabuse.com/deploying-a-flask-application-to-heroku/

Hurray !! You learned how to deploy NLP based Flask Application on Heroku.

Please clap if article helps you and share with your friends as well.

Happy Learning !!

--

--