Deploy and share your sentiment analysis app using Streamlit Sharing

A tutorial on creating a sentiment analysis application in under 30-minutes.

Rhys Kilian
6 min readAug 15, 2021
Streamlit logo
Source: Streamlit

Imagine this, you’ve spent the last two weeks developing a killer sentiment analysis model on your local machine. You’ve experimented with five different models, tuned your hyperparameters to perfection and tested the performance on a range of scenarios.

But now a non-technical Product Manager wants to review your model. What do you do?

You could run a live demo? Your calendars don’t line up for weeks.
You could share your virtual environment? They don’t know how to code.
You could get a developer to deploy the model? Their task list is two weeks long.

Unfortunately, this is a scenario that many Data Scientists find themselves in. But thanks to Streamlit Sharing you can now deploy and share your model in less than 30-minutes without help from a developer.

The Goal of this Tutorial

In this tutorial, I will show you how to deploy a sentiment analysis model using Streamlit Sharing. A user will be able to enter their text and the model will classify it as ‘POSITIVE’ or ‘NEGATIVE’ sentiment, along with a confidence score.

To see a live demo of my application on Streamlit Sharing: click here.
To view my source code on GitHub: click here.

A screenshot of the app you will develop in this tutorial
A screenshot of the app you will build in this tutorial

Step 1: Installing Libraries

The first step is to ensure you have all installed the required libraries.

Streamlit

Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science.

To install:

pip install streamlit

You should also request an invitation for Streamlit Sharing which allows you to deploy, manage, and share your apps with the world, directly from Streamlit.

It may take up to 2-days to receive your invitation. However, I received my invitation within 24-hours, even after requesting it on the weekend.

🤗 Transformers by Hugging Face

🤗 Transformers provides general-purpose architectures (BERT, GPT-2, etc.) for Natural Language Understanding (NLU) and Natural Language Generation (NLG) with over 32+ pre-trained models in 100+ languages. In only a few lines of code, you can have a state of the art sentiment analysis model downloaded and ready to go.

First, you need to install one of, or both, TensorFlow 2.0 and PyTorch. Please refer to the TensorFlow installation page and PyTorch installation page regarding the specific install command for your platform.

When installed, run the following command:

pip install transformers

Step 2: Project Set-Up

All your code will need to be contained within a Python file (.py). I personally use Visual Studio Code that comes installed in Anaconda, however, you can also download your Jupyter Notebook as a .py file.

I’ve called my file sentiment_analyser.py.

Now, import the libraries you downloaded in Step 1:

import streamlit as st
from transformers import pipeline

In the same directory, create a text file called requirements.txt and copy the version of the libraries you installed in Step 1. When it comes time to deploy your app, Streamlit Sharing will install the libraries in this file.

streamlit==0.86
transformers==4.8.1
torch==1.9.0

At this point, you should also create a GitHub repo. If you’ve never used GitHub before, I recommend checking out their documentation to set up your first repo.

Step 3: Create the User Interface Using Streamlit

Finally, something fun! We will now start by creating the structure of your Streamlit web application.

First, let’s add a title and some descriptive text by adding the following code to your sentiment_analyser.py file:

st.title('Sentiment Analyser App')
st.write('Welcome to my sentiment analysis app!')

To run your app locally (before we deploy it), navigate to your development directory in your terminal and run the following command:

streamlit run sentiment_analyser.py

If all goes to plan, you should have your Streamlit app popping up in a new browser window.

First Streamlit application output
Your first Streamlit application

We will now build a simple form that contains a text area for user input and a submit button. Our ‘user_input’ variable will be passed into our sentiment analysis model in Step 4.

form = st.form(key='sentiment-form')
user_input = form.text_area('Enter your text')
submit = form.form_submit_button('Submit')

After refreshing your application, it should now look like this:

Streamlit application with user input form
Streamlit application with user input form

Step 4: Classify User Input Using 🤗 Transformers

Hugging Face has an excellent tutorial on their 🤗 Transformer library. For this tutorial, we will be using the pipeline object that will connect the default sentiment analysis model with its necessary preprocessing and postprocessing steps.

If you were to run the following code in a Jupyter Notebook or .py file:

classifier = pipeline("sentiment-analysis")
classifier("I've been waiting for a HuggingFace course my whole life.")

It would output the classified sentiment label (‘POSITIVE or ‘NEGATIVE’), along with a confidence score.

[{'label': 'POSITIVE', 'score': 0.9598047137260437}]

Step 5: Linking Streamlit and the Sentiment Analysis Model

By now, you should see that we already have the basic building blocks of our application. To finish our app, let’s classify our ‘user_input’ variable using the 🤗 Transformer library to give our sentiment label and confidence score:

classifier = pipeline("sentiment-analysis")    
result = classifier(user_input)[0]
label = result['label']
score = result['score']

However, we only want to run out model after the user submitted the form. We can also display the result in a green banner (st.success) for ‘POSITIVE’ sentiment and in a red banner (st.error) for ‘NEGATIVE’ sentiment:

if submit:
classifier = pipeline("sentiment-analysis")
result = classifier(user_input)[0]
label = result['label']
score = result['score']
if label == 'POSITIVE':
st.success(f'{label} sentiment (score: {score})')
else:
st.error(f'{label} sentiment (score: {score})')

Once complete, push your changes to your GitHub repo.

If you want to see all the code in my sentiment_analyser.py file, please check out my GitHub repo.

Step 6: Deploy Your App to Streamlit Sharing

You should now have a functioning application on your local machine. But we want to be able to share our application just by sharing a single URL using Streamlit Sharing.

Once you have received your invitation to register for Streamlit Sharing, you can create a ‘New app’ on your app dashboard. Enter your GitHub username/repo, the name of your branch and the name of the Python file that contains your code. It should look something like this:

Streamlit sharing configuration
Streamlit Sharing configuration

Once you hit ‘Deploy!’, Streamlit will look for your requirements.txt file in your repo and install all the required libraries. After it has finished installing, you should receive a message that your application has been deployed successfully and you should be provided with a URL. For example, you can find my app here.

Congratulations! You have just deployed your sentiment analysis application that you can now share.

Conclusion

In this tutorial, I have shown you how you can build a simple interface using Streamlit to capture user input, classify the sentiment using Hugging Face’s 🤗 Transformer library, and deploy your application using Streamlit Sharing. We have only scratched the surface of what is possible using Streamlit (and Hugging Face). But now you should have the basic tools to share your prototypes with your colleagues, friends and family.

I encourage you to experiment. How can you make this app faster? What other NLP models can you deploy?

Share your apps with me on Twitter or add me on LinkedIn. More than happy to answer any questions you may have!

--

--