How to pin a Hugging Face background remover (2027)

Bria.ai

How to pin a Hugging Face background remover (2027)

Updated 2026-12-01

TL;DR

  • Pin the commit: Load the model with revision set to the full 40-character commit hash, so the weights and the custom code cannot change under you.

  • Review the code: BiRefNet and RMBG-2.0 load code from the repository with trust_remote_code=True. Read those files at the pinned commit.

  • Cache offline: Set HF_HUB_OFFLINE=1 in production so no call goes to the Hub at runtime.

  • Keep the license: Save the card's License text next to the hash. The RMBG-2.0 card says CC BY-NC 4.0, and commercial use needs an agreement with Bria.

  • No weights to pin: The Bria API takes a model parameter, RMBG2 or RMBG3, and Bria hosts the weights.

How do you use a Hugging Face background removal model in production without surprises? This guide pins one model in six steps, using BiRefNet as the worked example. Bria wrote it and makes the RMBG models.

Why pin a Hugging Face background removal model?

Without a pin, `from_pretrained` loads the latest version of the repository, so a new commit changes what runs in production.

The huggingface_hub download guide says snapshot_download() "downloads the latest revision by default" and that revision accepts a tag, a branch, a pull request or a commit hash. For models with custom code, the Transformers docs add: "load a custom model from a specific revision to avoid loading model code that may have changed." The BiRefNet README itself warns that the code on Hugging Face might not be the latest version.

  • Revision form: Tag; Accepted by revision: Yes; Note from the docs: Example v1.0

  • Revision form: Branch; Accepted by revision: Yes; Note from the docs: Example test-branch

  • Revision form: Pull request; Accepted by revision: Yes; Note from the docs: Example refs/pr/3

  • Revision form: Commit hash; Accepted by revision: Yes; Note from the docs: Full length, not 7 characters

  • Revision form: Bria API; Accepted by revision: Nothing to pin; Note from the docs: The model parameter takes RMBG2 or RMBG3, per the API reference

Source: huggingface_hub download guide, read 2026-12-02. A commit hash is the one form that does not move, which is why the steps below use it.

What should you pin and record for a background removal model?

Record five things per model: commit hash, license text, code files, weights file and access status.

  • Item: License text; Where to find it: Card License section and header tag; BiRefNet: MIT; RMBG-2.0: CC BY-NC 4.0, header tag bria-rmbg-2.0

  • Item: Custom code; Where to find it: custom_code tag, .py files in the repository; BiRefNet: birefnet.py, BiRefNet_config.py; RMBG-2.0: birefnet.py, BiRefNet_config.py

  • Item: Weights file; Where to find it: Files tab; BiRefNet: model.safetensors; RMBG-2.0: model.safetensors

  • Item: Access; Where to find it: Gated badge; BiRefNet: Not gated; RMBG-2.0: Gated, automatic approval

  • Item: Card last modified; Where to find it: Hugging Face API; BiRefNet: 2026-02-04; RMBG-2.0: 2026-11-16

Source: Hugging Face model pages and API, read 2026-12-02. The commit hash is read from the repository history on the day you pin.

How do you pin a Hugging Face model, step by step?

Six steps take a model from a card to a reproducible deployment.

  1. Save the license text. Copy the License section of the card into your repository. Note the header tag as well, because it can differ from the card text.

  2. Copy the full commit hash. Open the repository's commit history. The download guide states that a commit hash "must be the full-length hash instead of a 7-character commit hash". On 2026-12-02, the main branch of BiRefNet was at e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4, committed 2026-02-04.

  3. Read the code at that commit. trust_remote_code=True runs Python from the repository. For BiRefNet that is birefnet.py and BiRefNet_config.py.

  4. Load with the revision. See the code below.

  5. Cache the files. Download once into your image or volume, then set HF_HUB_OFFLINE=1.

  6. Test on your own images. Run a fixed set through the pinned model and compare the alpha output before each upgrade.

What does the pinned load look like?

Pass the commit hash as `revision` next to `trust_remote_code=True`.

Install the dependencies first. The BiRefNet repository lists them in requirements.txt, so run pip install -r https://huggingface.co/ZhengPeng7/BiRefNet/raw/e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4/requirements.txt for the same commit. That file pins torch==2.5.1 and leaves transformers unpinned, and current Transformers needs PyTorch 2.6 or newer. Run pip install -U torch torchvision next. The snippet then runs as shown.

from transformers import AutoModelForImageSegmentation

COMMIT = "e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4"
model = AutoModelForImageSegmentation.from_pretrained(
    "ZhengPeng7/BiRefNet",
    trust_remote_code=True,
    revision=COMMIT,
)

The pattern is the one in the Transformers docs. The BiRefNet card shows the same load without revision. Pass the same revision to snapshot_download when you build the image.

How do you stop production from calling the Hub?

Set `HF_HUB_OFFLINE=1` after the files are cached.

The huggingface_hub environment variable reference says that when it is set, "no HTTP calls will be made to the Hugging Face Hub" and only cached files are used. If a file is missing from the cache, it raises an error. Test this at build time, so a missing file fails the build and not a live request.

What changes for gated models like RMBG-2.0?

A gated model needs an approved access request, and Hugging Face grants it to individual users, not to organizations.

The Hub docs on gated models state both facts. RMBG-2.0 uses automatic approval. Download the files once with an authorized account and bake them into your build, so runtime never needs a token.

Is a pinned model enough for a commercial product?

No, because the license still decides what you may do with the weights.

The RMBG-2.0 card says commercial use "is subject to a commercial agreement with BRIA". Its page also says "For production / commercial deployment, use the Bria API". A pin fixes the version and does not change the license. Read the License section you saved in step 1.

When is the Bria API simpler than pinning?

When you do not want to host weights, because the request names the model and Bria runs it.

The model parameter in the API reference takes RMBG2 (the default) or RMBG3. In Bria's benchmark, RMBG 3.0 ranked first of 6 models, with an average rank of 1.51 across 234 hard images (benchmark post). The price is $0.018 per image for RMBG 3.0, checked 2026-12-02 on the pricing page.

Which approach should you choose?

Choose by who owns the license and the GPU.

  • Choose the API if you want a commercial license included and no GPU to run.

  • Choose a pinned BiRefNet if you want MIT weights and will review the code.

  • Choose a pinned RMBG-2.0 only with a commercial agreement with Bria.

Related posts

FAQs

Pass the full 40-character commit hash as the revision argument to from_pretrained or snapshot_download. The huggingface_hub guide says a commit hash must be the full-length hash and not a 7-character one. A tag, branch or pull request also works, but a commit hash cannot move. Copy it from the repository's commit history on the day you pin. Pass the same revision to both calls so the weights and code match.

With trust_remote_code set to true, Transformers runs Python code from the model repository. The Transformers docs advise loading a custom model from a specific revision to avoid loading model code that may have changed. Pinning the commit means the code you reviewed is the code that runs. Read the code files at that commit before you deploy. The BiRefNet README also warns that the code on Hugging Face might not be the latest version.

BiRefNet, RMBG-2.0 and RMBG-1.4 carry the custom_code tag and load with trust_remote_code set to true. BEN2 installs as a pip package from GitHub, so pin the git commit there. RMBG 3.0 loads through a Diffusers pipeline. Check the usage code on each card, because loading methods change when authors update a repository. Gated RMBG-2.0 also needs an approved access request before download.

Download the files once, then set the HF_HUB_OFFLINE environment variable to 1. The huggingface_hub reference says no HTTP calls are made to the Hub and only cached files are used. If a file is missing from the cache, an error is raised. Test this at build time so a missing file fails the build. Cache the files first, because offline mode only reads what is already cached.

Follow us on social media

discord
Social media logo
Social media card
Social media logo
Social media card
Social media logo