Datasets:
tau
/

Modalities:
Text
Libraries:
Datasets
File size: 8,174 Bytes
3e38ab8
 
 
 
 
 
127e3fd
 
3e38ab8
b847b51
 
 
 
127e3fd
b847b51
1ade361
3e38ab8
 
89c5f61
3e38ab8
127e3fd
89c5f61
3e38ab8
 
 
 
 
 
 
 
 
 
 
 
bf19c4f
3e38ab8
 
 
a1fa43e
2eacbde
1ade361
 
 
 
 
127e3fd
 
 
1ade361
127e3fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf19c4f
127e3fd
bf19c4f
1ade361
 
 
 
 
 
127e3fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ade361
4316980
 
 
 
 
 
1ade361
 
127e3fd
1ade361
 
 
 
 
 
 
 
 
 
 
 
4316980
 
 
 
1ade361
 
 
4316980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e38ab8
 
89c5f61
3e38ab8
 
 
 
1ade361
 
3e38ab8
1ade361
3e38ab8
 
a1fa43e
2eacbde
3e38ab8
1ade361
3e38ab8
 
89c5f61
3e38ab8
 
a1fa43e
2eacbde
1ade361
127e3fd
 
 
 
 
 
 
 
 
3e38ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ade361
3e38ab8
 
 
 
 
1ade361
3e38ab8
 
 
 
 
1ade361
 
3e38ab8
 
 
 
127e3fd
3e38ab8
 
 
1ade361
 
bf19c4f
1ade361
3e38ab8
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# coding=utf-8
# Lint as: python3
"""The SCROLLS benchmark."""

import json
import os
from abc import abstractmethod

import datasets
from citations_and_descriptions import (
    _SUMM_SCREEN_DESCRIPTION, _SUMM_SCREEN_CITATION,
    _GOV_REPORT_CITATION, _GOV_REPORT_DESCRIPTION,
    _ARXIV_CITATION, _ARXIV_DESCRIPTION,
    _FS_DESCRIPTION, _FS_CITATION,
)


class FSConfig(datasets.BuilderConfig):
    """BuilderConfig for FS."""

    def __init__(self, data_url, citation, url, max_source_length, tokenizer, **kwargs):
        """BuilderConfig for FS.
        Args:
          features: `list[string]`, list of the features that will appear in the
            feature dict. Should not include "label".
          data_url: `string`, url to download the zip file from.
          citation: `string`, citation for the data set.
          url: `string`, url for information about the data set.
          label_classes: `list[string]`, the list of classes for the label if the
            label is present as a string. Non-string labels will be cast to either
            'False' or 'True'.
          **kwargs: keyword arguments forwarded to super.
        """
        super(FSConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
        self.features = ["pid", self.source_key, self.target_key]
        self.data_url = data_url
        self.citation = citation
        self.url = url
        self.max_source_length = max_source_length
        self.tokenizer = tokenizer

    def remove_redundant_fields(self, example):
        for field in self.redundant_fields:
            del example[field]

    @abstractmethod
    def postprocess(self, s):
        pass

    @property
    @abstractmethod
    def original_source_key(self):
        pass

    @property
    @abstractmethod
    def original_target_key(self):
        pass

    @property
    @abstractmethod
    def train_file(self):
        pass

    @property
    @abstractmethod
    def validation_file(self):
        pass

    @property
    @abstractmethod
    def test_file(self):
        pass

    @property
    def source_key(self):
        return "source"

    @property
    def target_key(self):
        return "target"

    @property
    @abstractmethod
    def id_key(self):
        pass

    @property
    def redundant_fields(self):
        return []

    def process(self, example):  # TODO perhaps we can use this for base
        example[self.source_key] = example[self.original_source_key].strip()
        example[self.target_key] = example[self.original_target_key].strip() if example[self.original_target_key] else None


class ScrollsConfig(FSConfig):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @property
    def original_source_key(self):
        return "input"

    @property
    def original_target_key(self):
        return "output"

    @property
    def train_file(self):
        return "train.jsonl"

    @property
    def validation_file(self):
        return "validation.jsonl"

    @property
    def test_file(self):
        return "test.jsonl"

    @property
    def id_key(self):
        return "pid"

    @property
    def redundant_fields(self):
        return [self.original_source_key, self.original_target_key, "id"]



    def process_input(self, s):
        prefix = s.strip()
        suffix = "\nSummarize the above:"
        prefix = _truncate_prefix(prefix, suffix, self.max_source_length, self.tokenizer)
        return prefix + suffix


class ArxivConfig(FSConfig):
    # TODO properties etc...
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.train_file = "train.txt"
        self.validation_file = "val.txt"
        self.test_file = "test.txt"

        self.input_key = "article_text"
        self.output_key = "abstract_text"
        self.id_key = "article_id"
        self.redundant_fields = [self.input_key, self.output_key, self.id_key, 'labels', 'section_names', 'sections']

    def process_input(self, s):
        prefix = ' '.join(s)
        suffix = "\nSummarize the above:"
        prefix = _truncate_prefix(prefix, suffix, self.max_source_length, self.tokenizer)
        return prefix + suffix

    def process_output(self, s):
        # TODO remove "<S>" and "</S>" ?
        return ' '.join(s).replace("<S>", "").replace("</S>", "")


def _truncate_prefix(prefix, suffix, max_source_length, tokenizer):
    encoded_input = tokenizer.encode(prefix + suffix)

    while len(encoded_input) > max_source_length:
        overflow = len(encoded_input) - max_source_length
        tokenized_prefix = tokenizer.encode(prefix, add_special_tokens=False)
        if overflow > 0:
            tokenized_prefix = tokenized_prefix[:-overflow]
        prefix = tokenizer.decode(tokenized_prefix, skip_special_tokens=False).strip()
        encoded_input = tokenizer.encode(prefix + suffix)

    return prefix


class Fs(datasets.GeneratorBasedBuilder):
    """The SCROLLS benchmark."""

    DEFAULT_WRITER_BATCH_SIZE = 1000  # because Narrative QA is a rather large dataset
    BUILDER_CONFIGS = [
        ScrollsConfig(
            name="summ_screen_fd_debug",
            description=_SUMM_SCREEN_DESCRIPTION,
            data_url="https://huggingface.co/datasets/tau/fs/resolve/main/data/summ_screen_fd_debug.zip",
            citation=_SUMM_SCREEN_CITATION,
            url="https://github.com/mingdachen/SummScreen",
            max_source_length=None,
            tokenizer=None,
        ),
        ScrollsConfig(
            name="gov_report",
            description=_GOV_REPORT_CITATION,
            data_url="https://huggingface.co/datasets/tau/fs/resolve/main/data/gov_report.zip",
            citation=_GOV_REPORT_DESCRIPTION,
            url="https://gov-report-data.github.io/",
            max_source_length=None,
            tokenizer=None,
        ),
        # ArxivConfig(
        #     name="arxiv_debug",
        #     description=_ARXIV_CITATION,
        #     data_url="https://huggingface.co/datasets/tau/fs/resolve/main/data/arxiv_debug.zip",
        #     citation=_ARXIV_DESCRIPTION,
        #     url="https://github.com/armancohan/long-summarization",
        #     max_source_length=None,
        #     tokenizer=None,
        # ),
    ]

    def _info(self):
        features = {feature: datasets.Value("string") for feature in self.config.features}

        return datasets.DatasetInfo(
            description=_FS_DESCRIPTION + self.config.description,
            features=datasets.Features(features),
            homepage=self.config.url,
            citation=self.config.citation + "\n" + _FS_CITATION,
        )

    def _split_generators(self, dl_manager):
        dl_dir = dl_manager.download_and_extract(self.config.data_url)

        data_files = {} if self.config.data_files is not None else None
        if data_files is not None:
            for split, paths in self.config.data_files.items():
                data_files[split] = paths[0]

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, self.config.train_file),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, self.config.validation_file),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, self.config.test_file) if data_files is None else data_files[
                        "test"],
                },
            ),
        ]

    def _generate_examples(self, data_file):
        with open(data_file, encoding="utf-8") as f:
            for line in f:
                row = json.loads(line)

                row["pid"] = row[self.config.id_key]
                self.config.process(row)
                self.config.remove_redundant_fields(row)
                yield row["pid"], row


def _get_task_name_from_data_url(data_url):
    return data_url.split("/")[-1].split(".")[0]