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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import json
|
3 |
+
import ast
|
4 |
+
import pandas as pd
|
5 |
+
import csv
|
6 |
+
|
7 |
+
english_qa_datasets = ["lsat-ar", "lsat-lr", "lsat-rc", "logiqa-en", "sat-math", "sat-en", "aqua-rat",
|
8 |
+
"sat-en-without-passage", "gaokao-english"]
|
9 |
+
chinese_qa_datasets = ["logiqa-zh", "jec-qa-kd", "jec-qa-ca", "gaokao-chinese", "gaokao-geography", "gaokao-history",
|
10 |
+
"gaokao-biology", "gaokao-chemistry", "gaokao-physics", "gaokao-mathqa"]
|
11 |
+
english_cloze_datasets = ["math"]
|
12 |
+
chinese_cloze_datasets = ["gaokao-mathcloze"]
|
13 |
+
all_lst = english_qa_datasets + chinese_qa_datasets + english_cloze_datasets + chinese_cloze_datasets
|
14 |
+
HEAD= 'https://raw.githubusercontent.com/microsoft/AGIEval/main/data/v1/'
|
15 |
+
_URLS = {
|
16 |
+
e: {
|
17 |
+
"test": HEAD+e+'.jsonl',
|
18 |
+
} for e in all_lst
|
19 |
+
|
20 |
+
}
|
21 |
+
_URLS['few_shot'] ={'few_shot':'https://raw.githubusercontent.com/microsoft/AGIEval/main/data/few_shot_prompts.csv'}
|
22 |
+
|
23 |
+
class AgiEval(datasets.GeneratorBasedBuilder):
|
24 |
+
"""TODO: Short description of my dataset."""
|
25 |
+
|
26 |
+
|
27 |
+
BUILDER_CONFIGS = [
|
28 |
+
datasets.BuilderConfig(
|
29 |
+
name= e,
|
30 |
+
version=datasets.Version("2.0.1"),
|
31 |
+
description="",
|
32 |
+
) for e in all_lst
|
33 |
+
|
34 |
+
]
|
35 |
+
DEFAULT_CONFIG_NAME = "aqua_rat"
|
36 |
+
|
37 |
+
def _info(self):
|
38 |
+
|
39 |
+
if self.config.name == "aqua_rat":
|
40 |
+
features = datasets.Features(
|
41 |
+
{
|
42 |
+
"passage": datasets.Value("string"),
|
43 |
+
"question": datasets.Value("string"),
|
44 |
+
"options": datasets.features.Sequence(datasets.Value("string")),
|
45 |
+
"label": datasets.ClassLabel(num_classes=5, names=["A", "B", "C", "D", "E"]),
|
46 |
+
"solution": datasets.Value("string"),
|
47 |
+
}
|
48 |
+
)
|
49 |
+
elif self.config.name in ["sat_en", "sat_math", "sat-en-without-passage"]:
|
50 |
+
features = datasets.Features(
|
51 |
+
{"passage": datasets.Value("string"),
|
52 |
+
"question": datasets.Value("string"),
|
53 |
+
"options": datasets.features.Sequence(datasets.Value("string")),
|
54 |
+
"label": datasets.ClassLabel(num_classes=4, names=["A", "B", "C", "D"]),
|
55 |
+
"solution": datasets.Value("string"),
|
56 |
+
}
|
57 |
+
)
|
58 |
+
elif self.config.name in ['logiqa-en', 'logiqa-zh']:
|
59 |
+
# remove solution from other
|
60 |
+
features = datasets.Features(
|
61 |
+
{"passage": datasets.Value("string"),
|
62 |
+
"question": datasets.Value("string"),
|
63 |
+
"options": datasets.features.Sequence(datasets.Value("string")),
|
64 |
+
"label": datasets.ClassLabel(num_classes=4, names=["A", "B", "C", "D"]),
|
65 |
+
"solution": datasets.Value("string"),
|
66 |
+
}
|
67 |
+
)
|
68 |
+
elif self.config.name == 'math':
|
69 |
+
features = datasets.Features(
|
70 |
+
{
|
71 |
+
"passage": datasets.Value("string"),
|
72 |
+
"question": datasets.Value("string"),
|
73 |
+
"answer": datasets.Value("string"),
|
74 |
+
"solution": datasets.Value("string"),
|
75 |
+
"level": datasets.Value("int32"),
|
76 |
+
"type": datasets.Value("string"),
|
77 |
+
}
|
78 |
+
)
|
79 |
+
elif self.config.name == 'gaokao-mathcloze':
|
80 |
+
features = datasets.Features(
|
81 |
+
{
|
82 |
+
"passage": datasets.Value("string"),
|
83 |
+
"question": datasets.Value("string"),
|
84 |
+
"answer": datasets.Value("string"),
|
85 |
+
"solution": datasets.Value("string"),
|
86 |
+
}
|
87 |
+
)
|
88 |
+
elif self.config.name in ['lsat_lr', 'lsat_rc', 'lsat_ar']:
|
89 |
+
features = datasets.Features(
|
90 |
+
{
|
91 |
+
"passage": datasets.Value("string"),
|
92 |
+
"question": datasets.Value("string"),
|
93 |
+
"options": datasets.features.Sequence(datasets.Value("string")),
|
94 |
+
"label": datasets.ClassLabel(num_classes=5, names=["A", "B", "C", "D", "E"]),
|
95 |
+
"solution": datasets.Value("string"),
|
96 |
+
}
|
97 |
+
)
|
98 |
+
elif self.config.name in ['gaokao-mathqa', 'gaokao-chinese', 'gaokao-history', 'gaokao-geography', 'gaokao-biology', 'gaokao-chemistry', 'gaokao-english']:
|
99 |
+
features = datasets.Features(
|
100 |
+
{
|
101 |
+
"passage": datasets.Value("string"),
|
102 |
+
"question": datasets.Value("string"),
|
103 |
+
"options": datasets.features.Sequence(datasets.Value("string")),
|
104 |
+
"label": datasets.features.Sequence(datasets.Value("string")),
|
105 |
+
"solution": datasets.Value("string"),
|
106 |
+
}
|
107 |
+
)
|
108 |
+
elif self.config.name in ['gaokao-physics', 'jec-qa-ca', 'jec-qa-kd']:
|
109 |
+
features = datasets.Features(
|
110 |
+
{
|
111 |
+
"passage": datasets.Value("string"),
|
112 |
+
"question": datasets.Value("string"),
|
113 |
+
"options": datasets.features.Sequence(datasets.Value("string")),
|
114 |
+
"label": datasets.ClassLabel(num_classes=4, names=["A", "B", "C", "D"]),
|
115 |
+
"solution": datasets.Value("string"),
|
116 |
+
}
|
117 |
+
)
|
118 |
+
|
119 |
+
|
120 |
+
return datasets.DatasetInfo(
|
121 |
+
description=_DESCRIPTION,
|
122 |
+
features=features,
|
123 |
+
homepage=_HOMEPAGE,
|
124 |
+
license=_LICENSE,
|
125 |
+
citation=_CITATION,
|
126 |
+
)
|
127 |
+
|
128 |
+
def _split_generators(self, dl_manager):
|
129 |
+
_urls = _URLS[self.config.name]
|
130 |
+
urls = {
|
131 |
+
"test": _urls["test"],
|
132 |
+
"few_shot": _URLS["few_shot"]["few_shot"],
|
133 |
+
}
|
134 |
+
data_dir = dl_manager.download_and_extract(urls)
|
135 |
+
splits = [
|
136 |
+
datasets.SplitGenerator(
|
137 |
+
name=datasets.Split.TEST,
|
138 |
+
gen_kwargs={"filepath": data_dir["test"], "split": "test"},
|
139 |
+
),
|
140 |
+
]
|
141 |
+
splits.append(datasets.SplitGenerator(
|
142 |
+
name="few_shot",
|
143 |
+
gen_kwargs={"filepath": data_dir["few_shot"], "split": "few_shot"},
|
144 |
+
))
|
145 |
+
|
146 |
+
return splits
|
147 |
+
|
148 |
+
def _generate_examples(self, filepath, split):
|
149 |
+
# Mapping for column names in CSV to dataset names
|
150 |
+
names = {'aqua_rat': 'aqua-rat', 'sat_en': 'sat-en', 'sat_math': 'sat-math',
|
151 |
+
'lsat_ar': 'lsat-ar', 'lsat_lr': 'lsat-lr', 'lsat_rc': 'lsat-rc',
|
152 |
+
'logiqa': 'logiqa-en', 'math_agieval': 'math'}
|
153 |
+
|
154 |
+
if split == "few_shot":
|
155 |
+
# Load the data from the CSV
|
156 |
+
df = pd.read_csv(filepath, keep_default_na=False)
|
157 |
+
|
158 |
+
# Extract samples and explanations
|
159 |
+
samples = df[df.index % 2 == 0].reset_index(drop=True)
|
160 |
+
explanations = df[df.index % 2 != 0].reset_index(drop=True)
|
161 |
+
for key in range(samples.shape[0]):
|
162 |
+
try:
|
163 |
+
data = ast.literal_eval(samples[names[self.config.name]][key])
|
164 |
+
explanation_row = explanations[names[self.config.name]][key]
|
165 |
+
if self.config.name == "aqua_rat":
|
166 |
+
features = datasets.Features(
|
167 |
+
{
|
168 |
+
"passage": data["passage"],
|
169 |
+
"question": data["question"],
|
170 |
+
"options": data["options"],
|
171 |
+
"label": data["label"],
|
172 |
+
"solution": str(explanation_row),
|
173 |
+
}
|
174 |
+
)
|
175 |
+
elif self.config.name in ["sat_en", "sat_math", "sat-en-without-passage"]:
|
176 |
+
features = datasets.Features(
|
177 |
+
{
|
178 |
+
"passage": data["passage"],
|
179 |
+
"question": data["question"],
|
180 |
+
"options": data["options"],
|
181 |
+
"label": data["label"],
|
182 |
+
"solution": str(explanation_row),
|
183 |
+
}
|
184 |
+
)
|
185 |
+
elif self.config.name in ['logiqa-en', 'logiqa-zh']:
|
186 |
+
# remove solution from other
|
187 |
+
features = datasets.Features(
|
188 |
+
{
|
189 |
+
"passage": data["passage"],
|
190 |
+
"question": data["question"],
|
191 |
+
"options": data["options"],
|
192 |
+
"label": data["label"],
|
193 |
+
"solution": str(explanation_row),
|
194 |
+
}
|
195 |
+
)
|
196 |
+
elif self.config.name == 'math':
|
197 |
+
features = datasets.Features(
|
198 |
+
{
|
199 |
+
"passage": data["passage"],
|
200 |
+
"question": data["question"],
|
201 |
+
"answer": data["answer"],
|
202 |
+
"solution": str(explanation_row),
|
203 |
+
"level": data["level"],
|
204 |
+
"type": data["type"],
|
205 |
+
}
|
206 |
+
)
|
207 |
+
elif self.config.name == 'gaokao-mathcloze':
|
208 |
+
features = datasets.Features(
|
209 |
+
{
|
210 |
+
"passage": data["passage"],
|
211 |
+
"question": data["question"],
|
212 |
+
"answer": data["answer"],
|
213 |
+
"solution": str(explanation_row),
|
214 |
+
}
|
215 |
+
)
|
216 |
+
elif self.config.name in ['lsat_lr', 'lsat_rc', 'lsat_ar']:
|
217 |
+
features = datasets.Features(
|
218 |
+
{
|
219 |
+
"passage": data["passage"],
|
220 |
+
"question": data["passquestionage"],
|
221 |
+
"options": data["options"],
|
222 |
+
"label": data["label"],
|
223 |
+
"solution": str(explanation_row),
|
224 |
+
}
|
225 |
+
)
|
226 |
+
elif self.config.name in ['gaokao-mathqa', 'gaokao-chinese', 'gaokao-history', 'gaokao-geography', 'gaokao-biology', 'gaokao-chemistry', 'gaokao-english']:
|
227 |
+
features = datasets.Features(
|
228 |
+
{
|
229 |
+
"passage": data["passage"],
|
230 |
+
"question": data["question"],
|
231 |
+
"options": data["options"],
|
232 |
+
"label": data["label"],
|
233 |
+
"solution": str(explanation_row),
|
234 |
+
}
|
235 |
+
)
|
236 |
+
elif self.config.name in ['gaokao-physics', 'jec-qa-ca', 'jec-qa-kd']:
|
237 |
+
features = datasets.Features(
|
238 |
+
{
|
239 |
+
"passage": data["passage"],
|
240 |
+
"question": data["question"],
|
241 |
+
"options": data["options"],
|
242 |
+
"label": data["label"],
|
243 |
+
"solution": str(explanation_row),
|
244 |
+
}
|
245 |
+
)
|
246 |
+
except:
|
247 |
+
pass
|
248 |
+
else:
|
249 |
+
with open(filepath, encoding="utf-8") as f:
|
250 |
+
for key, row in enumerate(f):
|
251 |
+
data = json.loads(row)
|
252 |
+
if self.config.name == "aqua_rat":
|
253 |
+
yield key, {
|
254 |
+
"passage": data["passage"],
|
255 |
+
"question": data["question"],
|
256 |
+
"options": data["options"],
|
257 |
+
"label": data["label"],
|
258 |
+
"solution": data["other"]["solution"],
|
259 |
+
}
|
260 |
+
elif self.config.name in ["sat_en", "sat_math", "sat-en-without-passage"]:
|
261 |
+
label_index = "ABCDE".index(data["label"])
|
262 |
+
if label_index > len(data["options"]) - 1:
|
263 |
+
continue
|
264 |
+
else:
|
265 |
+
yield key, {
|
266 |
+
"passage": data["passage"],
|
267 |
+
"question": data["question"],
|
268 |
+
"options": data["options"],
|
269 |
+
"label": data["label"],
|
270 |
+
"solution": data["other"]["solution"],
|
271 |
+
}
|
272 |
+
|
273 |
+
elif self.config.name in ['logiqa-en', 'logiqa-zh']:
|
274 |
+
yield key, {
|
275 |
+
"passage": data["passage"],
|
276 |
+
"question": data["question"],
|
277 |
+
"options": data["options"],
|
278 |
+
"label": data["label"],
|
279 |
+
"solution": data["label"],
|
280 |
+
}
|
281 |
+
elif self.config.name == 'math':
|
282 |
+
if not data.get("level"):
|
283 |
+
data["level"] = data['other']['level']
|
284 |
+
if not data.get("type"):
|
285 |
+
data["type"] = data['other']['type']
|
286 |
+
yield key, {
|
287 |
+
"question": data["question"],
|
288 |
+
"answer": data["answer"],
|
289 |
+
"solution": data["other"]["solution"],
|
290 |
+
"level": data["level"],
|
291 |
+
"type": data["type"],
|
292 |
+
}
|
293 |
+
elif self.config.name == 'gaokao-mathcloze':
|
294 |
+
yield key, {
|
295 |
+
"passage": data["passage"],
|
296 |
+
"question": data["question"],
|
297 |
+
"answer": data["answer"],
|
298 |
+
"solution": data["answer"],
|
299 |
+
}
|
300 |
+
elif self.config.name in ['lsat_lr', 'lsat_rc', 'lsat_ar']:
|
301 |
+
yield key, {
|
302 |
+
"passage": data["passage"],
|
303 |
+
"question": data["passquestionage"],
|
304 |
+
"options": data["options"],
|
305 |
+
"label": data["label"],
|
306 |
+
"solution": data["label"],
|
307 |
+
}
|
308 |
+
elif self.config.name in ['gaokao-mathqa', 'gaokao-chinese', 'gaokao-history', 'gaokao-geography', 'gaokao-biology', 'gaokao-chemistry', 'gaokao-english']:
|
309 |
+
yield key, {
|
310 |
+
"passage": data["passage"],
|
311 |
+
"question": data["passquestionage"],
|
312 |
+
"options": data["options"],
|
313 |
+
"label": data["label"],
|
314 |
+
"solution": data["label"],
|
315 |
+
}
|
316 |
+
|
317 |
+
elif self.config.name in ['gaokao-physics', 'jec-qa-ca', 'jec-qa-kd']:
|
318 |
+
yield key, {
|
319 |
+
"passage": data["passage"],
|
320 |
+
"question": data["passquestionage"],
|
321 |
+
"options": data["options"],
|
322 |
+
"label": data["label"],
|
323 |
+
"solution": data["label"],
|
324 |
+
}
|