File size: 2,642 Bytes
f423317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Restore portable VERL JSONL and original JPEGs from a downloaded dataset release."""
import argparse
import json
from pathlib import Path
from tempfile import TemporaryDirectory

import pyarrow.parquet as pq


CONFIG = "counting_grounding_search-visual_probe"
TRAIN_FILENAME = "chart_search_counting_visual_probe_train_rollout_score_leq05_all.verl.jsonl"


def training_row(row, image_path):
    prompt = row["prompt"]
    return {
        "data_source": row["data_source"],
        "prompt": [dict(role=role, content=text) for role, text in zip(prompt["role"], prompt["content"], strict=True)],
        "images": [str(image_path)],
        "ability": row["ability"],
        "reward_model": row["reward_model"],
        "extra_info": row["extra_info"],
        "avg_pass_rate": row["avg_pass_rate"],
    }


def export(dataset, destination):
    shards = sorted((dataset / CONFIG).glob("train-*.parquet"))
    if not shards:
        raise ValueError(f"No training shards in {dataset / CONFIG}")
    destination = destination.resolve()
    if destination.exists():
        raise FileExistsError(f"Choose a new output directory: {destination}")
    destination.parent.mkdir(parents=True, exist_ok=True)
    count = 0
    with TemporaryDirectory(dir=destination.parent) as tmp:
        stage = Path(tmp) / "dataset"
        images = stage / "images"
        images.mkdir(parents=True)
        with (stage / TRAIN_FILENAME).open("w", encoding="utf-8") as out:
            for shard in shards:
                for batch in pq.ParquetFile(shard).iter_batches(batch_size=8):
                    for row in batch.to_pylist():
                        image = row["image"]
                        filename = image["path"]
                        if Path(filename).name != filename or filename != row["id"] + ".jpg":
                            raise ValueError(f"Unexpected JPEG name: {filename}")
                        with (images / filename).open("xb") as file:
                            file.write(image["bytes"])
                        native = training_row(row, destination / "images" / filename)
                        out.write(json.dumps(native, ensure_ascii=False) + "\n")
                        count += 1
        if count != 2042:
            raise ValueError(f"Expected 2042 training rows, got {count}")
        stage.rename(destination)
    return destination / TRAIN_FILENAME


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("output", type=Path)
    args = parser.parse_args()
    print(export(Path(__file__).resolve().parent, args.output))