| import argparse |
| import json |
| import shutil |
| import zipfile |
| from pathlib import Path |
|
|
| import pandas as pd |
| import pyarrow as pa |
| import pyarrow.parquet as pq |
|
|
|
|
| RAW_FILES = { |
| "train": "FunQA_train.json", |
| "validation": "FunQA_val.json", |
| "test": "FunQA_test.json", |
| "mcqa_test": "Funqa_mcqa_v1.json", |
| } |
|
|
| VIDEO_ARCHIVES = { |
| "validation": "val.zip", |
| "test": "test.zip", |
| "train": "train.zip", |
| } |
|
|
|
|
| def load_raw_rows(raw_dir: Path, split: str): |
| with (raw_dir / RAW_FILES[split]).open("r", encoding="utf-8") as f: |
| rows = json.load(f) |
| if split == "validation": |
| missing_videos = {"C_KT_6_6347_6427.mp4"} |
| rows = [row for row in rows if row.get("visual_input") not in missing_videos] |
| return rows |
|
|
|
|
| def write_parquet(rows, output_path: Path): |
| df = pd.DataFrame(rows) |
| output_path.parent.mkdir(parents=True, exist_ok=True) |
|
|
| if "output" in df.columns: |
| schema = pa.schema( |
| [ |
| ("instruction", pa.string()), |
| ("visual_input", pa.string()), |
| ("output", pa.string()), |
| ("task", pa.string()), |
| ] |
| ) |
| df = df[["instruction", "visual_input", "output", "task"]] |
| else: |
| schema = pa.schema( |
| [ |
| ("instruction", pa.string()), |
| ("visual_input", pa.string()), |
| ("gt", pa.string()), |
| ("id", pa.string()), |
| ] |
| ) |
| df = df[["instruction", "visual_input", "gt", "id"]] |
|
|
| table = pa.Table.from_pandas(df, schema=schema, preserve_index=False) |
| pq.write_table(table, output_path) |
|
|
|
|
| def move_raw_files(repo_root: Path, raw_dir: Path): |
| raw_dir.mkdir(parents=True, exist_ok=True) |
| for filename in list(RAW_FILES.values()) + list(VIDEO_ARCHIVES.values()): |
| src = repo_root / filename |
| dst = raw_dir / filename |
| if src.exists() and not dst.exists(): |
| shutil.move(str(src), str(dst)) |
|
|
|
|
| def extract_split_videos(raw_dir: Path, videos_dir: Path, split: str): |
| archive_name = VIDEO_ARCHIVES[split] |
| archive_path = raw_dir / archive_name |
| if not archive_path.exists(): |
| return |
| videos_dir.mkdir(parents=True, exist_ok=True) |
| marker = videos_dir / f".{split}_extracted" |
| if marker.exists(): |
| return |
| with zipfile.ZipFile(archive_path) as zf: |
| zf.extractall(videos_dir) |
| marker.write_text("ok\n", encoding="utf-8") |
|
|
|
|
| def normalize_video_layout(videos_dir: Path, split: str): |
| alias = {"validation": "val"}.get(split, split) |
| alias_root = videos_dir / alias |
| expected_root = videos_dir / split |
| if alias_root.exists() and alias_root != expected_root and not expected_root.exists(): |
| shutil.move(str(alias_root), str(expected_root)) |
|
|
| legacy_root = videos_dir / split / split |
| if legacy_root.exists(): |
| expected_root.mkdir(parents=True, exist_ok=True) |
| for child in legacy_root.iterdir(): |
| target = expected_root / child.name |
| if not target.exists(): |
| shutil.move(str(child), str(target)) |
|
|
| legacy_alias_root = videos_dir / split / alias |
| if legacy_alias_root.exists(): |
| expected_root.mkdir(parents=True, exist_ok=True) |
| for child in legacy_alias_root.iterdir(): |
| target = expected_root / child.name |
| if not target.exists(): |
| shutil.move(str(child), str(target)) |
|
|
|
|
| def ensure_layout(repo_root: Path): |
| move_raw_files(repo_root, repo_root / "raw") |
| for split in ["test", "validation", "train"]: |
| extract_split_videos(repo_root / "raw", repo_root / "videos", split) |
| normalize_video_layout(repo_root / "videos", split) |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Build a HF Hub-style FunQA dataset repo with original columns.") |
| parser.add_argument("--repo-root", type=Path, default=Path(".")) |
| args = parser.parse_args() |
|
|
| repo_root = args.repo_root.resolve() |
| ensure_layout(repo_root) |
|
|
| raw_dir = repo_root / "raw" |
| data_dir = repo_root / "data" |
|
|
| for split in RAW_FILES: |
| rows = load_raw_rows(raw_dir, split) |
| write_parquet(rows, data_dir / f"{split}.parquet") |
|
|
| print(f"Built parquet splits under: {data_dir}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|