Datasets:

ArXiv:
License:
yodas / yodas.py
xinjianl's picture
update loader to fix empty extraction issue
70db5c5 verified
raw
history blame
5.11 kB
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 = {}
extract_root_dir = Path(extracted_dir).parent
extracted_dir = list(extract_root_dir.glob('./*'))[0]
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