Datasets:
Commit
·
ea2779f
1
Parent(s):
3595f8b
Support streaming (#3)
Browse files- Use iter_archive for TAR file (5877ce5051fbbf4f7e9a67f8b2445ab6bde9e0fb)
- the_pile_openwebtext2.py +30 -30
the_pile_openwebtext2.py
CHANGED
@@ -14,11 +14,8 @@
|
|
14 |
# limitations under the License.
|
15 |
"""The OpenWebText2 Corpus"""
|
16 |
|
17 |
-
|
18 |
-
import glob
|
19 |
import io
|
20 |
import json
|
21 |
-
import os
|
22 |
|
23 |
import zstandard
|
24 |
|
@@ -70,18 +67,22 @@ class Openwebtext2(datasets.GeneratorBasedBuilder):
|
|
70 |
)
|
71 |
|
72 |
def _split_generators(self, dl_manager):
|
73 |
-
|
74 |
-
files = glob.glob(os.path.join(dl_dir, "*jsonl.zst"))
|
75 |
return [
|
76 |
-
datasets.SplitGenerator(
|
|
|
|
|
|
|
77 |
]
|
78 |
|
79 |
def _generate_examples(self, files):
|
80 |
"""Yields examples."""
|
81 |
_id = 0
|
82 |
-
for
|
|
|
|
|
83 |
reader = Reader()
|
84 |
-
for document, metadata in reader.read_jsonl(
|
85 |
yield _id, {
|
86 |
"title": metadata["title"],
|
87 |
"text": document,
|
@@ -95,25 +96,24 @@ class Reader:
|
|
95 |
def __init__(self):
|
96 |
pass
|
97 |
|
98 |
-
def read_jsonl(self,
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
if
|
117 |
-
|
118 |
-
|
119 |
-
yield text
|
|
|
14 |
# limitations under the License.
|
15 |
"""The OpenWebText2 Corpus"""
|
16 |
|
|
|
|
|
17 |
import io
|
18 |
import json
|
|
|
19 |
|
20 |
import zstandard
|
21 |
|
|
|
67 |
)
|
68 |
|
69 |
def _split_generators(self, dl_manager):
|
70 |
+
archive = dl_manager.download(_URL)
|
|
|
71 |
return [
|
72 |
+
datasets.SplitGenerator(
|
73 |
+
name=datasets.Split.TRAIN,
|
74 |
+
gen_kwargs={"files": dl_manager.iter_archive(archive)},
|
75 |
+
),
|
76 |
]
|
77 |
|
78 |
def _generate_examples(self, files):
|
79 |
"""Yields examples."""
|
80 |
_id = 0
|
81 |
+
for path, file in files:
|
82 |
+
if not path.endswith(".jsonl.zst"):
|
83 |
+
continue
|
84 |
reader = Reader()
|
85 |
+
for document, metadata in reader.read_jsonl(file, get_meta=True):
|
86 |
yield _id, {
|
87 |
"title": metadata["title"],
|
88 |
"text": document,
|
|
|
96 |
def __init__(self):
|
97 |
pass
|
98 |
|
99 |
+
def read_jsonl(self, fh, get_meta=False, autojoin_paragraphs=True, para_joiner="\n\n"):
|
100 |
+
self.fh = fh
|
101 |
+
cctx = zstandard.ZstdDecompressor()
|
102 |
+
reader = io.BufferedReader(cctx.stream_reader(fh))
|
103 |
+
for line in reader:
|
104 |
+
ob = json.loads(line)
|
105 |
+
# naive jsonl where each object is just the string itself, with no meta. For legacy compatibility.
|
106 |
+
if isinstance(ob, str):
|
107 |
+
assert not get_meta
|
108 |
+
yield ob
|
109 |
+
continue
|
110 |
+
|
111 |
+
text = ob["text"]
|
112 |
+
|
113 |
+
if autojoin_paragraphs and isinstance(text, list):
|
114 |
+
text = para_joiner.join(text)
|
115 |
+
|
116 |
+
if get_meta:
|
117 |
+
yield text, (ob["meta"] if "meta" in ob else {})
|
118 |
+
else:
|
119 |
+
yield text
|
|