2024-01-01 15:54:40 +08:00
|
|
|
import csv
|
|
|
|
import os
|
2024-04-26 20:13:00 +08:00
|
|
|
import random
|
|
|
|
import re
|
2024-01-01 15:54:40 +08:00
|
|
|
|
|
|
|
from datasets import Dataset
|
|
|
|
|
|
|
|
from opencompass.openicl import BaseEvaluator
|
2024-04-26 20:13:00 +08:00
|
|
|
from opencompass.registry import LOAD_DATASET, TEXT_POSTPROCESSORS
|
[Feature] Support ModelScope datasets (#1289)
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* udpate dataset for modelscope support
* update readme
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* update readme
* remove tydiqa japanese subset
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* update readme
* udpate dataset for modelscope support
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* remove tydiqa japanese subset
* update util
* remove .DS_Store
* fix md format
* move util into package
* update docs/get_started.md
* restore eval_api_zhipu_v2.py, add environment setting
* Update dataset
* Update
* Update
* Update
* Update
---------
Co-authored-by: Yun lin <yunlin@U-Q9X2K4QV-1904.local>
Co-authored-by: Yunnglin <mao.looper@qq.com>
Co-authored-by: Yun lin <yunlin@laptop.local>
Co-authored-by: Yunnglin <maoyl@smail.nju.edu.cn>
Co-authored-by: zhangsongyang <zhangsongyang@pjlab.org.cn>
2024-07-29 13:48:32 +08:00
|
|
|
from opencompass.utils import get_data_path
|
2024-01-01 15:54:40 +08:00
|
|
|
|
|
|
|
from .base import BaseDataset
|
|
|
|
|
|
|
|
|
|
|
|
@LOAD_DATASET.register_module()
|
|
|
|
class GPQADataset(BaseDataset):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load(path: str, name: str):
|
[Feature] Support ModelScope datasets (#1289)
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* udpate dataset for modelscope support
* update readme
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* update readme
* remove tydiqa japanese subset
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* update readme
* udpate dataset for modelscope support
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* remove tydiqa japanese subset
* update util
* remove .DS_Store
* fix md format
* move util into package
* update docs/get_started.md
* restore eval_api_zhipu_v2.py, add environment setting
* Update dataset
* Update
* Update
* Update
* Update
---------
Co-authored-by: Yun lin <yunlin@U-Q9X2K4QV-1904.local>
Co-authored-by: Yunnglin <mao.looper@qq.com>
Co-authored-by: Yun lin <yunlin@laptop.local>
Co-authored-by: Yunnglin <maoyl@smail.nju.edu.cn>
Co-authored-by: zhangsongyang <zhangsongyang@pjlab.org.cn>
2024-07-29 13:48:32 +08:00
|
|
|
path = get_data_path(path, local_mode=True)
|
2024-01-01 15:54:40 +08:00
|
|
|
cnt = 0
|
|
|
|
data = []
|
|
|
|
with open(os.path.join(path, name), 'r', encoding='utf-8') as f:
|
|
|
|
reader = csv.reader(f, delimiter=',')
|
|
|
|
for row in reader:
|
|
|
|
if row[7] == 'Question':
|
|
|
|
continue
|
|
|
|
cnt = cnt + 1
|
|
|
|
question = row[7]
|
2024-03-11 22:34:19 +08:00
|
|
|
# 第一个是正确选项
|
2024-01-01 15:54:40 +08:00
|
|
|
options = [row[8], row[9], row[10], row[11]]
|
2024-03-11 22:34:19 +08:00
|
|
|
shuffle_patterns = ['ABCD', 'BCDA', 'CDAB', 'DABC'] # 更新选项顺序
|
|
|
|
c = shuffle_patterns[cnt % 4]
|
|
|
|
line = {'question': question}
|
|
|
|
ground_truth = options[0]
|
2024-01-01 15:54:40 +08:00
|
|
|
for i in range(4):
|
2024-03-11 22:34:19 +08:00
|
|
|
line['ABCD'[i]] = options[ord(c[i]) - ord('A')]
|
2024-01-01 15:54:40 +08:00
|
|
|
for i in range(4):
|
2024-03-11 22:34:19 +08:00
|
|
|
if line['ABCD'[i]] == ground_truth:
|
2024-01-01 15:54:40 +08:00
|
|
|
line['answer'] = 'ABCD'[i]
|
|
|
|
break
|
2024-03-11 22:34:19 +08:00
|
|
|
data.append(line)
|
|
|
|
dataset = Dataset.from_list(data)
|
2024-01-01 15:54:40 +08:00
|
|
|
return dataset
|
|
|
|
|
|
|
|
|
|
|
|
class GPQAEvaluator(BaseEvaluator):
|
|
|
|
|
|
|
|
def score(self, predictions, references):
|
|
|
|
if len(predictions) != len(references):
|
2024-03-11 22:34:19 +08:00
|
|
|
return {'error': 'preds and refrs have different length'}
|
2024-01-01 15:54:40 +08:00
|
|
|
correct = 0
|
|
|
|
count = 0
|
|
|
|
details = []
|
|
|
|
for i, j in zip(predictions, references):
|
|
|
|
detail = {'pred': i, 'answer': j, 'correct': False}
|
|
|
|
count += 1
|
|
|
|
if i == j:
|
|
|
|
correct += 1
|
|
|
|
detail['correct'] = True
|
|
|
|
details.append(detail)
|
|
|
|
result = {'accuracy': 100 * correct / count, 'details': details}
|
|
|
|
return result
|
2024-04-26 20:13:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
@LOAD_DATASET.register_module()
|
[Feature] Support ModelScope datasets (#1289)
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* udpate dataset for modelscope support
* update readme
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* update readme
* remove tydiqa japanese subset
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* update readme
* udpate dataset for modelscope support
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* remove tydiqa japanese subset
* update util
* remove .DS_Store
* fix md format
* move util into package
* update docs/get_started.md
* restore eval_api_zhipu_v2.py, add environment setting
* Update dataset
* Update
* Update
* Update
* Update
---------
Co-authored-by: Yun lin <yunlin@U-Q9X2K4QV-1904.local>
Co-authored-by: Yunnglin <mao.looper@qq.com>
Co-authored-by: Yun lin <yunlin@laptop.local>
Co-authored-by: Yunnglin <maoyl@smail.nju.edu.cn>
Co-authored-by: zhangsongyang <zhangsongyang@pjlab.org.cn>
2024-07-29 13:48:32 +08:00
|
|
|
class GPQASimpleEvalDataset(BaseDataset):
|
|
|
|
"""GPQA dataset compatible with simple-eval."""
|
2024-04-26 20:13:00 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load(path: str, name: str):
|
[Feature] Support ModelScope datasets (#1289)
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* udpate dataset for modelscope support
* update readme
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* update readme
* remove tydiqa japanese subset
* add ceval, gsm8k modelscope surpport
* update race, mmlu, arc, cmmlu, commonsenseqa, humaneval and unittest
* update bbh, flores, obqa, siqa, storycloze, summedits, winogrande, xsum datasets
* format file
* format file
* update dataset format
* support ms_dataset
* udpate dataset for modelscope support
* merge myl_dev and update test_ms_dataset
* update readme
* udpate dataset for modelscope support
* update eval_api_zhipu_v2
* remove unused code
* add get_data_path function
* remove tydiqa japanese subset
* update util
* remove .DS_Store
* fix md format
* move util into package
* update docs/get_started.md
* restore eval_api_zhipu_v2.py, add environment setting
* Update dataset
* Update
* Update
* Update
* Update
---------
Co-authored-by: Yun lin <yunlin@U-Q9X2K4QV-1904.local>
Co-authored-by: Yunnglin <mao.looper@qq.com>
Co-authored-by: Yun lin <yunlin@laptop.local>
Co-authored-by: Yunnglin <maoyl@smail.nju.edu.cn>
Co-authored-by: zhangsongyang <zhangsongyang@pjlab.org.cn>
2024-07-29 13:48:32 +08:00
|
|
|
path = get_data_path(path, local_mode=True)
|
2024-04-26 20:13:00 +08:00
|
|
|
n_repeats = 4
|
|
|
|
data = []
|
|
|
|
with open(os.path.join(path, name), 'r', encoding='utf-8') as f:
|
|
|
|
reader = csv.reader(f, delimiter=',')
|
|
|
|
for row in reader:
|
|
|
|
if row[7] == 'Question':
|
|
|
|
continue
|
|
|
|
question = row[7]
|
|
|
|
# 第一个是正确选项
|
|
|
|
options = [row[8], row[9], row[10], row[11]]
|
|
|
|
line = {'question': question}
|
|
|
|
line['answer'] = 'A'
|
|
|
|
line['options'] = options
|
|
|
|
data.append(line)
|
|
|
|
|
|
|
|
data_list = data * n_repeats
|
|
|
|
rng = random.Random(0)
|
|
|
|
data_list = [
|
|
|
|
data | {
|
|
|
|
'permutation': rng.sample(range(4), 4)
|
|
|
|
} for data in data_list
|
|
|
|
]
|
|
|
|
for entry in data_list:
|
|
|
|
options = entry['options']
|
|
|
|
correct_options = [options[i] for i in entry['permutation']]
|
|
|
|
for i in range(4):
|
|
|
|
entry['ABCD'[i]] = correct_options[i]
|
|
|
|
correct_index = entry['permutation'].index(0)
|
|
|
|
correct_answer = 'ABCD'[correct_index]
|
|
|
|
entry['options'] = correct_options
|
|
|
|
entry['answer'] = correct_answer
|
|
|
|
|
|
|
|
dataset = Dataset.from_list(data_list)
|
|
|
|
return dataset
|
|
|
|
|
|
|
|
|
|
|
|
@TEXT_POSTPROCESSORS.register_module()
|
|
|
|
def GPQA_Simple_Eval_postprocess(text: str) -> str:
|
|
|
|
ANSWER_PATTERN = r'(?i)ANSWER\s*:\s*([A-D])'
|
|
|
|
match = re.search(ANSWER_PATTERN, text)
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
return None
|