|
11 | 11 | import logging |
12 | 12 | import os |
13 | 13 | from collections.abc import Callable |
| 14 | +from pathlib import Path |
14 | 15 | from typing import TYPE_CHECKING |
15 | 16 |
|
16 | 17 | from datasets import Dataset, Features, concatenate_datasets, load_dataset |
|
24 | 25 |
|
25 | 26 | _MAX_NUM_PROC = len(os.sched_getaffinity(0)) |
26 | 27 |
|
| 28 | +_EXTENSION_TO_BUILDER = { |
| 29 | + ".parquet": "parquet", |
| 30 | + ".csv": "csv", |
| 31 | + ".json": "json", |
| 32 | + ".jsonl": "json", |
| 33 | + ".txt": "text", |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +def _load_dataset_entry(path: str, split: str, **load_kwargs) -> Dataset: |
| 38 | + """Load *path* as a local data file, a local dataset directory, or a Hub repo id. |
| 39 | +
|
| 40 | + ``load_dataset(path, ...)`` auto-detects a local file from the bare |
| 41 | + ``path`` string, but that inference is unreliable for a single literal |
| 42 | + file (as opposed to a directory it can glob into) — it can report "no |
| 43 | + data file found" for a file that demonstrably exists. Passing the |
| 44 | + builder name explicitly via ``data_files=`` for local files sidesteps |
| 45 | + that ambiguity; local directories and Hub repo ids keep working through |
| 46 | + the normal bare-path form. |
| 47 | + """ |
| 48 | + local_path = Path(path) |
| 49 | + if local_path.is_file(): |
| 50 | + builder = _EXTENSION_TO_BUILDER.get(local_path.suffix) |
| 51 | + if builder is None: |
| 52 | + raise ValueError( |
| 53 | + f"Unsupported local dataset file extension '{local_path.suffix}' for '{path}'. " |
| 54 | + f"Supported extensions: {sorted(_EXTENSION_TO_BUILDER)}" |
| 55 | + ) |
| 56 | + return load_dataset(builder, data_files=str(local_path), split=split, **load_kwargs) |
| 57 | + return load_dataset(path, split=split, **load_kwargs) |
| 58 | + |
27 | 59 |
|
28 | 60 | def _resolve_num_proc(configured: int | None) -> int: |
29 | 61 | """Return the number of worker processes for ``.map()`` / ``.filter()``. |
@@ -137,7 +169,7 @@ def load_and_mix_datasets( |
137 | 169 | if entry.subset is not None: |
138 | 170 | load_kwargs["name"] = entry.subset |
139 | 171 |
|
140 | | - ds = load_dataset(entry.path, split=entry.split, **load_kwargs) |
| 172 | + ds = _load_dataset_entry(entry.path, entry.split, **load_kwargs) |
141 | 173 |
|
142 | 174 | # Apply optional per-dataset transform. |
143 | 175 | if entry.transform is not None: |
|
0 commit comments