ccccwx commited on
Commit
fbfc91b
·
verified ·
1 Parent(s): f4e5a94

Publish materialize.py

Browse files
Files changed (1) hide show
  1. materialize.py +52 -0
materialize.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Materialize Kelp splits from the pinned upstream Arrow file and published index."""
2
+ import argparse
3
+ import hashlib
4
+ from pathlib import Path
5
+
6
+ import pyarrow as pa
7
+ from datasets import Dataset, DatasetDict, load_dataset
8
+ from huggingface_hub import hf_hub_download
9
+
10
+ INDEX = "ccccwx/kelp-length-splits"
11
+ UPSTREAM = "Alibaba-AAIG/StreamGuardBench"
12
+ REV = "7fa1203b2d9be5671d3b40f2c1df0035deeb098c"
13
+ FILE = "wildguard/qwen3_8b/train/data-00000-of-00001.arrow"
14
+ SHA = "a4bfd41c1b94578c61f870189c01f4908aee20b6abf6e4784836430ce24a2a1d"
15
+
16
+
17
+ def main():
18
+ parser = argparse.ArgumentParser(description=__doc__)
19
+ parser.add_argument("config", choices=("natural", "length_balanced", "hard_length"))
20
+ parser.add_argument("output", type=Path)
21
+ parser.add_argument("--source", type=Path)
22
+ parser.add_argument("--local-index", type=Path)
23
+ args = parser.parse_args()
24
+ if args.output.exists():
25
+ parser.error("Choose a new output directory.")
26
+ upstream = args.source or Path(hf_hub_download(UPSTREAM, FILE, revision=REV, repo_type="dataset"))
27
+ with upstream.open("rb") as handle:
28
+ if hashlib.file_digest(handle, "sha256").hexdigest() != SHA:
29
+ raise ValueError("Unexpected upstream Arrow checksum.")
30
+ if args.local_index:
31
+ paths = {s: str(args.local_index / args.config / f"{s}.parquet")
32
+ for s in ("train", "validation", "test")}
33
+ index = load_dataset("parquet", data_files=paths)
34
+ else:
35
+ index = load_dataset(INDEX, args.config)
36
+ group_split, row_split = {}, {}
37
+ result = {}
38
+ with pa.memory_map(str(upstream)) as handle:
39
+ table = pa.ipc.open_stream(handle).read_all().select(["prompt", "response"])
40
+ for split, items in index.items():
41
+ for row, group in zip(items["source_row"], items["group_id"], strict=True):
42
+ if not 0 <= row < len(table) or row in row_split or group_split.setdefault(group, split) != split:
43
+ raise ValueError(f"Invalid or overlapping source row {row} in {split}")
44
+ row_split[row] = split
45
+ texts = table.take(list(items["source_row"]))
46
+ result[split] = Dataset(texts.append_column("label", pa.array(list(items["label"]), type=pa.int64())))
47
+ DatasetDict(result).save_to_disk(str(args.output))
48
+ print({split: len(items) for split, items in result.items()})
49
+
50
+
51
+ if __name__ == "__main__":
52
+ main()