Working with Hugging Face

CodeScoring.Save implements the HTTP API of the Hugging Face Hub that the huggingface_hub library and the hf CLI work with: the client only needs the Save repository URL in the HF_ENDPOINT variable. One repository type is supported:

  • a proxy repository — a caching proxy in front of huggingface.co. Models, datasets, and spaces are downloaded through Save and stay in its cache.

Hosted and group repositories are not supported for Hugging Face: a model cannot be published to Save with hf upload. For the full list, see Limitations.

Creating a Repository in the Web Interface

  1. In the sidebar, choose Projects and open the project.

  2. Click Create repository.

  3. Fill in the common fields Name, Key, Description, and Color — the same way as for other formats (see Repository management).

  4. In the Format field, choose Hugging Face.

  5. The Type field offers only Proxy — it is selected automatically.

  6. Fill in the proxy repository fields:

    • Proxy URL — the Hub URL: https://huggingface.co. Save accepts only URLs from the list of allowed hosts. By default, these are huggingface.co and hf.co; to connect a Hub mirror, the administrator adds its host to a backend environment variable, listing every allowed host: HF_PROXY_ALLOWED_UPSTREAM_HOSTS=huggingface.co,hf.co,hf-mirror.example.com. Save rejects a URL outside the list with the Failed to create a repository notification.

    • Cache TTL, seconds — how many seconds Save treats the Hub's answer about a branch or a tag (for example, which commit main points to) as current before checking it again. The default is 3600 (one hour). The value does not affect files: a file of a specific commit never changes and stays in the cache until it is deleted.

    • Authentication — how Save authenticates to the Hub:

      ValueFieldsWhat upstream receivesWhen to choose
      none—Requests without authenticationOnly public models, datasets, and spaces are needed
      bearerTokenAuthorization: Bearer <token>Gated or private Hub repositories are needed, or the higher request limits of an account

      In the Token field, enter a Hugging Face access token (hf_...); read permission is enough. Once saved, the token is never shown in the UI or returned by the API: on the edit form, a stored value appears as ******** — leave it as is to keep it. For details, see Upstream access for proxy repositories.

  7. Optionally attach Cleanup policy and click Create repository.

If the name you enter in the Name field contains the word huggingface (for example, huggingface proxy), the link under the field fills in Format, Type, and Proxy URL for you.

On the page of the created repository, the chain-link icon button opens Useful snippets — ready-made commands for this particular repository: Configure the client, Download a model, Whole repository / dataset, and From Python.

Creating through the API

The same can be done with an API request. Set format to huggingface and pass the Hugging Face token in the auth block:

curl -X POST https://save.example.com/api/v1/repos \
  -H "Content-Type: application/json" \
  -u "<username>:<password>" \
  -d '{
    "project": "ml",
    "name": "hf-proxy",
    "format": "huggingface",
    "repository_type": "proxy",
    "remote_url": "https://huggingface.co",
    "auth": {"auth_type": "bearer", "token": "<hf-token>"},
    "cache_ttl": 3600
  }'

For public Hub repositories, the auth block is not needed. A request with "repository_type": "hosted" returns 400.

Proxy repository specifics
  • A file is cached on the first download: Save serves it to the client while fetching it from upstream, and stores it after the client has received the last byte. A large file therefore appears in the repository tree some time after the download finishes.
  • The client gets every response from Save. There are no redirects to the Hugging Face CDN: Save downloads large (LFS) files from the CDN itself. Save therefore needs outbound access not only to huggingface.co but also to the CDN hosts the Hub redirects to (for example, cdn-lfs.huggingface.co and cas-bridge.xethub.hf.co; the list is defined by Hugging Face).
  • The Save token the client came with is not passed to upstream. Only the token from the repository's Token field goes to the Hub.
  • Save verifies a downloaded file against the checksum declared by the Hub. A mismatch is treated as an upstream error (upstream_checksum_mismatch).
  • Identical content is stored once: if a file did not change between commits, or lives in a Hub repository under two names, a single copy takes up storage space.
  • The Xet protocol is not supported. A client with hf_xet installed automatically falls back to a regular HTTP download — the file content is the same.

URL Scheme

All paths below are relative to the repository URL https://save.example.com/cs-save/hf/<project>/<repository> — this is the value of HF_ENDPOINT:

PathMethodWhat it is
<repo_id>/resolve/<revision>/<path>GET, HEADModel file
datasets/<repo_id>/resolve/<revision>/<path>GET, HEADDataset file
spaces/<repo_id>/resolve/<revision>/<path>GET, HEADSpace file
api/<type>/<repo_id>GETHub repository information
api/<type>/<repo_id>/revision/<revision>GETRevision information: the commit and the file list
api/<type>/<repo_id>/tree/<revision>[/<path>]GETFile tree of a revision
api/<type>/<repo_id>/refsGETBranches and tags
api/<type>/<repo_id>/paths-info/<revision>POSTInformation about selected paths
api/whoami-v2GETThe Save account the token belongs to

Here <repo_id> is the Hub repository identifier (google-bert/bert-base-uncased), <type> is models, datasets, or spaces, and <revision> is a branch, a tag, or a commit hash. If a revision name contains / (for example, refs/pr/1), it is encoded as %2F in the URL; the client does this itself.

Save answers a file request itself, without a redirect: 200 or 206, and the X-Repo-Commit, ETag, and Content-Length headers are always present. Resuming a download is supported: a single range in the Range header (bytes=N-, bytes=N-M, or bytes=-N). HEAD returns the same headers and does not start a download from upstream.

Any other Hub operation (file uploads, commits, Xet tokens, search) returns 404 with the X-Error-Code: EntryNotFound header.

Connecting the Client

The repository URL has the form https://save.example.com/cs-save/hf/<project>/<repository>. Set it in the HF_ENDPOINT variable without a trailing slash, and put a Save token in HF_TOKEN:

export HF_ENDPOINT=https://save.example.com/cs-save/hf/<project>/hf-proxy
export HF_TOKEN=<Save token>

HF_TOKEN is a Save token, not a Hugging Face one. The client sends it in the Authorization: Bearer header. The following work:

  • a robot account API key — for CI/CD and long downloads;
  • a user's session access token — returned by POST /api/v1/auth/login; by default, it is valid for 15 minutes.

The account needs the artifacts.download permission on the repository. To check that the client is connected and the token is accepted, run hf auth whoami: it shows the name of the Save account.

The huggingface_hub library and the tools built on it (for example, transformers) read the same variables. The variables are read when the library is imported, so set them before starting Python.

Robot accounts in CI

Create a robot account with the artifacts.download permission and pass its API key in HF_TOKEN — without the login and the sa$ prefix:

export HF_TOKEN=<api-key>

For details on robot accounts, see Authentication.

Anonymous access

If anonymous read is enabled, HF_TOKEN can be omitted. Keep in mind: without HF_TOKEN, the client sends Save the Hugging Face token if one is stored on the machine by hf auth login. With anonymous read enabled, Save serves such a request as anonymous; with it disabled, Save responds with 401. The HF_TOKEN value takes precedence over the stored token.

Client version

Use an up-to-date version of huggingface_hub: pip install -U huggingface_hub. In version 0.28.0, the client dropped the path from HF_ENDPOINT and sent requests to the host root — the bug is fixed in 0.28.1. Older versions use the huggingface-cli command instead of hf.

Downloading Models, Datasets, and Spaces

From then on, the client works as usual:

# A single model file
hf download google-bert/bert-base-uncased config.json

# A whole model
hf download hf-internal-testing/tiny-random-bert

# A part of a dataset
hf download rajpurkar/squad --repo-type dataset --include "plain_text/*"

# A space file
hf download gradio/hello_world app.py --repo-type space

The same from Python:

from huggingface_hub import snapshot_download

print(snapshot_download("hf-internal-testing/tiny-random-bert"))

Branches and tags on the Hub can be rewritten, while a commit is immutable. To make a build always get the same files, specify the commit hash:

hf download google-bert/bert-base-uncased config.json --revision <commit-sha>

Gated and Private Hub Repositories

Hugging Face grants access to gated models to a specific account after the terms on the model page are accepted. To download such models through Save:

  1. Get access to the model on huggingface.co and create an access token for that account.
  2. Open the repository in Save and click the pencil button (Edit repository) in the header.
  3. In the Authentication field, choose bearer, enter the token in the Token field, and click Save.

Without a token, or with the token of an account that has not been granted access, the client reports that the repository is gated. Save returns the Hub's response unchanged: the 401 or 403 status, the X-Error-Code: GatedRepo header, and a message marked upstream:. Private Hub repositories are connected the same way.

The upstream token is shared by all users of the repository

Save accesses the Hub on behalf of a single account — the one whose token is set in the repository. Everything available to that account can be downloaded by every Save user with the artifacts.download permission on the repository. If access to gated models must be restricted, create a separate proxy repository for them and grant permissions on it alone.

Browsing and Cleaning Up the Cache

Downloaded files are visible in the tree on the repository page. Every file is a separate artifact tied to the Hub repository and the commit it was downloaded from: the same file from two commits is two artifacts. Search, sorting, details, and actions on artifacts work the same way as for other formats (see Repository management).

A file can be deleted from the cache. There are two ways:

  • tick the checkbox to the left of the artifact in the repository tree and click Delete in the panel that appears above the tree. This way you can delete several files at once;
  • click the artifact in the repository tree and, in the right pane, choose Delete artifact in the three-dot menu (next to Download).

On the next request, Save downloads the deleted file from upstream again.

Models take up tens of gigabytes, so it is worth attaching a cleanup policy to the repository. The artifact version is the commit: the keep-latest-versions policy with the value 1 keeps, for every Hub repository, the files of the commit that was downloaded through Save last. A model, a dataset, and a space with the same name are counted separately.

Working When Upstream Is Unavailable

If huggingface.co is unavailable, Save keeps serving what is already in the cache:

  • files and whole models that were downloaded through this repository before — including to clients with an empty local cache;
  • information about a branch or a tag — from the cache during Cache TTL, and after it expires, the last known answer until upstream is back;
  • the "no such file" answer: if the Hub has already reported that a commit does not have the requested file, Save returns 404 with X-Error-Code: EntryNotFound. Clients request the optional files of a model on every load, and such probes do not turn into errors.

A file that has not been requested through Save yet cannot be downloaded: Save responds with 502 or 504. For offline environments, download the required models and revisions through Save in advance.

Limitations

  • Proxy repositories only. Publishing (hf upload, commits, creating Hub repositories) is not supported: such requests return 404.
  • Group repositories are not available for the format.
  • The Xet protocol is not supported; the client uses HTTP.
  • The git protocol is not supported: git clone with the Save repository URL does not work.
  • Hub search, organization lists, and commit history are not available through Save.

Repository URL Migration

Use case: migrating a Hugging Face proxy from Nexus / Artifactory to CodeScoring.Save.

SourceHF_ENDPOINT before migrationHF_ENDPOINT after migration
Nexushttps://nexus.host.ru/repository/huggingface-proxyhttps://save.example.com/cs-save/hf/<project>/hf-proxy
Artifactoryhttps://jfrog.host.ru/artifactory/api/huggingfaceml/hf-remotehttps://save.example.com/cs-save/hf/<project>/hf-proxy
Official Hubnot set (https://huggingface.co)https://save.example.com/cs-save/hf/<project>/hf-proxy

In HF_TOKEN, replace the former token with a Save token. If the credentials were passed in the URL itself (https://<user>:<password>@nexus.host.ru/...), remove them from HF_ENDPOINT. The HF_HUB_DISABLE_XET=1 variable, if it was set, is not required for Save.

Troubleshooting

Which Requests the Client Makes

Run the client with the HF_DEBUG variable — it prints every request as a curl command:

HF_DEBUG=1 hf download google-bert/bert-base-uncased config.json

All requests must go to the Save URL. If the client accesses huggingface.co, check that HF_ENDPOINT is set in the same environment the client runs in and that the huggingface_hub version is not 0.28.0. If there are no requests at all, check whether offline mode HF_HUB_OFFLINE=1 is enabled.

Checking Access and File Metadata

# The Save account the token belongs to
curl -H "Authorization: Bearer <Save token>" \
  https://save.example.com/cs-save/hf/<project>/hf-proxy/api/whoami-v2

# File headers without downloading the file
curl -I -H "Authorization: Bearer <Save token>" \
  https://save.example.com/cs-save/hf/<project>/hf-proxy/google-bert/bert-base-uncased/resolve/main/config.json

The response to the second request must contain the X-Repo-Commit, ETag, and Content-Length headers.

Response Codes

The cause of an error is given in the X-Error-Code and X-Error-Message headers — these are what the client shows — and in the error field of the response body. A message marked upstream: means the refusal came from the Hub, not from Save.

ResponseCauseWhat to do
401 without the X-Error-Code headerSave did not accept the client's tokenCheck HF_TOKEN: it must be a Save token, not a Hugging Face one
401 or 403, X-Error-Code: GatedRepoThe Hub did not grant access to a gated repositorySet the upstream token of an account with access in the repository
403, error: download_blockedThe download is blocked by a security policyThe reason is given in the message
404 or 401, X-Error-Code: RepoNotFoundThere is no such Save repository, or the Hub did not find the repository or does not show it without authenticationCheck <project> and <repository> in HF_ENDPOINT and the Hub repository identifier; for a private repository, set the upstream token
404, X-Error-Code: RevisionNotFoundThe Hub repository has no such branch, tag, or commitCheck the --revision value
404, X-Error-Code: EntryNotFoundThe revision has no such file, or the operation is not supportedCheck the file path
400, error: not_huggingface_repository or invalid_pathHF_ENDPOINT points to a repository of another format, or the path is invalidCheck the repository URL
416The Range starts beyond the end of the fileRequest a range within the file size — it is given in the Content-Range header of the response
429, error: upstream_failedThe Hub limited the number of requests from SaveRetry later; set an upstream token in the repository — account limits are higher than anonymous ones
502, error: upstream_checksum_mismatchThe content received from upstream did not match the checksumRetry the download. If the error persists, check whether a proxy between Save and upstream modifies the content
502 or 504, error: upstream_failedUpstream is unavailable or did not respond in timeCheck Save's access to huggingface.co and the CDN, and the proxy and certificate settings

A File Is Downloaded but Missing from the Repository Tree

  • The client does not download a file again if it is already in its local cache (~/.cache/huggingface): only what the client actually downloaded through Save gets into it. Repeat the download with an empty cache by setting another directory in HF_HOME.
  • A large file appears in the tree after Save finishes storing it. Refresh the page later.

Service Status

curl https://save.example.com/health

Repository Audit

curl -u "<username>:<password>" \
  "https://save.example.com/api/v1/admin/audit?resource_type=repository&q=hf-proxy&limit=50"
Was this page helpful?