Download export_data.py from jhlee11/FutureCAD: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/datasets/jhlee11/FutureCAD/resolve/main/export_data.py
- Command line
-
hf download hf://datasets/jhlee11/FutureCAD/export_data.py
-
curl -L -o export_data.py https://huggingface.co/datasets/jhlee11/FutureCAD/resolve/main/export_data.py
4.18 kB
| import argparse | |
| import json | |
| import re | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| DATASET = "FutureCAD" | |
| DEFAULT_REPO = "jhlee11/" + DATASET | |
| def get_file(source, relative, cache_dir): | |
| if Path(source).is_dir(): | |
| return Path(source) / relative | |
| from huggingface_hub import hf_hub_download | |
| return Path( | |
| hf_hub_download(source, relative, repo_type="dataset", cache_dir=cache_dir) | |
| ) | |
| def metadata_files(source, split): | |
| if Path(source).is_dir(): | |
| return [ | |
| p.relative_to(source).as_posix() | |
| for p in sorted(Path(source).glob(f"data/{split}-*.parquet")) | |
| ] | |
| from huggingface_hub import HfApi | |
| return sorted( | |
| p | |
| for p in HfApi().list_repo_files(source, repo_type="dataset") | |
| if p.startswith(f"data/{split}-") and p.endswith(".parquet") | |
| ) | |
| def write_file(path, payload): | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| if path.exists(): | |
| if path.read_bytes() != payload: | |
| raise FileExistsError(f"Refusing to replace different content: {path}") | |
| return | |
| temporary = path.with_suffix(path.suffix + ".partial") | |
| temporary.write_bytes(payload) | |
| temporary.replace(path) | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="Download or locally export the dataset into individual files." | |
| ) | |
| parser.add_argument( | |
| "--source", | |
| default=DEFAULT_REPO, | |
| help="Hub dataset ID or local repository directory", | |
| ) | |
| parser.add_argument( | |
| "--split", choices=["train", "validation", "val", "test", "all"], default="all" | |
| ) | |
| parser.add_argument( | |
| "--id", | |
| dest="sample_id", | |
| help="Export one exact ID; searches the selected splits", | |
| ) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--cache-dir", type=Path) | |
| parser.add_argument("--format", choices=["history", "descriptions"], default="history") | |
| parser.add_argument( | |
| "--description-level", choices=["overall", "detail"], default="detail" | |
| ) | |
| args = parser.parse_args() | |
| split = "validation" if args.split == "val" else args.split | |
| splits = ["train", "validation", "test"] if split == "all" else [split] | |
| count = 0 | |
| descriptions = {} | |
| for split in splits: | |
| files = metadata_files(args.source, split) | |
| if not files: | |
| raise FileNotFoundError(f"No Parquet files for {split} in {args.source}") | |
| for filename in files: | |
| path = get_file(args.source, filename, args.cache_dir) | |
| for batch in pq.ParquetFile(path).iter_batches(batch_size=128): | |
| for row in batch.to_pylist(): | |
| if args.sample_id and row["id"] != args.sample_id: | |
| continue | |
| if not re.fullmatch("\\d{8}", row["id"]): | |
| raise ValueError("Invalid CAD ID") | |
| if args.format == "descriptions": | |
| text = row[args.description_level + "_description"] | |
| if not isinstance(text, str) or not text.strip(): | |
| raise ValueError(f"Missing description for {row['id']}") | |
| if row["id"] in descriptions: | |
| raise ValueError(f"Duplicate CAD ID: {row['id']}") | |
| descriptions[row["id"]] = text | |
| else: | |
| json.loads(row["history"]) | |
| write_file( | |
| args.output / (row["id"] + ".json"), | |
| (row["history"] + "\n").encode("utf-8"), | |
| ) | |
| count += 1 | |
| if not count: | |
| raise ValueError(f"No matching sample: {args.sample_id}") | |
| if args.format == "descriptions": | |
| write_file( | |
| args.output / "descriptions.json", | |
| (json.dumps(descriptions, ensure_ascii=False, indent=2) + "\n").encode("utf-8"), | |
| ) | |
| print(f"Exported {count} {DATASET} samples to {args.output.resolve()}") | |
| if __name__ == "__main__": | |
| main() | |