_generate_configs.py 2.92 KB
Newer Older
ManuelFay's avatar
ManuelFay committed
1
2
3
4
"""
Take in a YAML, and output all other splits with this YAML
"""
import argparse
5
import os
ManuelFay's avatar
ManuelFay committed
6

7
8
import requests
import yaml
ManuelFay's avatar
ManuelFay committed
9
10
from tqdm import tqdm

James A. Michaelov's avatar
James A. Michaelov committed
11
from lm_eval.utils import logging
ManuelFay's avatar
ManuelFay committed
12

13

ManuelFay's avatar
ManuelFay committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
API_URL = "https://datasets-server.huggingface.co/splits?dataset=facebook/belebele"


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--base_yaml_path", required=True)
    parser.add_argument("--save_prefix_path", default="belebele")
    parser.add_argument("--cot_prompt_path", default=None)
    parser.add_argument("--task_prefix", default="")
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()

    # get filename of base_yaml so we can `"include": ` it in our other YAMLs.
    base_yaml_name = os.path.split(args.base_yaml_path)[-1]
31
    with open(args.base_yaml_path, encoding="utf-8") as f:
ManuelFay's avatar
ManuelFay committed
32
33
34
35
36
        base_yaml = yaml.full_load(f)

    if args.cot_prompt_path is not None:
        import json

37
        with open(args.cot_prompt_path, encoding="utf-8") as f:
ManuelFay's avatar
ManuelFay committed
38
39
40
41
42
            cot_file = json.load(f)

    def query():
        response = requests.get(API_URL)
        return response.json()["splits"]
43

James A. Michaelov's avatar
James A. Michaelov committed
44
45
    print(query())
    languages = [split["split"] for split in query()]
ManuelFay's avatar
ManuelFay committed
46

47
    for lang in tqdm([lang for lang in languages if "default" not in lang]):
ManuelFay's avatar
ManuelFay committed
48
49
        yaml_dict = {
            "include": base_yaml_name,
ManuelFay's avatar
ManuelFay committed
50
            "task": f"belebele_{args.task_prefix}_{lang}"
ManuelFay's avatar
ManuelFay committed
51
52
            if args.task_prefix != ""
            else f"belebele_{lang}",
James A. Michaelov's avatar
James A. Michaelov committed
53
            "test_split": lang,
54
            "fewshot_split": lang,
ManuelFay's avatar
ManuelFay committed
55
56
57
        }

        file_save_path = args.save_prefix_path + f"_{lang}.yaml"
James A. Michaelov's avatar
James A. Michaelov committed
58
        logging.info(f"Saving yaml for subset {lang} to {file_save_path}")
59
        with open(file_save_path, "w", encoding="utf-8") as yaml_file:
ManuelFay's avatar
ManuelFay committed
60
61
62
63
64
65
66
            yaml.dump(
                yaml_dict,
                yaml_file,
                width=float("inf"),
                allow_unicode=True,
                default_style='"',
            )
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

    # write group config out

    group_yaml_dict = {
        "group": f"belebele_{args.task_prefix}"
        if args.task_prefix != ""
        else "belebele",
        "task": [
            (
                f"belebele_{args.task_prefix}_{lang}"
                if args.task_prefix != ""
                else f"belebele_{lang}"
            )
            for lang in languages
            if "default" not in lang
        ],
        "aggregate_metric_list": [
            {"metric": "acc", "aggregation": "mean", "weight_by_size": False},
            {"metric": "acc_norm", "aggregation": "mean", "weight_by_size": False},
        ],
        "metadata": {"version": 0.0},
    }

    file_save_path = "_" + args.save_prefix_path + f"{args.task_prefix}.yaml"

    with open(file_save_path, "w", encoding="utf-8") as group_yaml_file:
        yaml.dump(
            group_yaml_dict,
            group_yaml_file,
            width=float("inf"),
            allow_unicode=True,
            default_style='"',
        )