import os from os.path import expanduser import shutil from soundfile import LibsndfileError from datasets import load_dataset, DatasetDict, Audio direction = os.getenv("DIRECTION", "enA-jaA") sides = set(direction.split("-")) dataset_id = os.getenv("DATASET_ID", 0) num_proc = int(os.getenv("NUM_PROC", 1)) hf_org = os.getenv("HF_ORG", "asahi417") hf_dataset = f"seamless-align-{direction}" dataset = load_dataset(f"{hf_org}/{hf_dataset}", f"subset_{dataset_id}", split="train") audio_loader = Audio() se_model = os.getenv("SE_MODEL", "metavoice") if se_model == "metavoice": from speaker_embedding_metavoice import MetaVoiceSE speaker_embedder = MetaVoiceSE() elif se_model == "pyannote": from speaker_embedding_pyannote import PyannoteSE speaker_embedder = PyannoteSE() else: raise ValueError(f"unknown speaker embedding: {se_model}") def error_file(example): for side in sides: try: audio_loader.decode_example(example[f"{side}.audio"]) except LibsndfileError: return False return True print(f"Num examples: {len(dataset)}") for s in sides: dataset = dataset.cast_column(f"{s}.audio", Audio(decode=False)) dataset = dataset.filter(error_file, num_proc=num_proc, desc="drop broken audio") for s in sides: dataset = dataset.cast_column(f"{s}.audio", Audio()) print(f"Num examples (after filtering): {len(dataset)}") def speaker_embedding(example): for side in sides: example[f"{side}.audio.speaker_embedding"] = speaker_embedder.get_speaker_embedding( example[f"{side}.audio"]["array"], example[f"{side}.audio"]["sampling_rate"] ) return example dataset = dataset.map( function=speaker_embedding, remove_columns=[f"{s}.audio" for s in sides] + [f"{s}.url" for s in sides] + [f"{s}.duration_start" for s in sides] + [f"{s}.duration_end" for s in sides], num_proc=num_proc, desc="attach speaker embedding dataset" ) DatasetDict({"train": dataset}).push_to_hub(f"{hf_org}/{hf_dataset}.speaker-embedding.{se_model}", config_name=f"subset_{dataset_id}") cache_dir = f"{expanduser('~')}/.cache/huggingface/datasets/{hf_org}___{hf_dataset}/subset_{dataset_id}" if os.path.exists(cache_dir): shutil.rmtree(cache_dir)