nouhad commited on
Commit
8c82822
·
verified ·
1 Parent(s): 919a9c3

Upload create_dataset.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. create_dataset.py +102 -0
create_dataset.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This script processes all multiplication JSONL files in:
3
+ /weka/oe-adapt-default/nouhad/data/multiplication/
4
+
5
+ Usage:
6
+ python scripts/data/rlvr/open_reasoner.py --push_to_hub
7
+ python scripts/data/rlvr/open_reasoner.py --push_to_hub --hf_entity ai2-adapt-dev
8
+ """
9
+
10
+ from collections import defaultdict
11
+ from dataclasses import dataclass
12
+ import os
13
+ import json
14
+ from typing import Optional
15
+
16
+ import datasets
17
+ from huggingface_hub import HfApi
18
+ from huggingface_hub.repocard import RepoCard
19
+ from transformers import HfArgumentParser
20
+
21
+ @dataclass
22
+ class Args:
23
+ push_to_hub: bool = False
24
+ hf_entity: Optional[str] = None
25
+
26
+ def process_file(file_path: str):
27
+ """
28
+ Processes a single JSONL file into a dataset.
29
+ """
30
+ print(f"Processing file: {file_path}")
31
+
32
+ with open(file_path, "r") as f:
33
+ data = f.readlines()
34
+
35
+ new_data = []
36
+ for item in data:
37
+ item = item.strip() # Remove extra spaces or newline characters
38
+ if not item: # Skip empty lines
39
+ continue
40
+ new_data.append(json.loads(item)) # Convert JSON string to Python dict
41
+
42
+ table = defaultdict(list)
43
+ for item in new_data:
44
+ assert "problem" in item and "answer" in item, "Missing expected keys in data"
45
+ table["messages"].append([
46
+ {"role": "user", "content": item["problem"]},
47
+ {"role": "assistant", "content": item["answer"]},
48
+ ])
49
+ table["ground_truth"].append(item["answer"])
50
+ table["dataset"].append("multiplication")
51
+
52
+ return datasets.Dataset.from_dict(table)
53
+
54
+ def main(args: Args):
55
+ data_dir = "/weka/oe-adapt-default/nouhad/data/multiplication/"
56
+ jsonl_files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.endswith(".jsonl")]
57
+
58
+ if not jsonl_files:
59
+ print(f"No JSONL files found in {data_dir}")
60
+ return
61
+
62
+ api = HfApi()
63
+ hf_entity = args.hf_entity if args.hf_entity else api.whoami()["name"]
64
+
65
+ for file_path in jsonl_files:
66
+ dataset = process_file(file_path)
67
+ dataset_name = os.path.basename(file_path).replace(".jsonl", "")
68
+
69
+ if args.push_to_hub:
70
+ repo_id = f"{hf_entity}/{dataset_name}"
71
+ print(f"Pushing dataset to Hub: {repo_id}")
72
+
73
+ dataset.push_to_hub(repo_id)
74
+ api.upload_file(
75
+ path_or_fileobj=__file__,
76
+ path_in_repo="create_dataset.py",
77
+ repo_type="dataset",
78
+ repo_id=repo_id,
79
+ )
80
+
81
+ # Add RepoCard
82
+ repo_card = RepoCard(
83
+ content=f"""\
84
+ # Multiplication Dataset - {dataset_name}
85
+
86
+ This dataset contains multiplication problems and their solutions.
87
+
88
+ ## Dataset Format
89
+
90
+ - `messages`: List of message dictionaries with user questions and assistant answers
91
+ - `ground_truth`: The correct solution for each problem
92
+ - `dataset`: Always "multiplication" to indicate its type
93
+ """
94
+ )
95
+ repo_card.push_to_hub(
96
+ repo_id,
97
+ repo_type="dataset",
98
+ )
99
+
100
+ if __name__ == "__main__":
101
+ parser = HfArgumentParser((Args,))
102
+ main(*parser.parse_args_into_dataclasses())