"""Raw RedPajamas Custom Loading Script""" import json import datasets from huggingface_hub import HfFileSystem _CITATION = """""" _DESCRIPTION = """""" _HOMEPAGE = "" _LICENSE = "" class RawRedPajamas(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.1.0") BUILDER_CONFIGS = [ datasets.BuilderConfig( name="contents_only", version=VERSION, description="This returns the content field only." ), ] DEFAULT_CONFIG_NAME = "contents_only" # It's not mandatory to have a default configuration. Just use one if it make sense. def _info(self): features = datasets.Features({"content": datasets.Value("string")}) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): fs = HfFileSystem(token=self.token) files = [file.split(f"/{self.repo_id}/")[-1] for file in fs.glob(f"datasets/{self.repo_id}/data/*.json")] downloaded_files = dl_manager.download_and_extract(files) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepaths": downloaded_files, }, ), ] # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` def _generate_examples(self, filepaths): for filepath in filepaths: with open(filepath, encoding="utf-8") as f: for key, row in enumerate(f): data = json.loads(row) content = data["raw_content"] yield key, { "content": content.strip().rstrip().lstrip(), }