-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.py
230 lines (175 loc) · 4.85 KB
/
loader.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
Load the dataset
"""
import json
from typing import TypeVar, Type
from enum import Enum
from abc import ABC, abstractmethod
from pydantic import BaseModel, Field
class AnswerType(Enum):
"""
Enum of the answer type
"""
Number = 1
Option = 2
Boolean = 3
class Problem(ABC, BaseModel):
"""
Interface of a problem BaseModel
"""
@abstractmethod
def problem(self) -> str:
"""
Give the description of the problem
"""
@abstractmethod
def answer(self) -> str:
"""
Give the answer of the problem
"""
@classmethod
@abstractmethod
def answer_type(cls) -> AnswerType:
""" """
@classmethod
@abstractmethod
def file_format(cls) -> str:
"""
File format which the problem stored as.
Available: json, jsonl.
"""
class MultiChoiceProblem(Problem):
"""
Interface of a multiple choice problem
The return of `answer()` must be one of [A, B, C, D, ...]
"""
@abstractmethod
def options(self) -> dict[str, str]:
"""
Option A of the problem
Returns:
A dict containing four keys: [A, B, C, D, ...], and their corresponding values
Notice that the values should not contain the option(e.g."A") itself
"""
T = TypeVar("T", bound=Problem)
def load_json(
file_path: str, model: Type[T], range_arg: range | None = None
) -> list[T]:
"""
Load a file which is a large json array and return a list of model instances.
Parameters:
- file_path: The path to the JSON file.
- model: The Pydantic model class to use for validation.
- range_arg: The range of the problems to be loaded.
Returns:
- A list of model instances.
"""
data_list = []
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
if isinstance(data, list):
for item_data in data:
item = model(**item_data)
data_list.append(item)
if range_arg is not None:
return [data_list[i] for i in range_arg]
return data_list
def load_jsonl(
file_path: str, model: Type[T], range_arg: range | None = None
) -> list[T]:
"""
Load a JSONL file and return a list of model instances.
Parameters:
- file_path: The path to the JSONL file.
- model: The Pydantic model class to use for validation.
- range_arg: The range of the problems to be loaded.
Returns:
- A list of model instances.
"""
data_list = []
with open(file_path, "r", encoding="utf-8") as file:
json_list = list(file)
for line in json_list:
item_data = json.loads(line)
item = model(**item_data)
data_list.append(item)
if range_arg:
return [data_list[i] for i in range_arg]
return data_list
class AddSub(Problem):
"""
model of a problem from AddSub dataset
"""
iIndex: int
lEquations: list[str]
lSolutions: list[str]
sQuestion: str
def problem(self) -> str:
return self.sQuestion
def answer(self) -> str:
return self.lSolutions[0]
@classmethod
def file_format(cls) -> str:
return "json"
@classmethod
def answer_type(cls) -> AnswerType:
return AnswerType.Number
class GSM8K(Problem):
"""
model of a problem from GSM8K dataset
"""
question: str
raw_answer: float = Field(alias="answer")
def problem(self) -> str:
return self.question
def answer(self) -> str:
return str(self.raw_answer)
@classmethod
def file_format(cls) -> str:
return "json"
@classmethod
def answer_type(cls) -> AnswerType:
return AnswerType.Number
class CoinFlip(Problem):
"""
model of a problem from CoinClip dataset
"""
targets_vec: list[int]
targets: str
inputs: str
def problem(self) -> str:
return self.inputs
def answer(self) -> str:
return self.targets
@classmethod
def file_format(cls) -> str:
return "json"
@classmethod
def answer_type(cls) -> AnswerType:
return AnswerType.Boolean
class AQuA(MultiChoiceProblem):
"""
model of a problem from AQuA dataset
"""
question: str
raw_options: list[str] = Field(alias="options")
rationale: str
correct: str
def problem(self) -> str:
return self.question
def answer(self) -> str:
return self.correct
def options(self) -> dict[str, str]:
res = {}
for index, option in enumerate(self.raw_options):
letter = chr(index + 65)
while option.startswith(f"{letter}("):
option = option[2:]
res[letter] = option
return res
@classmethod
def file_format(cls) -> str:
return "jsonl"
@classmethod
def answer_type(cls) -> AnswerType:
return AnswerType.Option