Skip to main content

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.

Updated: 2026-07-20·21 commands

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)

CommandLevel
transformers-cli --help
Show transformers-cli help
Basic
transformers-cli env
Show Transformers environment info
Basic
pip install transformers
Install Transformers library
Basic
pip install transformers[torch]
Install Transformers (with PyTorch)
Basic
pip install transformers[tf-cpu]
Install Transformers (with TensorFlow CPU)
Basic
pipeline text-generation
Use pipeline API for text generation
Intermediate
module env
Run transformers-cli env as a module
Intermediate
pipeline sentiment-analysis
Sentiment analysis pipeline
Basic
pipeline image-classification
Image classification pipeline
Intermediate
pipeline summarization
Text summarization pipeline
Intermediate
pipeline translation
Translation pipeline (English to French)
Intermediate
export TRANSFORMERS_CACHE
Set Transformers cache directory
Basic
export HF_HOME
Set HuggingFace home directory
Basic
pipeline fill-mask
Mask filling pipeline (BERT type)
Intermediate
pipeline question-answering
Question answering pipeline
Intermediate

ai-model(6)

CommandLevel
transformers-cli download
Download pretrained model
Intermediate
transformers-cli download --cache-dir
Specify download cache directory
Intermediate
AutoModel from_pretrained
Load pretrained model with AutoModel
Intermediate
AutoTokenizer from_pretrained
Load matching tokenizer for a model
Intermediate
AutoModelForCausalLM
Load causal language model (GPT/Llama type)
Intermediate
AutoModelForSeq2SeqLM
Load sequence-to-sequence model (T5/BART type)
Intermediate

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-20.