|
import csv |
|
import datasets |
|
from datasets import BuilderConfig, GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split |
|
|
|
_PROMPTS_URLS = { |
|
"dev": "automatic/validation.csv", |
|
"train": "automatic/train.csv", |
|
} |
|
|
|
_PROMPTS_FILTERED_URLS = { |
|
"dev": "automatic/validation.csv", |
|
"train": "automatic/train.csv", |
|
} |
|
|
|
_ARCHIVES = { |
|
"dev": "automatic.tar.gz", |
|
"train": "automatic.tar.gz", |
|
} |
|
|
|
_PATH_TO_CLIPS = { |
|
"dev": "validation", |
|
"train": "train", |
|
} |
|
|
|
class NurcSPConfig(BuilderConfig): |
|
def __init__(self, prompts_type="original", **kwargs): |
|
super().__init__(**kwargs) |
|
self.prompts_type = prompts_type |
|
|
|
class NurcSPDataset(GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
NurcSPConfig(name="original", description="Original audio prompts", prompts_type="original"), |
|
NurcSPConfig(name="filtered", description="Filtered audio prompts", prompts_type="filtered"), |
|
] |
|
|
|
def _info(self): |
|
return DatasetInfo( |
|
features=datasets.Features( |
|
{ |
|
"audio_name": datasets.Value("string"), |
|
"file_path": datasets.Value("string"), |
|
"text": datasets.Value("string"), |
|
"start_time": datasets.Value("string"), |
|
"end_time": datasets.Value("string"), |
|
"duration": datasets.Value("string"), |
|
"quality": datasets.Value("string"), |
|
"speech_genre": datasets.Value("string"), |
|
"speech_style": datasets.Value("string"), |
|
"variety": datasets.Value("string"), |
|
"accent": datasets.Value("string"), |
|
"sex": datasets.Value("string"), |
|
"age_range": datasets.Value("string"), |
|
"num_speakers": datasets.Value("string"), |
|
"speaker_id": datasets.Value("string"), |
|
"audio": datasets.Audio(sampling_rate=16_000), |
|
} |
|
) |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
prompts_urls = _PROMPTS_URLS if self.config.prompts_type == "original" else _PROMPTS_FILTERED_URLS |
|
|
|
|
|
prompts_path = dl_manager.download_and_extract(prompts_urls) |
|
archive = dl_manager.download_and_extract(_ARCHIVES) |
|
|
|
|
|
return [ |
|
SplitGenerator( |
|
name=Split.TRAIN, |
|
gen_kwargs={ |
|
"prompts_path": prompts_path["train"], |
|
"path_to_clips": _PATH_TO_CLIPS["train"], |
|
"audio_files": archive["train"], |
|
} |
|
), |
|
SplitGenerator( |
|
name=Split.VALIDATION, |
|
gen_kwargs={ |
|
"prompts_path": prompts_path["dev"], |
|
"path_to_clips": _PATH_TO_CLIPS["dev"], |
|
"audio_files": archive["dev"], |
|
} |
|
), |
|
] |
|
|
|
def _generate_examples(self, prompts_path, path_to_clips, audio_files): |
|
|
|
examples = {} |
|
with open(prompts_path, "r", encoding='utf-8') as f: |
|
csv_reader = csv.DictReader(f) |
|
for row in csv_reader: |
|
examples[row['file_path']] = { |
|
key: row[key] |
|
for key in row.keys() |
|
} |
|
|
|
|
|
id_ = 0 |
|
for root, _, files in datasets.utils.py_utils.walk(audio_files): |
|
if path_to_clips in root: |
|
for fname in files: |
|
file_path = f"{path_to_clips}/{fname}" |
|
if file_path in examples: |
|
full_path = f"{root}/{fname}" |
|
with open(full_path, "rb") as audio_file: |
|
audio = {"path": file_path, "bytes": audio_file.read()} |
|
yield id_, {**examples[file_path], "audio": audio} |
|
id_ += 1 |