Support streaming
#3
by
albertvillanova
HF Staff
- opened
- the_pile_stack_exchange.py +52 -17
the_pile_stack_exchange.py
CHANGED
|
@@ -13,8 +13,9 @@
|
|
| 13 |
# See the License for the specific language governing permissions and
|
| 14 |
# limitations under the License.
|
| 15 |
"""The Stack Exchange Corpus"""
|
| 16 |
-
|
| 17 |
import os
|
|
|
|
| 18 |
from pathlib import Path
|
| 19 |
|
| 20 |
import datasets
|
|
@@ -58,24 +59,58 @@ class ThePileStackExchange(datasets.GeneratorBasedBuilder):
|
|
| 58 |
)
|
| 59 |
|
| 60 |
def _split_generators(self, dl_manager):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
def _generate_examples(self,
|
| 71 |
"""Yields examples."""
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
document = f.read()
|
| 80 |
yield _id, {"domain": domain, "text": document}
|
| 81 |
_id += 1
|
|
|
|
| 13 |
# See the License for the specific language governing permissions and
|
| 14 |
# limitations under the License.
|
| 15 |
"""The Stack Exchange Corpus"""
|
| 16 |
+
import io
|
| 17 |
import os
|
| 18 |
+
import zipfile
|
| 19 |
from pathlib import Path
|
| 20 |
|
| 21 |
import datasets
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
def _split_generators(self, dl_manager):
|
| 62 |
+
if dl_manager.is_streaming:
|
| 63 |
+
archive = dl_manager.download(_URL)
|
| 64 |
+
return [
|
| 65 |
+
datasets.SplitGenerator(
|
| 66 |
+
name=datasets.Split.TRAIN,
|
| 67 |
+
gen_kwargs={
|
| 68 |
+
"files": dl_manager.iter_archive(archive),
|
| 69 |
+
"is_streaming": dl_manager.is_streaming,
|
| 70 |
+
},
|
| 71 |
+
),
|
| 72 |
+
]
|
| 73 |
+
else:
|
| 74 |
+
dl_dir = dl_manager.download_and_extract(_URL)
|
| 75 |
+
zips = [str(f) for f in (Path(dl_dir) / "out").iterdir()]
|
| 76 |
+
extracted = dl_manager.extract(zips, num_proc=os.cpu_count())
|
| 77 |
+
# non-dir extracteds are zero-size unknown things
|
| 78 |
+
dirs = [path for path in extracted if os.path.isdir(path)]
|
| 79 |
+
return [
|
| 80 |
+
datasets.SplitGenerator(
|
| 81 |
+
name=datasets.Split.TRAIN,
|
| 82 |
+
gen_kwargs={
|
| 83 |
+
"files": dl_manager.iter_files(dirs),
|
| 84 |
+
"is_streaming": dl_manager.is_streaming,
|
| 85 |
+
},
|
| 86 |
+
),
|
| 87 |
+
]
|
| 88 |
|
| 89 |
+
def _generate_examples(self, files, is_streaming):
|
| 90 |
"""Yields examples."""
|
| 91 |
+
if is_streaming:
|
| 92 |
+
_id = 0
|
| 93 |
+
for path, file in files:
|
| 94 |
+
if not path.startswith("out/"):
|
| 95 |
+
continue
|
| 96 |
+
file_content = file.read()
|
| 97 |
+
with zipfile.ZipFile(io.BytesIO(file_content)) as zip_file:
|
| 98 |
+
for name in zip_file.namelist():
|
| 99 |
+
if not name.endswith(".txt"):
|
| 100 |
+
continue
|
| 101 |
+
domain = name.split(".")[0]
|
| 102 |
+
with zip_file.open(name, mode="r") as f:
|
| 103 |
+
document = f.read().decode(encoding="utf-8")
|
| 104 |
+
yield _id, {"domain": domain, "text": document}
|
| 105 |
+
_id += 1
|
| 106 |
+
else:
|
| 107 |
+
_id = 0
|
| 108 |
+
for file in files:
|
| 109 |
+
path = Path(file)
|
| 110 |
+
if not path.name.endswith(".txt"):
|
| 111 |
+
continue
|
| 112 |
+
domain = path.name.split(".")[0]
|
| 113 |
+
with path.open(mode="r", encoding="utf-8") as f:
|
| 114 |
document = f.read()
|
| 115 |
yield _id, {"domain": domain, "text": document}
|
| 116 |
_id += 1
|