Hugging Face Hub Basics: Models, Datasets and Tokens

JS
Jai Singh
02 Dec, 2026 3 min read 4 views

The Hugging Face Hub is a Git-based hosting service for models, datasets and demos. Fine-tuners use it to download base models and datasets, to store adapters and checkpoints with version history, and to share results with a team. Learning five or six operations covers almost everything you will do.

Repositories and revisions

Every model and dataset is a repository identified by an owner and a name, such as owner/name. Repositories are versioned with Git, so each upload creates a commit with a hash. You can load the latest files or pin an exact commit with the revision argument. Pinning is important: if the author updates the repository, your next run will silently use new weights unless you pinned.

import os
from transformers import AutoModelForCausalLM, AutoTokenizer

name = os.environ["BASE_MODEL"]
rev = os.environ.get("BASE_MODEL_REVISION", "main")

tok = AutoTokenizer.from_pretrained(name, revision=rev)
model = AutoModelForCausalLM.from_pretrained(name, revision=rev)

In production code, set the revision to a commit hash, not main.

Access tokens

Create tokens in your account settings. Prefer a fine-grained token with the least permission needed: read-only for downloading, write for uploading. Give each machine or CI job its own token so you can revoke one without affecting others. Provide the token through the HF_TOKEN environment variable or the login command; do not hard-code it in scripts or notebooks you share.

Downloading files

Most libraries download automatically on first use and cache the files. To fetch a full repository snapshot ahead of time, for example on a machine that will later be offline, use snapshot_download:

import os
from huggingface_hub import snapshot_download

path = snapshot_download(
    repo_id=os.environ["BASE_MODEL"],
    revision=os.environ.get("BASE_MODEL_REVISION", "main"),
    allow_patterns=["*.json", "*.safetensors", "tokenizer*"],
)
print("saved to", path)

The allow_patterns filter skips files you do not need, such as duplicate weight formats. The function returns the local path inside the cache.

Uploading your results

After training, push the adapter or model. The HfApi class provides explicit calls:

from huggingface_hub import HfApi

api = HfApi()
repo_id = "your-username/support-tone-adapter"   # replace with your own repo
api.create_repo(repo_id, private=True, exist_ok=True)
api.upload_folder(folder_path="out/final", repo_id=repo_id)

Make new repositories private by default, and make them public only after you review the contents and the license. Training libraries can also push for you: in the Trainer arguments, push_to_hub=True uploads checkpoints as training progresses.

Datasets on the Hub

Datasets work the same way. load_dataset("owner/name") downloads and caches them. Read the dataset card for the license, the collection method and known issues before you train. Private datasets work with a token and are a convenient way to share training data across a team without emailing files.

Write a model card

A model card is the README of a repository. For a fine-tune, include:

  • The base model and revision, with a link and its license.
  • What the model is for and what it should not be used for.
  • Training data description, size and any filtering.
  • Training settings: method, rank, learning rate, epochs, seed.
  • Evaluation results on your held-out set and how you measured them.
  • Known limitations and safety notes.

Good habits

  • Pin revisions for models and datasets in every training run.
  • Use read-only tokens in CI jobs that only download.
  • Do not upload private or personal data to a public repository, and remember that Git history keeps deleted files.
  • Store adapters, which are small, instead of full merged copies when possible.
  • Set a cache location with HF_HOME if your home drive is small.

Working offline and in CI

Some training machines have no internet access. Download the model and dataset on a connected machine with snapshot_download, copy the cache folder across, and set the environment variable HF_HUB_OFFLINE to 1 so libraries use only local files and fail fast instead of waiting on network timeouts. In CI, cache the Hub folder between runs to avoid re-downloading multi-gigabyte weights on every job.

Safetensors and pickle files

Modern repositories store weights as safetensors files, a format that holds only tensors and cannot run code when loaded. Older repositories may still use pickle-based files, which can execute arbitrary code on load. Prefer safetensors, and avoid passing trust_remote_code=True unless you have read the code in the repository, because it runs Python from the repo on your machine.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.