Publish GenScale benchmark with attribution, checksums and Croissant RAI metadata (part 2)
2fd687f verified Download scripts/make_genscale_parquet.py from lingxiao2049/GenScale: direct link, hf CLI and curl.
- Browser
- Download file 5.91 kB
-
https://huggingface.co/datasets/lingxiao2049/GenScale/resolve/main/scripts/make_genscale_parquet.py
- Command line
-
hf download hf://datasets/lingxiao2049/GenScale/scripts/make_genscale_parquet.py
-
curl -L -o make_genscale_parquet.py https://huggingface.co/datasets/lingxiao2049/GenScale/resolve/main/scripts/make_genscale_parquet.py
5.91 kB
| #!/usr/bin/env python3 | |
| """Regenerate GenScale Parquet/CSV files from the cleaned anonymized JSON. | |
| Usage: | |
| python scripts/make_genscale_parquet.py --benchmark GenScale_Benchmark_anonymous_clean.json --out_dir data | |
| """ | |
| import argparse, json | |
| from pathlib import Path | |
| import pandas as pd | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| def jd(x): | |
| return json.dumps(x, ensure_ascii=False) if x is not None else None | |
| def flatten(d, prefix=""): | |
| out = {} | |
| if not isinstance(d, dict): | |
| return out | |
| for k, v in d.items(): | |
| key = f"{prefix}{k}" if prefix else str(k) | |
| if isinstance(v, dict): | |
| out.update(flatten(v, key + ".")) | |
| elif isinstance(v, list): | |
| out[key] = jd(v) | |
| else: | |
| out[key] = v | |
| return out | |
| def parse_pair(k): | |
| return k.split("_to_", 1) if isinstance(k, str) and "_to_" in k else (None, None) | |
| def write_parquet(df, path): | |
| pq.write_table(pa.Table.from_pandas(df, preserve_index=False), path) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--benchmark", required=True) | |
| ap.add_argument("--out_dir", default="data") | |
| args = ap.parse_args() | |
| out_dir = Path(args.out_dir) | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| data = json.load(open(args.benchmark, encoding="utf-8")) | |
| entry_rows, pair_rows, edit_rows = [], [], [] | |
| for entry in data: | |
| task_id = entry.get("task_id") | |
| task = task_id.split("_")[0] if task_id else None | |
| gt = entry.get("gt_ratios") or {} | |
| entry_row = { | |
| "entry_id": task_id, "task_id": task_id, "task": task, "scenario": entry.get("scenario"), | |
| "source_task_id": entry.get("source_task_id"), "source_task_type": entry.get("source_task_type"), | |
| "prompt_type": entry.get("prompt_type"), "num_objects": entry.get("num_objects", len(entry.get("objects_included", []) or [])), | |
| "num_pairs": len(gt) if isinstance(gt, dict) else 0, "objects_included": jd(entry.get("objects_included")), | |
| "prompt": entry.get("prompt"), "reference_image_path": entry.get("reference_image_path"), | |
| "image_path": entry.get("image_path"), "raw_listing_title": entry.get("raw_listing_title"), | |
| "source_size_score": entry.get("source_size_score"), "pair_key": entry.get("pair_key"), | |
| "generated_ratio": entry.get("generated_ratio"), "target_ratio": entry.get("target_ratio"), | |
| } | |
| for pk in ["product_scale", "refine_meta", "edit_plan"]: | |
| if isinstance(entry.get(pk), dict): | |
| entry_row.update(flatten(entry[pk], pk + ".")) | |
| elif pk in entry: | |
| entry_row[pk] = jd(entry.get(pk)) | |
| entry_rows.append(entry_row) | |
| if isinstance(gt, dict): | |
| for idx, (pair_key, pair_val) in enumerate(gt.items()): | |
| object_a, object_b = parse_pair(pair_key) | |
| pr = pair_val if isinstance(pair_val, dict) else {"target_ratio": pair_val} | |
| acc = pr.get("acceptable_range") | |
| row = { | |
| "pair_id": f"{task_id}__pair_{idx:02d}", "entry_id": task_id, "task_id": task_id, "task": task, | |
| "scenario": entry.get("scenario"), "source_task_id": entry.get("source_task_id"), | |
| "source_task_type": entry.get("source_task_type"), "prompt_type": entry.get("prompt_type"), | |
| "pair_key": pair_key, "object_a": object_a, "object_b": object_b, | |
| "target_ratio_a_to_b": pr.get("target_ratio"), | |
| "acceptable_ratio_min": acc[0] if isinstance(acc, list) and len(acc) > 0 else None, | |
| "acceptable_ratio_max": acc[1] if isinstance(acc, list) and len(acc) > 1 else None, | |
| "generated_ratio_a_to_b": entry.get("generated_ratio") if entry.get("pair_key") == pair_key else None, | |
| "source_size_score": entry.get("source_size_score"), "objects_included": jd(entry.get("objects_included")), | |
| "prompt": entry.get("prompt"), "reference_image_path": entry.get("reference_image_path"), | |
| "image_path": entry.get("image_path"), | |
| } | |
| if isinstance(entry.get("product_scale"), dict): | |
| row.update(flatten(entry["product_scale"], "product_scale.")) | |
| if isinstance(entry.get("edit_plan"), dict): | |
| row.update(flatten(entry["edit_plan"], "edit_plan.")) | |
| pair_rows.append(row) | |
| if isinstance(entry.get("edit_plan"), dict): | |
| ep = entry["edit_plan"] | |
| object_a, object_b = parse_pair(entry.get("pair_key")) | |
| edit_rows.append({ | |
| "entry_id": task_id, "task_id": task_id, "scenario": entry.get("scenario"), | |
| "source_task_id": entry.get("source_task_id"), "source_task_type": entry.get("source_task_type"), | |
| "prompt_type": entry.get("prompt_type"), "pair_key": entry.get("pair_key"), | |
| "object_a": object_a, "object_b": object_b, "generated_ratio_a_to_b": entry.get("generated_ratio"), | |
| "target_ratio_a_to_b": entry.get("target_ratio"), "source_size_score": entry.get("source_size_score"), | |
| "edit_target_object": ep.get("edit_target_object"), "reference_object": ep.get("reference_object"), | |
| "scale_factor": ep.get("scale_factor"), "direction": ep.get("direction"), "prompt": entry.get("prompt"), | |
| "image_path": entry.get("image_path"), "reference_image_path": entry.get("reference_image_path"), | |
| }) | |
| for df, name in [(pd.DataFrame(entry_rows), "genscale_entries"), (pd.DataFrame(pair_rows), "genscale_pairs"), (pd.DataFrame(edit_rows), "genscale_task3_edit_plans")]: | |
| write_parquet(df, out_dir / f"{name}.parquet") | |
| df.to_csv(out_dir / f"{name}.csv", index=False) | |
| if __name__ == "__main__": | |
| main() | |