Spaces:
Build error
Build error
File size: 16,640 Bytes
546a9ba |
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
from os import path
from tqdm import tqdm
from typing import List, Generator, Optional, Union
from datasets import Dataset
from dataset.st_dataset import SummInstance, SummDataset
# Set directory to load non_huggingface dataset scripts
FILE_DIRECTORY_PATH = path.dirname(path.realpath(__file__))
BASE_NONHUGGINGFACE_DATASETS_PATH = path.join(
FILE_DIRECTORY_PATH, "non_huggingface_datasets_builders"
)
# Huggingface Datasets
class CnndmDataset(SummDataset):
"""
The CNN/DM dataset
"""
dataset_name = "CNN/DailyMail"
is_query_based = False
is_dialogue_based = False
is_multi_document = False
huggingface_dataset = True
huggingface_page = "https://huggingface.co/datasets/cnn_dailymail"
def __init__(self):
super().__init__(
dataset_args=(
"cnn_dailymail",
"3.0.0",
)
)
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
article: str = instance["article"]
highlights: str = instance["highlights"]
summ_instance = SummInstance(source=article, summary=highlights)
yield summ_instance
class MultinewsDataset(SummDataset):
"""
The Multi News dataset
"""
dataset_name = "Multinews"
is_query_based = False
is_dialogue_based = False
is_multi_document = True
huggingface_dataset = True
huggingface_page = "https://huggingface.co/datasets/multi_news"
def __init__(self):
super().__init__(dataset_args=("multi_news",))
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
document: list = [
doc for doc in instance["document"].split("|||||") if doc
] # removes the empty string generated
# since each doc ends with the delimiting token '|||||'
# the final doc creates an empty string
summary: str = instance["summary"]
summ_instance = SummInstance(source=document, summary=summary)
yield summ_instance
class SamsumDataset(SummDataset):
"""
The SAMsum Dataset
"""
dataset_name = "Samsum"
is_query_based = False
is_dialogue_based = True
is_multi_document = False
huggingface_dataset = True
huggingface_page = "https://huggingface.co/datasets/samsum"
def __init__(self):
super().__init__(dataset_args=("samsum",))
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
dialogue: List = instance["dialogue"].split(
"\r\n"
) # split each dialogue into a list of strings such as
# ["speaker1 : utter..", "speaker2 : utter..."]
summary: str = instance["summary"]
summ_instance = SummInstance(source=dialogue, summary=summary)
yield summ_instance
class XsumDataset(SummDataset):
"""
The Xsum Dataset
"""
dataset_name = "Xsum"
huggingface_dataset = True
huggingface_page = "https://huggingface.co/datasets/xsum"
is_query_based = False
is_dialogue_based = False
is_multi_document = False
def __init__(self):
super().__init__(dataset_args=("xsum",))
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
document: List = instance["document"]
summary: str = instance["summary"]
summ_instance = SummInstance(source=document, summary=summary)
yield summ_instance
class PubmedqaDataset(SummDataset):
"""
The Pubmed QA dataset
"""
dataset_name = "Pubmedqa"
is_query_based = True
is_dialogue_based = False
is_multi_document = False
huggingface_dataset = True
huggingface_page = "https://huggingface.co/datasets/pubmed_qa"
def __init__(self, seed=None):
super().__init__(
dataset_args=(
"pubmed_qa",
"pqa_artificial",
)
)
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
context: str = " ".join(instance["context"]["contexts"])
answer: str = instance["long_answer"]
query: str = instance["question"]
summ_instance = SummInstance(source=context, summary=answer, query=query)
yield summ_instance
class MlsumDataset(SummDataset):
"""
The MLsum Dataset - A multi-lingual dataset featuring 5 languages
Includes 1.5 million news articles and their corresponding summaries
"de" - German
"es" - Spanish
"fr" - French
"ru" - Russian
"tu" - Turkish
"""
dataset_name = "MlSum"
is_query_based = False
is_dialogue_based = False
is_multi_document = False
huggingface_dataset = True
huggingface_page = "https://huggingface.co/datasets/mlsum"
supported_languages = ["de", "es", "fr", "ru", "tu"]
mlsum_instantiation_guide = """The languages supported for the Mlsum Dataset are:
de - German
es - Spanish
fr - French
ru - Russian
tu - Turkish
Examples to instantiate the dataset:
1. Dataset with only one language
dataset = MlsumDataset({language_token})
dataset = MlsumDataset("es")
dataset = MlsumDataset("tu")...
2. Dataset with a multiple languages
dataset = MlsumDataset({list of language_token})
dataset = MlsumDataset(["es","de"])
dataset = MlsumDataset(["es","de", "tu"])...
3. Dataset with all supported languages (default)
dataset = MlsumDataset(all)
dataset = MlsumDataset()
"""
def __init__(self, languages: Optional[Union[str, List[str]]] = "all"):
super().__init__(dataset_args=(languages,))
def _load_dataset_safe(self, languages: Optional[Union[str, List[str]]]):
"""
Overrides the parent class method
Method loads multiple datasets of different languages provided in :param languages:
It then concatenates these datasets into one combined dataset
:rtype: datasetDict containing the combined dataset
:param languages: Optional, either a string or list of strings specifying the languages
to load
"""
print(MlsumDataset.mlsum_instantiation_guide)
# Choose languages to download articles
if languages == "all":
selected_languages = MlsumDataset.supported_languages
elif isinstance(languages, list):
for language in languages:
assert self.is_supported(language)
selected_languages = languages
else:
assert self.is_supported(languages)
selected_languages = [languages]
# Concatenate selected languaeges into one dataset
language_datasets = []
for language in selected_languages:
dataset = super()._load_dataset_safe(
"mlsum",
language,
)
language_datasets.append(dataset)
mlsum_dataset = self._concatenate_dataset_dicts(language_datasets)
return mlsum_dataset
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
article: List = instance["text"]
summary: str = instance["summary"]
summ_instance = SummInstance(source=article, summary=summary)
yield summ_instance
def is_supported(self, language: str):
"""
Checks whether the requested langues is supported
:param language: string containing the requested language
:rtype bool:
"""
if language not in MlsumDataset.supported_languages:
print(MlsumDataset.mlsum_instantiation_guide)
raise ValueError(
f"The language(s): '{language}' entered is not supported. See above message for usage info"
)
else:
return True
# Non-huggingface datasets
class ScisummnetDataset(SummDataset):
"""
The SciSummNet dataset. As a dataset not included by huggingface, we need to do manually download, set basic
information for the dataset
"""
dataset_name = "ScisummNet"
version = "1.1.0"
description = (
"A summary of scientific papers should ideally incorporate the impact of the papers on the "
"research community reflected by citations. To facilitate research in citation-aware scientific "
"paper summarization (Scisumm), the CL-Scisumm shared task has been organized since 2014 for "
"papers in the computational linguistics and NLP domain."
)
is_dialogue_based = False
is_multi_document = False
is_query_based = False
huggingface_dataset = False
builder_script_path = path.join(
BASE_NONHUGGINGFACE_DATASETS_PATH, dataset_name.lower() + ".py"
)
def __init__(self, seed=None):
super().__init__()
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
docs: List = [
instance["document_xml"],
instance["citing_sentences_annotated.json"],
]
summary: str = instance["summary"]
summ_instance = SummInstance(source=docs, summary=summary)
yield summ_instance
class SummscreenDataset(SummDataset):
"""
The SummScreen dataset. As a dataset not included by huggingface, we need to do manually download, set basic
information for the dataset
"""
dataset_name = "Summscreen"
version = "1.1.0"
is_dialogue_based = True
is_multi_document = False
is_query_based = False
huggingface_dataset = False
builder_script_path = path.join(
BASE_NONHUGGINGFACE_DATASETS_PATH, dataset_name.lower() + ".py"
)
def __init__(self, seed=None):
super().__init__()
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
transcript: List = instance[
"transcript"
] # convert string into a list of string dialogues
recap: str = instance["recap"]
summ_instance = SummInstance(source=transcript, summary=recap)
yield summ_instance
class QMsumDataset(SummDataset):
"""
QMSum Dataset
"""
dataset_name = "QMsum"
description = """
QMSum is a new human-annotated benchmark for query-based multi-domain meeting summarization task,
which consists of 1,808 query-summary pairs over 232 meetings in multiple domains.
"""
is_dialogue_based = True
is_multi_document = False
is_query_based = True
huggingface_dataset = False
builder_script_path = path.join(
BASE_NONHUGGINGFACE_DATASETS_PATH, dataset_name.lower() + ".py"
)
def __init__(self):
super().__init__()
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
for query_set in (
instance["general_query_list"] + instance["specific_query_list"]
):
meeting: List = [
utterance["speaker"] + " : " + utterance["content"]
for utterance in instance["meeting_transcripts"]
]
query: str = query_set["query"]
summary: str = query_set["answer"]
summ_instance = SummInstance(
source=meeting, summary=summary, query=query
)
yield summ_instance
class ArxivDataset(SummDataset):
"""
The Arxiv Dataset
"""
dataset_name = "Arxiv_longsummarization"
description = """
A summarization dataset comprised of pairs of scientific papers.
The dataset provides a challenging testbed for abstractive summarization.
It contains papers and their abstracts.
"""
is_dialogue_based = False
is_multi_document = False
is_query_based = False
huggingface_dataset = False
builder_script_path = path.join(
BASE_NONHUGGINGFACE_DATASETS_PATH, dataset_name.lower() + ".py"
)
def __init__(self):
print(
"*****************",
"***Attention***",
"This dataset is quite large (approx 5Gb and will need about 15 Gb for the extraction process",
"Cancel/interrupt the download if size and time constraints will not be met",
"*****************",
sep="\n",
)
super().__init__()
def _process_data(self, data: Dataset) -> Generator[SummInstance, None, None]:
"""
Overrides the SummDataset '_process_data()' method
This method processes the data contained in the dataset
and puts each data instance into a SummInstance object
:param dataset: a train/validation/test dataset
:rtype: a generator yielding SummInstance objects
"""
for instance in tqdm(data):
article: List = instance["article_text"]
abstract: str = " ".join(instance["abstract_text"])
summ_instance = SummInstance(source=article, summary=abstract)
yield summ_instance
|