utils.py 1.81 KB
Newer Older
baberabb's avatar
baberabb committed
1
import os
2
3
from typing import List, Union

Baber's avatar
Baber committed
4
from lm_eval.tasks._config_loader import load_yaml
5

baberabb's avatar
baberabb committed
6
7

# {{{CI}}}
8
# This is the path where the output for the changed files for the tasks folder is stored
baberabb's avatar
baberabb committed
9
# FILE_PATH = file_path = ".github/outputs/tasks_all_changed_and_modified_files.txt"
10

baberabb's avatar
baberabb committed
11

baberabb's avatar
baberabb committed
12
13
14
# reads a text file and returns a list of words
# used to read the output of the changed txt from tj-actions/changed-files
def load_changed_files(file_path: str) -> List[str]:
Baber's avatar
Baber committed
15
    with open(file_path, encoding="utf-8") as f:
baberabb's avatar
baberabb committed
16
        content = f.read()
17
        words_list = list(content.split())
baberabb's avatar
baberabb committed
18
    return words_list
19
20


21
# checks the txt file for list of changed files.
22
23
24
# if file ends with .yaml then check yaml and load the config.
# if the config task is a string, it's a task config.
# if the config task is a list, it's a group config.
25
26
27
def parser(full_path: List[str]) -> List[str]:
    _output = set()
    for x in full_path:
28
        if x.endswith(".yaml") and os.path.exists(x):
Baber's avatar
Baber committed
29
            config = load_yaml(x, recursive=True, resolve_func=True)
30
31
32
33
            if isinstance(config["task"], str):
                _output.add(config["task"])
            elif isinstance(config["task"], list):
                _output.add(config["group"])
34
    return list(_output)
baberabb's avatar
baberabb committed
35
36


baberabb's avatar
baberabb committed
37
def new_tasks() -> Union[List[str], None]:
baberabb's avatar
baberabb committed
38
39
40
41
42
    FILENAME = ".github/outputs/tasks_all_changed_and_modified_files.txt"
    if os.path.exists(FILENAME):
        # If tasks folder has changed then we get the list of files from FILENAME
        # and parse the yaml files to get the task names.
        return parser(load_changed_files(FILENAME))
43
    if os.getenv("API") is not None:
baberabb's avatar
baberabb committed
44
45
46
47
        # Or if API has changed then we set the ENV variable API to True
        # and run  given tasks.
        return ["arc_easy", "hellaswag", "piqa", "wikitext"]
    # if both not true just do arc_easy
48
    return None