Spaces:
Sleeping
Sleeping
Upload f5_tts/train/datasets/prepare_wenetspeech4tts.py with huggingface_hub
Browse files
f5_tts/train/datasets/prepare_wenetspeech4tts.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# generate audio text map for WenetSpeech4TTS
|
| 2 |
+
# evaluate for vocab size
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
sys.path.append(os.getcwd())
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
from concurrent.futures import ProcessPoolExecutor
|
| 11 |
+
from importlib.resources import files
|
| 12 |
+
from tqdm import tqdm
|
| 13 |
+
|
| 14 |
+
import torchaudio
|
| 15 |
+
from datasets import Dataset
|
| 16 |
+
|
| 17 |
+
from f5_tts.model.utils import convert_char_to_pinyin
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def deal_with_sub_path_files(dataset_path, sub_path):
|
| 21 |
+
print(f"Dealing with: {sub_path}")
|
| 22 |
+
|
| 23 |
+
text_dir = os.path.join(dataset_path, sub_path, "txts")
|
| 24 |
+
audio_dir = os.path.join(dataset_path, sub_path, "wavs")
|
| 25 |
+
text_files = os.listdir(text_dir)
|
| 26 |
+
|
| 27 |
+
audio_paths, texts, durations = [], [], []
|
| 28 |
+
for text_file in tqdm(text_files):
|
| 29 |
+
with open(os.path.join(text_dir, text_file), "r", encoding="utf-8") as file:
|
| 30 |
+
first_line = file.readline().split("\t")
|
| 31 |
+
audio_nm = first_line[0]
|
| 32 |
+
audio_path = os.path.join(audio_dir, audio_nm + ".wav")
|
| 33 |
+
text = first_line[1].strip()
|
| 34 |
+
|
| 35 |
+
audio_paths.append(audio_path)
|
| 36 |
+
|
| 37 |
+
if tokenizer == "pinyin":
|
| 38 |
+
texts.extend(convert_char_to_pinyin([text], polyphone=polyphone))
|
| 39 |
+
elif tokenizer == "char":
|
| 40 |
+
texts.append(text)
|
| 41 |
+
|
| 42 |
+
audio, sample_rate = torchaudio.load(audio_path)
|
| 43 |
+
durations.append(audio.shape[-1] / sample_rate)
|
| 44 |
+
|
| 45 |
+
return audio_paths, texts, durations
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def main():
|
| 49 |
+
assert tokenizer in ["pinyin", "char"]
|
| 50 |
+
|
| 51 |
+
audio_path_list, text_list, duration_list = [], [], []
|
| 52 |
+
|
| 53 |
+
executor = ProcessPoolExecutor(max_workers=max_workers)
|
| 54 |
+
futures = []
|
| 55 |
+
for dataset_path in dataset_paths:
|
| 56 |
+
sub_items = os.listdir(dataset_path)
|
| 57 |
+
sub_paths = [item for item in sub_items if os.path.isdir(os.path.join(dataset_path, item))]
|
| 58 |
+
for sub_path in sub_paths:
|
| 59 |
+
futures.append(executor.submit(deal_with_sub_path_files, dataset_path, sub_path))
|
| 60 |
+
for future in tqdm(futures, total=len(futures)):
|
| 61 |
+
audio_paths, texts, durations = future.result()
|
| 62 |
+
audio_path_list.extend(audio_paths)
|
| 63 |
+
text_list.extend(texts)
|
| 64 |
+
duration_list.extend(durations)
|
| 65 |
+
executor.shutdown()
|
| 66 |
+
|
| 67 |
+
if not os.path.exists("data"):
|
| 68 |
+
os.makedirs("data")
|
| 69 |
+
|
| 70 |
+
print(f"\nSaving to {save_dir} ...")
|
| 71 |
+
dataset = Dataset.from_dict({"audio_path": audio_path_list, "text": text_list, "duration": duration_list})
|
| 72 |
+
dataset.save_to_disk(f"{save_dir}/raw", max_shard_size="2GB") # arrow format
|
| 73 |
+
|
| 74 |
+
with open(f"{save_dir}/duration.json", "w", encoding="utf-8") as f:
|
| 75 |
+
json.dump(
|
| 76 |
+
{"duration": duration_list}, f, ensure_ascii=False
|
| 77 |
+
) # dup a json separately saving duration in case for DynamicBatchSampler ease
|
| 78 |
+
|
| 79 |
+
print("\nEvaluating vocab size (all characters and symbols / all phonemes) ...")
|
| 80 |
+
text_vocab_set = set()
|
| 81 |
+
for text in tqdm(text_list):
|
| 82 |
+
text_vocab_set.update(list(text))
|
| 83 |
+
|
| 84 |
+
# add alphabets and symbols (optional, if plan to ft on de/fr etc.)
|
| 85 |
+
if tokenizer == "pinyin":
|
| 86 |
+
text_vocab_set.update([chr(i) for i in range(32, 127)] + [chr(i) for i in range(192, 256)])
|
| 87 |
+
|
| 88 |
+
with open(f"{save_dir}/vocab.txt", "w") as f:
|
| 89 |
+
for vocab in sorted(text_vocab_set):
|
| 90 |
+
f.write(vocab + "\n")
|
| 91 |
+
print(f"\nFor {dataset_name}, sample count: {len(text_list)}")
|
| 92 |
+
print(f"For {dataset_name}, vocab size is: {len(text_vocab_set)}\n")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
max_workers = 32
|
| 97 |
+
|
| 98 |
+
tokenizer = "pinyin" # "pinyin" | "char"
|
| 99 |
+
polyphone = True
|
| 100 |
+
dataset_choice = 1 # 1: Premium, 2: Standard, 3: Basic
|
| 101 |
+
|
| 102 |
+
dataset_name = (
|
| 103 |
+
["WenetSpeech4TTS_Premium", "WenetSpeech4TTS_Standard", "WenetSpeech4TTS_Basic"][dataset_choice - 1]
|
| 104 |
+
+ "_"
|
| 105 |
+
+ tokenizer
|
| 106 |
+
)
|
| 107 |
+
dataset_paths = [
|
| 108 |
+
"<SOME_PATH>/WenetSpeech4TTS/Basic",
|
| 109 |
+
"<SOME_PATH>/WenetSpeech4TTS/Standard",
|
| 110 |
+
"<SOME_PATH>/WenetSpeech4TTS/Premium",
|
| 111 |
+
][-dataset_choice:]
|
| 112 |
+
save_dir = str(files("f5_tts").joinpath("../../")) + f"/data/{dataset_name}"
|
| 113 |
+
print(f"\nChoose Dataset: {dataset_name}, will save to {save_dir}\n")
|
| 114 |
+
|
| 115 |
+
main()
|
| 116 |
+
|
| 117 |
+
# Results (if adding alphabets with accents and symbols):
|
| 118 |
+
# WenetSpeech4TTS Basic Standard Premium
|
| 119 |
+
# samples count 3932473 1941220 407494
|
| 120 |
+
# pinyin vocab size 1349 1348 1344 (no polyphone)
|
| 121 |
+
# - - 1459 (polyphone)
|
| 122 |
+
# char vocab size 5264 5219 5042
|
| 123 |
+
|
| 124 |
+
# vocab size may be slightly different due to jieba tokenizer and pypinyin (e.g. way of polyphoneme)
|
| 125 |
+
# please be careful if using pretrained model, make sure the vocab.txt is same
|