cheesechaser.utils.huggingface
This module provides utility functions for interacting with the Hugging Face Hub.
It includes functions to retrieve the Hugging Face API token, create API clients, and access the Hugging Face file system. These utilities are designed to be efficient through the use of caching mechanisms.
get_hf_token
- cheesechaser.utils.huggingface.get_hf_token() str | None [source]
Retrieve the Hugging Face API token from the environment variables.
This function is cached to avoid repeated environment variable lookups.
- Returns:
The Hugging Face API token if set, otherwise None.
- Return type:
Optional[str]
- Usage:
token = get_hf_token() if token:
print(“Token found”)
- else:
print(“Token not set in environment variables”)
get_hf_client
- cheesechaser.utils.huggingface.get_hf_client() HfApi [source]
Create and return a Hugging Face API client.
This function is cached to reuse the same client instance across multiple calls. The client is initialized with the API token retrieved from get_hf_token().
- Returns:
An instance of the Hugging Face API client.
- Return type:
HfApi
- Usage:
client = get_hf_client() # Use the client to interact with the Hugging Face Hub models = client.list_models()
get_hf_fs
- cheesechaser.utils.huggingface.get_hf_fs() HfFileSystem [source]
Create and return a Hugging Face file system instance.
This function is cached to reuse the same file system instance across multiple calls. The file system is initialized with the API token retrieved from get_hf_token().
- Returns:
An instance of the Hugging Face file system.
- Return type:
HfFileSystem
- Usage:
fs = get_hf_fs() # Use the file system to interact with files on the Hugging Face Hub files = fs.ls(‘username/repo_name’)