|
import json |
|
import os |
|
|
|
import datasets |
|
|
|
from .configs import CONFIGS_MAP |
|
|
|
_BASE_URL = ( |
|
"https://huggingface.co/datasets/formospeech/AudioSCAN-XTTSv2-female/resolve/main/" |
|
) |
|
_AUDIO_URL = _BASE_URL + "audio.tar.gz" |
|
|
|
|
|
class AudioSCANConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for CommonVoice.""" |
|
|
|
def __init__(self, **kwargs): |
|
super(AudioSCANConfig, self).__init__( |
|
version=datasets.Version("0.1.0", ""), **kwargs |
|
) |
|
|
|
|
|
class LibrispeechASR(datasets.GeneratorBasedBuilder): |
|
DEFAULT_WRITER_BATCH_SIZE = 256 |
|
DEFAULT_CONFIG_NAME = "addprim_jump" |
|
BUILDER_CONFIGS = [] |
|
for config_name in CONFIGS_MAP.keys(): |
|
BUILDER_CONFIGS.append( |
|
AudioSCANConfig( |
|
name=config_name, |
|
description=None, |
|
) |
|
) |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description="", |
|
features=datasets.Features( |
|
{ |
|
"audio": datasets.Audio(sampling_rate=16_000), |
|
"out": datasets.Value("string"), |
|
"in": datasets.Value("string"), |
|
} |
|
), |
|
license=None, |
|
supervised_keys=("audio", "out"), |
|
homepage=None, |
|
citation=None, |
|
task_templates=None, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
archive_path = dl_manager.download(_AUDIO_URL) |
|
local_extracted_archive = ( |
|
dl_manager.extract(archive_path) if not dl_manager.is_streaming else {} |
|
) |
|
|
|
split_meta_urls = { |
|
split_name: _BASE_URL + split_meta_path |
|
for split_name, split_meta_path in CONFIGS_MAP[self.config.name].items() |
|
} |
|
|
|
split_meta_paths = dl_manager.download_and_extract(split_meta_urls) |
|
splits = [] |
|
for split_name in CONFIGS_MAP[self.config.name].keys(): |
|
split = datasets.SplitGenerator( |
|
name=split_name, |
|
gen_kwargs={ |
|
"meta_path": split_meta_paths[split_name], |
|
"local_extracted_archive": local_extracted_archive, |
|
"files": dl_manager.iter_archive(archive_path), |
|
}, |
|
) |
|
splits.append(split) |
|
|
|
return splits |
|
|
|
def _generate_examples(self, meta_path, local_extracted_archive, files): |
|
with open(meta_path, "r") as f: |
|
metadata = json.loads("[" + ",".join(f.readlines()) + "]") |
|
|
|
audio_data = {} |
|
for path, file in files: |
|
filename = os.path.basename(path) |
|
audio_data[filename] = file.read() |
|
key = 0 |
|
for item in metadata: |
|
path = ( |
|
os.path.join(local_extracted_archive, item["audio_path"]) |
|
if local_extracted_archive |
|
else item["audio_path"] |
|
) |
|
yield ( |
|
key, |
|
{ |
|
"in": item["in"], |
|
"out": item["out"], |
|
"audio": { |
|
"path": path, |
|
"bytes": audio_data[item["audio_path"]], |
|
}, |
|
}, |
|
) |
|
key += 1 |
|
|