CoolFace
Modelpublic

RASMUS/Finnish-ASR-Canary-v2

sourceHugging Facemitupdated 7mo agoView on Hugging Face
0likes1.2kdownloads
code_switching_manifest_creation.py178 linesDownload Raw Back to code_switching
1# Copyright (c) 2022, NVIDIA CORPORATION.  All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15import argparse16import logging17import os18import random19from nemo.collections.asr.parts.utils.manifest_utils import read_manifest, write_manifest20 21# Checks -22# (Recommendation) Please normalize the text for each language (avoid numbers, special characters, punctuation)23# Please ensure that the audio_filepaths are absolute locations24 25 26parser = argparse.ArgumentParser(description='Create synthetic code-switching data manifest from monolingual data')27 28parser.add_argument("--manifest_language1", default=None, type=str, help='Manifest file for language 1', required=True)29parser.add_argument("--manifest_language2", default=None, type=str, help='Manifest file for language 2', required=True)30parser.add_argument(31    "--manifest_save_path", default=None, type=str, help='Path to save created CS indermediate manifest', required=True32)33parser.add_argument(34    "--id_language1", default=None, type=str, help='Identifier for language 1, eg: en, es, hi', required=True35)36parser.add_argument(37    "--id_language2", default=None, type=str, help='Identifier for language 2, eg: en, es, hi', required=True38)39parser.add_argument("--max_sample_duration_sec", default=19, type=int, help='Maximum duration of sample (sec)')40parser.add_argument("--min_sample_duration_sec", default=16, type=int, help='Minimum duration of sample (sec)')41parser.add_argument("--dataset_size_required_hrs", default=1, type=int, help='Duration of dataset required (hrs)')42 43args = parser.parse_args()44 45 46def create_cs_manifest(47    data_lang_0: list,48    data_lang_1: list,49    lid_lang_0: str,50    lid_lang_1: str,51    max_sample_duration_sec: int,52    min_sample_duration_sec: int,53    data_requirement_hrs: int,54):55    """56    Args:57        data_lang_0: Manifest entries from first langauge58        data_lang_1: Manifest entries from second langauge59        lid_lang_0: Language ID marker for first langauge60        lid_lang_1: Language ID marker for second langauge61        max_sample_duration_sec: Maximum permissible duration of generated CS sample in sec62        min_sample_duration_sec: Minimum permissible duration of generated CS sample in sec63        data_requirement_hrs: Required size of generated corpus64 65    Returns:66        Created synthetic CS manifest as list67 68    """69 70    total_duration = 071    constructed_data = []72    sample_id = 073 74    num_samples_lang0 = len(data_lang_0)75    num_samples_lang1 = len(data_lang_1)76 77    while total_duration < (data_requirement_hrs * 3600):78 79        created_sample_duration_sec = 080        created_sample_dict = {}81        created_sample_dict['lang_ids'] = []82        created_sample_dict['texts'] = []83        created_sample_dict['paths'] = []84        created_sample_dict['durations'] = []85 86        while created_sample_duration_sec < min_sample_duration_sec:87 88            lang_selection = random.randint(0, 1)89 90            if lang_selection == 0:91                index = random.randint(0, num_samples_lang0 - 1)92                sample = data_lang_0[index]93                lang_id = lid_lang_094            else:95                index = random.randint(0, num_samples_lang1 - 1)96                sample = data_lang_1[index]97                lang_id = lid_lang_198 99            if (created_sample_duration_sec + sample['duration']) > max_sample_duration_sec:100                continue101            else:102                created_sample_duration_sec += sample['duration']103                created_sample_dict['lang_ids'].append(lang_id)104                created_sample_dict['texts'].append(sample['text'])105                created_sample_dict['paths'].append(sample['audio_filepath'])106                created_sample_dict['durations'].append(sample['duration'])107 108        created_sample_dict['total_duration'] = created_sample_duration_sec109 110        # adding a uid which will be used to save the generated audio file later111        created_sample_dict['uid'] = sample_id112        sample_id += 1113 114        constructed_data.append(created_sample_dict)115        total_duration += created_sample_duration_sec116 117    return constructed_data118 119 120def main():121 122    manifest0 = args.manifest_language1123    manifest1 = args.manifest_language2124    lid0 = args.id_language1125    lid1 = args.id_language2126    min_sample_duration = args.min_sample_duration_sec127    max_sample_duration = args.max_sample_duration_sec128    dataset_requirement = args.dataset_size_required_hrs129    manifest_save_path = args.manifest_save_path130 131    # Sanity Checks132    if (manifest0 is None) or (not os.path.exists(manifest0)):133        logging.error('Manifest for language 1 is incorrect')134        exit135 136    if (manifest1 is None) or (not os.path.exists(manifest1)):137        logging.error('Manifest for language 2 is incorrect')138        exit139 140    if lid0 is None:141        logging.error('Please provide correct language code for language 1')142        exit143 144    if lid1 is None:145        logging.error('Please provide correct language code for language 2')146        exit147 148    if manifest_save_path is None:149        logging.error('Please provide correct manifest save path')150        exit151 152    if min_sample_duration >= max_sample_duration:153        logging.error('Please ensure max_sample_duration > min_sample_duration')154        exit155 156    # Reading data157    logging.info('Reading manifests')158    data_language0 = read_manifest(manifest0)159    data_language1 = read_manifest(manifest1)160 161    # Creating the CS data Manifest162    logging.info('Creating CS manifest')163    constructed_data = create_cs_manifest(164        data_language0, data_language1, lid0, lid1, max_sample_duration, min_sample_duration, dataset_requirement165    )166 167    # Saving Manifest168    logging.info('saving manifest')169    write_manifest(manifest_save_path, constructed_data)170 171    print("Synthetic CS manifest saved at :", manifest_save_path)172 173    logging.info('Done!')174 175 176if __name__ == "__main__":177    main()178