cd /news/natural-language-processing/classify-text-in-10-lines-with-a-hug… Β· home β€Ί topics β€Ί natural-language-processing β€Ί article
[ARTICLE Β· art-71892] src=sourcefeed.dev β†— pub= topic=natural-language-processing verified=true sentiment=↑ positive

Classify Text in 10 Lines with a Hugging Face Transformers Pipeline

A tutorial by Mariana Souza demonstrates how to classify text sentiment in 10 lines of Python using a Hugging Face Transformers pipeline with a pretrained DistilBERT model, running entirely on CPU with no GPU or training required. The script, built with transformers 5.14.1 and PyTorch 2.13.0, labels text as POSITIVE or NEGATIVE using the 67M-parameter distilbert-base-uncased-finetuned-sst-2-english model, achieving scores above 0.99 on example reviews.

read4 min views1 publishedJul 24, 2026
Classify Text in 10 Lines with a Hugging Face Transformers Pipeline
Image: Sourcefeed (auto-discovered)

Install Transformers, load a pretrained DistilBERT model, and classify sentiment locally with no GPU or training.

Mariana Souza

What you'll build #

A Python script that labels any text as POSITIVE

or NEGATIVE

sentiment using a pretrained DistilBERT model from the Hugging Face Hub β€” running entirely on your CPU. The core logic is 10 lines. No training, no GPU, no API key.

Prerequisites #

Python 3.10+β€”Transformersv5 requires it. Verified here with Python 3.13.5.** Verified library versions:transformers 5.14.1,PyTorch2.13.0 (CPU build).~1.5 GB free disk**β€” the CPU PyTorch wheel plus a ~270 MB model download.** Internet access on first run**β€” the model downloads once, then loads from the local cache at~/.cache/huggingface/hub

.- No Hugging Face account or token needed; the model is public.

Commands below are for macOS/Linux. On Windows, activate the virtual environment with .venv\Scripts\activate

instead.

1. Create a project and virtual environment #

Keep this isolated from your system Python β€” Transformers pulls in a dozen dependencies you don't want globally.

mkdir first-pipeline && cd first-pipeline
python3 -m venv .venv
source .venv/bin/activate

Your prompt should now show (.venv)

.

2. Install PyTorch and Transformers #

Transformers v5 is PyTorch-only (TensorFlow and Flax backends were dropped in 5.0). Install the CPU build of torch explicitly β€” on Linux this skips several gigabytes of CUDA libraries you don't need for this tutorial:

pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install transformers

Confirm both landed:

python -c "import torch, transformers; print(torch.__version__, transformers.__version__)"

Expected: 2.13.0+cpu 5.14.1

(plain 2.13.0

on macOS) or newer.

3. Write the classifier #

Create classify.py

:

from transformers import pipeline

classifier = pipeline(
    "text-classification",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)

reviews = [
    "The battery life on this laptop is incredible.",
    "Shipping took three weeks and the box arrived crushed.",
]

for review, result in zip(reviews, classifier(reviews)):
    print(f"{result['label']:<8} {result['score']:.4f}  {review}")

Two things worth knowing. pipeline()

bundles the tokenizer, model, and post-processing into one callable β€” you pass raw strings, it returns dicts with a label

and a score

(the model's probability for that label). And the model is pinned explicitly: pipeline("text-classification")

alone works, but it logs a "No model was supplied" warning and leaves you at the mercy of whatever default Hugging Face picks later. The pinned checkpoint, distilbert-base-uncased-finetuned-sst-2-english, is a 67M-parameter DistilBERT fine-tuned on Stanford Sentiment Treebank movie reviews β€” small enough to run comfortably on any laptop CPU.

4. Run it #

python classify.py

The first run downloads the model (~268 MB) and tokenizer files, then caches them. Subsequent runs load from disk and classify both sentences in well under a second.

Verify it works #

You should see exactly this (scores may differ in the last decimal places across platforms):

POSITIVE 0.9998  The battery life on this laptop is incredible.
NEGATIVE 0.9997  Shipping took three weeks and the box arrived crushed.

If both labels match with scores above 0.99, everything works. Swap in your own sentences and rerun β€” no re-download happens.

Troubleshooting #

** ModuleNotFoundError: No module named 'transformers'** β€” you're running a Python outside the virtual environment. Check that your prompt shows

(.venv)

, re-run source .venv/bin/activate

, and confirm with which python

that it points into .venv

.** OSError: We couldn't connect to 'https://huggingface.co' to load the files, and couldn't find them in the cached files.** β€” the Hub is unreachable on first download. Check your connection and proxy settings, and make sure

HF_HUB_OFFLINE

isn't set in your shell (echo $HF_HUB_OFFLINE

should print nothing).** Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.** β€” informational, not an error; the download still works. To silence it, create a free token at huggingface.co/settings/tokens and export it as

HF_TOKEN

.** ERROR: Could not find a version that satisfies the requirement transformers** β€” pip found no compatible wheel, which almost always means Python older than 3.10. Run

python3 --version

and install a current Python before recreating the venv.## Next steps

  • Pass top_k=None

topipeline()

to get scores for every label instead of just the winner β€” useful for confidence thresholds. - Swap the model string for cardiffnlp/twitter-roberta-base-sentiment-latest(adds aneutral

class, tuned for social media) β€” nothing else in the code changes. That interchangeability is the pipeline API's whole point. - Try pipeline("zero-shot-classification")

to classify against your own arbitrary labels with no fine-tuning. - Got an NVIDIA GPU? Install the CUDA build of torch and pass device="cuda"

topipeline()

. - The Pipeline API referencelists every supported task, and the freeHugging Face LLM Coursegoes from here to fine-tuning your own models.

Sources & further reading #

Transformers Installationβ€” huggingface.co - Pipelines API Referenceβ€” huggingface.co - Transformers Quickstartβ€” huggingface.co - DistilBERT base uncased finetuned SST-2 model cardβ€” huggingface.co - PyTorch Get Started Locallyβ€” pytorch.org

Mariana SouzaΒ· Senior Editor

Mariana covers the fast-moving world of machine learning and generative AI, with a particular focus on how these technologies are reshaping development workflows. When she isn't stress-testing the latest foundation models, she's usually at a local hackathon.

Discussion 0 #

No comments yet

Be the first to weigh in.

── more in #natural-language-processing 4 stories Β· sorted by recency
── more on @hugging face 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain β€” perfect for shipping the agent you just read about.

$git push zahid main
β†’ Live at https://your-agent.zahid.host βœ“
Get free account β†’ Pricing
from €0/mo Β· no card required
LIVE [news/classify-text-in-10-…] indexed:0 read:4min 2026-07-24 Β· β€”