Background
Let’s have a quick look at what my prototype does first :

This article will focus on how we can deploy this NLP web application on sentiment analysis to the cloud. The in-depth topic of how to build the NLP model for this project will be covered in future article. The model was built using following data source and models.
Source Data : 1,600,000 labelled tweets from twitter api
Model : Logistic Regresion
Text Vectorizer : HashingVectorizer
Introduction
Pre-requisite
- A free Heroku account https://www.heroku.com/free – Highly recommend to get it verified to get 1,000 hours/month for free which allows your single application to have zero downtime since there is only ~740 hours/month.
- A free cron-job account https://cron-job.org/en/ – To set a job that wakes the web application periodically.
- Basic knowledge of Python and web framework (GET/POST).
Script Architecture



Deployment
1.Download the repository from the medium-article branch of my Github repository link : https://github.com/cmy113/sentiment-analysis-project/tree/medium-article
- Login to https://dashboard.heroku.com/apps and create a new app. Input your unique app name and choose the region which is closest to you.


- Download and install the Heroku CLI locally using this link: https://devcenter.heroku.com/articles/heroku-cli
- Navigate to Deploy section of your app and follow the instruction in Deploy using Heroku Git

Login to heroku, navigate to the project directory that you downloaded from Github in Step 1, initialise git, define the remote branch, add all the files, commit and finally push to Heroku


- The web application is up and running now, let’s navigate to the website and have a look to see if it’s working as intended.

- [Optional] Remember that we mention that we want to have a zero downtime application? In order to achieve that, we need to wake the web application periodically as Heroku will sleep the application after 30 minutes of inactivity. Of course, this will consume 24*31 = 744 hours of your free dyno hours. Skip this if you want to build different web applications using the free dyno hours.
Login to https://cron-job.org/en/. Click on CREATE CRONJOB and input the following:

We can then see that the GET request is executed successfully based on the history

- Done! Yay! We mange to build and deploy a zero downtime NLP web application to cloud with zero cost!
What Can You Further Modify
So after you have a quick taste on how to deploy the sample NLP web application, the next question is : how can you get more out of it? You can either use this repository as a kickstart for your existing NLP projects or there are extra 5 things that you can modify on top of it :
- Integrate the Heroku application with GitHub, you can fork my repository on Github and sync the repository with Heroku. Beware that I use medium-article branch for the demo of this article, I might be doing some occasional update to my main branch. In Heroku, you can enable automated deployment whereby it will allow you to recreate the web application everytime you push to your Github repository! I find it pretty magical! 🙂

- You will need to install a virtual environment on your local environment to modify/test my existing code locally. You can follow the sample steps outlined below to create the required virtual environment.
# Assume you have installed python 3 in your local environment
# I am using python 3.8.5 for this project but the project should work for any version of 3.8
# Can refer to this tutorial here as well on how to create virtual env https://www.youtube.com/watch?v=N5vscPTWKOk
# Identify where your python3 path is
# e.g. /usr/local/bin/python
which python
# Install virtualenv package
pip install virtualenv
# Navigate to home directory
# Create an Environments directory that stores all your virtual environment
# Navigate to Environments directory for the next step
cd
mkdir Environments
cd Environments
# Create an empty virtualenv named sentiment-analysis-project
virtualenv -p /usr/local/bin/python sentiment-analysis-project
# Activate the virtual environment
source sentiment-analysis-project/bin/activate
# Navigate to downloaded project repository from Github
# Install project dependency from requirements.txt in the virtual environment
cd Downloads/sentiment-analysis-project-medium-article
pip install -r requirements.txt
# You might need to download wordnet from nltk if you have not done it before. Open python terminal and run below command:
# python
# import nltk
# nltk.download('wordnet')
# Please follow this if you have trouble installing the nltk dataset
https://stackoverflow.com/questions/38916452/nltk-download-ssl-certificate-verify-failed
# Finally trigger the application
python wsgi.py
# Yay! Time to test/modify the code based on your liking and requirement
You should be able to access the web application locally by going to http://127.0.0.1:5000/
- Edit the model.py based on how you want to pre-process your input text as this project was focusing on Twitter styled data. You could also change the vectorizer used as I was using TfidfVectorizer initially but I had problem deploying it to Heroku as the massive vocabulary was causing the memory to overload in the free tier version of Heroku. Using HashingVectorizer will overcome this limitation.
- Replace LogisticRegression.pickle file in the static folder with your own model file and test it’s corresponding output either locally/Heroku web application. Beware that not all model offer probability score like Logistitc Regression. The current precision of my model based on the 1.6 million Twitter dataset is about 0.81 (there’s still a lot of room for improvement!).
- Improve the web visualisation by editing the css/html files, integrating with Bootstrap as well as changing the images/gifs/icons based on your preference. I have kept the design simple and barebone so that anyone could understand the code easily and further modify the visualisation with ease.
Conclusion
I sincerely hope that this article could help anyone who has built their own Natural Language Processing (NLP) model on Sentiment Analysis but do not know how to deploy it visually (something that I trampled upon when I was learning NLP) or for those who is keen to find out on how to deploy a basic, long-running web application for free! I will also leave my app running at https://cmy-sentiment-analysis.herokuapp.com so anyone can visit it anytime! I have tried my best to keep my codebase relatively clean and simple with lots of comments so anyone with limited knowledge of Python/web framework should be able to pick it up real quick! Do comment below if you find this article useful or have any suggestions for me to improve. Cheers!