import gzip import json import datasets logger = datasets.logging.get_logger(__name__) _URL = "https://huggingface.co/datasets/allenai/pes2o" _VARIANTS = ["v1", "v2"] _N_SHARDS_PER_SPLIT = { "v1": {"train": {'s2orc': 10, 's2ag': 10}, "valid": {'s2orc': 1, 's2ag': 1}}, "v2": {"train": {'s2orc': 10, 's2ag': 10}, "valid": {'s2orc': 1, 's2ag': 1}}, } _DATA_URL = "\ https://huggingface.co/datasets/allenai/pes2o/resolve/main/\ {name}/{subset}/{split}/{shard:05d}.json.gz\ " _DESCRIPTION = "\ The PES2O dataset is a collection of ~40M creative commmon licensed academic \ papers, cleaned, filtered, and formatted for pre-training of language models. \ It is derived from the Semantic Scholar Open Research Corpus(Lo et al, 2020), \ or S2ORC.\ " _CITATION = "" class pes2o(datasets.GeneratorBasedBuilder): """Pretraining Efficiently on S2ORC!""" BUILDER_CONFIGS = [datasets.BuilderConfig(name) for name in _VARIANTS] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "added": datasets.Value("string"), "created": datasets.Value("string"), "id": datasets.Value("string"), "source": datasets.Value("string"), "text": datasets.Value("string"), "version": datasets.Value("string") } ), supervised_keys=None, homepage=_URL, citation=_CITATION, ) def _split_generators(self, dl_manager): data_urls = {} for split in ["train", "validation"]: n_shards = _N_SHARDS_PER_SPLIT[self.config.name][split] data_urls[split] = [ _DATA_URL.format( name=self.config.name, split=split, subset=subset, index=index ) for subset, n_shards in n_shards.items() for index in range(n_shards) ] train_downloaded_files = dl_manager.download( data_urls["train"] ) validation_downloaded_files = dl_manager.download( data_urls["validation"] ) return [ datasets.SplitGenerator( name=str(datasets.Split.TRAIN), gen_kwargs={ "filepaths": train_downloaded_files }), datasets.SplitGenerator( name=str(datasets.Split.VALIDATION), gen_kwargs={ "filepaths": validation_downloaded_files } ), ] def _generate_examples(self, filepaths): """This function returns the examples in the raw (text) form by iterating on all the files.""" id_ = 0 for filepath in filepaths: logger.info("generating examples from = %s", filepath) with gzip.open(open(filepath, "rb"), "rt", encoding="utf-8") as f: for line in f: if line: example = json.loads(line) yield id_, example id_ += 1