# coding=utf-8 # Copyright 2023 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. """ NENA Speech Dataset""" import csv import os import datasets from tqdm import tqdm from .dialects import DIALECTS from .release_stats import STATS _HOMEPAGE = "https://nena.ames.cam.ac.uk/" _LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/" _BASE_URL = "https://huggingface.co/datasets/mnazari/nena_speech_1_0_test/resolve/main/" _AUDIO_URL = _BASE_URL + "audio/{dialect}/{split}.tar" _TRANSCRIPT_URL = _BASE_URL + "transcript/{dialect}/{split}.tsv" import datasets class NENASpeechConfig(datasets.BuilderConfig): """BuilderConfig for NENASpeech.""" def __init__(self, name, version, **kwargs): self.dialect = kwargs.pop("dialect", None) self.release_date = kwargs.pop("release_date", None) self.speakers=kwargs.pop("speakers", None) self.total_examples=kwargs.pop("total_examples", None) self.total_translated=kwargs.pop("total_translated", None) self.total_labeled=kwargs.pop("total_labeled", None) self.total_unlabeled=kwargs.pop("total_unlabeled", None) description = ( f"NENA Speech dataset in the {self.dialect} dialect released on {self.release_date}. " f"The dataset currently consists of {self.total_unlabeled:.2f} minutes of unlabeled " f"speech, {self.total_labeled:.2f} of transcribed speech, and {self.total_examples} " f"multimodal translation examples. More examples are actively being crowdsourced." ) super(NENASpeechConfig, self).__init__( name=name, version=datasets.Version(version), description=description, **kwargs, ) class NENASpeech(datasets.GeneratorBasedBuilder): DEFAULT_WRITER_BATCH_SIZE = 1000 BUILDER_CONFIGS = [ NENASpeechConfig( name=dialect, version=STATS["version"], dialect=DIALECTS[dialect], release_date=STATS["date"], speakers=dialect_stats["speakers"], total_examples=dialect_stats["totalExamples"], total_translated=dialect_stats["examplesTranslated"], total_labeled=dialect_stats["durationLabeled"] / 60, total_unlabeled=dialect_stats["durationUnlabeled"] / 60, ) for dialect, dialect_stats in STATS["dialects"].items() ] def _info(self): total_dialects = len(STATS["dialects"]) total_examples = STATS["totalExamples"] total_labeled = STATS["durationLabeled"] / 60 total_unlabeled = STATS["durationUnlabeled"] / 60 description = ( "NENA Speech is a multimodal dataset to help teach machines how real people speak " "the Northeastern Neo-Aramaic dialects. The dataset currently consists of " f"{total_unlabeled:.2f} minutes of unlabeled speech, {total_labeled:.2f} minutes of " f"transcribed speech, and {total_examples} multimodal translation examples in " f"{total_dialects} dialects. More examples are actively being crowdsourced." ) features = datasets.Features( { "transcription": datasets.Value("string"), "translation": datasets.Value("string"), "audio": datasets.features.Audio(sampling_rate=48_000), "locale": datasets.Value("string"), "proficiency": datasets.Value("string"), "age": datasets.Value("string"), "crowdsourced": datasets.Value("bool"), "unlabeled": datasets.Value("bool"), "interrupted": datasets.Value("bool"), "client_id": datasets.Value("string"), "path": datasets.Value("string"), } ) return datasets.DatasetInfo( description=description, homepage=_HOMEPAGE, license=_LICENSE, features=features, supervised_keys=None, ) def _split_generators(self, dl_manager): dialect = self.config.name audio_urls = {} splits = ("train", "dev", "test") for split in splits: audio_urls[split] = _AUDIO_URL.format(dialect=dialect, split=split) archive_paths = dl_manager.download(audio_urls) local_extracted_archive_paths = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else {} meta_urls = {split: _TRANSCRIPT_URL.format(dialect=dialect, split=split) for split in splits} meta_paths = dl_manager.download_and_extract(meta_urls) split_generators = [] split_names = { "train": datasets.Split.TRAIN, "dev": datasets.Split.VALIDATION, "test": datasets.Split.TEST, } for split in splits: split_generators.append( datasets.SplitGenerator( name=split_names.get(split, split), gen_kwargs={ "local_extracted_archive_paths": local_extracted_archive_paths.get(split), "archive": dl_manager.iter_archive(archive_paths.get(split)), "meta_path": meta_paths[split], }, ), ) return split_generators def _generate_examples(self, local_extracted_archive_paths, archive, meta_path): data_fields = list(self._info().features.keys()) metadata = {} with open(meta_path, encoding="utf-8") as f: reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE) for row in tqdm(reader, desc="Reading metadata..."): row["crowdsourced"] = row["crowdsourced"] == "True" row["interrupted"] = row["interrupted"] == "True" for field in data_fields: if field not in row: row[field] = "" metadata[row["path"]] = row for path, file in archive: _, filename = os.path.split(path) if filename in metadata: result = dict(metadata[filename]) path = os.path.join(local_extracted_archive_paths, path) if local_extracted_archive_paths else path result["audio"] = {"path": path, "bytes": file.read()} result["path"] = path yield path, result