NLP and Streamlit on Heroku

How I deployed my Streamlit based NLP application on Heroku

Rahul Bhattacharya
Towards Data Science

--

Photo by Karim Sakhibgareev on Unsplash

In this article, I will describe how I deployed my fake-news detection web-app on Heroku. The backend NLP model was built and trained using Spacy libraries. The UI was built using Streamlit.

Streamlit is an open source framework that provides APIs for quickly building nice data visualization web apps in Python. You can access their official website https://www.streamlit.io/ for more details.

Streamlit can be installed in your environment using the simple command:

pip install streamlit

The great thing about Streamlit is that it lets you build UI components using just Python and your favorite code editor, no prior experience needed with HTML, JavaScript etc. For a quick guide on how to build Streamlit apps, refer to: https://github.com/streamlit/streamlit

In my case, I have named my code file as StreamLitFakeNews.py. You can find my full code in my GitHub repository https://github.com/rahulbtchrya/fakenewsdetectai

The main components in StreamLitFakeNews.py are:

  1. load_model() : This will load the pre-saved model using the spacy.load method and obtain the built-in text categorizer pipe using the nlp.get_pipe method.
  2. predict() : This function receives the news text, feeds it to the text categorizer pipeline and processes the predictions.
  3. run() : This is the main function which contains the logic for displaying the UI components and processing the input and output.

Once the coding is complete, you can run it locally from the command prompt using the command:

streamlit run StreamLitFakeNews.py

This will spin up a webserver on your local machine and display the application in your browser. You can do your testing here to make sure everything works fine.

The next step is deploying the application on Heroku.

Heroku requires you to create a new GitHub repository for your application and link it with Heroku. This repository should contain all the artifacts needed for your projects ie. code files, the saved model, the requirements.txt file, images etc. Below is a snapshot of my repository:

Below are the main components:

  1. StreamLitFakeNews.py : This is my python code file.
  2. requirements.txt : This file contains all the environmental requirements.
  3. models : This folder contains my saved model artifacts.
  4. data : This folder contains my data and image files
  5. setup.sh : This file is required to run Streamlit in Heroku.
  6. Procfile : This is a file required by Heroku to run the application. It basically tells Heroku what command to execute. In my case it contains only one line:
web: sh setup.sh && streamlit run StreamLitFakeNews.py

The filename for Procfile should start with an upper-case P, otherwise Heroku will not recognize it. Also, make sure that the repository is not private, else Heroku will not be able to access it during deployment.

And now, its time to deploy our app on Heroku!

If you haven’t already, create a free account on https://www.heroku.com/

Also download and install the Heroku CLI from https://devcenter.heroku.com/articles/heroku-cli. Its also good to have the Git CLI installed as well.

Once your Heroku account is created and you are logged in, you will land-up on the Heroku dashboard. Click on the “New” button and select the “Create New App” option. Provide a name and region and click the “Create App” button

Once the app is created, we need to connect it to our GitHub repository. Open the app in the Heroku dashboard and go to the “Deploy” tab. In the section named “Deployment method” , select the GitHub option. It will ask for the login credentials to your GitHub repository. Once you enter the details, it will be connected to your repository.

Now the app is ready for deployment. Go to the section “Manual Deploy” and click on the “Deploy Branch” button.

This will start the deployment process which would go on for a few minutes. Here, Heroku will use your requirements.txt file to install all the environmental dependencies. The Procfile will be used to start your application. You can view the deployment logs as the process goes on.

Once the deployment is complete, you can verify the app from Heroku command line using the command:

heroku apps

Before we can run the app, we need to add a Dyno. A Dyno is a container for running apps in Heroku. You can have one free dyno under the free account. We add a dyno using the command:

heroku ps:scale web=1

Once a dyno is allocated and running, we can go back to our app on the Heroku dashboard and check the “resources” tab:

Now our app is ready! We can access it by clicking the “Open app” button from the dashboard. You can also open it directly in the browser using the url:

https://<your-app-name>.herokuapp.com/

You can also view the runtime logs from the command line using the command:

heroku logs — tail — app fakenewsdetectai

Feel free to check-out my app: https://fakenewsdetectai.herokuapp.com/

Thanks for reading this article. As you can see, libraries like Streamlit make it super easy to create rich UI based applications with just a few lines of code. Also, with cloud-based platforms like Heroku, you can not only deploy your apps with just a few clicks, you can also host it for free and showcase it to the world. Developers have never had it so good!!

--

--