Repository navigation
Load a dataset from a cloud storage directory given as an fsspec URL - #9163
behroozazarkhalili wants to merge 1 commit into
Conversation
load_dataset("s3://bucket/dir") was treated as a relative local path and failed, while data_files="s3://..." already worked. A non-local, non-Hub URL is now resolved like a local directory over the remote filesystem, with storage_options passed to it.
|
Note on CI: on Python 3.10 the new |
|
Tested 74ae268 offline on Windows/Python 3.12.14: the 8 fsspec-URL cases pass. I also checked a conflicting README/.huggingface.yaml pair through load_dataset in both streaming modes: the standalone YAML wins, selecting its config, split, and actual rows. Those 2 extra cases pass (10 total). This adds coverage for the new factory's metadata merge, which the README-only case does not exercise. Reversing the merge precedence only inside FsspecDatasetModuleFactory makes both added cases fail with train instead of validation. No defect found; this is a suggested regression guard, with mutation rather than parent-revision evidence. Append to tests/test_load.py using the existing helper/imports: @pytest.mark.parametrize("streaming", [False, True])
def test_fsspec_directory_standalone_yaml_overrides_card(mockfs, streaming):
_write_mockfs_csv_dataset(mockfs, "bucket/overrides")
with mockfs.open("bucket/overrides/README.md", "w") as f:
f.write("---\nconfigs:\n- config_name: card\n data_files:\n - split: train\n path: train.csv\n---\n")
with mockfs.open("bucket/overrides/.huggingface.yaml", "w") as f:
f.write("configs:\n- config_name: standalone\n data_files:\n - split: validation\n path: test.csv\n")
ds = load_dataset("mock://bucket/overrides", storage_options=mockfs.storage_options, streaming=streaming)
assert set(ds) == {"validation"}
assert ds["validation"].config_name == "standalone"
assert list(ds["validation"]) == [{"text": "c"}]Ruff check/format pass. This uses the repository's mockfs fixture and short local test/cache paths; it does not validate live S3 credentials, Python 3.10, or the full loading suite. AI disclosure: Codex designed and ran these checks under @JonnyFaeth's standing authorization; Jonathan has not personally reviewed this run. |
Closes #5281
data_files="s3://..."withstorage_optionsalready works, but the form asked for in #5281, a cloud storage directory as the first argument, doesn't: the URL is treated as a relative local path and fails withCouldn't find any data file at <cwd>/s3:/bucket/dir.With this change a directory given as a fsspec URL is read like a local directory:
As proposed in #5281 (comment). I didn't get a go-ahead there, so feel free to close this if you prefer to keep the first argument local-or-Hub only.
How it works:
dataset_module_factorysends a path that is a remote URL (is_remote_url), after thehf://datasets/andhf://buckets/prefixes are handled as before, to a newFsspecDatasetModuleFactory. Local paths, Hub repositories and buckets take the same branches as before.HubBucketDatasetModuleFactory, with the filesystem coming fromurl_to_fsand the storage options selected by_prepare_path_and_storage_options, likeresolve_patterndoes: it readsREADME.md/.huggingface.yamlif present, gets the patterns withget_data_patterns, resolves them withDataFilesDict.from_patternsand infers the builder from the extensions.download_configis passed to each of these so the credentials reach the filesystem.data_dir,data_files(relative to the directory),streaming=True, and configs defined in a dataset card work as for a local directory.FileNotFoundError: Couldn't find a directory at <url>.The factory repeats most of
HubBucketDatasetModuleFactory.get_module. I left the bucket factory untouched becausetests/test_buckets.pypatchesdatasets.load.HfFileSystem; I can merge the two in this PR if you want.Docs: a bullet in the
pathdocstring ofload_dataset/load_dataset_builderand a short section infilesystems.mdx.Tests (5 new functions, 8 cases, in
tests/test_load.py, on themockfsfixture): splits from file names in map and streaming mode with and without a trailing slash, the builder'sdata_filesanddataset_name,data_diranddata_files, a dataset card with a config, missing and empty directories.tests/test_load.py,tests/test_buckets.pyandtests/test_data_files.pyon Python 3.12: 3 tests fail onmainin my environment (test_load_dataset_builder_then_edit_then_load_againand twotest_data_files.pytests); on this branch 2 of the same 3 fail and the 8 new cases pass.