forked from saprmarks/dictionary_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
27 lines (23 loc) · 790 Bytes
/
utils.py
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
from datasets import load_dataset
import zstandard as zstd
import io
import json
def hf_dataset_to_generator(dataset_name, split='train', streaming=True):
dataset = load_dataset(dataset_name, split=split, streaming=streaming)
def gen():
for x in iter(dataset):
yield x['text']
return gen()
def zst_to_generator(data_path):
"""
Load a dataset from a .jsonl.zst file.
The jsonl entries is assumed to have a 'text' field
"""
compressed_file = open(data_path, 'rb')
dctx = zstd.ZstdDecompressor()
reader = dctx.stream_reader(compressed_file)
text_stream = io.TextIOWrapper(reader, encoding='utf-8')
def generator():
for line in text_stream:
yield json.loads(line)['text']
return generator()