Build a Sentiment Analysis App with Hugging Face and Streamlit
Build a Sentiment Analysis App with Hugging Face and Streamlit
Sentiment analysis helps computers understand if text expresses positive, negative, or neutral emotions. In this tutorial, we’ll build a web app that analyzes emotions in text using Hugging Face transformers and Streamlit. You’ll learn how to use pre-trained AI models to create a professional sentiment analyzer that can process any text input and provide confidence scores for its predictions.
Let’s get started.
Step 1: Setting up the environment
Before we start coding, we need to set up our development environment with all the necessary tools.
Create a project directory
First, create a new folder for the project and navigate into it:
mkdir sentiment-analysis-app cd sentiment-analysis-app
Set up a Python virtual environment
A virtual environment keeps the project dependencies isolated from other Python projects on your computer:
python -m venv sentiment_env # Activate on Windows: sentiment_env\Scripts\activate
# Activate on Mac/Linux: source sentiment_env/bin/activate
Install required libraries
Install all the libraries we’ll need for sentiment analysis:
pip install transformers torch streamlit pandas plotly
Here’s what each library does:
- transformers: Provides access to Hugging Face pre-trained models
- torch: The deep learning framework that runs the models
- streamlit: Creates web interfaces with Python
- pandas: Handles data in table format
Create project files
Create two Python files in your project directory:
- sentiment_utils.py - Contains helper functions
- app.py- Main application file
This will be our final project structure:
sentiment-analysis-app/├── app.py ├── sentiment_utils.py
Step 2: Build a sentiment analysis with Hugging Face Transformers
Loading pre-trained models from Hugging Face
Let’s start by creating functions to load and use sentiment analysis models. Open sentiment_utils.py and add this code:
from transformers import pipeline import streamlit as st @st.cache_resource def load_model(model_name): """Load and cache the sentiment analysis model.""" try: return pipeline("sentiment-analysis", model=model_name, return_all_scores=True) except Exception as e: st.error(f"Error loading model {model_name}: {str(e)}") return None
The @st.cache_resource decorator saves the model in memory so it doesn’t reload every time someone uses the app. The pipeline function creates a ready-to-use sentiment analyzer that can process text and return sentiment predictions.
Creating a function to analyze text sentiment
Add this function to analyze any text:
def analyze_sentiment(text, classifier): """Analyze sentiment of the given text.""" if not text.strip(): return None try: result = classifier(text) return result[0] except Exception as e: st.error(f"Error analyzing sentiment: {str(e)}") return None
The analyze_sentiment() function takes text input, sends it to the classifier, and returns results showing whether the text is positive or negative, along with confidence scores.
Adding visual elements with emojis
Make the results more user-friendly with emojis:
def get_sentiment_emoji(label): """Map sentiment labels to emojis.""" emoji_map = { 'POSITIVE': '😊', 'NEGATIVE': '😞', 'NEUTRAL': '😐', } return emoji_map.get(label.upper(), '🤔')
When the sentiment analysis model processes text, it returns results in the following format:
[ {'label': 'POSITIVE', 'score': 0.9998}, {'label': 'NEGATIVE', 'score': 0.0002} ]
The scores represent confidence levels on a scale from 0 to 1. A score of 0.9998 means the model is 99.98% confident the text is positive, while 0.0002 indicates only 0.02% confidence for negative sentiment.
Step 3: Building a Streamlit interface
Setting up the streamlit app structure
Create your main application in app.py:
import streamlit as st import pandas as pd from sentiment_utils import load_model, analyze_sentiment, display_single_result # Set page configuration st.set_page_config( page_title="Sentiment Analysis", page_icon="💬", layout="centered" ) def main(): st.title("💬 Sentiment Analysis with Transformers") st.markdown("Enter text below to analyze sentiment using a state-of-the-art mo
Implementing model loading with progress indicators
Add the model loading section:
# Load the best model only model_name = "siebert/sentiment-roberta-large-english" # Initialize model with loading message with st.spinner("Loading model... This may take a moment on first run."): classifier = load_model(model_name) if classifier is None: st.error("Failed to load sentiment analysis model. Please refresh the page.") return
The model loading section handles three essential tasks:
- Loads the siebert/sentiment-roberta-large-english model for accurate predictions
- Shows a loading spinner so users know the model is being loaded (especially useful the first time)
- Displays an error message and asks users to refresh the page if the model fails to load
Loads the siebert/sentiment-roberta-large-english model for accurate predictions
Shows a loading spinner so users know the model is being loaded (especially useful the first time)
Displays an error message and asks users to refresh the page if the model fails to load
Creating an interactive text input interface
Add the text input area and analyze button:
# Text input area text_input = st.text_area( label="Enter your text:", placeholder="Type or paste your text here... (e.g., 'I love this new product! It works perfectly.')", height=150, max_chars=1000 ) # Analyze button if st.button("Analyze Sentiment", type="primary", use_container_width=True): if text_input.strip(): with st.spinner("Analyzing sentiment..."): results = analyze_sentiment(text_input, classifier) if results: display_single_result(results, text_input) else: st.warning("⚠️ Please enter some text to analyze.") if __name__ == "__main__": main()
Displaying results with metrics and tables
Back in sentiment_utils.py, add the display function:
def display_single_result(results, text_input=None): """Display sentiment analysis results with metrics, table, and chart.""" if not results: return st.subheader("Analysis Results") # Get the sentiment with highest confidence best_result = max(results, key=lambda x: x['score']) # Display metrics col1, col2 = st.columns(2) with col1: st.metric( "Predicted Sentiment", f"{get_sentiment_emoji(best_result['label'])} {best_result['label']}", f"{best_result['score']:.2%}" ) with col2: st.metric("Confidence Score", f"{best_result['score']:.2%}") # Display all scores in a table st.subheader("Detailed Scores") df = pd.DataFrame(results) df['score'] = df['score'].apply(lambda x: f"{x:.2%}") df['emoji'] = df['label'].apply(get_sentiment_emoji) df = df[['emoji', 'label', 'score']] df.columns = ['', 'Sentiment', 'Confidence'] st.dataframe(df, use_container_width=True, hide_index=True)
- The display_single_result() function accomplishes three key objectives: Shows the main predicted sentiment clearly with an emoji and confidence score.
- Displays all sentiment results in a neat table with labels, scores, and emojis.
- Makes the output easy to understand and visually appealing for users.
Adding model information display
Enhance the app with model details:
# Display model info with st.expander("ℹ️ Model Information"): st.write(f"**Model:** {model_name}") st.write("This model is fine-tuned on 15 datasets and achieves state-of-the-art performance.") st.write("It classifies text as either POSITIVE or NEGATIVE sentiment.")
Step 4: Exploring different Hugging Face models
Now, let’s compare popular Hugging Face sentiment analysis models:
How to switch between models
To use a different model, you need to change the model name in your code. This can be done using the following code:
# For Twitter analysis model_name = "cardiffnlp/twitter-roberta-base-sentiment" # For multilingual support model_name = "nlptown/bert-base-multilingual-uncased-sentiment" # For fast performance model_name = "distilbert-base-uncased-finetuned-sst-2-english"
Step 5: Running and Testing Your Application
Now that you’ve built the sentiment analysis app, let’s run it and test its functionality.
Running the application
To run the application, navigate to the project directory and make sure the virtual environment is activated, then start the Streamlit app:
streamlit run app.py
This command will start the app, and you should see output similar to this:
Local URL: http://localhost:8501 Network URL: http://192.168.1.100:8501
You can now view the Streamlit app in the browser. To view the app, open your web browser and go to http://localhost:8501 .
Let’s now test the Application.
Testing the application
Screenshot of Sentiment analysis application using Hugging Face and Streamlit
Try these sample texts to see how well your sentiment analyzer works:
- Positive examples: “I absolutely love this new smartphone! The camera quality is amazing, and the battery lasts all day.” “Thank you for the excellent customer service. You exceeded my expectations!”
- Negative examples: “This product is terrible. It broke after just one week of use.” “I’m really disappointed with the slow delivery and poor packaging.”
- Mixed/neutral examples: “The hotel was okay. Good location, but the room was small.” “The meeting has been scheduled for next Tuesday at 3 PM.”
- “I absolutely love this new smartphone! The camera quality is amazing, and the battery lasts all day.”
- “Thank you for the excellent customer service. You exceeded my expectations!”
- “This product is terrible. It broke after just one week of use.”
- “I’m really disappointed with the slow delivery and poor packaging.”
- “The hotel was okay. Good location, but the room was small.”
- “The meeting has been scheduled for next Tuesday at 3 PM.”
When we test the app with this text, “I absolutely love this new smartphone! The camera quality is amazing, and the battery lasts all day.” It produces the following output:
Screenshot of the sentiment analysis tool using Hugging face and Streamlit
The first time you run the app, it may take a moment to download and load the model. Subsequent analyses will be much faster thanks to Streamlit’s caching.
Conclusion
In this tutorial, we built a complete sentiment analysis web application. We:
- Set up a Python environment with Hugging Face transformers and Streamlit
- Created functions to load pre-trained models and analyze text sentiment
- Built an interactive web interface with text input and visual results display
- Learned about different sentiment analysis models available on Hugging Face
You now have a working sentiment analysis app that can classify any text as positive or negative with confidence scores. If you want to learn more about building apps using Hugging Face and Streamlit, you can check out this free course on Introduction to Hugging Face on Codecademy.
Frequently asked questions
1. Which Hugging Face model is best for sentiment analysis?
For most English text, siebert/sentiment-roberta-large-english offers the best balance of accuracy and speed. It’s trained on 15 different datasets and handles various text types well. However, if you’re analyzing tweets, use cardiffnlp/twitter-roberta-base-sentiment, and for multiple languages, try nlptown/bert-base-multilingual-uncased-sentiment.
2. Can I use Hugging Face models for other languages?
Yes! Many models support multiple languages. The nlptown/bert-base-multilingual-uncased-sentiment model works with over 100 languages. You can also find language-specific models by searching Hugging Face’s model hub for your target language.
3. What do the confidence scores mean?
Confidence scores show how sure the model is about its prediction. A score of 0.95 (95%) means the model is very confident, while 0.60 (60%) suggests uncertainty. The scores for all possible sentiments always add up to 1.0 (100%). Higher confidence usually means more reliable results.
4. How accurate are Hugging Face sentiment models?
The best models achieve 90-95% accuracy on standard test sets. However, accuracy can vary based on your specific text type. Models perform best on text similar to their training data. Always test with your own examples to verify accuracy for your use case.
5. Can I fine-tune these models for my specific use case?
Absolutely! If the pre-trained models don’t perfectly fit your needs, you can fine-tune them with your own labelled data. This process teaches the model to better understand your specific domain, whether it’s medical reviews, legal documents, or customer feedback. Hugging Face provides excellent tutorials on fine-tuning.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
- Step 1: Setting up the environment
- Step 2: Build a sentiment analysis with Hugging Face Transformers
- Step 3: Building a Streamlit interface
- Step 4: Exploring different Hugging Face models
- Step 5: Running and Testing Your Application
- Conclusion
- Frequently asked questions
Complete Payment to Unlock Full Guide & Source Code
You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.
Ready to access the complete step-by-step tutorial?
Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.
Build a Private AI Chatbot Using Google’s Gemma 3n
Next BlueprintBuild a Summer Reading List using ChatGPT
Related Blueprints & Guides
View all 166 →A Complete Guide to Vercel's AI SDK
Vercel AI SDK is a powerful TypeScript-first toolkit that provides a unified interface for building AI applications with Next.js and other modern frameworks. It eliminates the traditional complexity of managing multiple ...
AI Agents & AutomationAG-UI: How the Agent-User Interaction Protocol Works
AG-UI (Agent-User Interaction Protocol) is an open, lightweight protocol that standardizes the way AI agents interact with frontend apps. Consider it a common language that eliminates the need for custom integration code...
Generative AI FoundationsAI vs Generative AI: Understanding the Difference
Artificial Intelligence, or AI, is everywhere, from chatbots helping you shop to recommendation engines suggesting your next binge. Yet not all AI is the same, and the term is often used loosely, leaving many confused be...
Ready to build production-grade AI systems?
Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.