Datasets:
Download scripts/load_dataset.py from brainz/CulturalMenuBench: direct link, hf CLI and curl.
- Browser
- Download file 4.02 kB
-
https://huggingface.co/datasets/brainz/CulturalMenuBench/resolve/main/scripts/load_dataset.py
- Command line
-
hf download hf://datasets/brainz/CulturalMenuBench/scripts/load_dataset.py
-
curl -L -o load_dataset.py https://huggingface.co/datasets/brainz/CulturalMenuBench/resolve/main/scripts/load_dataset.py
4.02 kB
| #!/usr/bin/env python3 | |
| """Validate question-to-image joins without calling a model or inventing outputs.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| import tarfile | |
| from pathlib import Path, PurePosixPath | |
| def rows(path): | |
| return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()] | |
| def relative_file(root, relative): | |
| path = PurePosixPath(relative) | |
| if path.is_absolute() or ".." in path.parts or "\\" in relative: | |
| raise ValueError("Unsafe relative file") | |
| candidate = root / relative | |
| candidate.resolve().relative_to(root.resolve()) | |
| return candidate | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--questions", type=Path, required=True) | |
| parser.add_argument("--source-root", type=Path, help="Internal audit only: use original media tree and fallback tar") | |
| parser.add_argument("--limit", type=int) | |
| args = parser.parse_args() | |
| if args.limit is not None and args.limit < 1: | |
| parser.error("limit must be positive") | |
| root = Path(__file__).resolve().parents[1] | |
| manifest_rows = rows(root / "media_manifest.jsonl") | |
| manifest = {row["image_id"]: row for row in manifest_rows} | |
| if len(manifest) != len(manifest_rows): | |
| raise ValueError("Duplicate media IDs") | |
| sources = {} | |
| if args.source_root: | |
| sources = {row["image_id"]: row["source_relative_path"] for row in rows(root / "internal/media_sources.jsonl")} | |
| selected = rows(args.questions) | |
| if len({row["item_id"] for row in selected}) != len(selected): | |
| raise ValueError("Duplicate question IDs") | |
| if args.limit: | |
| selected = selected[:args.limit] | |
| checked = set() | |
| failures = [] | |
| archive = None | |
| try: | |
| for question in selected: | |
| domain = {"是", "否"} if question["answer_type"] == "binary" else {"A", "B", "C", "D"} | |
| if question["ground_truth"] not in domain: | |
| raise ValueError("Invalid answer domain") | |
| for part in question["input"]: | |
| if part["type"] != "image_url": | |
| continue | |
| reference = part["image_url"]["url"] | |
| if not reference.startswith("asset://"): | |
| raise ValueError("Unexpected image URI") | |
| image_id = reference[len("asset://"):] | |
| if image_id in checked: | |
| continue | |
| image = manifest[image_id] | |
| checked.add(image_id) | |
| try: | |
| if args.source_root: | |
| if image["status"] == "archived_only": | |
| if archive is None: | |
| archive = tarfile.open(relative_file(args.source_root, "data/non_bmp_image_fallback.tar")) | |
| member = archive.getmember(sources[image_id]) | |
| if not member.isfile(): | |
| raise ValueError("Non-file archive member") | |
| payload = archive.extractfile(member).read() | |
| else: | |
| payload = relative_file(args.source_root, sources[image_id]).read_bytes() | |
| else: | |
| payload = relative_file(root, image["package_path"]).read_bytes() | |
| if len(payload) != image["bytes"] or hashlib.sha256(payload).hexdigest() != image["sha256"]: | |
| raise ValueError("Image integrity mismatch") | |
| except (OSError, KeyError, ValueError) as error: | |
| failures.append({"image_id": image_id, "error_type": type(error).__name__}) | |
| finally: | |
| if archive is not None: | |
| archive.close() | |
| print(json.dumps({"questions_checked": len(selected), "unique_images_checked": len(checked), "failed_images": len(failures), "failures": failures[:20], "model_calls": 0}, ensure_ascii=False, indent=2)) | |
| raise SystemExit(1 if failures else 0) | |
| if __name__ == "__main__": | |
| main() | |