|
| 1 | +import json |
| 2 | +import os.path as osp |
| 3 | +import re |
| 4 | +from os import environ |
| 5 | + |
| 6 | +from datasets import Dataset |
| 7 | + |
| 8 | +from opencompass.openicl.icl_evaluator import BaseEvaluator |
| 9 | +from opencompass.registry import (ICL_EVALUATORS, LOAD_DATASET, |
| 10 | + TEXT_POSTPROCESSORS) |
| 11 | +from opencompass.utils import get_data_path |
| 12 | + |
| 13 | +from .base import BaseDataset |
| 14 | + |
| 15 | + |
| 16 | +@LOAD_DATASET.register_module() |
| 17 | +class BBEHDataset(BaseDataset): |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def load(path: str, name: str): |
| 21 | + path = get_data_path(path) |
| 22 | + if environ.get('DATASET_SOURCE') == 'ModelScope': |
| 23 | + from modelscope import MsDataset |
| 24 | + dataset = MsDataset.load(path, subset_name=name, split='test') |
| 25 | + else: |
| 26 | + with open(osp.join(path, f'{name}/task.json'), 'r') as f: |
| 27 | + data = json.load(f)['examples'] |
| 28 | + dataset = Dataset.from_list(data) |
| 29 | + return dataset |
| 30 | + |
| 31 | + |
| 32 | +@TEXT_POSTPROCESSORS.register_module('bbeh_freeform') |
| 33 | +def bbeh_freeform_postprocess(text: str) -> str: |
| 34 | + # Extract answer using specified prefixes |
| 35 | + prefixes = [ |
| 36 | + 'The answer is: ', 'The answer is ', 'The final answer is: ', |
| 37 | + 'The final answer is ' |
| 38 | + ] |
| 39 | + answer = text |
| 40 | + for prefix in prefixes: |
| 41 | + if prefix in text: |
| 42 | + answer = text.split(prefix)[-1] |
| 43 | + break |
| 44 | + |
| 45 | + # Remove formatting markup |
| 46 | + if '\\boxed' in answer: |
| 47 | + answer = re.sub(r'\\boxed{(.*?)}', r'\1', answer) # latex box |
| 48 | + if '\\text' in answer: |
| 49 | + answer = re.sub(r'\\text(?:tt)?{(.*?)}', r'\1', answer) # text/texttt |
| 50 | + if '**' in answer: |
| 51 | + answer = re.sub(r'\*\*(.*?)\*\*', r'\1', answer) # bold |
| 52 | + |
| 53 | + # Take first line and clean |
| 54 | + if '\n' in answer: |
| 55 | + answer = answer.split('\n')[0].strip() |
| 56 | + |
| 57 | + return answer.strip().lower() |
| 58 | + |
| 59 | + |
| 60 | +@TEXT_POSTPROCESSORS.register_module('bbeh_mcq') |
| 61 | +def bbeh_mcq_postprocess(text: str) -> str: |
| 62 | + # Extract answer using specified prefixes |
| 63 | + prefixes = [ |
| 64 | + 'The answer is: ', 'The answer is ', 'The final answer is: ', |
| 65 | + 'The final answer is ' |
| 66 | + ] |
| 67 | + answer = text |
| 68 | + for prefix in prefixes: |
| 69 | + if prefix in text: |
| 70 | + answer = text.split(prefix)[-1] |
| 71 | + break |
| 72 | + |
| 73 | + # Remove parentheses if present |
| 74 | + answer = answer.strip('()') |
| 75 | + |
| 76 | + # Take first line and clean |
| 77 | + if '\n' in answer: |
| 78 | + answer = answer.split('\n')[0].strip() |
| 79 | + |
| 80 | + return answer.strip().lower() |
| 81 | + |
| 82 | + |
| 83 | +@ICL_EVALUATORS.register_module() |
| 84 | +class BBEHEvaluator(BaseEvaluator): |
| 85 | + |
| 86 | + def score(self, predictions, references): |
| 87 | + if len(predictions) != len(references): |
| 88 | + return { |
| 89 | + 'error': 'predictions and references have different length' |
| 90 | + } |
| 91 | + |
| 92 | + processed_preds = [bbeh_freeform_postprocess(p) for p in predictions] |
| 93 | + # References are already in correct format |
| 94 | + processed_refs = [r.lower() for r in references] |
| 95 | + |
| 96 | + details = [] |
| 97 | + correct_count = 0 |
| 98 | + |
| 99 | + for pred, ref in zip(processed_preds, processed_refs): |
| 100 | + correct = False |
| 101 | + |
| 102 | + # Rule 1: Exact match |
| 103 | + if pred == ref: |
| 104 | + correct = True |
| 105 | + # Rule 2: Match after removing quotes/brackets |
| 106 | + elif pred == ref.strip("'\"()[]"): |
| 107 | + correct = True |
| 108 | + # Rule 4: Comma - separated answers |
| 109 | + elif ',' in ref: |
| 110 | + norm_pred = re.sub(r'\s*,\s*', ',', pred) |
| 111 | + norm_ref = re.sub(r'\s*,\s*', ',', ref) |
| 112 | + if norm_pred == norm_ref: |
| 113 | + correct = True |
| 114 | + |
| 115 | + details.append({'pred': pred, 'answer': ref, 'correct': correct}) |
| 116 | + correct_count += int(correct) |
| 117 | + |
| 118 | + score = (correct_count / len(predictions)) * 100 |
| 119 | + return {'score': score, 'details': details} |
| 120 | + |
| 121 | + |
| 122 | +@ICL_EVALUATORS.register_module() |
| 123 | +class BBEHEvaluator_mcq(BaseEvaluator): |
| 124 | + |
| 125 | + def score(self, predictions, references): |
| 126 | + if len(predictions) != len(references): |
| 127 | + return { |
| 128 | + 'error': 'predictions and references have different length' |
| 129 | + } |
| 130 | + |
| 131 | + processed_preds = [bbeh_mcq_postprocess(p) for p in predictions] |
| 132 | + # References are already in correct format |
| 133 | + processed_refs = [r.lower().strip('()') for r in references] |
| 134 | + |
| 135 | + details = [] |
| 136 | + correct_count = 0 |
| 137 | + |
| 138 | + for pred, ref in zip(processed_preds, processed_refs): |
| 139 | + correct = False |
| 140 | + |
| 141 | + # Rule 1: Exact match |
| 142 | + if pred == ref: |
| 143 | + correct = True |
| 144 | + |
| 145 | + details.append({'pred': pred, 'answer': ref, 'correct': correct}) |
| 146 | + correct_count += int(correct) |
| 147 | + |
| 148 | + score = (correct_count / len(predictions)) * 100 |
| 149 | + return {'score': score, 'details': details} |
0 commit comments