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