File size: 5,014 Bytes
a533b56 |
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 |
import os
from collections import OrderedDict
from pathlib import Path
import datasets
import os
from .meta import lang2shard_cnt
class YodasConfig(datasets.BuilderConfig):
"""BuilderConfig for Yodas."""
def __init__(self, lang, version, **kwargs):
self.language = lang
self.base_data_path = f"data/{lang}"
description = (
f"Youtube speech to text dataset in {self.language}."
)
super(YodasConfig, self).__init__(
name=lang,
version=datasets.Version(version),
description=description,
**kwargs,
)
DEFAULT_CONFIG_NAME = "all"
LANGS = list(lang2shard_cnt.keys())
VERSION = "1.0.0"
class Yodas(datasets.GeneratorBasedBuilder):
"""Yodas dataset."""
BUILDER_CONFIGS = [
YodasConfig(lang, version=VERSION) for lang in LANGS
]
VERSION = datasets.Version("1.0.0")
def _info(self):
return datasets.DatasetInfo(
description="Yodas",
features=datasets.Features(
OrderedDict(
[
("id", datasets.Value("string")),
("utt_id", datasets.Value("string")),
("audio", datasets.Audio(sampling_rate=16_000)),
("text", datasets.Value("string")),
]
)
),
supervised_keys=None,
homepage="", # TODO
citation="", # TODO
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
# TODO
total_cnt = lang2shard_cnt[self.config.name]
idx_lst = [f"{i:08d}" for i in range(total_cnt)]
audio_tar_files = dl_manager.download([f"{self.config.base_data_path}/audio/{i:08d}.tar.gz" for i in range(total_cnt)])
text_files = dl_manager.download([f"{self.config.base_data_path}/text/{i:08d}.txt" for i in range(total_cnt)])
#duration_files = dl_manager.download([f"{self.config.base_data_path}/duration/{i:08d}.txt" for i in range(total_cnt)])
if dl_manager.is_streaming:
audio_archives = [dl_manager.iter_archive(audio_tar_file) for audio_tar_file in audio_tar_files]
text_archives = [dl_manager.extract(text_file) for text_file in text_files]
else:
print("extracting audio ...")
extracted_audio_archives = dl_manager.extract(audio_tar_files)
audio_archives = []
text_archives = []
for idx, audio_tar_file, extracted_dir, text_file in zip(idx_lst, audio_tar_files, extracted_audio_archives, text_files):
audio_archives.append(str(extracted_dir)+'/'+idx)
text_archives.append(text_file)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"is_streaming": dl_manager.is_streaming,
"audio_archives": audio_archives,
'text_archives': text_archives,
},
),
]
def _generate_examples(self, is_streaming, audio_archives, text_archives):
"""Yields examples."""
id_ = 0
if is_streaming:
for tar_file, text_file in zip(audio_archives, text_archives):
utt2text = {}
with open(text_file) as f:
for id_, row in enumerate(f):
row = row.strip().split(maxsplit=1)
utt2text[row[0]] = row[1]
for path, audio_f in tar_file:
path = Path(path)
utt_id = path.stem
if utt_id in utt2text:
result = {
'id': id_,
'utt_id': utt_id,
'audio': {"path": None, "bytes": audio_f.read()},
'text': utt2text[utt_id]
}
yield id_, result
id_ += 1
else:
for extracted_dir, text_file in zip(audio_archives, text_archives):
utt2text = {}
print(extracted_dir)
with open(text_file) as f:
for _, row in enumerate(f):
row = row.strip().split(maxsplit=1)
utt2text[row[0]] = row[1]
for audio_file in list(Path(extracted_dir).glob('*')):
utt_id = audio_file.stem
if utt_id in utt2text:
result = {
'id': id_,
'utt_id': utt_id,
'audio': {"path": str(audio_file.absolute()), "bytes": open(audio_file, 'rb').read()},
'text': utt2text[utt_id]
}
yield id_, result
id_ += 1
|