Skip to main content

Practical Transformers — Hugging Face Ecosystem

[ ABORT TO HUD ]
SEQ. 1
SEQ. 2
SEQ. 3

Practical Transformers

🤗 Hugging Face Ecosystem⏱ 12 min⭐ 100 BASE XP⌨ HANDS-ON LAB

Hands-On with Hugging Face Transformers

The Transformers library provides high-level APIs that abstract away model complexity while giving you full control when you need it.

The pipeline() API - Instant Inference

The fastest way to use any model - one line of code for text generation, classification, summarization, translation, and more:

from transformers import pipeline

# Text generation
generator = pipeline("text-generation", model="mistralai/Mistral-Small-4", device_map="auto")
result = generator("Explain Docker networking in simple terms", max_new_tokens=256)
print(result[0]["generated_text"])

# Sentiment analysis
classifier = pipeline("sentiment-analysis")
print(classifier("I love open-source AI!"))  # [{'label': 'POSITIVE', 'score': 0.999}]

# Summarization
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
print(summarizer(long_article, max_length=130))

AutoModel Variants

ClassUse CaseOutput
AutoModelForCausalLMText generation (GPT-style)Next-token logits
AutoModelForSeq2SeqLMTranslation, summarization (T5-style)Encoder-decoder output
AutoModelForSequenceClassificationClassification, sentimentClass logits
AutoModelForTokenClassificationNER, POS taggingPer-token labels
AutoModelEmbeddings, custom headsHidden states

The Trainer Class

HF's Trainer handles the training loop, evaluation, logging, checkpointing, mixed precision, and distributed training:

from transformers import Trainer, TrainingArguments
from datasets import load_dataset

dataset = load_dataset("imdb", split="train[:1000]")

args = TrainingArguments(
    output_dir="./results",
    num_train_epochs=3,
    per_device_train_batch_size=8,
    learning_rate=2e-5,
    fp16=True,
    logging_steps=10,
    eval_strategy="epoch",        # Transformers v5 (replaces removed evaluation_strategy)
    save_strategy="epoch",
)

trainer = Trainer(
    model=model,
    args=args,
    train_dataset=dataset,
    eval_dataset=eval_dataset,
    processing_class=tokenizer,   # Transformers v5 unified processor/tokenizer arg
)
trainer.train()

The Datasets Library

HF datasets provides streaming, memory-mapped, and cached access to 500K+ datasets:

from datasets import load_dataset

# Load from Hub
ds = load_dataset("tatsu-lab/alpaca", split="train")

# Stream large datasets without downloading fully
ds = load_dataset("allenai/c4", split="train", streaming=True)
for example in ds:
    process(example)

# Load from local files
ds = load_dataset("json", data_files="my_data.jsonl")
💡 Custom Training Loop: If Trainer is too opinionated, use accelerate for a lightweight wrapper around PyTorch's native training loop with automatic multi-GPU, mixed precision, and DeepSpeed support.
⌨ HANDS-ON LABGenerate Text with Hugging Face Pipelines
⭐ +150 XP

Initialize an end-to-end Hugging Face text-generation pipeline with an open-source SLM.

1Run a Python one-liner creating a pipeline for Qwen/Qwen2.5-0.5B and generate a completion.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 1 — type "hint" if stuck
KNOWLEDGE CHECK
QUERY 1 // 3
What is the fastest way to run inference with any HF model?
Write a custom PyTorch loop
Use the pipeline() API
Export to ONNX first
Use raw model.forward()