Metadata-Version: 2.5
Name: hfpin
Version: 0.2.0
Summary: Pin Hugging Face models and datasets in your code to exact commits
Project-URL: Homepage, https://github.com/DipPansuriya/hfpin
Project-URL: Issues, https://github.com/DipPansuriya/hfpin/issues
Author: Dip Pansuriya
License-Expression: MIT
License-File: LICENSE
Keywords: huggingface,mlops,pinning,security,supply-chain,transformers
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# hfpin

**Pin the Hugging Face models and datasets in your code to exact commits.**

```python
# Before: downloads whatever is on the Hub today
model = AutoModel.from_pretrained("google-bert/bert-base-uncased")

# After `hfpin pin`: always the same files
model = AutoModel.from_pretrained("google-bert/bert-base-uncased", revision="86b5e0934494bd15c9632b12f734a8a67f723594")
```

## Why

`from_pretrained("org/model")` downloads the latest commit of the `main` branch. That commit can change at any time:

- **Silent changes.** The owner updates weights, tokenizer or config, and your results change with no code change on your side.
- **Supply chain attacks.** If the owner's account is compromised, the attacker's files go straight into your app. With `trust_remote_code=True`, that includes Python code that runs on your machine.
- **Reproducibility.** A paper, a test, or a training run from last month may not give the same result today.

Pinning a commit sha (`revision="<sha>"`) fixes all three. It is the same idea as pinning GitHub Actions to a sha, and Hugging Face [recommends it](https://huggingface.co/docs/huggingface_hub/guides/download#from-specific-version). Doing it by hand means looking up a 40-character sha for every model, which is why almost nobody does. `hfpin` does it for you.

## Install

```bash
pip install hfpin
```

Requires Python 3.10+. No dependencies.

## Usage

### Find unpinned downloads

```console
$ hfpin check
train.py:5:7: unpinned model 'bert-base-uncased' (AutoTokenizer.from_pretrained): add revision=<commit sha>
train.py:10:7: model 'distilbert/distilbert-base-uncased-finetuned-sst-2-english' (pipeline) uses revision 'main', which can change: use a commit sha
train.py:11:6: unpinned dataset 'stanfordnlp/imdb' (load_dataset): add revision=<commit sha>
train.py:13:7: warning: trust_remote_code runs Python code from 'microsoft/phi-2' and the code can change at any time

3 problem(s) found. Run `hfpin pin` to fix.
```

`check` works offline and exits with code 1 when it finds a problem, so it fits in CI and pre-commit hooks.

Options:

| Option | What it does |
| --- | --- |
| `--online` | Also checks that every repo and revision exists on the Hub. Catches typos, model names made up by AI assistants, and renamed repos (see below). |
| `--deny-license PATTERN` | Fails on models or datasets whose license matches, for example `--deny-license "cc-by-nc*"`. Repeatable. Implies `--online`. |
| `--allow-mutable` | Accepts branch and tag revisions such as `revision="v1.0"`. |
| `--format text\|json\|github` | Output format. `github` prints workflow annotations. |
| `--exclude GLOB` | Skips matching files. Repeatable. |

### Pin them

```console
$ hfpin pin
train.py:5:7: bert-base-uncased@main -> 86b5e0934494 (now 'google-bert/bert-base-uncased', use --rename)
train.py:10:7: distilbert/distilbert-base-uncased-finetuned-sst-2-english@main -> 714eb0fa89d2
train.py:11:6: stanfordnlp/imdb@main -> e6281661ce1c
Pinned 3 download(s) in 1 file(s).
```

`pin` looks up the current commit of each repo (or of the branch or tag you already use) and edits only the call, keeping your formatting. Use `--dry-run` to see a diff first. For private or gated repos, log in with `huggingface-cli login` or set `HF_TOKEN`.

To update a model later, delete its `revision=` and run `hfpin pin` again.

### Fix renamed and legacy ids

Many popular repos moved under an organization, for example `gsm8k` is now `openai/gsm8k` and `bert-base-uncased` is now `google-bert/bert-base-uncased`. The Hub still redirects the old names, but `huggingface_hub` 2.x rejects ids without a namespace:

```text
HfUriError: Invalid HF URI 'hf://datasets/gsm8k@.../.huggingface.yaml'. Repository id must be 'namespace/name', got 'gsm8k'.
```

`hfpin check --online` reports these. Ids without a namespace are errors, and other renamed repos are warnings. `hfpin pin --rename` replaces them with the canonical id (and pins them too):

```console
$ hfpin pin --rename
gsm8k.py:12:19: gsm8k -> openai/gsm8k, gsm8k@main -> 740312add88f
Pinned 1 download(s), renamed 1 id(s) in 1 file(s).
```

### GitHub Action

```yaml
- uses: actions/checkout@v4
- uses: DipPansuriya/hfpin@v0.2.0
  with:
    paths: src          # default: .
    args: --online --deny-license "cc-by-nc*"   # optional
    hf-token: ${{ secrets.HF_TOKEN }}           # optional, for private or gated repos
```

Problems show up as annotations on the pull request diff.

## What it detects

| Call | Repo type |
| --- | --- |
| `*.from_pretrained(...)` (transformers, diffusers, peft, timm, ...) | model |
| `pipeline(task, model=...)` | model |
| `SentenceTransformer(...)` | model |
| `hf_hub_download(...)`, `snapshot_download(...)` | model, dataset or space (`repo_type=`) |
| `load_dataset(...)` | dataset |

It only looks at repo names written as string literals. It skips local paths, built-in dataset loaders such as `load_dataset("csv", ...)`, and calls whose `revision=` is a variable.

To ignore a call, add `# hfpin: ignore` on any of its lines.

## Compared with Bandit

[Bandit](https://github.com/PyCQA/bandit) rule B615 warns about unpinned Hugging Face downloads. `hfpin` also fixes them, checks that the repos exist, and checks licenses.

## License

MIT
