my_uspto / my_upsto.py
jordiclive's picture
Upload folder using huggingface_hub
a961415
raw
history blame
3.17 kB
from datasets import DatasetBuilder, GeneratorBasedBuilder, SplitGenerator, Features, Value
import json
import datasets
import os
from glob import glob
import zstandard as zstd
from datasets.utils import Version
import io
class MyUSPTODataset(GeneratorBasedBuilder):
VERSION = Version("1.0.0")
def _info(self):
return datasets.DatasetInfo(
description="USPTO Patents Dataset",
features=datasets.Features(
{
"text": datasets.Value("string"),
}
),
supervised_keys=None,
)
def _split_generators(self, dl_manager):
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data_dir": "train",
"split": None,
},
),
datasets.SplitGenerator(
name="validation_pile",
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data_dir": "val",
"split": "pile",
},
),
datasets.SplitGenerator(
name="validation_domain",
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data_dir": "val",
"split": "domain",
},
),
datasets.SplitGenerator(
name="test_pile",
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data_dir": "test_pile",
"split": "pile"
},
),
datasets.SplitGenerator(
name="test_domain",
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data_dir": "test_domain",
"split": "domain"
},
),
]
def _generate_examples(self, data_dir, split):
dctx = zstd.ZstdDecompressor()
idx = -1
file_paths = glob(os.path.join(data_dir, f"*.jsonl.zst"))
if split is not None:
file_paths = [f for f in file_paths if split in f]
for file in file_paths:
with open(file, "rb") as f:
reader = dctx.stream_reader(f)
buffer = io.BufferedReader(reader)
for _, line in enumerate(buffer.readlines()):
data = json.loads(line)
idx += 1
yield idx, data
# if __name__ == "__main__":
# import os
# import shutil
# import glob
# cache_directory = "/Users/jordanclive/.cache/huggingface/datasets"
#
# dataset = MyUSPTODataset()
#
# #
# dataset = MyUSPTODataset()
# dataset.download_and_prepare()
# dataset = dataset.as_dataset()
# # # x = 1
# # print(dataset)
# # print(dataset["train"][0])
# # print(dataset["test"][0])
# # print(dataset["validation"][0])