|
| 1 | +import os |
| 2 | +import json |
1 | 3 | from typing import TYPE_CHECKING, Callable, Dict, List |
2 | 4 |
|
3 | 5 | import pandas as pd |
4 | 6 |
|
| 7 | +from langtest.datahandler.utils import ensure_download_and_unzip |
| 8 | + |
5 | 9 | if TYPE_CHECKING: |
6 | 10 | from langtest.utils.custom_types.sample import Sample |
7 | 11 |
|
@@ -75,3 +79,72 @@ def medexqa(subset="all", *args, **kwargs) -> List["Sample"]: |
75 | 79 |
|
76 | 80 | transformed_samples.append(sample) |
77 | 81 | return transformed_samples |
| 82 | + |
| 83 | + |
| 84 | +@register_predefined_dataset("headqa") |
| 85 | +def headqa(*args, **kwargs) -> List["Sample"]: |
| 86 | + """Load the HeadQA dataset.""" |
| 87 | + from langtest.utils.custom_types import QASample |
| 88 | + |
| 89 | + headqa_dir = os.path.join(os.path.expanduser("~"), ".langtest", "datasets", "headqa") |
| 90 | + |
| 91 | + ensure_download_and_unzip( |
| 92 | + "https://huggingface.co/datasets/dvilares/head_qa/resolve/main/data/head-qa-es-en-pdfs.zip", |
| 93 | + extract_to=headqa_dir, |
| 94 | + ) |
| 95 | + |
| 96 | + file_path = os.path.join(headqa_dir, "HEAD_EN", "test_HEAD_EN.json") |
| 97 | + |
| 98 | + with open( |
| 99 | + file_path, |
| 100 | + "r", |
| 101 | + encoding="utf-8", |
| 102 | + ) as f: |
| 103 | + head_qa = json.load(f) |
| 104 | + |
| 105 | + def clean_answers(answers): |
| 106 | + return "\n".join( |
| 107 | + f"{chr(answer['aid'] + 64)}) {answer['atext'].strip()}" for answer in answers |
| 108 | + ) |
| 109 | + |
| 110 | + df = ( |
| 111 | + pd.DataFrame.from_dict(head_qa["exams"], orient="index") |
| 112 | + .reset_index(drop=True) |
| 113 | + .assign( |
| 114 | + exam_id=lambda x: x.index, |
| 115 | + name=lambda x: x["name"].str.strip(), |
| 116 | + year=lambda x: x["year"].str.strip(), |
| 117 | + category=lambda x: x["category"].str.strip(), |
| 118 | + ) |
| 119 | + .pipe( |
| 120 | + lambda x: pd.json_normalize( |
| 121 | + x.to_dict("records"), |
| 122 | + record_path="data", |
| 123 | + meta=["exam_id", "name", "year", "category"], |
| 124 | + ) |
| 125 | + ) |
| 126 | + .assign( |
| 127 | + qid=lambda x: x["qid"].str.strip().astype(int), |
| 128 | + qtext=lambda x: x["qtext"].str.strip(), |
| 129 | + ra=lambda x: x["ra"].str.strip().astype(int), |
| 130 | + options=lambda x: x["answers"].apply(clean_answers), |
| 131 | + ) |
| 132 | + .query("ra != 0") |
| 133 | + .assign( |
| 134 | + answer=lambda x: x["ra"].map(lambda value: chr(value + 64)), |
| 135 | + )[["qid", "qtext", "options", "answer"]] |
| 136 | + ) |
| 137 | + |
| 138 | + transformed_samples = [] |
| 139 | + |
| 140 | + for sample in df.iterrows(): |
| 141 | + sample = QASample( |
| 142 | + dataset_name="headqa", |
| 143 | + original_context="-", |
| 144 | + original_question=sample[1]["qtext"], |
| 145 | + options=sample[1]["options"], |
| 146 | + expected_results=sample[1]["answer"], |
| 147 | + ) |
| 148 | + |
| 149 | + transformed_samples.append(sample) |
| 150 | + return transformed_samples |
0 commit comments