🤗 Filesystem (hffs
) is a package that provides a pythonic fsspec-compatible file interface to the Hugging Face Hub. It builds on top of the Hugging Face Hub client library to read and write files and inspect repositories on the Hub.
pip install hffs
HfFileSystem
is the library’s main class that holds connection information and enables typical filesystem style operations like cp
, mv
, ls
, du
, glob
, get_file
, put_file
etc.
>>> from hffs import HfFileSystem
>>> fs = HfFileSystem()
>>> # List files in a directory
>>> fs.ls("datasets/my-username/my-dataset-repo/data", detail=False)
['datasets/my-username/my-dataset-repo/data/train.csv', 'datasets/my-username/my-dataset-repo/data/test.csv']
>>> # List all ".csv" files in a repo
>>> fs.glob("datasets/my-username/my-dataset-repo/**.csv")
['datasets/my-username/my-dataset-repo/data/train.csv', 'datasets/my-username/my-dataset-repo/data/test.csv']
>>> # Read the contents of a remote file
>>> with fs.open("datasets/my-username/my-dataset-repo/data/train.csv", "r") as f:
... train_data = f.readlines()
>>> # Read all the contents of a remote file at once as a string
>>> train_data = fs.read_text("datasets/my-username/my-dataset-repo/data/train.csv")
>>> # Write a remote file
>>> with fs.open("datasets/my-username/my-dataset-repo/data/validation.csv", "w") as f:
... f.write("text,label")
... f.write("Fantastic movie!,good")
The prefix for datasets is “datasets/”, the prefix for spaces is “spaces/” and models don’t need a prefix in the URL.
The optional revision
argument can be passed to open a filesystem from a specific commit (any revision such as a branch or a tag name or a commit hash).
Unlike Python’s built-in open
, fsspec
’s open
defaults to binary mode, "rb"
. This means you must explicitly set encoding as "r"
for reading and "w"
for writing in text mode.
🤗 Filesystem can be used with any library that integrates fsspec
, and the URL has the following structure:
hf://[<repo_type_prefix>]<repo_id>/<path/in/repo>
The revision
parameter is optional. Most integrations also allow you to pass optional parameters to the filesystem’s initializer as storage_options
, a dictionary mapping parameter names to their values:
>>> storage_options = {"revision": "main"}
In many cases, you must be logged in with a Hugging Face account to interact with the Hub:
huggingface-cli login
Refer to the Login section of the Hugging Face Hub client library documentation to learn more about authentication methods on the Hub.
It is also possible to login programmatically by passing your token
as an argument to HfFileSystem
:
>>> import hffs
>>> fs = hffs.HfFileSystem(token=token)
If you login this way, be careful not to accidentally leak the token when sharing your source code!
As 🤗 Filesystem is based on fsspec, it is compatible with most of the APIs that it offers. For more details, check out the fsspec’s API Reference.
Read the Integration Zoo guide to learn more about libraries that integrate with fsspec
, allowing convenient access to the Hub through 🤗 Filesystem.
If you have questions about 🤗 Filesystem, feel free to join and ask the community on our forum.