# Classify Text in 10 Lines with a Hugging Face Transformers Pipeline

> Source: <https://sourcefeed.dev/a/classify-text-in-10-lines-with-a-hugging-face-transformers-pipeline>
> Published: 2026-07-24 11:40:52+00:00

# Classify Text in 10 Lines with a Hugging Face Transformers Pipeline

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

[Mariana Souza](https://sourcefeed.dev/u/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+**—[Transformers](https://huggingface.co/docs/transformers)v5 requires it. Verified here with Python 3.13.5.** Verified library versions:**transformers 5.14.1,[PyTorch](https://pytorch.org)2.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
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`

:

``` python
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](https://huggingface.co/distilbert/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`

to`pipeline()`

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](https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest)(adds a`neutral`

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"`

to`pipeline()`

. - The
[Pipeline API reference](https://huggingface.co/docs/transformers/main_classes/pipelines)lists every supported task, and the free[Hugging Face LLM Course](https://huggingface.co/learn/llm-course)goes from here to fine-tuning your own models.

## Sources & further reading

-
[Transformers Installation](https://huggingface.co/docs/transformers/en/installation)— huggingface.co -
[Pipelines API Reference](https://huggingface.co/docs/transformers/en/main_classes/pipelines)— huggingface.co -
[Transformers Quickstart](https://huggingface.co/docs/transformers/en/quicktour)— huggingface.co -
[DistilBERT base uncased finetuned SST-2 model card](https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english)— huggingface.co -
[PyTorch Get Started Locally](https://pytorch.org/get-started/locally/)— pytorch.org

[Mariana Souza](https://sourcefeed.dev/u/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.
