Filesystem utilities for HuggingFace cache management.
Provides low-level functions for managing the local cache directory structure, file paths, and locking mechanisms.
Examples
# Ensure cache directory exists
:ok = HfHub.FS.ensure_cache_dir()
# Get repository path
path = HfHub.FS.repo_path("bert-base-uncased", :model)
# Get file path in repository
path = HfHub.FS.file_path("bert-base-uncased", :model, "config.json")
# Acquire file lock for concurrent downloads
{:ok, lock} = HfHub.FS.lock_file("bert-base-uncased", "pytorch_model.bin")
# ... download file ...
:ok = HfHub.FS.unlock_file(lock)
Summary
Functions
Gets the configured cache directory.
Ensures the cache directory exists.
Gets the local path for a file in a repository.
Acquires a lock on a file for concurrent download protection.
Gets the local path for a repository.
Releases a file lock.
Functions
@spec cache_dir() :: Path.t()
Gets the configured cache directory.
Examples
dir = HfHub.FS.cache_dir()
# => "/home/user/.cache/huggingface"
@spec ensure_cache_dir() :: :ok | {:error, term()}
Ensures the cache directory exists.
Creates the cache directory and any necessary subdirectories if they don't exist.
Examples
:ok = HfHub.FS.ensure_cache_dir()
@spec file_path( HfHub.repo_id(), HfHub.repo_type(), HfHub.filename(), HfHub.revision() ) :: Path.t()
Gets the local path for a file in a repository.
Arguments
repo_id- Repository IDrepo_type- Type of repositoryfilename- Name of the filerevision- Git revision (defaults to "main")
Examples
path = HfHub.FS.file_path("bert-base-uncased", :model, "config.json")
# => "/home/user/.cache/huggingface/hub/models--bert-base-uncased/snapshots/main/config.json"
@spec lock_file(HfHub.repo_id(), HfHub.filename()) :: {:ok, reference()} | {:error, term()}
Acquires a lock on a file for concurrent download protection.
Returns {:ok, lock} where lock is a reference that can be used to unlock the file.
Arguments
repo_id- Repository IDfilename- Name of the file
Examples
{:ok, lock} = HfHub.FS.lock_file("bert-base-uncased", "pytorch_model.bin")
# ... perform download ...
:ok = HfHub.FS.unlock_file(lock)
@spec repo_path(HfHub.repo_id(), HfHub.repo_type()) :: Path.t()
Gets the local path for a repository.
Arguments
repo_id- Repository IDrepo_type- Type of repository (:model,:dataset, or:space)
Examples
path = HfHub.FS.repo_path("bert-base-uncased", :model)
# => "/home/user/.cache/huggingface/hub/models--bert-base-uncased"
Releases a file lock.
Arguments
lock- Lock reference fromlock_file/2
Examples
{:ok, lock} = HfHub.FS.lock_file("bert-base-uncased", "config.json")
:ok = HfHub.FS.unlock_file(lock)