Skip to content

Add Hugging Face filesystem support to fsspec #1997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mkdocs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Iceberg works with the concept of a FileIO which is a pluggable module for readi
- **hdfs**: `PyArrowFileIO`
- **abfs**, **abfss**: `FsspecFileIO`
- **oss**: `PyArrowFileIO`
- **hf**: `FsspecFileIO`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to allow PyArrowFileIO as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no HF filesystem implementation in arrow C++ yet unfortunately ! But hopefully soon


You can also set the FileIO explicitly:

Expand Down Expand Up @@ -193,6 +194,17 @@ PyIceberg uses [S3FileSystem](https://arrow.apache.org/docs/python/generated/pya

<!-- markdown-link-check-enable-->

### Hugging Face

<!-- markdown-link-check-disable -->

| Key | Example | Description |
| ----------- | ------------------------ | --------------------------------------------------------- |
| hf.endpoint | <https://huggingface.co> | Configure the endpoint for Hugging Face |
| hf.token | hf_xxx | The Hugging Face token to access HF Datasets repositories |

<!-- markdown-link-check-enable-->

### PyArrow

<!-- markdown-link-check-disable -->
Expand Down
76 changes: 57 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyiceberg/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
GCS_SERVICE_HOST = "gcs.service.host"
GCS_DEFAULT_LOCATION = "gcs.default-bucket-location"
GCS_VERSION_AWARE = "gcs.version-aware"
HF_ENDPOINT = "hf.endpoint"
HF_TOKEN = "hf.token"
PYARROW_USE_LARGE_TYPES_ON_READ = "pyarrow.use-large-types-on-read"


Expand Down Expand Up @@ -306,6 +308,7 @@ def delete(self, location: Union[str, InputFile, OutputFile]) -> None:
"viewfs": [ARROW_FILE_IO],
"abfs": [FSSPEC_FILE_IO],
"abfss": [FSSPEC_FILE_IO],
"hf": [FSSPEC_FILE_IO],
}


Expand Down
12 changes: 12 additions & 0 deletions pyiceberg/io/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
GCS_SESSION_KWARGS,
GCS_TOKEN,
GCS_VERSION_AWARE,
HF_ENDPOINT,
HF_TOKEN,
S3_ACCESS_KEY_ID,
S3_CONNECT_TIMEOUT,
S3_ENDPOINT,
Expand Down Expand Up @@ -208,6 +210,15 @@ def _adls(properties: Properties) -> AbstractFileSystem:
)


def _hf(properties: Properties) -> AbstractFileSystem:
from huggingface_hub import HfFileSystem

return HfFileSystem(
endpoint=properties.get(HF_ENDPOINT),
token=properties.get(HF_TOKEN),
)


SCHEME_TO_FS = {
"": _file,
"file": _file,
Expand All @@ -218,6 +229,7 @@ def _adls(properties: Properties) -> AbstractFileSystem:
"abfss": _adls,
"gs": _gs,
"gcs": _gs,
"hf": _hf,
}


Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ boto3 = { version = ">=1.24.59", optional = true }
s3fs = { version = ">=2023.1.0", optional = true }
adlfs = { version = ">=2023.1.0", optional = true }
gcsfs = { version = ">=2023.1.0", optional = true }
huggingface-hub = { version = ">=0.24.0", optional = true }
psycopg2-binary = { version = ">=2.9.6", optional = true }
sqlalchemy = { version = "^2.0.18", optional = true }
getdaft = { version = ">=0.2.12", optional = true }
Expand Down Expand Up @@ -306,6 +307,7 @@ sql-postgres = ["sqlalchemy", "psycopg2-binary"]
sql-sqlite = ["sqlalchemy"]
gcsfs = ["gcsfs"]
rest-sigv4 = ["boto3"]
hf = ["huggingface-hub"]
pyiceberg-core = ["pyiceberg-core"]

[tool.pytest.ini_options]
Expand Down Expand Up @@ -427,6 +429,10 @@ ignore_missing_imports = true
module = "gcsfs.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "huggingface_hub.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "packaging.*"
ignore_missing_imports = true
Expand Down