jsonlsingle2 / dataset.py
jerryjalapeno's picture
Create dataset.py
0a3f17c
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])