import os import torch from datasets import load_dataset, DatasetDict from encodec_audio_tokenizer import EncodecTokenizer direction = os.getenv("DIRECTION", "enA-jaA") sides = set(direction.split("-")) dataset_id = os.getenv("DATASET_ID", 0) batch_size = int(os.getenv("BATCH_SIZE", 64)) 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") tokenizer = EncodecTokenizer.from_pretrained() def tokenize(batch): for side in sides: wav = torch.concat([i["array"] for i in batch[f"{side}.audio"]]) sr = [i["sampling_rate"] for i in batch[f"{side}.audio"]] batch[f"{side}.audio.tokens"] = tokenizer.wav_to_tokens(wav=wav, sample_rate=sr).numpy().tolist() return batch dataset = dataset.map( function=tokenize, 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], batched=True, batch_size=batch_size, num_proc=num_proc, desc="tokenize dataset" ) DatasetDict({"train": dataset}).push_to_hub( f"{hf_org}/{hf_dataset}.tokenized", config_name=f"subset_{dataset_id}" )