Library cheatsheet
Hugging Face Cheatsheet
The hf command line, and Transformers and Datasets code for downloading, running, training and sharing models.
Command line (hf)
- Install the Hub client — Install the library that provides the hf command.
- Log in — Sign in with an access token from your account settings, to use private models or upload.
- Check who you are logged in as — Print the account the CLI is authenticated as.
- Authenticate with an environment variable — Set a token for scripts and CI instead of logging in interactively. Keep it out of source control.
- Download a model — Download all files of a model repository into the local cache.
- Download one file — Download a single file from a repository.
- Download a dataset — Download a dataset repository by naming its type.
- Download into a folder — Save files in a folder of your choice instead of the cache.
- Create a repository — Create a model repository under your account.
- Upload files — Upload a local folder to the root of a repository.
Cache and environment
- Move the cache — Store downloaded models and datasets on a larger disk.
- Work offline — Use only files already in the cache and make no network requests.
- Download a repository from Python — Download a full repository snapshot and get its local path.
- Download one file from Python — Download a single file and get its local path.
Transformers
- Install Transformers — Install the library, PyTorch and Accelerate for loading large models across devices.
- Classify text in two lines — Use a pipeline for a task with a default model.
- Generate text with a pipeline — Generate text with a specific model.
- Load a model and tokenizer — Load a language model, placing it on the best available device.
- Generate a chat reply — Format messages with the model's chat template, generate, and decode only the new tokens.
- Tokenize text — Convert text to token IDs as PyTorch tensors.
- Share a model — Upload a fine-tuned model and tokenizer to your account. Log in first.
Datasets
- Install Datasets — Install the Datasets library.
- Load a dataset — Load the training split of a dataset from the Hub.
- Load a local file — Load a CSV, JSON or Parquet file as a dataset.
- Transform every row — Apply a function to the dataset, in batches for speed.
- Filter rows — Keep only rows that satisfy a condition.
- Make a train and test split — Hold out 10% of the rows for testing.
- Stream a huge dataset — Iterate without downloading the whole dataset first.
- Share a dataset — Upload a dataset to your account. Log in first.
Related cheatsheets
- Matplotlib and seaborn 3 cheatsheet — Draw line, scatter, bar and histogram charts with Matplotlib, and statistical charts with seaborn.
- pandas and NumPy 3 cheatsheet — Load, clean, reshape, group and analyze tabular data with pandas, and work with arrays in NumPy.
- Polars 2 cheatsheet — Fast DataFrames in Python: expressions, filtering, grouping, joins, lazy queries and file formats.
- Prisma ORM cheatsheet — Model your database in a schema, run migrations, and query with the type-safe Prisma Client. Examples use the prisma-client-js generator; check the docs for newer generators.
- PyTorch 2 cheatsheet — Tensors, autograd, models, training loops, saving and speed-ups in PyTorch 2.
- React 19 cheatsheet — Hooks, components and patterns for React 19 in TypeScript or JavaScript.