File size: 4,283 Bytes
5540f6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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()
Free AI Image Generator No sign-up. Instant results. Open Now