Datasets:
Download materialize.py from spock-oss/sel-27b-training: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/datasets/spock-oss/sel-27b-training/resolve/main/materialize.py
- Command line
-
hf download hf://datasets/spock-oss/sel-27b-training/materialize.py
-
curl -L -o materialize.py https://huggingface.co/datasets/spock-oss/sel-27b-training/resolve/main/materialize.py
3.29 kB
| """Extract the SEL Parquet snapshot into raw records for sel prepare.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| import re | |
| import tempfile | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| SPLITS = ("train", "val", "test") | |
| IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp"} | |
| def materialize(snapshot: Path, output: Path) -> None: | |
| output = output.resolve() | |
| if output.exists(): | |
| raise FileExistsError(output) | |
| output.parent.mkdir(parents=True, exist_ok=True) | |
| with tempfile.TemporaryDirectory(dir=output.parent) as temporary: | |
| root = Path(temporary) / "records" | |
| root.mkdir() | |
| for split in SPLITS: | |
| previous_index = -1 | |
| dataset = pq.ParquetFile(snapshot / "data" / f"{split}.parquet") | |
| with (root / f"{split}.jsonl").open("w", encoding="utf-8") as manifest: | |
| for batch in dataset.iter_batches(batch_size=32): | |
| for row in batch.to_pylist(): | |
| rid = row["id"] | |
| if not re.fullmatch(rf"{split}-[0-9]+", rid): | |
| raise ValueError(f"Invalid row ID: {rid}") | |
| if row["original_index"] <= previous_index or row["split"] != split: | |
| raise ValueError(f"Unexpected split or order for {rid}") | |
| previous_index = row["original_index"] | |
| directory = root / split / rid | |
| directory.mkdir(parents=True) | |
| record = json.loads(row["record_json"]) | |
| if image := row["image"]: | |
| name, data = image["path"], image["bytes"] | |
| if Path(name).name != name or Path(name).suffix not in IMAGE_EXTENSIONS: | |
| raise ValueError(f"Invalid image filename: {name}") | |
| if name != record.get("source_image"): | |
| raise ValueError(f"Image record mismatch for {rid}") | |
| if hashlib.sha256(data).hexdigest() != row["source_image_sha256"]: | |
| raise ValueError(f"Image checksum mismatch for {rid}") | |
| (directory / name).write_bytes(data) | |
| artifact = directory / "example.json" | |
| artifact.write_text(json.dumps(record, ensure_ascii=False) + "\n", encoding="utf-8") | |
| entry = { | |
| "artifact_path": artifact.relative_to(root).as_posix(), | |
| "task_id": row["task_id"], | |
| "selection_mode": row["selection_mode"], | |
| "query_first": row["query_first"], | |
| "environment_prompt": row["environment_prompt"], | |
| } | |
| manifest.write(json.dumps(entry, ensure_ascii=False) + "\n") | |
| print(f"Materialized {split}: {dataset.metadata.num_rows:,} records") | |
| root.rename(output) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("snapshot", type=Path) | |
| parser.add_argument("output", type=Path) | |
| args = parser.parse_args() | |
| materialize(args.snapshot, args.output) | |