File size: 2,314 Bytes
492e4ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e5b3a7
492e4ea
 
 
 
 
3e5b3a7
492e4ea
3e5b3a7
492e4ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e5b3a7
492e4ea
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
import logging

import pandas as pd
import requests

TRAIN_SPLIT_URL_NI = "https://raw.githubusercontent.com/allenai/natural-instructions/6174af63465999768fbc09f5dd8a7f1a5dfe9abc/splits/default/train_tasks.txt"
TEST_SPLIT_URL_NI = "https://raw.githubusercontent.com/allenai/natural-instructions/6174af63465999768fbc09f5dd8a7f1a5dfe9abc/splits/default/test_tasks.txt"
TASK_URL_NI = "https://raw.githubusercontent.com/allenai/natural-instructions/6174af63465999768fbc09f5dd8a7f1a5dfe9abc/tasks/"

# A total of 876 English tasks from the Natural Instructions dataset (757 tasks from the 'train' split and 119 tasks from the 'test' split)
TASKS_LIST_NI_TRAIN = pd.read_csv(TRAIN_SPLIT_URL_NI, delimiter="\t", header=None, names=["task_names"])["task_names"].tolist()
TASKS_LIST_NI_TEST = pd.read_csv(TEST_SPLIT_URL_NI, delimiter="\t", header=None, names=["task_names"])["task_names"].tolist()

split_to_task_list = {
    "train": TASKS_LIST_NI_TRAIN,
    "test": TASKS_LIST_NI_TEST,
}

def get_all_prompted_examples_ni(task, task_name):
    examples = []
    for example in task["Instances"]:
        for output in example["output"]:
            examples.append(
                {
                    "task_name": task_name,
                    "id": example["id"],
                    "task_name": task_name,
                    "definition": task["Definition"][0],
                    "inputs": example["input"],
                    "targets": output,
                }
            )
    return examples

def get_tasky_examples_ni(split):
    task_list = split_to_task_list[split]
    for task_name in task_list:
        with open(f"{task_name}_{split}.jsonl", "w") as f:
            try:
                task_url = TASK_URL_NI + task_name + ".json"
                task_data = json.loads(requests.get(task_url).text)
            except Exception as e:
                logging.exception(
                    f"There was an issue in loading the file {task_name}: {e} "
                )
                continue
            examples = get_all_prompted_examples_ni(task_data, task_name)
            if examples:
                for example in examples:
                    f.write(json.dumps(example) + "\n")

if __name__ == "__main__":
    get_tasky_examples_ni("train")
    get_tasky_examples_ni("test")