File size: 1,565 Bytes
0a3f17c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import DatasetBuilder, DatasetInfo, SplitGenerator, Split

class CustomJSONLDataset(DatasetBuilder):
    VERSION = "1.0.0"

    def _info(self) -> DatasetInfo:
        return DatasetInfo(
            description="Custom dataset from local JSONL files.",
            features={"text": "string"},
            supervised_keys=None,
        )

    def _split_generators(self, dl_manager):
        urls = [f"output_json_{i}.jsonl" for i in range(2, 3)]
        
        # The download_and_extract function can handle multiple files and will 
        # download them in parallel if possible.
        paths = dl_manager.download_and_extract(urls)

        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={"filepaths": paths},
            ),
        ]

    def _generate_examples(self, filepaths):
        """Yields examples from the dataset."""
        import json

        for filepath in filepaths:
            with open(filepath, "r", encoding="utf-8") as f:
                for idx, line in enumerate(f):
                    data = json.loads(line.strip())
                    yield idx, {"text": data["text"]}

# The following lines are useful for testing locally, to ensure the script works as expected.
# They're not necessary when the script is integrated into a HuggingFace Datasets repository.
if __name__ == "__main__":
    from datasets import load_dataset

    # This will use the custom dataset loading script.
    dataset = load_dataset("path_to_this_script.py")
    print(dataset["train"][0])