-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_samples.py
More file actions
147 lines (103 loc) · 4.08 KB
/
Copy pathbuild_samples.py
File metadata and controls
147 lines (103 loc) · 4.08 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from datasets import load_dataset
from huggingface_hub import HfApi, get_token
from pathlib import Path
import json
def build_samples():
dataset = load_dataset("eth-nlped/mathdial")
train = []
test = []
for train_sample in dataset["train"]:
problem = train_sample["question"].strip()
user_work = train_sample["student_incorrect_solution"].strip()
conversation_turns = train_sample["conversation"].split("|EOM|")
conversation = []
starter_turn = {
"role": "user",
"content": problem + "\n" + user_work
}
conversation.append(starter_turn)
for turn in conversation_turns:
turn = turn.strip()
if turn.startswith("Teacher:"):
try:
turn = turn.split(")", maxsplit=1)[1]
except IndexError:
print(repr(turn))
continue
turn = turn.strip()
tutor_turn = {
"role": "assistant",
"content": turn
}
conversation.append(tutor_turn)
else:
turn = turn.split(":", maxsplit=1)[1]
turn = turn.strip()
user_turn = {
"role": "user",
"content": turn
}
conversation.append(user_turn)
train.append(conversation)
for test_sample in dataset["test"]:
problem = test_sample["question"].strip()
user_work = test_sample["student_incorrect_solution"].strip()
conversation_turns = test_sample["conversation"].split("|EOM|")
conversation = []
starter_turn = {
"role": "user",
"content": problem + "\n" + user_work
}
conversation.append(starter_turn)
for turn in conversation_turns:
turn = turn.strip()
if turn.startswith("Teacher:"):
try:
turn = turn.split(")", maxsplit=1)[1]
except IndexError:
print(repr(turn))
continue
turn = turn.strip()
tutor_turn = {
"role": "assistant",
"content": turn
}
conversation.append(tutor_turn)
else:
turn = turn.split(":", maxsplit=1)[1]
turn = turn.strip()
user_turn = {
"role": "user",
"content": turn
}
conversation.append(user_turn)
test.append(conversation)
train_structured = []
for sample in train:
train_structured.append({"messages": sample})
test_structured = []
for sample in test:
test_structured.append({"messages": sample})
out_path = Path(__file__).resolve().parents[0] / "samples"
out_path.mkdir(parents=True, exist_ok=True)
with open(out_path / "train.json", "w") as con:
json.dump(train_structured, con)
with open(out_path / "test.json", "w") as con:
json.dump(test_structured, con)
print("samples complete")
if get_token() is not None:
api = HfApi()
api.upload_file(
repo_id="Carson-Shively/ai-math-tutor",
repo_type="dataset",
path_or_fileobj=out_path / "train.json",
path_in_repo="train.json"
)
api.upload_file(
repo_id="Carson-Shively/ai-math-tutor",
repo_type="dataset",
path_or_fileobj=out_path / "test.json",
path_in_repo="test.json"
)
if __name__ == "__main__":
build_samples()