Delete audio.py
Browse files
audio.py
DELETED
|
@@ -1,221 +0,0 @@
|
|
| 1 |
-
# coding=utf-8
|
| 2 |
-
# Copyright 2021 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
-
# you may not use this file except in compliance with the License.
|
| 6 |
-
# You may obtain a copy of the License at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
-
# See the License for the specific language governing permissions and
|
| 14 |
-
# limitations under the License.
|
| 15 |
-
""" Common Voice Dataset"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
import os
|
| 19 |
-
|
| 20 |
-
import datasets
|
| 21 |
-
from datasets.tasks import AutomaticSpeechRecognition
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
_DATA_URL = "https://voice-prod-bundler-ee1969a6ce8178826482b88e843c335139bd3fb4.s3.amazonaws.com/cv-corpus-6.1-2020-12-11/{}.tar.gz"
|
| 25 |
-
|
| 26 |
-
_CITATION = """\
|
| 27 |
-
@inproceedings{,
|
| 28 |
-
author = },
|
| 29 |
-
title = {},
|
| 30 |
-
booktitle = {},
|
| 31 |
-
pages = {},
|
| 32 |
-
year =
|
| 33 |
-
}
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
_DESCRIPTION = """\
|
| 37 |
-
"""
|
| 38 |
-
|
| 39 |
-
_HOMEPAGE = "https://Audio.mozilla.org/en/datasets"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
class AudioConfig(datasets.BuilderConfig):
|
| 44 |
-
"""BuilderConfig for Audio."""
|
| 45 |
-
|
| 46 |
-
def __init__(self, name, sub_version, **kwargs):
|
| 47 |
-
"""
|
| 48 |
-
Args:
|
| 49 |
-
data_dir: `string`, the path to the folder containing the files in the
|
| 50 |
-
downloaded .tar
|
| 51 |
-
citation: `string`, citation for the data set
|
| 52 |
-
url: `string`, url for information about the data set
|
| 53 |
-
**kwargs: keyword arguments forwarded to super.
|
| 54 |
-
"""
|
| 55 |
-
self.sub_version = sub_version
|
| 56 |
-
self.language = kwargs.pop("language", None)
|
| 57 |
-
self.date_of_snapshot = kwargs.pop("date", None)
|
| 58 |
-
self.size = kwargs.pop("size", None)
|
| 59 |
-
self.validated_hr_total = kwargs.pop("val_hrs", None)
|
| 60 |
-
self.total_hr_total = kwargs.pop("total_hrs", None)
|
| 61 |
-
self.num_of_voice = kwargs.pop("num_of_voice", None)
|
| 62 |
-
description = f"Common Voice 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}"
|
| 63 |
-
super(AudioConfig, self).__init__(
|
| 64 |
-
name=name, version=datasets.Version("6.1.0", ""), description=description, **kwargs
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
class Audio(datasets.GeneratorBasedBuilder):
|
| 69 |
-
|
| 70 |
-
DEFAULT_WRITER_BATCH_SIZE = 1000
|
| 71 |
-
BUILDER_CONFIGS = [
|
| 72 |
-
AudioConfig(
|
| 73 |
-
name=lang_id,
|
| 74 |
-
language=_LANGUAGES[lang_id]["Language"],
|
| 75 |
-
sub_version=_LANGUAGES[lang_id]["Version"],
|
| 76 |
-
date=_LANGUAGES[lang_id]["Date"],
|
| 77 |
-
size=_LANGUAGES[lang_id]["Size"],
|
| 78 |
-
val_hrs=_LANGUAGES[lang_id]["Validated_Hr_Total"],
|
| 79 |
-
total_hrs=_LANGUAGES[lang_id]["Overall_Hr_Total"],
|
| 80 |
-
num_of_voice=_LANGUAGES[lang_id]["Number_Of_Voice"],
|
| 81 |
-
)
|
| 82 |
-
for lang_id in _LANGUAGES.keys()
|
| 83 |
-
]
|
| 84 |
-
|
| 85 |
-
def _info(self):
|
| 86 |
-
features = datasets.Features(
|
| 87 |
-
{
|
| 88 |
-
"id": datasets.Value("string"),
|
| 89 |
-
"path": datasets.Value("string"),
|
| 90 |
-
"audio": datasets.Audio(sampling_rate=48_000),
|
| 91 |
-
"sentence": datasets.Value("string"),
|
| 92 |
-
"up_votes": datasets.Value("int64"),
|
| 93 |
-
"down_votes": datasets.Value("int64"),
|
| 94 |
-
"age": datasets.Value("string"),
|
| 95 |
-
"gender": datasets.Value("string"),
|
| 96 |
-
"accent": datasets.Value("string"),
|
| 97 |
-
"locale": datasets.Value("string"),
|
| 98 |
-
"segment": datasets.Value("string"),
|
| 99 |
-
}
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
return datasets.DatasetInfo(
|
| 103 |
-
description=_DESCRIPTION,
|
| 104 |
-
features=features,
|
| 105 |
-
supervised_keys=None,
|
| 106 |
-
homepage=_HOMEPAGE,
|
| 107 |
-
license=_LICENSE,
|
| 108 |
-
citation=_CITATION,
|
| 109 |
-
task_templates=[AutomaticSpeechRecognition(audio_column="audio", transcription_column="sentence")],
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
def _split_generators(self, dl_manager):
|
| 113 |
-
"""Returns SplitGenerators."""
|
| 114 |
-
# Download the TAR archive that contains the audio files:
|
| 115 |
-
archive_path = dl_manager.download(_DATA_URL.format(self.config.name))
|
| 116 |
-
|
| 117 |
-
# First we locate the data using the path within the archive:
|
| 118 |
-
path_to_data = "/".join(["cv-corpus-6.1-2020-12-11", self.config.name])
|
| 119 |
-
path_to_clips = "/".join([path_to_data, "clips"])
|
| 120 |
-
metadata_filepaths = {
|
| 121 |
-
split: "/".join([path_to_data, f"{split}.tsv"])
|
| 122 |
-
for split in ["train", "test", "dev", "other", "validated", "invalidated"]
|
| 123 |
-
}
|
| 124 |
-
# (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
|
| 125 |
-
local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else None
|
| 126 |
-
|
| 127 |
-
# To access the audio data from the TAR archives using the download manager,
|
| 128 |
-
# we have to use the dl_manager.iter_archive method.
|
| 129 |
-
#
|
| 130 |
-
# This is because dl_manager.download_and_extract
|
| 131 |
-
# doesn't work to stream TAR archives in streaming mode.
|
| 132 |
-
# (we have to stream the files of a TAR archive one by one)
|
| 133 |
-
#
|
| 134 |
-
# The iter_archive method returns an iterable of (path_within_archive, file_obj) for every
|
| 135 |
-
# file in the TAR archive.
|
| 136 |
-
|
| 137 |
-
return [
|
| 138 |
-
datasets.SplitGenerator(
|
| 139 |
-
name=datasets.Split.TRAIN,
|
| 140 |
-
gen_kwargs={
|
| 141 |
-
"local_extracted_archive": local_extracted_archive,
|
| 142 |
-
"archive_iterator": dl_manager.iter_archive(
|
| 143 |
-
archive_path
|
| 144 |
-
), # use iter_archive here to access the files in the TAR archives
|
| 145 |
-
"metadata_filepath": metadata_filepaths["train"],
|
| 146 |
-
"path_to_clips": path_to_clips,
|
| 147 |
-
},
|
| 148 |
-
),
|
| 149 |
-
datasets.SplitGenerator(
|
| 150 |
-
name=datasets.Split.TEST,
|
| 151 |
-
gen_kwargs={
|
| 152 |
-
"local_extracted_archive": local_extracted_archive,
|
| 153 |
-
"archive_iterator": dl_manager.iter_archive(
|
| 154 |
-
archive_path
|
| 155 |
-
), # use iter_archive here to access the files in the TAR archives
|
| 156 |
-
"metadata_filepath": metadata_filepaths["test"],
|
| 157 |
-
"path_to_clips": path_to_clips,
|
| 158 |
-
},
|
| 159 |
-
),
|
| 160 |
-
datasets.SplitGenerator(
|
| 161 |
-
name=datasets.Split.VALIDATION,
|
| 162 |
-
gen_kwargs={
|
| 163 |
-
"local_extracted_archive": local_extracted_archive,
|
| 164 |
-
"archive_iterator": dl_manager.iter_archive(
|
| 165 |
-
archive_path
|
| 166 |
-
), # use iter_archive here to access the files in the TAR archives
|
| 167 |
-
"metadata_filepath": metadata_filepaths["dev"],
|
| 168 |
-
"path_to_clips": path_to_clips,
|
| 169 |
-
},
|
| 170 |
-
),
|
| 171 |
-
]
|
| 172 |
-
|
| 173 |
-
def _generate_examples(self, local_extracted_archive, archive_iterator, metadata_filepath, path_to_clips):
|
| 174 |
-
"""Yields examples."""
|
| 175 |
-
data_fields = list(self._info().features.keys())
|
| 176 |
-
|
| 177 |
-
# audio is not a header of the csv files
|
| 178 |
-
data_fields.remove("audio")
|
| 179 |
-
path_idx = data_fields.index("path")
|
| 180 |
-
|
| 181 |
-
all_field_values = {}
|
| 182 |
-
metadata_found = False
|
| 183 |
-
# Here we iterate over all the files within the TAR archive:
|
| 184 |
-
for path, f in archive_iterator:
|
| 185 |
-
# Parse the metadata CSV file
|
| 186 |
-
if path == metadata_filepath:
|
| 187 |
-
metadata_found = True
|
| 188 |
-
lines = f.readlines()
|
| 189 |
-
headline = lines[0].decode("utf-8")
|
| 190 |
-
|
| 191 |
-
column_names = headline.strip().split("\t")
|
| 192 |
-
assert (
|
| 193 |
-
column_names == data_fields
|
| 194 |
-
), f"The file should have {data_fields} as column names, but has {column_names}"
|
| 195 |
-
for line in lines[1:]:
|
| 196 |
-
field_values = line.decode("utf-8").strip().split("\t")
|
| 197 |
-
# set full path for mp3 audio file
|
| 198 |
-
audio_path = "/".join([path_to_clips, field_values[path_idx]])
|
| 199 |
-
all_field_values[audio_path] = field_values
|
| 200 |
-
# Else, read the audio file and yield an example
|
| 201 |
-
elif path.startswith(path_to_clips):
|
| 202 |
-
assert metadata_found, "Found audio clips before the metadata TSV file."
|
| 203 |
-
if not all_field_values:
|
| 204 |
-
break
|
| 205 |
-
if path in all_field_values:
|
| 206 |
-
# retrieve the metadata corresponding to this audio file
|
| 207 |
-
field_values = all_field_values[path]
|
| 208 |
-
|
| 209 |
-
# if data is incomplete, fill with empty values
|
| 210 |
-
if len(field_values) < len(data_fields):
|
| 211 |
-
field_values += (len(data_fields) - len(field_values)) * ["''"]
|
| 212 |
-
|
| 213 |
-
result = {key: value for key, value in zip(data_fields, field_values)}
|
| 214 |
-
|
| 215 |
-
# set audio feature
|
| 216 |
-
path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path
|
| 217 |
-
result["audio"] = {"path": path, "bytes": f.read()}
|
| 218 |
-
# set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
|
| 219 |
-
result["path"] = path if local_extracted_archive else None
|
| 220 |
-
|
| 221 |
-
yield path, result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|