Transformers Cheatsheet
Transformers is the core library of the HuggingFace ecosystem, providing a unified interface to thousands of pretrained models. Supports PyTorch, TensorFlow, and JAX backends. The `pipeline` API enables one-liner inference for text classification, generation, summarization, translation, QA, image classification, and more.
Quick Start
``bash
# Install
pip install transformers
# Check environment transformers-cli env
# Download a model (without loading) transformers-cli download bert-base-uncased
# One-liner sentiment analysis python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love Transformers!'))"
# Text generation with GPT-2
python -c "from transformers import pipeline; print(pipeline('text-generation', model='gpt2')('Once upon a time')[0]['generated_text'])"
`
Pipeline API
The pipeline function is the easiest entry point. It handles model loading, tokenization, and output decoding automatically. Supported tasks: sentiment-analysis, text-generation, summarization, translation_xx_to_yy, fill-mask, question-answering, image-classification, image-segmentation, object-detection, zero-shot-classification, and more.
Auto Classes
AutoModel, AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM, and other Auto classes automatically select the correct model architecture based on the model name. This is the recommended way to load models programmatically.
Model Loading
Models are identified by their HuggingFace Hub ID. Popular models include: BERT (bert-base-uncased), GPT-2 (gpt2), T5 (t5-small), BART (facebook/bart-large), Llama (meta-llama/Llama-3.1-8b), Mistral (mistralai/Mistral-7B-v0.1), and thousands more.
Environment
TRANSFORMERS_CACHE controls the model cache directory. HF_HOME is the root HuggingFace directory. The transformers-cli env` command prints all relevant environment info including installed backends, CUDA version, and cache locations.
Startup & Modes(15)
| Command | Level | ||
|---|---|---|---|
transformers-cli --helpShow transformers-cli help | Basic | transformers-cli --help | |
transformers-cli envShow Transformers environment info | Basic | transformers-cli env | |
pip install transformersInstall Transformers library | Basic | pip install transformers | |
pip install transformers[torch]Install Transformers (with PyTorch) | Basic | pip install transformers[torch] | |
pip install transformers[tf-cpu]Install Transformers (with TensorFlow CPU) | Basic | pip install transformers[tf-cpu] | |
pipeline text-generationUse pipeline API for text generation | Intermediate | python -c 'from transformers import pipeline; pipe = pipeline("text-generation", model="gpt2"); print(pipe("Hello")[0]["generated_text"])'
| |
module envRun transformers-cli env as a module | Intermediate | python -m transformers.commands.run transformers-cli env | |
pipeline sentiment-analysisSentiment analysis pipeline | Basic | python -c 'from transformers import pipeline; print(pipeline("sentiment-analysis")("I love this!"))'
| |
pipeline image-classificationImage classification pipeline | Intermediate | python -c 'from transformers import pipeline; print(pipeline("image-classification")("photo.jpg"))'
| |
pipeline summarizationText summarization pipeline | Intermediate | python -c 'from transformers import pipeline; print(pipeline("summarization")("Long article text here..."))'
| |
pipeline translationTranslation pipeline (English to French) | Intermediate | python -c 'from transformers import pipeline; print(pipeline("translation_en_to_fr")("Hello"))'
| |
export TRANSFORMERS_CACHESet Transformers cache directory | Basic | export TRANSFORMERS_CACHE=/data/hf-cache | |
export HF_HOMESet HuggingFace home directory | Basic | export HF_HOME=/data/hf-home | |
pipeline fill-maskMask filling pipeline (BERT type) | Intermediate | python -c 'from transformers import pipeline; print(pipeline("fill-mask")("I love [MASK]!"))'
| |
pipeline question-answeringQuestion answering pipeline | Intermediate | python -c 'from transformers import pipeline; qa = pipeline("question-answering"); print(qa(question="What is AI?", context="AI is the simulation of intelligence."))'
|
ai-model(6)
| Command | Level | ||
|---|---|---|---|
transformers-cli downloadDownload pretrained model | Intermediate | transformers-cli download bert-base-uncased | |
transformers-cli download --cache-dirSpecify download cache directory | Intermediate | transformers-cli download bert-base-uncased --cache-dir /data/models | |
AutoModel from_pretrainedLoad pretrained model with AutoModel | Intermediate | python -c 'from transformers import AutoModel; AutoModel.from_pretrained("bert-base-uncased")'
| |
AutoTokenizer from_pretrainedLoad matching tokenizer for a model | Intermediate | python -c 'from transformers import AutoTokenizer; tok = AutoTokenizer.from_pretrained("bert-base-uncased"); print(tok("Hello world"))'
| |
AutoModelForCausalLMLoad causal language model (GPT/Llama type) | Intermediate | python -c 'from transformers import AutoModelForCausalLM; AutoModelForCausalLM.from_pretrained("gpt2")'
| |
AutoModelForSeq2SeqLMLoad sequence-to-sequence model (T5/BART type) | Intermediate | python -c 'from transformers import AutoModelForSeq2SeqLM; AutoModelForSeq2SeqLM.from_pretrained("t5-small")'
|