File size: 1,304 Bytes
4a04b75
 
 
 
 
 
 
 
 
 
 
 
 
 
a5f4f1e
4a04b75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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}"
    )