File size: 3,029 Bytes
b555dc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
This script is used to convert https://github.com/Open-Reasoner-Zero/Open-Reasoner-Zero
Usage: 

python scripts/data/rlvr/open_reasoner.py --push_to_hub
python scripts/data/rlvr/open_reasoner.py --push_to_hub --hf_entity ai2-adapt-dev
"""

from collections import defaultdict
from dataclasses import dataclass
import os
from typing import Optional

import datasets
from huggingface_hub import HfApi
from huggingface_hub.repocard import RepoCard
from transformers import HfArgumentParser

@dataclass
class Args:
    push_to_hub: bool = False
    file_path: str = "orz_math_57k_collected.json"
    hf_entity: Optional[str] = None

def main(args: Args):
    # download https://github.com/Open-Reasoner-Zero/Open-Reasoner-Zero/raw/refs/heads/main/data/orz_math_57k_collected.json
    import requests
    import json

    if not os.path.exists(args.file_path):
        url = "https://github.com/Open-Reasoner-Zero/Open-Reasoner-Zero/raw/refs/heads/main/data/orz_math_57k_collected.json"
        response = requests.get(url)
        with open(args.file_path, "w") as f:
            f.write(response.text)
    
    with open(args.file_path, "r") as f:
        data = json.load(f)
    
    table = defaultdict(list)
    for item in data:
        assert len(item) == 2 # 1 question 2 ground truth
        table["messages"].append([
            {"role": "user", "content": item[0]["value"]},
            {"role": "assistant", "content": item[1]["ground_truth"]["value"]},
        ])
        table["ground_truth"].append(item[1]["ground_truth"]["value"])
        table["dataset"].append("math")
    dataset = datasets.Dataset.from_dict(table)

    if args.push_to_hub:
        api = HfApi()
        if not args.hf_entity:
            args.hf_entity = HfApi().whoami()["name"]
        file_path = args.file_path.split(".json")[0]
        repo_id = f"{args.hf_entity}/rlvr_{file_path}"
        print(f"Pushing dataset to Hub: {repo_id}")
        dataset.push_to_hub(repo_id)
        api.upload_file(
            path_or_fileobj=__file__,
            path_in_repo="create_dataset.py",
            repo_type="dataset",
            repo_id=repo_id,
        )

        # Add RepoCard
        repo_card = RepoCard(
            content=f"""\
# Open Reasoner Dataset

This dataset is converted from [Open-Reasoner-Zero](https://github.com/Open-Reasoner-Zero/Open-Reasoner-Zero)'s math dataset.

Check out https://github.com/allenai/open-instruct/blob/main/scripts/data/rlvr/open_reasoner.py for the conversion script.

## Dataset Format

The dataset contains math problems and their solutions in a conversational format:

- `messages`: List of message dictionaries with user questions and assistant answers
- `ground_truth`: The correct solution for each problem
- `dataset`: Always "math" to indicate this is from the math datases""")
        repo_card.push_to_hub(
            repo_id,
            repo_type="dataset",
        )

if __name__ == "__main__":
    parser = HfArgumentParser((Args))
    main(*parser.parse_args_into_dataclasses())