|
|
import json |
|
|
import os |
|
|
from datasets import load_dataset, DatasetInfo, DatasetDict, SplitGenerator, Split, Features, Value, Audio |
|
|
|
|
|
_DESCRIPTION = """ |
|
|
Custom version of the Common Voice dataset with additional test_freq split including custom audio and metadata. |
|
|
""" |
|
|
|
|
|
_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)}, |
|
|
year = 2020 |
|
|
} |
|
|
""" |
|
|
|
|
|
_HOMEPAGE = "https://commonvoice.mozilla.org/en/datasets" |
|
|
_LICENSE = "https://creativecommons.org/publicdomain/zero/1.0/" |
|
|
|
|
|
class CustomCommonVoice(datasets.GeneratorBasedBuilder): |
|
|
"""Builder for a modified Common Voice dataset including a custom test_freq split.""" |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
|
datasets.BuilderConfig(name="test_freq", description="Custom rare test split of the Common Voice dataset."), |
|
|
] |
|
|
|
|
|
DEFAULT_CONFIG_NAME = "test_freq" |
|
|
|
|
|
def _info(self): |
|
|
return DatasetInfo( |
|
|
description=_DESCRIPTION, |
|
|
features=Features({ |
|
|
"id": Value("string"), |
|
|
"utterance": Value("int32"), |
|
|
"from": Value("string"), |
|
|
"value": Value("string"), |
|
|
"emotion": Value("string"), |
|
|
"file_name": Value("string"), |
|
|
"audio": Audio(sampling_rate=16_000), |
|
|
}), |
|
|
supervised_keys=("audio", "value"), |
|
|
homepage=_HOMEPAGE, |
|
|
citation=_CITATION, |
|
|
license=_LICENSE, |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
"""Returns SplitGenerators.""" |
|
|
test_freq_dir = os.path.abspath("test_freq") |
|
|
test_freq_metadata = os.path.join(test_freq_dir, "metadata.jsonl") |
|
|
|
|
|
return [ |
|
|
SplitGenerator( |
|
|
name=Split.TEST, |
|
|
gen_kwargs={"metadata_path": test_freq_metadata, "audio_dir": test_freq_dir}), |
|
|
] |
|
|
|
|
|
def _generate_examples(self, metadata_path, audio_dir): |
|
|
"""Yields examples.""" |
|
|
with open(metadata_path, 'r', encoding='utf-8') as f: |
|
|
for line in f: |
|
|
metadata = json.loads(line) |
|
|
audio_path = os.path.join(audio_dir, metadata['file_name']) |
|
|
yield metadata['id'], { |
|
|
"id": metadata['id'], |
|
|
"utterance": metadata['utterance'], |
|
|
"from": metadata['from'], |
|
|
"value": metadata['value'], |
|
|
"emotion": metadata['emotion'], |
|
|
"file_name": metadata['file_name'], |
|
|
"audio": audio_path, |
|
|
} |
|
|
|