Situo commited on
Commit
a2912e8
·
verified ·
1 Parent(s): 3555402

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. .gitattributes +2 -0
  2. convert_to_verl_parquet.py +194 -0
  3. images.zip +3 -0
  4. tool_rl.jsonl +3 -0
  5. tool_sft.jsonl +3 -0
.gitattributes CHANGED
@@ -58,3 +58,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
58
  # Video files - compressed
59
  *.mp4 filter=lfs diff=lfs merge=lfs -text
60
  *.webm filter=lfs diff=lfs merge=lfs -text
 
 
 
58
  # Video files - compressed
59
  *.mp4 filter=lfs diff=lfs merge=lfs -text
60
  *.webm filter=lfs diff=lfs merge=lfs -text
61
+ tool_rl.jsonl filter=lfs diff=lfs merge=lfs -text
62
+ tool_sft.jsonl filter=lfs diff=lfs merge=lfs -text
convert_to_verl_parquet.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Convert portable DuoChart RL JSONL data to the Parquet schema expected by VeRL."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import json
8
+ import os
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ import pyarrow as pa
13
+ import pyarrow.parquet as pq
14
+
15
+
16
+ VERL_SCHEMA = pa.schema(
17
+ [
18
+ pa.field("data_source", pa.string()),
19
+ pa.field(
20
+ "prompt",
21
+ pa.list_(
22
+ pa.struct(
23
+ [
24
+ pa.field("content", pa.string()),
25
+ pa.field("role", pa.string()),
26
+ ]
27
+ )
28
+ ),
29
+ ),
30
+ pa.field("images", pa.list_(pa.struct([pa.field("image", pa.string())]))),
31
+ pa.field("ability", pa.string()),
32
+ pa.field(
33
+ "reward_model",
34
+ pa.struct(
35
+ [
36
+ pa.field("ground_truth", pa.string()),
37
+ pa.field("style", pa.string()),
38
+ ]
39
+ ),
40
+ ),
41
+ pa.field(
42
+ "extra_info",
43
+ pa.struct(
44
+ [
45
+ pa.field("question", pa.string()),
46
+ pa.field(
47
+ "tools_kwargs",
48
+ pa.struct(
49
+ [
50
+ pa.field(
51
+ "python_interpreter",
52
+ pa.struct(
53
+ [
54
+ pa.field(
55
+ "create_kwargs",
56
+ pa.struct([pa.field("image", pa.string())]),
57
+ )
58
+ ]
59
+ ),
60
+ )
61
+ ]
62
+ ),
63
+ ),
64
+ pa.field("type", pa.string()),
65
+ ]
66
+ ),
67
+ ),
68
+ pa.field("agent_name", pa.string()),
69
+ ]
70
+ )
71
+
72
+
73
+ def parse_args() -> argparse.Namespace:
74
+ parser = argparse.ArgumentParser(description=__doc__)
75
+ parser.add_argument(
76
+ "input",
77
+ nargs="?",
78
+ type=Path,
79
+ default=Path("tool_rl.jsonl"),
80
+ help="Portable DuoChart RL JSONL file.",
81
+ )
82
+ parser.add_argument(
83
+ "output",
84
+ nargs="?",
85
+ type=Path,
86
+ default=Path("train.parquet"),
87
+ help="Output VeRL Parquet file.",
88
+ )
89
+ parser.add_argument("--batch-size", type=int, default=2048)
90
+ parser.add_argument("--overwrite", action="store_true")
91
+ return parser.parse_args()
92
+
93
+
94
+ def render_prompt(messages: list[dict[str, Any]]) -> tuple[list[dict[str, str]], list[str]]:
95
+ prompt: list[dict[str, str]] = []
96
+ image_paths: list[str] = []
97
+
98
+ for message in messages:
99
+ role = str(message["role"])
100
+ content = message.get("content", "")
101
+ if isinstance(content, str):
102
+ rendered = content
103
+ elif isinstance(content, list):
104
+ pieces: list[str] = []
105
+ for item in content:
106
+ if item.get("type") == "image":
107
+ image = str(item["image"])
108
+ if image.startswith(("/", "file://")):
109
+ raise ValueError(f"Absolute image path remains in input: {image}")
110
+ pieces.append("<image>")
111
+ image_paths.append(image)
112
+ elif item.get("type") == "text":
113
+ pieces.append(str(item.get("text", "")))
114
+ else:
115
+ raise ValueError(f"Unsupported message content item: {item}")
116
+ rendered = "".join(pieces)
117
+ else:
118
+ raise ValueError(f"Unsupported message content: {type(content).__name__}")
119
+ prompt.append({"content": rendered, "role": role})
120
+
121
+ if not image_paths:
122
+ raise ValueError("RL example has no image")
123
+ return prompt, image_paths
124
+
125
+
126
+ def convert_row(row: dict[str, Any]) -> dict[str, Any]:
127
+ metadata = row.get("metadata") or {}
128
+ if "qa_id" in metadata or "qa_id" in row:
129
+ raise ValueError("qa_id must be removed before Parquet conversion")
130
+
131
+ prompt, image_paths = render_prompt(row["messages"])
132
+ answer = row.get("answer")
133
+ ground_truth = answer if isinstance(answer, str) else str(answer)
134
+ first_image = image_paths[0]
135
+
136
+ return {
137
+ "data_source": "chartool",
138
+ "prompt": prompt,
139
+ "images": [{"image": image} for image in image_paths],
140
+ "ability": "chart",
141
+ "reward_model": {"ground_truth": ground_truth, "style": "rule"},
142
+ "extra_info": {
143
+ "question": str(metadata.get("question", "")),
144
+ "tools_kwargs": {
145
+ "python_interpreter": {"create_kwargs": {"image": first_image}}
146
+ },
147
+ "type": str(metadata.get("type", "")),
148
+ },
149
+ "agent_name": "tool_agent",
150
+ }
151
+
152
+
153
+ def flush(writer: pq.ParquetWriter, rows: list[dict[str, Any]]) -> None:
154
+ if rows:
155
+ writer.write_table(pa.Table.from_pylist(rows, schema=VERL_SCHEMA))
156
+ rows.clear()
157
+
158
+
159
+ def main() -> None:
160
+ args = parse_args()
161
+ input_path = args.input.resolve()
162
+ output_path = args.output.resolve()
163
+ if output_path.exists() and not args.overwrite:
164
+ raise FileExistsError(f"Output exists; pass --overwrite to replace it: {output_path}")
165
+ if args.batch_size <= 0:
166
+ raise ValueError("--batch-size must be positive")
167
+
168
+ output_path.parent.mkdir(parents=True, exist_ok=True)
169
+ temporary = output_path.with_suffix(output_path.suffix + ".tmp")
170
+ count = 0
171
+ batch: list[dict[str, Any]] = []
172
+
173
+ with input_path.open("r", encoding="utf-8") as src, pq.ParquetWriter(
174
+ temporary, VERL_SCHEMA, compression="zstd"
175
+ ) as writer:
176
+ for line_number, line in enumerate(src, 1):
177
+ if not line.strip():
178
+ continue
179
+ try:
180
+ row = json.loads(line)
181
+ batch.append(convert_row(row))
182
+ except Exception as exc:
183
+ raise ValueError(f"Failed to convert {input_path}:{line_number}") from exc
184
+ count += 1
185
+ if len(batch) >= args.batch_size:
186
+ flush(writer, batch)
187
+ flush(writer, batch)
188
+
189
+ os.replace(temporary, output_path)
190
+ print(f"Converted {count} rows to {output_path}")
191
+
192
+
193
+ if __name__ == "__main__":
194
+ main()
images.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a69b296f8e4cb69eacc6598d8ea01e4d1aa7ee2a420bfe4fadfbefef3714757e
3
+ size 2416662117
tool_rl.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:59b9cad42a9eeb914e9fd1ce28603d16ae879ad8f1bc7c4c85578c8042c555b0
3
+ size 122872919
tool_sft.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1a6c8b93883f019c42bad34a6e0105529acd35fcdd2b0065ef24daf781194479
3
+ size 24318639