How to Run Sentiment Analysis with a Hugging Face Pipeline
Sentiment analysis decides whether a piece of text is positive or negative: a happy review or an angry one. A pretrained model can do it without any training on your part, and Hugging Face Transformers wraps the whole job in a pipeline. This guide classifies two customer reviews with Transformers 5.24.1 on Python 3.14.
Syntax
from transformers import pipeline
classifier = pipeline("sentiment-analysis", model="<model>", revision="<commit>")
results = classifier(<text or list of texts>)
| Part | What it means |
|---|---|
pipeline("sentiment-analysis", …) | Builds a ready-to-use classifier: it downloads the model the first time, then turns text into the model's input, runs it and reads the answer |
model="<model>" | The model on the Hugging Face Hub, as <owner>/<name> |
revision="<commit>" | The exact version of the model to use, so it can't change under you |
classifier(…) | Classifies one text or a list of texts, and returns one {"label": …, "score": …} per text |
The pipelines documentation calls them "a great and easy way to use models for inference". "sentiment-analysis" is another name for the "text-classification" task.
The Example
Two reviews of an online shop, one happy and one not. The model is distilbert/distilbert-base-uncased-finetuned-sst-2-english, the one Transformers itself uses for this task when you don't name a model. It was trained on movie reviews and knows two labels, POSITIVE and NEGATIVE.
Prerequisites
- uv. This guide uses uv 0.13.10. The project uses Python 3.14, which uv downloads if needed.
- About 300 MB for the model, downloaded on the first run.
Step 1: Create the Project
uv init --python 3.14 sentiment-demo
cd sentiment-demo
uv add transformers torch
That's the project with Transformers 5.24.1 and PyTorch 2.15.0.
Step 2: Write the Script
from transformers import pipeline
classifier = pipeline(
"sentiment-analysis",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0fa89d2f80546fda750413ed43d93601a13",
)
reviews = [
"The delivery was quick and the support team was lovely.",
"I waited three weeks and nobody answered my emails.",
]
for review, result in zip(reviews, classifier(reviews)):
print(f"{result['label']:<9}{result['score']:.4f} {review}")

classifier(reviews) classifies both reviews in one call and returns the results in the same order, so zip pairs each result with its review. The revision is the model's commit on the Hub; hf models info <model> --expand sha prints the current one.
Step 3: Run It
uv run python sentiment.py
W1201 05:02:16.612000 50094 .venv/lib/python3.14/site-packages/torch/distributed/elastic/multiprocessing/redirects.py:35] NOTE: Redirects are currently not supported in MacOs.
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
config.json: 100%|█████████████████████████████████████████████████| 629/629 [00:00<00:00, 4.40MB/s]
model.safetensors: downloading bytes: ██████████████████████████████████████████| 250MB, 8.62MB/s
model.safetensors: reconstructing file: 100%|██████████████████████████| 268MB / 268MB, 18.8MB/s
Loading weights: 100%|██████████████████████████████████████████| 104/104 [00:00<00:00, 5154.84it/s]
tokenizer_config.json: 100%|██████████████████████████████████████| 48.0/48.0 [00:00<00:00, 326kB/s]
vocab.txt: 100%|█████████████████████████████████████████████████| 232k/232k [00:00<00:00, 2.79MB/s]
POSITIVE 0.9999 The delivery was quick and the support team was lovely.
NEGATIVE 0.9997 I waited three weeks and nobody answered my emails.

The first run downloaded the model's files, config.json, model.safetensors (268 MB), and the tokenizer's files, and loaded its weights. Then both reviews got the label you'd expect, with a score close to 1. The score is how sure the model is of its label, from 0 to 1. A second run skips the downloads.
The other lines are notices, not errors:
NOTE: Redirects are currently not supported in MacOs.comes from PyTorch on a Mac. Transformers 5.24 triggers it on import, and it doesn't affect results.Warning: You are sending unauthenticated requests to the HF Hub.comes from the Hub: downloads without an account work, with lower rate limits.
The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out model | Transformers picks a default and warns: No model was supplied, defaulted to distilbert/distilbert-base-uncased-finetuned-sst-2-english and revision 714eb0f | Name the model and its revision, as in the script |
| Expecting a "neutral" label | This model only knows POSITIVE and NEGATIVE, so a neutral sentence still gets one of them: "This runs on the processor." got NEGATIVE with a score of 0.97 | Choose a model trained with the labels you need |
| Classifying text that isn't English | The model was trained on English movie reviews | Choose a model for your language on the Hub |
FAQ
How sure is "0.9999"?
Very sure, by the model's own measure. It's still a guess: a high score on text unlike the model's training data can be confidently wrong.
Can I run it on many texts at once?
Yes. Pass a list, as the script does. For long lists, batch_size processes several texts together.
Conclusion
A sentiment-analysis pipeline with a pinned DistilBERT model labelled the happy review POSITIVE (0.9999) and the unhappy one NEGATIVE (0.9997), with no training and about fifteen lines of Python.
Additional Resources
- Pipelines — every task and its options.
- Pipeline tutorial — devices, batching and more.
- distilbert-base-uncased-finetuned-sst-2-english — the model's card.
