# Building a Sentiment Analysis App with Streamlit and Hugging Face Transformers
In this tutorial, we will create a sentiment analysis app using Streamlit and Hugging Face's Transformers library. This app will classify the sentiment of a given text as positive, neutral, or negative.
# Prerequisites
Before we start, make sure you have Python installed on your system. You also need to install the following libraries:
transformers
numpy
scipy
streamlit
You can install these libraries using pip:
pip install transformers numpy scipy streamlit
# Step 1: Importing the Required Libraries
First, we need to import the required libraries and modules:
import streamlit as st
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
import numpy as np
from scipy.special import softmax
# Step 2: Loading the Pre-trained Model and Tokenizer
We will use a pre-trained model from the Hugging Face model hub. In this example, we are using the cardiffnlp/twitter-roberta-base-sentiment-latest
model.
model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
tokenizer = AutoTokenizer.from_pretrained(model_path)
config = AutoConfig.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
# Step 3: Defining Example Options
We will provide some example text options for the user to choose from:
options = {
"Option 1 - I love this! It's amazing.": "I love this! It's amazing.",
"Option 2 - Neutral text here.": "Neutral text here.",
"Option 3 - I hate this, it's terrible.": "I hate this, it's terrible."
}
# Step 4: Defining the Sentiment Analysis Function
Next, we define a function that takes a text input, processes it using the tokenizer and the model, and returns the sentiment scores.
def sentiment_analysis(text):
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
scores_ = output.logits.detach().numpy()
scores_ = softmax(scores_)
labels = ['Negative', 'Neutral', 'Positive']
scores = {l: float(s) for (l, s) in zip(labels, scores_[0])}
return scores
# Step 5: Creating the Streamlit Interface
Now, we create the Streamlit interface for our sentiment analysis function. This interface includes a text input, a button to trigger the analysis, and columns to display the results.
st.title('Sentiment Analysis app')
st.write('This app classifies text as having a positive, neutral, or negative sentiment.')
option = st.selectbox(
"Choose an option to fill the text input:",
options.keys()
)
input_text = st.text_area(
"or write to the input text",
value=options[option]
)
result = {
"Negative": 0,
"Neutral": 0,
"Positive": 0
}
if st.button("Analyze"):
result = sentiment_analysis(input_text)
col1, col2, col3 = st.columns(3)
col1.metric("Negative", str(round(result["Negative"] * 100, 2)) + '%', "")
col2.metric("Neutral", str(round(result["Neutral"] * 100, 2)) + '%', "")
col3.metric("Positive", str(round(result["Positive"] * 100, 2)) + '%', "")
# Step 6: Using cnvrg to launch the App
- Click Apps in your project.
- Select Generic App.
- For Command, type the command required to run your app.
streamlit run streamlit-app.py --server.port 9200
- For Image, select a relevant Docker image or custom image that suits your application.
- For Compute, select the appropriate compute resources to run your application.
- Click Publish. cnvrg will deploy your application based on the provided configuration.
Here is the complete code for the sentiment analysis app:
import streamlit as st
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
import numpy as np
from scipy.special import softmax
model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
tokenizer = AutoTokenizer.from_pretrained(model_path)
config = AutoConfig.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
options = {
"Option 1 - I love this! It's amazing.": "I love this! It's amazing.",
"Option 2 - Neutral text here.": "Neutral text here.",
"Option 3 - I hate this, it's terrible.": "I hate this, it's terrible."
}
def sentiment_analysis(text):
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
scores_ = output.logits.detach().numpy()
scores_ = softmax(scores_)
labels = ['Negative', 'Neutral', 'Positive']
scores = {l: float(s) for (l, s) in zip(labels, scores_[0])}
return scores
st.title('Sentiment Analysis app')
st.write('This app classifies text as having a positive, neutral, or negative sentiment.')
option = st.selectbox(
"Choose an option to fill the text input:",
options.keys()
)
input_text = st.text_area(
"or write to the input text",
value=options[option]
)
result = {
"Negative": 0,
"Neutral": 0,
"Positive": 0
}
if st.button("Analyze"):
result = sentiment_analysis(input_text)
col1, col2, col3 = st.columns(3)
col1.metric("Negative", str(round(result["Negative"] * 100, 2)) + '%', "")
col2.metric("Neutral", str(round(result["Neutral"] * 100, 2)) + '%', "")
col3.metric("Positive", str(round(result["Positive"] * 100, 2)) + '%', "")