feat(loader): updated last batch folder pointer in the index, added loading script
Browse files
batch.py
ADDED
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from pandas import read_csv
|
3 |
+
|
4 |
+
from datasets import GeneratorBasedBuilder, Value, Version, BuilderConfig, Features, DatasetInfo, SplitGenerator, Split, Audio, Sequence
|
5 |
+
|
6 |
+
_DESCRIPTION = '''
|
7 |
+
The dataset contains threads parsed from the /b/ board of 2ch archive
|
8 |
+
'''
|
9 |
+
|
10 |
+
_HOMEPAGE = 'https://huggingface.co/datasets/zeio/batch'
|
11 |
+
|
12 |
+
_LICENSE = 'Apache License Version 2.0'
|
13 |
+
|
14 |
+
_CLUSTER = '{first_page:04d}-{last_page:04d}'
|
15 |
+
_URLS = {
|
16 |
+
'written': 'https://huggingface.co/datasets/zeio/batch/resolve/main/threads-compressed/{cluster}.tar.xz',
|
17 |
+
'spoken': 'https://huggingface.co/datasets/zeio/batch-speech/raw/main/threads-compressed/{cluster}.tar.xz'
|
18 |
+
}
|
19 |
+
_INDEX = 'https://huggingface.co/datasets/zeio/batch/resolve/main/index.tsv'
|
20 |
+
|
21 |
+
_N_ITEMS = 1750
|
22 |
+
_N_BATCH = 20
|
23 |
+
|
24 |
+
|
25 |
+
class Batch(GeneratorBasedBuilder):
|
26 |
+
|
27 |
+
VERSION = Version('06.11.2023')
|
28 |
+
|
29 |
+
BUILDER_CONFIGS = [
|
30 |
+
BuilderConfig(
|
31 |
+
name = 'written',
|
32 |
+
version = VERSION,
|
33 |
+
description = 'The base modification which contains only text representation of threads, which are divided into topics, which in turn are made of posts'
|
34 |
+
),
|
35 |
+
BuilderConfig(
|
36 |
+
name = 'spoken',
|
37 |
+
version = VERSION,
|
38 |
+
description = (
|
39 |
+
'An extended configuration of the dataset in which besides text some threads have an associated audio data with speech '
|
40 |
+
'generated for text in the respective thread using an alternating speaker pattern'
|
41 |
+
)
|
42 |
+
)
|
43 |
+
]
|
44 |
+
|
45 |
+
DEFAULT_CONFIG_NAME = 'written'
|
46 |
+
|
47 |
+
def _info(self):
|
48 |
+
if self.config.name == 'written':
|
49 |
+
features = Features({
|
50 |
+
'title': Value('string'),
|
51 |
+
'topics': Sequence({
|
52 |
+
'posts': Sequence({
|
53 |
+
'text': Value('string')
|
54 |
+
})
|
55 |
+
})
|
56 |
+
})
|
57 |
+
elif self.config.name == 'spoken':
|
58 |
+
features = Features({
|
59 |
+
'title': Value('string'),
|
60 |
+
'speech': Audio(sampling_rate = 48_000),
|
61 |
+
'topics': Sequence({
|
62 |
+
'posts': Sequence({
|
63 |
+
'text': Value('string')
|
64 |
+
})
|
65 |
+
})
|
66 |
+
})
|
67 |
+
else:
|
68 |
+
raise ValueError(f'Unknown config: {self.config.name}')
|
69 |
+
|
70 |
+
return DatasetInfo(
|
71 |
+
description=_DESCRIPTION,
|
72 |
+
features = features,
|
73 |
+
homepage=_HOMEPAGE,
|
74 |
+
license=_LICENSE
|
75 |
+
)
|
76 |
+
|
77 |
+
def _split_generators(self, dl_manager):
|
78 |
+
name = self.config.name
|
79 |
+
|
80 |
+
url = _URLS['written']
|
81 |
+
spoken_url = _URLS['spoken'] if name == 'spoken' else None
|
82 |
+
|
83 |
+
offset = 0
|
84 |
+
|
85 |
+
written = {}
|
86 |
+
spoken = None if spoken_url is None else {}
|
87 |
+
|
88 |
+
while offset < _N_ITEMS:
|
89 |
+
cluster = _CLUSTER.format(first_page = offset, last_page = (offset := min(offset + _N_BATCH - 1, _N_ITEMS)))
|
90 |
+
written[f'threads/{cluster}'] = dl_manager.download_and_extract(url.format(cluster = cluster))
|
91 |
+
if spoken is not None:
|
92 |
+
try:
|
93 |
+
spoken[f'threads/{cluster}'] = dl_manager.download_and_extract(spoken_url.format(cluster = cluster))
|
94 |
+
except: # speech for some clusters may be missing
|
95 |
+
pass
|
96 |
+
|
97 |
+
index = dl_manager.download_and_extract(_INDEX)
|
98 |
+
|
99 |
+
# print(clusters)
|
100 |
+
# print(index)
|
101 |
+
|
102 |
+
return [
|
103 |
+
SplitGenerator(
|
104 |
+
name = Split.TRAIN,
|
105 |
+
gen_kwargs = {
|
106 |
+
'written': written,
|
107 |
+
'spoken': spoken,
|
108 |
+
'index': index
|
109 |
+
}
|
110 |
+
)
|
111 |
+
]
|
112 |
+
|
113 |
+
def _generate_examples(self, written: dict, index: str, spoken: dict = None):
|
114 |
+
for i, row in read_csv(index, sep = '\t').iterrows():
|
115 |
+
# print(row)
|
116 |
+
|
117 |
+
path = os.path.join(written[row['path']], f'{row["thread"]}.txt')
|
118 |
+
|
119 |
+
topics = []
|
120 |
+
posts = []
|
121 |
+
|
122 |
+
# def append_topic():
|
123 |
+
# nonlocal posts, topics
|
124 |
+
|
125 |
+
# if len(posts) > 0:
|
126 |
+
# topics.append({'posts': posts})
|
127 |
+
# posts = []
|
128 |
+
|
129 |
+
with open(path, 'r', encoding = 'utf-8') as file:
|
130 |
+
for line in file.read().split('\n'):
|
131 |
+
if line:
|
132 |
+
posts.append({'text': line})
|
133 |
+
# else:
|
134 |
+
# append_topic()
|
135 |
+
elif len(posts) > 0:
|
136 |
+
topics.append({'posts': posts})
|
137 |
+
posts = []
|
138 |
+
|
139 |
+
# append_topic()
|
140 |
+
|
141 |
+
item = {
|
142 |
+
'title': row['title'],
|
143 |
+
'topics': topics
|
144 |
+
}
|
145 |
+
|
146 |
+
if spoken is not None:
|
147 |
+
speech_cluster_path = spoken.get(row['path'])
|
148 |
+
|
149 |
+
if speech_cluster_path is not None:
|
150 |
+
speech_file_path = os.path.join(speech_cluster_path, f'{row["thread"]}.mp3')
|
151 |
+
|
152 |
+
if os.path.isfile(speech_file_path):
|
153 |
+
item['speech'] = speech_file_path
|
154 |
+
|
155 |
+
yield i, item
|
156 |
+
|
157 |
+
# if sound is None:
|
158 |
+
# yield i, dict(row)
|
159 |
+
# else:
|
160 |
+
# data = dict(row)
|
161 |
+
|
162 |
+
# folder = data['folder']
|
163 |
+
# filename = data['filename']
|
164 |
+
|
165 |
+
# if folder == folder and filename == filename: # if folder and filename are not nan
|
166 |
+
# data['sound'] = os.path.join(sound, folder, f'{filename}.ogg')
|
167 |
+
# else:
|
168 |
+
# data['sound'] = NA
|
169 |
+
|
170 |
+
# data.pop('folder')
|
171 |
+
# data.pop('filename')
|
172 |
+
|
173 |
+
# yield i, data
|
index.tsv
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 670682159
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3cd48ee7b5f3a51dc02bc64a248c72d6198aa7a0244ab72597829d2d4cd37342
|
3 |
size 670682159
|