|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" Common Voice Dataset""" |
|
|
|
|
|
import datasets |
|
from datasets.tasks import AutomaticSpeechRecognition |
|
|
|
|
|
_DATA_URL = "https://drive.google.com/uc?export=download&id=13PIifohVjBB5ffSCm4eXyF8O9I30HOj2" |
|
|
|
_DESCRIPTION = """\ |
|
Common Voice is Mozilla's initiative to help teach machines how real people speak. |
|
The dataset currently consists of 7,335 validated hours of speech in 60 languages, but we’re always adding more voices and languages. |
|
""" |
|
|
|
_LANGUAGES = { |
|
"vi": { |
|
"Language": "Vietnamese", |
|
"Date": "2020-12-11", |
|
"Size": "50 MB", |
|
"Version": "vi_1h_2020-12-11", |
|
"Validated_Hr_Total": 0.74, |
|
"Overall_Hr_Total": 1, |
|
"Number_Of_Voice": 62, |
|
}, |
|
} |
|
|
|
|
|
class CustomCommonVoiceConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for CommonVoice.""" |
|
|
|
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"Common Voice speech to text dataset in {self.language} version " \ |
|
f"{self.sub_version} of {self.date_of_snapshot}. " \ |
|
f"The dataset comprises {self.validated_hr_total} of validated transcribed speech data from " \ |
|
f"{self.num_of_voice} speakers. The dataset has a size of {self.size} " |
|
super(CustomCommonVoiceConfig, self).__init__( |
|
name=name, version=datasets.Version("1.0", ""), description=description, **kwargs |
|
) |
|
|
|
|
|
class CustomCommonVoice(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_WRITER_BATCH_SIZE = 1000 |
|
BUILDER_CONFIGS = [ |
|
CustomCommonVoiceConfig( |
|
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( |
|
{ |
|
"client_id": datasets.Value("string"), |
|
"path": datasets.Value("string"), |
|
"audio": datasets.Audio(sampling_rate=48_000), |
|
"sentence": datasets.Value("string"), |
|
"up_votes": datasets.Value("int64"), |
|
"down_votes": datasets.Value("int64"), |
|
"age": datasets.Value("string"), |
|
"gender": datasets.Value("string"), |
|
"accent": datasets.Value("string"), |
|
"locale": datasets.Value("string"), |
|
"segment": datasets.Value("string"), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
supervised_keys=None, |
|
task_templates=[ |
|
AutomaticSpeechRecognition(audio_file_path_column="path", transcription_column="sentence") |
|
], |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
archive = dl_manager.download(_DATA_URL) |
|
path_to_data = "/".join("data_1") |
|
path_to_clips = "/".join([path_to_data, "audio"]) |
|
path_to_script = "/".join([path_to_data, "script"]) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"files": dl_manager.iter_archive(archive), |
|
"filepath": "/".join([path_to_data, "train_custom_common_voice.csv"]), |
|
"path_to_clips": path_to_clips, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"files": dl_manager.iter_archive(archive), |
|
"filepath": "/".join([path_to_data, "test_custom_common_voice.csv"]), |
|
"path_to_clips": path_to_clips, |
|
}, |
|
), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
] |
|
|
|
def _generate_examples(self, files, filepath, path_to_clips): |
|
"""Yields examples.""" |
|
data_fields = list(self._info().features.keys()) |
|
|
|
|
|
data_fields.remove("audio") |
|
path_idx = data_fields.index("path") |
|
|
|
all_field_values = {} |
|
metadata_found = False |
|
for path, f in files: |
|
if path == 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") |
|
|
|
audio_path = "/".join([path_to_clips, field_values[path_idx]]) |
|
all_field_values[audio_path] = field_values |
|
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: |
|
field_values = all_field_values[path] |
|
|
|
|
|
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)} |
|
|
|
|
|
result["audio"] = {"path": path, "bytes": f.read()} |
|
|
|
yield path, result |
|
|