File size: 2,764 Bytes
1d8865b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #!/usr/bin/env python3
"""Build the Hugging Face dataset files under ``hf/``.
The parquet files embed the 48 kHz WAV audio, so a download of
``hf/data/*.parquet`` is self-contained. The README in ``hf/`` is the dataset
card; ``hf/manifests`` and ``hf/SHA256SUMS`` are the provenance copy.
Run with the optional dependencies::
uv run --no-project --with datasets --with soundfile --with pyarrow \
python scripts/export_hf.py
"""
from __future__ import annotations
import json
import shutil
from pathlib import Path
from datasets import Audio, Dataset, Features, Sequence, Value
ROOT = Path(__file__).resolve().parent.parent
OUT = ROOT / "hf"
SPLITS = {"train": "train.jsonl", "test": "test.jsonl", "anchor": "anchor.jsonl"}
FEATURES = Features(
{
"uid": Value("string"),
"id": Value("string"),
"set": Value("string"),
"split": Value("string"),
"speaker": Value("string"),
"audio": Audio(sampling_rate=48000),
"bytes": Value("int64"),
"audio_sha256": Value("string"),
"duration": Value("float32"),
"sample_rate": Value("int32"),
"channels": Value("int16"),
"bits_per_sample": Value("int16"),
"text": Value("string"),
"norm": Value("string"),
"lang": Value("string"),
"category": Value("string"),
"en_spans": Sequence(Sequence(Value("int32"))),
"source": Value("string"),
}
)
def load_jsonl(path: Path) -> list[dict]:
return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line]
def main() -> int:
data_dir = OUT / "data"
data_dir.mkdir(parents=True, exist_ok=True)
for split, name in SPLITS.items():
rows = load_jsonl(ROOT / "manifests" / name)
for row in rows:
path = ROOT / row["audio"]
row["audio"] = {"bytes": path.read_bytes(), "path": None}
dataset = Dataset.from_list(rows, features=FEATURES)
path = data_dir / f"{split}-00000-of-00001.parquet"
dataset.to_parquet(str(path))
print(f"{split:6} {len(dataset):5d} rows {path.stat().st_size / 1e6:7.1f} MB")
for name in (
"README.md",
"DATA_CARD.md",
"NOTICE",
"CITATION.cff",
"LICENSE-CC-BY-4.0.txt",
"LICENSE-APACHE-2.0",
"SHA256SUMS",
):
shutil.copy2(ROOT / name, OUT / name)
for directory in ("manifests", "text", "scripts", "docs", "releases"):
shutil.copytree(
ROOT / directory,
OUT / directory,
dirs_exist_ok=True,
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"),
)
print(f"wrote {OUT}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|