hyvoxpopuli / hyvoxpopuli.py
Edmon02's picture
Rename app.py to hyvoxpopuli.py
24bbb14 verified
raw
history blame
3.83 kB
from collections import defaultdict
import os
import json
import csv
import datasets
_BASE_DATA_DIR = "data/"
_AUDIO_ARCHIVE_PATH = _BASE_DATA_DIR + "{split}/{split}_dataset.tar.gz"
_METADATA_PATH = _BASE_DATA_DIR + "{split}.tsv"
class Hyvoxpopuli(datasets.GeneratorBasedBuilder):
"""The HyVoxPopuli dataset."""
def _info(self):
features = datasets.Features(
{
"audio_id": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16_000),
"raw_text": datasets.Value("string"),
"normalized_text": datasets.Value("string"),
"gender": datasets.Value("string"), # TODO: ClassVar?
"speaker_id": datasets.Value("string"),
"is_gold_transcript": datasets.Value("bool"),
"accent": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
features=features
)
def _split_generators(self, dl_manager):
splits = ["train", "dev", "test"]
audio_urls = defaultdict(dict)
for split in splits:
audio_urls[split] = [_AUDIO_ARCHIVE_PATH.format(split=split)]
meta_urls = defaultdict(dict)
for split in splits:
meta_urls[split] = _METADATA_PATH.format(split=split)
# dl_manager.download_config.num_proc = len(urls)
meta_paths = dl_manager.download_and_extract(meta_urls)
audio_paths = dl_manager.download(audio_urls)
local_extracted_audio_paths = (
dl_manager.extract(audio_paths)
)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["train"]],
"local_extracted_archives_paths": local_extracted_audio_paths["train"],
"metadata_paths": meta_paths["train"],
}
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["dev"]],
"local_extracted_archives_paths": local_extracted_audio_paths["dev"],
"metadata_paths": meta_paths["dev"],
}
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["test"]],
"local_extracted_archives_paths": local_extracted_audio_paths["test"],
"metadata_paths": meta_paths["test"],
}
),
]
def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
features = ["raw_text", "normalized_text", "speaker_id", "gender", "is_gold_transcript", "accent"]
meta_path = metadata_paths
with open(meta_path) as f:
metadata = {x["id"]: x for x in csv.DictReader(f, delimiter="\t")}
for audio_archive, local_extracted_archive_path in zip(audio_archives, local_extracted_archives_paths):
for audio_filename, audio_file in audio_archive:
audio_id = audio_filename.split(os.sep)[-1].split(".wav")[0]
path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path else audio_filename
yield audio_id, {
"audio_id": audio_id,
**{feature: metadata[audio_id][feature] for feature in features},
"audio": {"path": path, "bytes": audio_file.read()},
}