File size: 2,394 Bytes
ae41410
 
 
 
 
 
 
 
7b0629a
ae41410
8abefbc
7b0629a
8abefbc
 
988aeff
ae41410
 
 
 
988aeff
 
 
 
 
ae41410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8abefbc
c4aa90b
0e6dc0c
7b0629a
ae41410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea7b41d
0e6dc0c
988aeff
0e6dc0c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import datasets
import os
import json

_DESCRIPTION = "lm-polygraph wrapper for xsum dataset"

_DATA_DIRECTORY = "."
VERSION = datasets.Version("0.0.1")

_CONFIG = {
    "dataset": "xsum",
    "splits": ["train", "validation", "test"],
    "input_column": "document",
    "output_column": "summary",
    "prompt": "Here's the text and it's short one-sentence summary.\n\nText:\n{text}\n\nSummary (one sentence):\n",
}


def _prepare_dataset(dataset):
    x, y = dataset[_CONFIG["input_column"]], dataset[_CONFIG["output_column"]]
    if _CONFIG.get("prompt"):
        for i in range(len(x)):
            x[i] = _CONFIG["prompt"].format(text=x[i])
    return x, y


class PolygraphXsum(datasets.GeneratorBasedBuilder):
    """lm-polygraph wrapper for xsum dataset"""

    def _info(self):
        features = datasets.Features(
            {
                "input": datasets.Value("string"),
                "output": datasets.Value("string"),
            }
        )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
        )

    def _split_generators(self, dl_manager):
        dataset = datasets.load_dataset(_CONFIG["dataset"], trust_remote_code=True)

        def download_custom_dataset(src_url: str, dst_path: str):
            split = src_url
            x, y = _prepare_dataset(dataset[split])
            result_dataset = datasets.Dataset.from_dict({"input": x, "output": y})
            result_dataset.save_to_disk(dst_path)
        downloaded_files = dl_manager.download_custom({split: split for split in _CONFIG["splits"]}, download_custom_dataset)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": downloaded_files["train"],
                }),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "filepath": downloaded_files["validation"],
                }),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": downloaded_files["test"],
                })
        ]

    def _generate_examples(self, filepath):
        dataset = datasets.Dataset.load_from_disk(filepath)
        for i in range(len(dataset)):
            yield i, dataset[i]