samromur_milljon / samromur_milljon.py
carlosdanielhernandezmena's picture
Changing how the audio_id is gotten
30864d0 verified
from collections import defaultdict
import os
import json
import csv
import datasets
_NAME="samromur_milljon"
_VERSION="1.0.0"
_AUDIO_EXTENSIONS=".flac"
_DESCRIPTION = """
Samrómur Milljón consists of approximately 1 million of speech recordings (967 hours) collected through the platform samromur.is; the transcripts accompanying these recordings were automatically verified using various ASR systems such as: Wav2Vec, Whisper and NeMo.
"""
_CITATION = """
@misc{menasamromurmilljon2023,
title={Samrómur Milljón, Audio and Transcriptions},
author={Hernández Mena, Carlos Daniel and Guðnason, Jón},
publisher={Reykjavík University}
year={2023},
url={https://huggingface.co/datasets/language-and-voice-lab/samromur_milljon},
}
"""
_HOMEPAGE = "https://huggingface.co/datasets/language-and-voice-lab/samromur_milljon"
_LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/"
_BASE_DATA_DIR = "corpus/"
_METADATA_FEM_LT_18_YRS = os.path.join(_BASE_DATA_DIR,"files","metadata_fem_lt_18_yrs.tsv")
_METADATA_FEM_18TO49_YRS = os.path.join(_BASE_DATA_DIR,"files","metadata_fem_18to49_yrs.tsv")
_METADATA_FEM_GT_49_YRS = os.path.join(_BASE_DATA_DIR,"files","metadata_fem_gt_49_yrs.tsv")
_METADATA_MALE_LT_18_YRS = os.path.join(_BASE_DATA_DIR,"files","metadata_male_lt_18_yrs.tsv")
_METADATA_MALE_18TO49_YRS = os.path.join(_BASE_DATA_DIR,"files","metadata_male_18to49_yrs.tsv")
_METADATA_MALE_GT_49_YRS = os.path.join(_BASE_DATA_DIR,"files","metadata_male_gt_49_yrs.tsv")
_METADATA_OTHER = os.path.join(_BASE_DATA_DIR,"files","metadata_other.tsv")
_TARS_FEM_LT_18_YRS = os.path.join(_BASE_DATA_DIR,"files","tars_fem_lt_18_yrs.paths")
_TARS_FEM_18TO49_YRS = os.path.join(_BASE_DATA_DIR,"files","tars_fem_18to49_yrs.paths")
_TARS_FEM_GT_49_YRS = os.path.join(_BASE_DATA_DIR,"files","tars_fem_gt_49_yrs.paths")
_TARS_MALE_LT_18_YRS = os.path.join(_BASE_DATA_DIR,"files","tars_male_lt_18_yrs.paths")
_TARS_MALE_18TO49_YRS = os.path.join(_BASE_DATA_DIR,"files","tars_male_18to49_yrs.paths")
_TARS_MALE_GT_49_YRS = os.path.join(_BASE_DATA_DIR,"files","tars_male_gt_49_yrs.paths")
_TARS_OTHER = os.path.join(_BASE_DATA_DIR,"files","tars_other.paths")
class SamromurMilljonConfig(datasets.BuilderConfig):
"""BuilderConfig for The Samrómur Milljón"""
def __init__(self, name, **kwargs):
name=_NAME
super().__init__(name=name, **kwargs)
class SamromurMilljon(datasets.GeneratorBasedBuilder):
"""Samrómur Milljón"""
VERSION = datasets.Version(_VERSION)
BUILDER_CONFIGS = [
SamromurMilljonConfig(
name=_NAME,
version=datasets.Version(_VERSION),
)
]
def _info(self):
features = datasets.Features(
{
"audio_id": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16000),
"speaker_id": datasets.Value("string"),
"gender": datasets.Value("string"),
"age": datasets.Value("string"),
"duration": datasets.Value("float32"),
"verified_with": datasets.Value("string"),
"normalized_text": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
metadata_fem_lt_18_yrs=dl_manager.download_and_extract(_METADATA_FEM_LT_18_YRS)
metadata_fem_18to49_yrs=dl_manager.download_and_extract(_METADATA_FEM_18TO49_YRS)
metadata_fem_gt_49_yrs=dl_manager.download_and_extract(_METADATA_FEM_GT_49_YRS)
metadata_male_lt_18_yrs=dl_manager.download_and_extract(_METADATA_MALE_LT_18_YRS)
metadata_male_18to49_yrs=dl_manager.download_and_extract(_METADATA_MALE_18TO49_YRS)
metadata_male_gt_49_yrs=dl_manager.download_and_extract(_METADATA_MALE_GT_49_YRS)
metadata_other=dl_manager.download_and_extract(_METADATA_OTHER)
tars_fem_lt_18_yrs=dl_manager.download_and_extract(_TARS_FEM_LT_18_YRS)
tars_fem_18to49_yrs=dl_manager.download_and_extract(_TARS_FEM_18TO49_YRS)
tars_fem_gt_49_yrs=dl_manager.download_and_extract(_TARS_FEM_GT_49_YRS)
tars_male_lt_18_yrs=dl_manager.download_and_extract(_TARS_MALE_LT_18_YRS)
tars_male_18to49_yrs=dl_manager.download_and_extract(_TARS_MALE_18TO49_YRS)
tars_male_gt_49_yrs=dl_manager.download_and_extract(_TARS_MALE_GT_49_YRS)
tars_other=dl_manager.download_and_extract(_TARS_OTHER)
hash_tar_files=defaultdict(dict)
with open(tars_fem_lt_18_yrs,'r') as f:
hash_tar_files['fem_lt_18_yrs']=[path.replace('\n','') for path in f]
with open(tars_fem_18to49_yrs,'r') as f:
hash_tar_files['fem_18to49_yrs']=[path.replace('\n','') for path in f]
with open(tars_fem_gt_49_yrs,'r') as f:
hash_tar_files['fem_gt_49_yrs']=[path.replace('\n','') for path in f]
with open(tars_male_lt_18_yrs,'r') as f:
hash_tar_files['male_lt_18_yrs']=[path.replace('\n','') for path in f]
with open(tars_male_18to49_yrs,'r') as f:
hash_tar_files['male_18to49_yrs']=[path.replace('\n','') for path in f]
with open(tars_male_gt_49_yrs,'r') as f:
hash_tar_files['male_gt_49_yrs']=[path.replace('\n','') for path in f]
with open(tars_other,'r') as f:
hash_tar_files['other']=[path.replace('\n','') for path in f]
hash_meta_paths={"fem_lt_18_yrs":metadata_fem_lt_18_yrs,
"fem_18to49_yrs":metadata_fem_18to49_yrs,
"fem_gt_49_yrs":metadata_fem_gt_49_yrs,
"male_lt_18_yrs":metadata_male_lt_18_yrs,
"male_18to49_yrs":metadata_male_18to49_yrs,
"male_gt_49_yrs":metadata_male_gt_49_yrs,
"other":metadata_other}
audio_paths = dl_manager.download(hash_tar_files)
splits=["fem_lt_18_yrs","fem_18to49_yrs","fem_gt_49_yrs","male_lt_18_yrs","male_18to49_yrs","male_gt_49_yrs","other"]
local_extracted_audio_paths = (
dl_manager.extract(audio_paths) if not dl_manager.is_streaming else
{
split:[None] * len(audio_paths[split]) for split in splits
}
)
return [
datasets.SplitGenerator(
name="female_lt_18_yrs",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["fem_lt_18_yrs"]],
"local_extracted_archives_paths": local_extracted_audio_paths["fem_lt_18_yrs"],
"metadata_paths": hash_meta_paths["fem_lt_18_yrs"],
}
),
datasets.SplitGenerator(
name="female_18to49_yrs",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["fem_18to49_yrs"]],
"local_extracted_archives_paths": local_extracted_audio_paths["fem_18to49_yrs"],
"metadata_paths": hash_meta_paths["fem_18to49_yrs"],
}
),
datasets.SplitGenerator(
name="female_gt_49_yrs",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["fem_gt_49_yrs"]],
"local_extracted_archives_paths": local_extracted_audio_paths["fem_gt_49_yrs"],
"metadata_paths": hash_meta_paths["fem_gt_49_yrs"],
}
),
datasets.SplitGenerator(
name="male_lt_18_yrs",
gen_kwargs={
"audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["male_lt_18_yrs"]],
"local_extracted_archives_paths": local_extracted_audio_paths["male_lt_18_yrs"],
"metadata_paths": hash_meta_paths["male_lt_18_yrs"],
}
),
datasets.SplitGenerator(
name="male_18to49_yrs",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["male_18to49_yrs"]],
"local_extracted_archives_paths": local_extracted_audio_paths["male_18to49_yrs"],
"metadata_paths": hash_meta_paths["male_18to49_yrs"],
}
),
datasets.SplitGenerator(
name="male_gt_49_yrs",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["male_gt_49_yrs"]],
"local_extracted_archives_paths": local_extracted_audio_paths["male_gt_49_yrs"],
"metadata_paths": hash_meta_paths["male_gt_49_yrs"],
}
),
datasets.SplitGenerator(
name="other",
gen_kwargs={
"audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["other"]],
"local_extracted_archives_paths": local_extracted_audio_paths["other"],
"metadata_paths": hash_meta_paths["other"],
}
),
]
def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):
features = ["speaker_id","gender","age","duration","verified_with","normalized_text"]
with open(metadata_paths) as f:
metadata = {x["audio_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(_AUDIO_EXTENSIONS)[0]
audio_id =os.path.splitext(os.path.basename(audio_filename))[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()},
}