| import os |
| from collections import OrderedDict |
| from pathlib import Path |
| import datasets |
| import glob |
| import os |
|
|
| from datasets.arrow_dataset import config |
|
|
|
|
| class Gigaspeech2Config(datasets.BuilderConfig): |
| """BuilderConfig for Yodas.""" |
|
|
| def __init__(self, config_name, version, **kwargs): |
| config_split = config_name.split("-") |
| self.language = config_split[0] |
| print(config_split) |
| self.split = config_split[1] |
| self.index = config_split[2] if len(config_split)>=3 else None |
| self.base_data_path = f"data/{self.language}" |
|
|
| description = ( |
| f"Youtube speech to text dataset in {self.language}." |
| ) |
| super(Gigaspeech2Config, self).__init__( |
| name=config_name, |
| version=datasets.Version(version), |
| description=description, |
| **kwargs, |
| ) |
|
|
|
|
| DEFAULT_CONFIG_NAME = "all" |
| LANGS = ['id', 'th', 'vi'] |
| LANGS_SPLITS={ |
| "id":2, |
| "th":2, |
| "vi":2 |
| } |
| VERSION = "1.0.1" |
| CONFIG_NAME=[] |
| for lang in LANGS: |
| CONFIG_NAME.append(f"{lang}-test") |
| CONFIG_NAME.append(f"{lang}-dev") |
| CONFIG_NAME.append(f"{lang}-train_raw") |
| CONFIG_NAME.append(f"{lang}-train_refine") |
| for i in range(LANGS_SPLITS[lang]): |
| CONFIG_NAME.append(f"{lang}-train_raw-{i}") |
| CONFIG_NAME.append(f"{lang}-train_refine-{i}") |
|
|
| class Gigaspeech2(datasets.GeneratorBasedBuilder): |
| """Yodas dataset.""" |
|
|
| BUILDER_CONFIGS = [ |
| Gigaspeech2Config(config_name, version=VERSION) for config_name in CONFIG_NAME |
| ] |
|
|
| VERSION = datasets.Version("1.0.1") |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description="Gigaspeech2", |
| features=datasets.Features( |
| OrderedDict( |
| [ |
| |
| |
| ("audio", datasets.Audio(sampling_rate=16_000)), |
| ("text", datasets.Value("string")), |
| ] |
| ) |
| ), |
| supervised_keys=None, |
| homepage="", |
| citation="", |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| """Returns SplitGenerators.""" |
| |
| lang = self.config.language |
| split = self.config.split |
| index = self.config.index |
| if index is not None: |
| assert int(index)<LANGS_SPLITS[lang], f"split {self.config.name} not found" |
|
|
| text_file = dl_manager.download([f"{self.config.base_data_path}/{split}.tsv"]) |
| if split == "test": |
| audio_tar_files = dl_manager.download([f"{self.config.base_data_path}/{split}.tar.gz"]) |
| elif split == "dev": |
| audio_tar_files = dl_manager.download([f"{self.config.base_data_path}/{split}.tar.gz"]) |
| elif index is not None: |
| audio_tar_files = dl_manager.download([f"{self.config.base_data_path}/train/{index}.tar.gz"]) |
| else: |
| audio_tar_files = dl_manager.download([f"{self.config.base_data_path}/train/{i}.tar.gz" for i in range(LANGS_SPLITS[lang])]) |
|
|
| if dl_manager.is_streaming: |
| audio_archives = [dl_manager.iter_archive(audio_tar_file) for audio_tar_file in audio_tar_files] |
|
|
| else: |
| print("extracting audio ...") |
| extracted_audio_archives = dl_manager.extract(audio_tar_files) |
|
|
| audio_archives = [] |
| for extracted_dir in extracted_audio_archives: |
| audio_archives.extend(glob.glob("{}/**/*.wav".format(str(extracted_dir)), recursive=True)) |
|
|
| if split=="test": |
| dataset_split = datasets.Split.TEST |
| elif split=="dev": |
| dataset_split = datasets.Split.VALIDATION |
| else: |
| dataset_split = datasets.Split.TRAIN |
|
|
| return [ |
| datasets.SplitGenerator( |
| name=dataset_split, |
| gen_kwargs={ |
| "is_streaming": dl_manager.is_streaming, |
| "audio_archives": audio_archives, |
| 'tsv_file': text_file[0], |
| }, |
| ), |
| ] |
|
|
| def _generate_examples(self, is_streaming, audio_archives, tsv_file): |
| """Yields examples.""" |
|
|
| id_ = 0 |
|
|
| with open(tsv_file, 'r') as f: |
| lines = f.read().splitlines() |
| lines = [line.split("\t") for line in lines] |
| id2label={line[0]:line[1] for line in lines} |
|
|
| if is_streaming: |
| for tar_file in audio_archives: |
| for path, audio_f in tar_file: |
| path = Path(path) |
| utt_id = path.stem |
| if utt_id not in id2label: |
| continue |
| result = { |
| 'id': id_, |
| 'utt_id': utt_id, |
| 'audio': {"path": None, "bytes": audio_f.read()}, |
| 'text': id2label[utt_id], |
| } |
|
|
| yield id_, result |
| id_ += 1 |
| else: |
| for audio_file in audio_archives: |
| utt_id = Path(audio_file).stem |
| if utt_id not in id2label: |
| continue |
| result = { |
| 'id': id_, |
| 'utt_id': utt_id, |
| 'audio': {"path": audio_file, "bytes": open(audio_file, 'rb').read()}, |
| 'text': id2label[utt_id] |
| } |
|
|
| yield id_, result |
| id_ += 1 |
|
|
|
|
|
|