HfHub Logo

HfHub

Hex.pmDocumentationCILicense

Elixir client for HuggingFace Hub — dataset/model metadata, file downloads, caching, and authentication. An Elixir port of Python's huggingface_hub.

hf_hub_ex provides a robust, production-ready interface to the HuggingFace Hub API, enabling Elixir applications to seamlessly access models, datasets, and spaces. This library is designed to be the foundational layer for porting Python HuggingFace libraries (datasets, evaluate, transformers) to the BEAM ecosystem.

Features

Installation

def deps do
[
{:hf_hub, "~> 0.3.0"}
]
end

Then run:

mix deps.get

Guides

Start here for production-oriented usage:

Quick start

Runtime configuration

Host applications should read OS environment variables at their boundary (for example, config/runtime.exs) and pass values into :hf_hub config:

import Config
if token = System.get_env("HF_TOKEN") do
config :hf_hub, token: token
end
if cache_dir = System.get_env("HF_HUB_CACHE") || System.get_env("HF_HOME") do
config :hf_hub, cache_dir: cache_dir
end
if System.get_env("HF_HUB_OFFLINE") in ["1", "true", "TRUE", "yes", "YES"] do
config :hf_hub, offline: true
end

Library calls also accept explicit token: options:

token = System.fetch_env!("HF_TOKEN")

Create a dataset repo

{:ok, repo} =
HfHub.Repo.create(
"my-org/my-artifact-bundle",
repo_type: :dataset,
private: false,
token: token
)

Upload a folder with LFS support

{:ok, info} =
HfHub.Commit.upload_folder(
"/path/to/exported_bundle",
"my-org/my-artifact-bundle",
repo_type: :dataset,
token: token,
commit_message: "v1.0.0: initial artifact bundle",
ignore_patterns: ["*.log.jsonl", "*.tmp", ".DS_Store"]
)

For large safetensors/model bundles, prefer conservative LFS settings:

{:ok, info} =
HfHub.Commit.upload_folder(
"/path/to/exported_bundle",
"my-org/my-artifact-bundle",
repo_type: :dataset,
token: token,
commit_message: "v1.0.0: initial artifact bundle",
ignore_patterns: ["*.log.jsonl", "*.tmp", ".DS_Store"],
max_workers: 1,
lfs_upload_timeout: 60 * 60 * 1000,
lfs_task_timeout: 65 * 60 * 1000
)

See Uploads and LFS for the multipart protocol notes and operational rationale.

Tag a release

{:ok, tag} =
HfHub.Git.create_tag(
"my-org/my-artifact-bundle",
"v1.0.0",
repo_type: :dataset,
message: "Initial public release",
token: token
)

This uses the Python-client-compatible endpoint shape:

POST /api/datasets/my-org/my-artifact-bundle/tag/main
{"tag":"v1.0.0","message":"Initial public release"}

Download a file

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

Offline/cache helpers

if HfHub.offline_mode?() do
IO.puts("Only cached files will be used")
end
case HfHub.try_to_load_from_cache("bert-base-uncased", "config.json") do
{:ok, path} -> File.read!(path)
{:error, :not_cached} -> :download_or_fail
end

API overview

HfHub.Repo

Repository lifecycle helpers:

HfHub.Commit

Commit and upload helpers:

HfHub.Git

Git refs and release helpers:

HfHub.Download

Download and snapshot helpers:

HfHub.Api

Hub metadata APIs:

Other modules

Python-client alignment

hf_hub_ex intentionally follows Python huggingface_hub route and payload shapes for the artifact-publishing surface:

Examples

The examples/ directory contains runnable scripts:

./examples/run_all.sh
mix run examples/list_datasets.exs
mix run examples/list_models.exs
mix run examples/download_file.exs
mix run examples/snapshot_download.exs
mix run examples/auth_demo.exs

See examples/README.md for details.

Testing

mix format --check-formatted
mix compile --warnings-as-errors
mix test
mix credo --strict
mix dialyzer
mix docs --warnings-as-errors

Roadmap

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for your changes
  4. Run the quality gates above
  5. Open a pull request

License

MIT License - See LICENSE for details.

Acknowledgments


Built with ❤️ by the North-Shore-AI team