# coding=utf-8 # Copyright 2021 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Magic Hub Dataset""" import os import datasets from datasets.tasks import AutomaticSpeechRecognition _DATA_URL = "./fil_tsv.tar.gz" #https://huggingface.co/datasets/Khalsuu/filipino_dataset_script/resolve/main/fil.zip # _CITATION = """\ # @inproceedings{commonvoice:2020, # author = {Ardila, R. and Branson, M. and Davis, K. and Henretty, M. and Kohler, M. and Meyer, J. and Morais, R. and Saunders, L. and Tyers, F. M. and Weber, G.}, # title = {Common Voice: A Massively-Multilingual Speech Corpus}, # booktitle = {Proceedings of the 12th Conference on Language Resources and Evaluation (LREC 2020)}, # pages = {4211--4215}, # year = 2020 # } # """ _DESCRIPTION = """\ Magic Hub's initiative to help teach machines how real people speak. They wanted to provide structured data that will help enthusiasts and researchers to spend more time on training models rather than cleaning and structuring data. """ _HOMEPAGE = "https://magichub.com/datasets/filipino-scripted-speech-corpus-daily-use-sentence/" _LICENSE = "https://magichub.com/magic-data-open-source-license/" _LANGUAGES = { "fil": { "Language": "Filipino", "Date": "2022-4-20", "Size": "414 MB", "Version": "ab_1h_2020-12-11", "Validated_Hr_Total": 4.58, "Overall_Hr_Total": 4.58, "Number_Of_Voice": 10, }, } class FilipinoVoiceConfig(datasets.BuilderConfig): """BuilderConfig for Filipino Speech.""" def __init__(self, name, sub_version, **kwargs): """ Args: data_dir: `string`, the path to the folder containing the files in the downloaded .tar citation: `string`, citation for the data set url: `string`, url for information about the data set **kwargs: keyword arguments forwarded to super. """ self.sub_version = sub_version self.language = kwargs.pop("language", None) self.date_of_snapshot = kwargs.pop("date", None) self.size = kwargs.pop("size", None) self.validated_hr_total = kwargs.pop("val_hrs", None) self.total_hr_total = kwargs.pop("total_hrs", None) self.num_of_voice = kwargs.pop("num_of_voice", None) description = f"Magic Hub speech to text dataset in {self.language} version {self.sub_version} of {self.date_of_snapshot}. The dataset comprises {self.validated_hr_total} of validated transcribed speech data from {self.num_of_voice} speakers. The dataset has a size of {self.size}" super(FilipinoVoiceConfig, self).__init__( name=name, version=datasets.Version("1.0.0", ""), description=description, **kwargs ) class FilipinoVoice(datasets.GeneratorBasedBuilder): DEFAULT_WRITER_BATCH_SIZE = 1000 BUILDER_CONFIGS = [ FilipinoVoiceConfig( name=lang_id, language=_LANGUAGES[lang_id]["Language"], sub_version=_LANGUAGES[lang_id]["Version"], date=_LANGUAGES[lang_id]["Date"], size=_LANGUAGES[lang_id]["Size"], val_hrs=_LANGUAGES[lang_id]["Validated_Hr_Total"], total_hrs=_LANGUAGES[lang_id]["Overall_Hr_Total"], num_of_voice=_LANGUAGES[lang_id]["Number_Of_Voice"], ) for lang_id in _LANGUAGES.keys() ] def _info(self): features = datasets.Features( { "CHANNEL": datasets.Value("string"), "UTTRANS_ID": datasets.Value("string"), "SPEAKER_ID": datasets.Value("string"), "PROMPT": datasets.Value("string"), "TRANSCRIPTION": datasets.Value("string"), "audio": datasets.Audio(sampling_rate=16_000), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, supervised_keys=None, homepage=_HOMEPAGE, license=_LICENSE, # citation=_CITATION, task_templates=[AutomaticSpeechRecognition(task="automatic-speech-recognition", transcription_column="sentence")], ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # Download the TAR archive that contains the audio files: archive_path = dl_manager.download(_DATA_URL.format(self.config.name)) # First we locate the data using the path within the archive: # path_to_data = "/".join(["cv-corpus-6.1-2020-12-11", self.config.name]) path_to_data = "fil" path_to_clips = "/".join([path_to_data, "clips"]) metadata_filepaths = { split: "/".join([path_to_data, f"{split}.txt"]) for split in ["train", "test"] } # print("Archive path:" + archive_path) # print("Path to clips:" + path_to_clips) # print(metadata_filepaths) # (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files: local_extracted_archive = dl_manager.extract(archive_path) # print("Local extracted archive:") # print(local_extracted_archive) # To access the audio data from the TAR archives using the download manager, # we have to use the dl_manager.iter_archive method. # # This is because dl_manager.download_and_extract # doesn't work to stream TAR archives in streaming mode. # (we have to stream the files of a TAR archive one by one) # # The iter_archive method returns an iterable of (path_within_archive, file_obj) for every # file in the TAR archive. return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "local_extracted_archive": local_extracted_archive, "archive_iterator": dl_manager.iter_archive( archive_path ), # use iter_archive here to access the files in the TAR archives "metadata_filepath": metadata_filepaths["train"], "path_to_clips": path_to_clips, }, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "local_extracted_archive": local_extracted_archive, "archive_iterator": dl_manager.iter_archive( archive_path ), # use iter_archive here to access the files in the TAR archives "metadata_filepath": metadata_filepaths["test"], "path_to_clips": path_to_clips, }, ), ] def _generate_examples(self, local_extracted_archive, archive_iterator, metadata_filepath, path_to_clips): """Yields examples.""" data_fields = list(self._info().features.keys()) # print(data_fields) # audio is not a header of the csv files data_fields.remove("audio") path_idx = data_fields.index("UTTRANS_ID") # print(path_idx) # print("Requested metadata: " + metadata_filepath) # print(archive_iterator) all_field_values = {} metadata_found = False # Here we iterate over all the files within the TAR archive: for path, f in archive_iterator: print(path, ", ", f) # Parse the metadata CSV file if path == metadata_filepath: metadata_found = True lines = f.readlines() headline = lines[0].decode("utf-8") column_names = headline.strip().split("\t") assert ( column_names == data_fields ), f"The file should have {data_fields} as column names, but has {column_names}" for line in lines[1:]: field_values = line.decode("utf-8").strip().split("\t") # set full path for mp3 audio file audio_path = "/".join([path_to_clips, field_values[path_idx]]) all_field_values[audio_path] = field_values # Else, read the audio file and yield an example elif path.startswith(path_to_clips): assert metadata_found, "Found audio clips before the metadata TSV file." if not all_field_values: break if path in all_field_values: # retrieve the metadata corresponding to this audio file field_values = all_field_values[path] # print("Found field values") # print(field_values) # if data is incomplete, fill with empty values if len(field_values) < len(data_fields): field_values += (len(data_fields) - len(field_values)) * ["''"] result = {key: value for key, value in zip(data_fields, field_values)} # set audio feature result["audio"] = {"path": path, "bytes": f.read()} # set path to None if the audio file doesn't exist locally (i.e. in streaming mode) result["UTTRANS_ID"] = os.path.join(local_extracted_archive, path) if local_extracted_archive else None yield path, result