|
| 1 | +from typing import Iterable, Dict |
| 2 | +import gzip |
| 3 | +import json |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +ROOT = os.path.dirname(os.path.abspath(__file__)) |
| 8 | +MULTILINGUAL_HUMANEVAL_METADATA = os.path.join(ROOT, "data", "multilingual_humaneval", "metadata.json") |
| 9 | +with open(MULTILINGUAL_HUMANEVAL_METADATA, "r", encoding="utf-8") as fr: |
| 10 | + MULTILINGUAL_HUMANEVAL_METADATA = json.load(fr) |
| 11 | +HUMAN_EVAL_PYTHON = os.path.join(ROOT, "data", "multilingual_humaneval", MULTILINGUAL_HUMANEVAL_METADATA["python"]) |
| 12 | +HUMAN_EVAL = HUMAN_EVAL_PYTHON |
| 13 | + |
| 14 | + |
| 15 | +def read_problems(evalset_file: str = HUMAN_EVAL_PYTHON) -> Dict[str, Dict]: |
| 16 | + return {task["task_id"]: task for task in stream_jsonl(evalset_file)} |
| 17 | + |
| 18 | + |
| 19 | +def stream_jsonl(filename: str) -> Iterable[Dict]: |
| 20 | + """ |
| 21 | + Parses each jsonl line and yields it as a dictionary |
| 22 | + """ |
| 23 | + if filename.endswith(".gz"): |
| 24 | + with open(filename, "rb") as gzfp: |
| 25 | + with gzip.open(gzfp, 'rt') as fp: |
| 26 | + for line in fp: |
| 27 | + if any(not x.isspace() for x in line): |
| 28 | + yield json.loads(line) |
| 29 | + else: |
| 30 | + with open(filename, "r") as fp: |
| 31 | + for line in fp: |
| 32 | + if any(not x.isspace() for x in line): |
| 33 | + yield json.loads(line) |
| 34 | + |
| 35 | + |
| 36 | +def write_jsonl(filename: str, data: Iterable[Dict], append: bool = False): |
| 37 | + """ |
| 38 | + Writes an iterable of dictionaries to jsonl |
| 39 | + """ |
| 40 | + if append: |
| 41 | + mode = 'ab' |
| 42 | + else: |
| 43 | + mode = 'wb' |
| 44 | + filename = os.path.expanduser(filename) |
| 45 | + if filename.endswith(".gz"): |
| 46 | + with open(filename, mode) as fp: |
| 47 | + with gzip.GzipFile(fileobj=fp, mode='wb') as gzfp: |
| 48 | + for x in data: |
| 49 | + gzfp.write((json.dumps(x) + "\n").encode('utf-8')) |
| 50 | + else: |
| 51 | + with open(filename, mode) as fp: |
| 52 | + for x in data: |
| 53 | + fp.write((json.dumps(x) + "\n").encode('utf-8')) |
| 54 | + |
| 55 | + |
| 56 | +def get_metadata(dataset, metadata_type="problem"): |
| 57 | + assert metadata_type in ["problem", "example"] |
| 58 | + assert dataset in ["mbxp", "multi-humaneval", "mathqa-x"], f"Unsupported dataset {dataset}" |
| 59 | + dataset_dirmap = {"mbxp": "mbxp", |
| 60 | + "multi-humaneval": "multilingual_humaneval", |
| 61 | + "mathqa-x": "multilingual_mathqa"} |
| 62 | + typemap = {"problem": "metadata.json", |
| 63 | + "example": "metadata_examples.json"} |
| 64 | + datadir = os.path.join(ROOT, "data", dataset_dirmap[dataset]) |
| 65 | + path = os.path.join(datadir, typemap[metadata_type]) |
| 66 | + with open(path, "r") as f: |
| 67 | + metadata = json.load(f) |
| 68 | + return metadata, datadir |
| 69 | + |
| 70 | + |
| 71 | +def get_supported_langs(dataset): |
| 72 | + metadata, _ = get_metadata(dataset, metadata_type="problem") |
| 73 | + return list(metadata.keys()) |
| 74 | + |
| 75 | + |
| 76 | +def get_data(dataset="mbxp", language="python"): |
| 77 | + metadata, datadir = get_metadata(dataset, metadata_type="problem") |
| 78 | + if language.lower() not in metadata: |
| 79 | + raise ValueError(f"Language {language} not found in metadata file") |
| 80 | + datafile = metadata[language.lower()] |
| 81 | + print(f"Loading {dataset} | language = {language}") |
| 82 | + return read_problems(os.path.join(datadir, datafile)) |
| 83 | + |
| 84 | + |
| 85 | +# due to similar format, examples from mbxp are sufficient to be used |
| 86 | +# for few-shot prompting in multi-humaneval |
| 87 | +def get_examples(dataset="mbxp", language="python", num_examples=None): |
| 88 | + assert dataset in ["mbxp"], f"No fewshot examples in dataset {dataset}" |
| 89 | + metadata, datadir = get_metadata(dataset=dataset, metadata_type="example") |
| 90 | + if language.lower() not in metadata: |
| 91 | + raise ValueError(f"Language {language} not found in metadata file") |
| 92 | + datafile = metadata[language.lower()] |
| 93 | + print(f"Loading examples from {dataset} | language = {language}") |
| 94 | + # use streams |
| 95 | + if num_examples is None: |
| 96 | + # return the entire stream |
| 97 | + return stream_jsonl(os.path.join(datadir, datafile)) |
| 98 | + else: |
| 99 | + problems = get_data(dataset=dataset, language=language) |
| 100 | + stream = get_examples(dataset=dataset, language=language) |
| 101 | + examples = [] |
| 102 | + for idx, example in enumerate(stream): |
| 103 | + if idx == num_examples: |
| 104 | + break |
| 105 | + task_id = example["task_id"] |
| 106 | + prompt = problems[task_id]["prompt"] |
| 107 | + example["prompt"] = prompt |
| 108 | + examples.append(example) |
| 109 | + return examples |
0 commit comments