HfHub (HfHub v0.2.0)

Copy Markdown View Source

Elixir client for HuggingFace Hub.

HfHub provides a comprehensive interface to the HuggingFace Hub API, enabling Elixir applications to access models, datasets, and spaces.

Features

  • Hub API Client — Fetch metadata for models, datasets, and spaces
  • File Downloads — Stream files from HuggingFace repositories with resume support
  • Smart Caching — Local file caching with LRU eviction and integrity checks
  • Filesystem Utilities — Manage local HuggingFace cache directory structure
  • Authentication — Token-based authentication for private repositories

Quick Start

# Get model information
{:ok, model_info} = HfHub.Api.model_info("bert-base-uncased")

# Download a model file
{:ok, path} = HfHub.Download.hf_hub_download(
  repo_id: "bert-base-uncased",
  filename: "config.json",
  repo_type: :model
)

# Check cache
cached? = HfHub.Cache.cached?(
  repo_id: "bert-base-uncased",
  filename: "config.json"
)

Configuration

Configure in config/config.exs:

config :hf_hub,
  token: System.get_env("HF_TOKEN"),
  cache_dir: Path.expand("~/.cache/huggingface"),
  endpoint: "https://huggingface.co"

Modules

Summary

Types

A repository reference (Bumblebee-compatible).

Types

filename()

@type filename() :: String.t()

repo_id()

@type repo_id() :: String.t()

repo_type()

@type repo_type() :: :model | :dataset | :space

repository()

@type repository() :: HfHub.Repository.t()

A repository reference (Bumblebee-compatible).

Can be either:

  • {:hf, repository_id} - HuggingFace repository
  • {:hf, repository_id, opts} - HuggingFace repository with options
  • {:local, directory} - Local directory

revision()

@type revision() :: String.t()

Functions

cached_download(url, opts \\ [])

See HfHub.Hub.cached_download/2.

file_listing_url(repository_id, subdir, revision)

See HfHub.Hub.file_listing_url/3.

file_url(repository_id, filename, revision)

See HfHub.Hub.file_url/3.

get_repo_files(repository)

See HfHub.RepoFiles.get_repo_files/1.

is_offline_mode()

This function is deprecated. Use offline_mode?/0 instead.
@spec is_offline_mode() :: boolean()

Alias for offline_mode?/0 for Python compatibility.

Deprecated: Use offline_mode?/0 instead.

offline_mode?()

@spec offline_mode?() :: boolean()

Check if offline mode is enabled.

Offline mode can be enabled via:

  • HF_HUB_OFFLINE=1 environment variable
  • Application.put_env(:hf_hub, :offline, true)

When offline mode is enabled, no network requests are made and only cached files are used.

Examples

if HfHub.offline_mode?() do
  IO.puts("Running in offline mode")
end

try_to_load_from_cache(repo_id, filename, opts \\ [])

@spec try_to_load_from_cache(repo_id(), filename(), keyword()) ::
  {:ok, Path.t()} | {:error, :not_cached}

Try to load a file from cache without network access.

Returns {:ok, path} if the file exists in cache, {:error, :not_cached} otherwise. Does not attempt any network requests, even if offline mode is not enabled.

This is useful when you want to check if a file is available locally before deciding whether to download it.

Arguments

  • repo_id - Repository ID (e.g., "bert-base-uncased")
  • filename - Name of the file to look up
  • opts - Options

Options

  • :revision - Git revision. Defaults to "main".
  • :repo_type - Type of repository (:model, :dataset, or :space). Defaults to :model.

Examples

case HfHub.try_to_load_from_cache("bert-base-uncased", "config.json") do
  {:ok, path} ->
    # File is cached, use it
    File.read!(path)
  {:error, :not_cached} ->
    # File not cached, need to download
    {:ok, path} = HfHub.Download.hf_hub_download(
      repo_id: "bert-base-uncased",
      filename: "config.json"
    )
    File.read!(path)
end