Build a Sentiment Analyzer with Python and Hugging Face
Build a real sentiment analysis application with Python and Hugging Face Transformers. Learn how pretrained NLP models work, run inference locally, interpret confidence scores, process multiple reviews, expose the model through an API, and understand when sentiment models can fail.

Tools used: Python, VS Code, Jupyter, Hugging Face Transformers, PyTorch, Git, GitHub
Prerequisites: Basic Python syntax, functions, lists, and installing Python packages.
Build a Sentiment Analyzer with Python and Hugging Face
Can a Python program understand whether someone is happy, angry, disappointed, or satisfied by reading a sentence?
Yes — and you can build a working version without training a neural network from scratch.
In this project, we'll build a small sentiment analyzer using Python and Hugging Face Transformers.
By the end, you'll be able to give the program text such as:
"This laptop is excellent. The battery life is amazing."and receive a prediction similar to:
POSITIVE
Confidence: 99%+We'll start with the smallest useful implementation, then gradually turn it into something closer to a real application.
What we're building
The final idea is simple:
User enters text
│
▼
Python application
│
▼
Pretrained NLP model
│
▼
Sentiment prediction
│
├── POSITIVE
└── NEGATIVEFor example:
Input:
"The new update made the application much faster."
Output:
POSITIVEAnother example:
Input:
"The application keeps crashing and the support team never replied."
Output:
NEGATIVEThis is a text classification problem.
We are asking the model to classify a piece of text into a category.
The fastest way to see it working
Before discussing transformers, tokens, models, or neural networks, let's build the thing.
Create a project:
mkdir sentiment-analyzer
cd sentiment-analyzerCreate a virtual environment:
python -m venv .venvActivate it on Windows:
.venv\Scripts\activateOn macOS or Linux:
source .venv/bin/activateInstall Transformers with PyTorch support:
pip install "transformers[torch]"Hugging Face's current installation documentation uses this setup for installing Transformers with PyTorch support.
Your first sentiment analyzer
Create:
sentiment.pyAdd:
from transformers import pipeline
analyzer = pipeline("sentiment-analysis")
text = "The new Karyvio learning resources are extremely useful."
result = analyzer(text)
print(result)Run:
python sentiment.pyYou should receive a result containing a label and score.
Conceptually:
[
{
"label": "POSITIVE",
"score": 0.99...
}
]The exact score will depend on the model and input.
And that's already a working NLP application.
What just happened?
The important line is:
analyzer = pipeline("sentiment-analysis")The Hugging Face pipeline() API provides a high-level interface for inference across many machine learning tasks.
For sentiment analysis, it handles much of the work involved in preparing the text and passing it through a pretrained model.
The simplified flow is:
Your sentence
│
▼
Tokenizer
│
▼
Transformer model
│
▼
Classification
│
▼
POSITIVE / NEGATIVEYou didn't train the model.
You used an already-trained model for inference.
That distinction is important.
Training vs inference
There are two very different activities:
Training
Large dataset
│
▼
Machine learning algorithm
│
▼
Learn patterns
│
▼
Trained modelInference
New text
│
▼
Trained model
│
▼
PredictionOur project is performing inference.
We're taking a pretrained NLP model and asking it to make predictions on new text.
That is one reason pretrained models are so useful for developers.
You can build an application around an existing model without first becoming a researcher who trains a transformer from scratch.
Try real examples
Let's create a small collection of reviews.
from transformers import pipeline
analyzer = pipeline("sentiment-analysis")
reviews = [
"The laptop is fast and the battery lasts all day.",
"The application crashes every time I open it.",
"The documentation is clear and easy to follow.",
"The latest update made the website slower.",
"Customer support solved my problem quickly.",
]
for review in reviews:
result = analyzer(review)[0]
print(f"Text: {review}")
print(f"Label: {result['label']}")
print(f"Score: {result['score']:.4f}")
print()The output will look conceptually like:
Text: The laptop is fast and the battery lasts all day.
Label: POSITIVE
Score: 0.99
Text: The application crashes every time I open it.
Label: NEGATIVE
Score: 0.99The exact probabilities are model outputs, not guaranteed truth.
That distinction becomes very important in production.
What does the score mean?
Suppose the model returns:
{
"label": "POSITIVE",
"score": 0.97
}It is tempting to read this as:
"The model is 97% correct."
That's not what it means.
The score represents the model's confidence for that prediction according to its output distribution.
It does not guarantee that the prediction is correct.
For example:
Prediction:
POSITIVE
Score:
0.97doesn't mean:
97% probability that a human would agree.Model confidence and real-world accuracy are different concepts.
Let's make the code reusable
Instead of putting everything in one script, create a function:
from transformers import pipeline
analyzer = pipeline("sentiment-analysis")
def analyze_sentiment(text):
result = analyzer(text)[0]
return {
"label": result["label"],
"score": result["score"],
}
result = analyze_sentiment(
"This product is surprisingly good."
)
print(result)Now your application has a reusable interface:
analyze_sentiment(text)That makes it easier to use the model from:
- a command-line application
- a web application
- a REST API
- a batch-processing script
- a data-analysis workflow
Build a simple command-line version
Let's make the project interactive.
from transformers import pipeline
analyzer = pipeline("sentiment-analysis")
print("Sentiment Analyzer")
print("Type 'exit' to stop.")
while True:
text = input("\nEnter text: ")
if text.lower() == "exit":
break
result = analyzer(text)[0]
print(f"Sentiment: {result['label']}")
print(f"Confidence: {result['score']:.2%}")Now you can run:
python sentiment.pyand type:
I really enjoyed using this application.The model processes the sentence and returns its prediction.
You have now turned a pretrained model into a small interactive AI tool.
But how does the model understand text?
Computers don't process a sentence the way humans do.
A transformer model needs the text converted into numerical representations.
A simplified pipeline looks like:
"I love Python"
│
▼
Tokenizer
│
▼
Token IDs
│
▼
Transformer
│
▼
Representations
│
▼
Classifier
│
▼
POSITIVEThe tokenizer breaks text into tokens and converts those tokens into values the model can process.
For example, a sentence might conceptually become:
"I" → token
"love" → token
"Python" → tokenThe actual tokenization depends on the model and tokenizer.
You should not assume that every word becomes exactly one token.
Where Transformers come in
Transformers are a family of neural-network architectures that became extremely important for modern natural language processing.
They are used for tasks such as:
- text classification
- question answering
- summarization
- translation
- text generation
- named entity recognition
- speech-related tasks
- multimodal applications
Hugging Face's current Transformers documentation provides task-specific pipelines across text, audio, vision, and multimodal use cases.
For our project, we only need one task:
text classificationUse an explicit model
For a real project, you may want to specify exactly which model you're using instead of relying on a task-level default.
For example:
from transformers import pipeline
analyzer = pipeline(
"text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)
text = "The product quality is excellent."
result = analyzer(text)
print(result)The referenced DistilBERT model is a pretrained checkpoint fine-tuned on the SST-2 sentiment dataset and is available through Hugging Face's model hub. Its model card also documents its intended task and limitations.
Using an explicit model gives you a reproducible model choice.
Instead of:
Which model did the application use?you know:
This application uses:
distilbert/distilbert-base-uncased-finetuned-sst-2-englishThat matters when you deploy the project.
Why use a pretrained model?
Imagine trying to build sentiment analysis from zero.
You would need:
Collect text
↓
Label examples
↓
Clean data
↓
Split dataset
↓
Choose architecture
↓
Train model
↓
Evaluate
↓
Tune
↓
DeployThat can be a valuable learning exercise.
But it is not necessary for every application.
With a pretrained model:
Choose model
↓
Install library
↓
Load model
↓
Send text
↓
Use predictionThis is much faster for prototyping.
A real-world example: analyze product reviews
Imagine an e-commerce application receives thousands of reviews.
You could process them:
Reviews
│
▼
Sentiment model
│
├── Positive
└── NegativeThen calculate:
Positive reviews: 82%
Negative reviews: 18%You could also group reviews by product:
Laptop A
├── Positive: 91%
└── Negative: 9%
Laptop B
├── Positive: 68%
└── Negative: 32%Now sentiment analysis becomes useful business infrastructure rather than just an AI demo.
Batch processing multiple reviews
For a larger list:
from transformers import pipeline
analyzer = pipeline("sentiment-analysis")
reviews = [
"Amazing performance and excellent battery.",
"The screen broke after two days.",
"Fast delivery and good packaging.",
"The application is difficult to use.",
]
results = analyzer(reviews)
for review, result in zip(reviews, results):
print({
"text": review,
"sentiment": result["label"],
"score": round(result["score"], 4),
})The useful idea here is that your application can process many inputs rather than calling the model manually for every sentence.
Hugging Face documents pipeline support for multiple inputs and different inference tasks.
Turn the model into an API
Now let's make the project more realistic.
A frontend application shouldn't have to know how Transformers works.
Instead:
Frontend
│
│ POST /predict
▼
Python API
│
▼
Sentiment Model
│
▼
JSON responseOne simple implementation can use FastAPI.
Install it:
pip install fastapi uvicornCreate:
app.pyAdd:
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
app = FastAPI()
analyzer = pipeline(
"text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)
class TextRequest(BaseModel):
text: str
@app.post("/predict")
def predict(request: TextRequest):
result = analyzer(request.text)[0]
return {
"label": result["label"],
"score": result["score"],
}Start the API:
uvicorn app:app --reloadNow your application has:
POST /predictwith a request body such as:
{
"text": "I love this application."
}and a response similar to:
{
"label": "POSITIVE",
"score": 0.99
}The exact score will vary.
Your architecture just changed
You now have:
┌──────────────┐
│ Frontend │
└──────┬───────┘
│
│ POST /predict
▼
┌──────────────┐
│ FastAPI │
└──────┬───────┘
│
▼
┌──────────────┐
│ Transformer │
│ Model │
└──────┬───────┘
│
▼
PredictionThis pattern can be extended into many AI applications.
The first limitation you will notice
Sentiment analysis sounds simple until language becomes complicated.
Consider:
"The battery is so good that I only have to charge it twice a day."A human may interpret this as sarcasm.
A sentiment model may interpret it differently.
Another example:
"Great. Another update that broke everything."The word:
Greatlooks positive.
But the actual sentence is negative.
This is why natural language processing is difficult.
Human language contains:
- sarcasm
- context
- ambiguity
- slang
- domain-specific terminology
- negation
- mixed opinions
- cultural differences
A model can produce a confident prediction and still be wrong.
Mixed sentiment is another problem
Consider:
"The camera is excellent, but the battery life is terrible."Is this:
POSITIVEor:
NEGATIVEThe answer depends on what you are trying to measure.
The customer has expressed both positive and negative opinions.
A simple binary sentiment classifier may not capture that complexity.
This is where more advanced NLP systems become useful.
Domain matters
A model trained on general English may not understand specialized language equally well.
Compare:
"The model is overfitting."to:
"The model is amazing."In a machine-learning discussion, "overfitting" has a technical meaning.
In another domain, terminology can have completely different patterns.
For production applications, always test the model on data that resembles your real users and use case.
Don't judge an AI model from five examples
This is a common beginner mistake.
Someone runs:
5 sentences
↓
5 predictions
↓
Looks accurateand concludes:
"The model works perfectly."
That's not evaluation.
A better process is:
Realistic test dataset
↓
Model predictions
↓
Compare against labels
↓
Calculate metrics
↓
Analyze failuresFor classification systems, useful metrics can include:
- accuracy
- precision
- recall
- F1 score
- confusion matrix
The right metric depends on the application and the costs of different errors.
A simple evaluation dataset
You could create:
texts = [
"Excellent product and fantastic support.",
"The application crashes constantly.",
"Very happy with my purchase.",
"The service was disappointing.",
]
expected = [
"POSITIVE",
"NEGATIVE",
"POSITIVE",
"NEGATIVE",
]Then compare predictions with your expected labels.
For a real evaluation, however, use a substantially larger representative dataset.
A tiny hand-written test list is useful for smoke testing, not for claiming model quality.
Confidence thresholds are not a magic fix
You might think:
if score > 0.9:
accept_prediction()That can be useful in some systems.
But it doesn't automatically mean:
score > 0.9
=
correctA model can be confidently wrong.
A production system may therefore use:
High confidence
↓
Automatic processing
Low confidence
↓
Human reviewFor example:
Prediction
│
┌────────┴────────┐
│ │
confidence confidence
high low
│ │
▼ ▼
automate reviewThis is often a better design than pretending the model is always correct.
Where this becomes a real product feature
A sentiment model can become part of:
Customer feedback analysis
Reviews
↓
Sentiment
↓
DashboardSupport-ticket prioritization
Customer message
↓
Sentiment
↓
Priority signalSocial listening
Posts
↓
Classification
↓
Trend analysisProduct analytics
Reviews
↓
Sentiment
↓
Product-level trendsThe model is only one component.
The real application is:
Data
↓
Model
↓
Business logic
↓
User experienceHow to improve the project
Once the basic application works, don't immediately jump to a larger model.
Improve the project one layer at a time.
Version 1
Python script
+
Pretrained modelVersion 2
Python script
+
Multiple inputs
+
Better outputVersion 3
FastAPI
+
REST endpointVersion 4
Database
+
Stored predictionsVersion 5
Frontend
+
Charts
+
Review dashboardVersion 6
Evaluation dataset
+
Monitoring
+
Human reviewThat progression is much more valuable for a portfolio than simply writing:
"Built a sentiment analysis project."
A portfolio-ready version
If you're using this project for your developer portfolio, don't stop at:
Used Hugging Face Transformers.Show the engineering.
Your repository could contain:
sentiment-analyzer/
├── app.py
├── sentiment.py
├── requirements.txt
├── README.md
├── tests/
│ └── test_predictions.py
└── data/
└── sample_reviews.csvYour README should explain:
Problem
Solution
Model
API
Example requests
Example responses
Evaluation
Limitations
How to run
Future improvementsThat demonstrates much more than a screenshot of a prediction.
What you actually learned
This small project introduced several important AI concepts.
Pretrained model
↓
Inference
↓
Tokenization
↓
Text classification
↓
Confidence score
↓
API integration
↓
Evaluation
↓
Production limitationsThese are building blocks you can reuse in other NLP applications.
Sentiment analysis vs generative AI
It is also useful to understand what this project is not.
A sentiment classifier generally answers:
"What category does this text belong to?"A generative AI model can perform tasks such as:
"Write a summary."
"Generate an email."
"Explain this code."
"Create a response."Conceptually:
Classification
Text
↓
Labelversus:
Generation
Prompt
↓
Generated textBoth can use transformer architectures, but they solve different problems.
When a simple classifier is better than an LLM
You don't always need a giant generative model.
If your task is:
Classify 1 million reviewsand the required output is simply:
POSITIVE
NEGATIVEa dedicated classification model may be more appropriate than sending every review to a large generative model.
Think about the task first.
Simple classification problem
↓
Use an appropriate classifier
Open-ended reasoning/generation
↓
Consider a generative modelThe best AI architecture is not necessarily the largest model.
A useful project upgrade: three labels
Our basic example uses:
POSITIVE
NEGATIVEYou could extend the application to support:
POSITIVE
NEGATIVE
NEUTRALBut don't simply change the output labels in your code and assume the model can now predict a new class.
The model itself must support the labels.
This is an important machine-learning lesson:
Application logic cannot create capabilities that the trained model does not have.
If you need a different label space, choose an appropriate model or fine-tune one for the task.
Another upgrade: sentiment dashboard
Imagine collecting:
10,000 customer reviewsYour pipeline could become:
Reviews
↓
NLP model
↓
Predictions
↓
Database
↓
AnalyticsThen your dashboard could show:
Overall sentiment
Positive ████████████████ 72%
Neutral █████ 21%
Negative ██ 7%You could also analyze sentiment over time:
Release 1
↓
Mostly positive
Release 2
↓
Negative spike
Release 3
↓
RecoveryNow the model is generating information that can support product decisions.
Production questions you should eventually ask
Before putting an AI model into a real application, ask:
1. What data will it receive?
User reviews?
Support tickets?
Social posts?
Internal documents?
2. What happens when the prediction is wrong?
Can a wrong result cause harm?
3. How will performance be measured?
Accuracy?
F1?
Human review?
Business outcomes?
4. How will the model be updated?
Will the application stay on the same model?
Will you evaluate newer models?
5. What happens to user data?
Will text be stored?
For how long?
Who can access it?
6. Can humans override the prediction?
For important decisions, this can be extremely valuable.
A clean mental model for AI projects
When you're building an AI application, think in layers:
┌──────────────────────────┐
│ User Experience │
├──────────────────────────┤
│ Application Logic │
├──────────────────────────┤
│ AI / ML Model │
├──────────────────────────┤
│ Data Processing │
├──────────────────────────┤
│ Data │
└──────────────────────────┘The model is not the entire application.
That's one of the most important lessons for developers entering AI engineering.
Turn this into a Karyvio portfolio project
If you're learning AI/ML for a job, make the project demonstrate engineering rather than only model usage.
A strong progression would be:
Step 1
Build sentiment classifier
↓
Step 2
Create REST API
↓
Step 3
Add frontend
↓
Step 4
Store predictions
↓
Step 5
Create analytics dashboard
↓
Step 6
Evaluate on a labeled dataset
↓
Step 7
Document limitations
↓
Step 8
Deploy itYour final portfolio project could then demonstrate:
- Python
- NLP
- Transformers
- REST APIs
- FastAPI
- model inference
- data processing
- evaluation
- frontend integration
- deployment
- documentation
That's much stronger than a notebook containing only:
pipeline("sentiment-analysis")Final takeaway
You don't need to train a transformer from scratch to build something useful with AI.
A pretrained model can become the intelligence layer inside a normal software application.
The basic pattern is:
Input
↓
Preprocessing
↓
Pretrained model
↓
Prediction
↓
Application logic
↓
User experienceStart with the tiny version.
Then make it useful.
Then measure it.
Then expose it through an API.
Then build a product around it.
That's the difference between trying an AI model and building an AI application.
Project challenge
Take the project one step further.
Instead of analyzing one sentence at a time, create a CSV file:
review
"The product is fantastic"
"Terrible customer service"
"Average experience"
"Very useful application"
"The update broke my workflow"Build a Python program that:
- Reads the CSV.
- Runs sentiment analysis.
- Adds a
sentimentcolumn. - Adds a
scorecolumn. - Saves a new CSV.
- Calculates the percentage of each sentiment.
- Prints the most confident predictions.
- Documents examples where the model appears incorrect.
That turns this tutorial into a small NLP data-processing project you can actually put on GitHub.
Official references
For implementation details, use the current Hugging Face Transformers documentation and the model's own model card rather than relying on copied snippets from old tutorials.
The main references for this project are:
Hugging Face Transformers
https://huggingface.co/docs/transformers/
Transformers Pipeline
https://huggingface.co/docs/transformers/main/pipeline_tutorial
Transformers Installation
https://huggingface.co/docs/transformers/installation
DistilBERT sentiment model
https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-englishFor production use, always test the selected model against data representative of your actual application rather than assuming a pretrained model's demo performance will transfer directly to your use case.







Comments (0)
Be the first to share your thoughts.