Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+1
- README.md+116-9
- app.pyAdded+19
- apt.txtAdded+3
- denoised_audio.wavAdded+3
- inference_gradio.pyAdded+603
- lemas_tts/__init__.pyAdded+6
- lemas_tts/api.pyAdded+252
- lemas_tts/configs/multilingual_grl.yamlAdded+78
- lemas_tts/configs/multilingual_prosody.yamlAdded+78
- lemas_tts/infer/frontend.pyAdded+251
- lemas_tts/infer/infer_cli.pyAdded+386
- lemas_tts/infer/text_norm/__init__.pyAdded
- lemas_tts/infer/text_norm/cn_tn.pyAdded+824
- lemas_tts/infer/text_norm/en_tn.pyAdded+178
- lemas_tts/infer/text_norm/gp2py.pyAdded+148
- lemas_tts/infer/text_norm/id_tn.pyAdded+275
- lemas_tts/infer/text_norm/jieba_dict.txtAdded
- lemas_tts/infer/text_norm/pinyin-lexicon-r.txtAdded+4120
- lemas_tts/infer/text_norm/symbols.pyAdded+419
- lemas_tts/infer/text_norm/tokenizer.pyAdded+219
- lemas_tts/infer/text_norm/txt2pinyin.pyAdded+225
- lemas_tts/infer/utils_infer.pyAdded+651
- lemas_tts/model/backbones/README.mdAdded+20
- lemas_tts/model/backbones/dit.pyAdded+254
- lemas_tts/model/backbones/ecapa_tdnn.pyAdded+931
- lemas_tts/model/backbones/mmdit.pyAdded+189
- lemas_tts/model/backbones/prosody_encoder.pyAdded+433
- lemas_tts/model/backbones/unett.pyAdded+250
- lemas_tts/model/cfm.pyAdded+899
- lemas_tts/model/modules.pyAdded+802
- lemas_tts/model/utils.pyAdded+190
- lemas_tts/scripts/inference_gradio.pyAdded+584
- requirements.txtAdded+182
- uvr5/gui_data/constants.pyAdded+1147
- uvr5/lib_v5/mdxnet.pyAdded+140
- uvr5/lib_v5/mixer.ckptAdded+3
- uvr5/lib_v5/modules.pyAdded+74
- uvr5/lib_v5/pyrb.pyAdded+92
- uvr5/lib_v5/spec_utils.pyAdded+703
- uvr5/lib_v5/vr_network/__init__.pyAdded+1
- uvr5/lib_v5/vr_network/layers.pyAdded+143
- uvr5/lib_v5/vr_network/layers_new.pyAdded+126
- uvr5/lib_v5/vr_network/model_param_init.pyAdded+59
- uvr5/lib_v5/vr_network/modelparams/1band_sr16000_hl512.jsonAdded+19
- uvr5/lib_v5/vr_network/modelparams/1band_sr32000_hl512.jsonAdded+19
- uvr5/lib_v5/vr_network/modelparams/1band_sr33075_hl384.jsonAdded+19
- uvr5/lib_v5/vr_network/modelparams/1band_sr44100_hl1024.jsonAdded+19
- uvr5/lib_v5/vr_network/modelparams/1band_sr44100_hl256.jsonAdded+19
- uvr5/lib_v5/vr_network/modelparams/1band_sr44100_hl512.jsonAdded+19
32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textdenoised_audio.wav filter=lfs diff=lfs merge=lfs -text1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768234567896970717273747576777879808182838485861287888990919293949596979899100101102103104105106107108109110111112113114115116117118119# LEMAS-TTS Gradio Demo (Hugging Face Space)
This folder is a **clean, inference-only** version of LEMAS-TTS, organized for easy deployment on **Hugging Face Spaces**.
It keeps only:- the inference models & configs (`lemas_tts`)- pretrained checkpoints and vocab (`pretrained_models`)- the bundled UVR5 denoiser (`uvr5`)- a Gradio web UI (`inference_gradio.py`, `app.py`)
---
## 1. Features
- Zero-shot TTS: clone voice from a reference audio + reference text- Multilingual text input (Chinese / English / ES / IT / PT / DE, etc.)- Optional UVR5-based reference denoising- Two custom LEMAS checkpoints: - `multilingual_prosody_custom` - `multilingual_acc_grl_custom`
---
## 2. Project Structure
```textLEMAS-TTS_gradio/ app.py # HF Space entrypoint (Gradio Blocks) inference_gradio.py # Full Gradio UI & logic requirements.txt # Minimal runtime dependencies
lemas_tts/ # Core LEMAS-TTS package (inference only) api.py # F5TTS API (used by the UI) configs/ # Model configs (F5TTS / E2TTS) infer/ # Inference utilities & text frontend model/ # DiT backbone, utils, etc.
pretrained_models/ # All local assets needed for inference ckpts/ F5TTS_v1_Base_vocos_custom_multilingual_prosody/model_2698000.pt F5TTS_v1_Base_vocos_custom_multilingual_acc_grl/model_2680000.pt prosody_encoder/... vocos-mel-24khz/... data/ multilingual_prosody_custom/vocab.txt multilingual_acc_grl_custom/vocab.txt test_examples/*.wav # Demo audios used in the UI uvr5/ models/MDX_Net_Models/model_data/*.onnx, *.json
uvr5/ # Bundled UVR5 implementation for denoising```
`lemas_tts.api.F5TTS` automatically resolves `pretrained_models/` based on the repo layout, so no extra path configuration is required.
---
## 3. How to Run Locally
```bashcd LEMAS-TTS_gradiopip install -r requirements.txtpython app.py```
Then open the printed URL (default `http://127.0.0.1:7860`) in your browser.
---title: LEMAS TTSemoji: 🔥colorFrom: redcolorTo: indigosdk: gradiosdk_version: 6.2.0app_file: app.pypinned: false
## 4. Hugging Face Space Setup
1. Create a new Space (type: **Gradio**).2. Upload the contents of `LEMAS-TTS_gradio/` to the Space repo: - `app.py` - `inference_gradio.py` - `requirements.txt` - `lemas_tts/` - `pretrained_models/` - `uvr5/`3. In the Space settings, choose a GPU hardware profile (the model is heavy).4. The Space will automatically run `app.py` and launch the Gradio Blocks named `app`.
No extra arguments are needed; all paths are relative inside the repo.
---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference## 5. Usage Tips
- **Reference Text** should match the reference audio roughly in content and language for best voice cloning.- **Denoise**: - Turn on if your reference audio is noisy; it runs UVR5 on CPU. - Turn off if the reference is already clean (saves time).- **Seed**: - `-1` → random seed - Any other integer → reproducible output
---
## 6. 中文说明(简要)
这个目录是专门为 **Hugging Face Space** 打包的 **推理版 LEMAS-TTS**:
- 只保留推理相关代码(`lemas_tts`)、预训练模型(`pretrained_models`)和 UVR5 去噪模块(`uvr5`)- Gradio 入口为 `app.py`,内部调用 `inference_gradio.py` 里的 `app`(一个 `gr.Blocks` 界面)- `pretrained_models/` 下已经包含: - 自定义多语种 prosody / accent GRL 的 finetune 权重 - vocoder(`vocos-mel-24khz`) - prosody encoder - 以及示例语音 `test_examples/*.wav`
在本地或 Space 中运行步骤:
```bashpip install -r requirements.txtpython app.py```
然后在浏览器中打开提示的链接即可使用零样本 TTS Demo。
12345678910111213141516171819"""Gradio entrypoint for Hugging Face Spaces.
This file simply re-exports the `app` Blocks defined in `inference_gradio.py`so that Spaces can discover and launch it."""
import gradio as gr # noqa: F401
from inference_gradio import app as _app
# Expose as both `app` and `demo` for maximum compatibilityapp = _appdemo = _app
if __name__ == "__main__": app.queue(api_open=True).launch()
123ffmpegespeak-ngespeak123version https://git-lfs.github.com/spec/v1oid sha256:e7d715f294233999f56d51424b1ab8e9d28aed5dfb0821b427c2fb4f4abaa3aasize 1386548123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603import gcimport osimport platformimport psutilimport tempfilefrom glob import globimport tracebackimport clickimport gradio as grimport torchimport torchaudioimport soundfile as sffrom pathlib import Path
from cached_path import cached_path
from lemas_tts.api import TTS, PRETRAINED_ROOT, CKPTS_ROOT
# Global variablestts_api = Nonelast_checkpoint = ""last_device = ""last_ema = None
# Device detectiondevice = ( "cuda" if torch.cuda.is_available() else "xpu" if torch.xpu.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
REPO_ROOT = Path(__file__).resolve().parent
# HF location for pretrained assets (used as a fallback when local files are missing)HF_PRETRAINED_ROOT = "hf://LEMAS-Project/LEMAS-TTS/pretrained_models"
# 1) 指向你仓库里的 libespeak-ng.soESPEAK_LIB = PRETRAINED_ROOT / "espeak-ng-lib" / "libespeak-ng.so"os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = str(ESPEAK_LIB)
# 2) 指向你仓库里的 espeak-ng-dataESPEAK_DATA_DIR = PRETRAINED_ROOT / "espeak-ng-data"os.environ["ESPEAK_DATA_PATH"] = str(ESPEAK_DATA_DIR)os.environ["ESPEAKNG_DATA_PATH"] = str(ESPEAK_DATA_DIR)
class UVR5: """Small wrapper around the bundled uvr5 implementation for denoising."""
def __init__(self, model_dir: Path, code_dir: Path): self.model = self.load_model(str(model_dir), str(code_dir))
def load_model(self, model_dir: str, code_dir: str): import sys import json
if code_dir not in sys.path: sys.path.append(code_dir)
from multiprocess_cuda_infer import ModelData, Inference
model_path = os.path.join(model_dir, "Kim_Vocal_1.onnx") config_path = os.path.join(model_dir, "MDX-Net-Kim-Vocal1.json") configs = json.loads(open(config_path, "r", encoding="utf-8").read()) model_data = ModelData( model_path=model_path, audio_path=model_dir, result_path=model_dir, device="cpu", process_method="MDX-Net", base_dir=model_dir, # keep base_dir and model_dir the same (paths under `pretrained_models`) **configs, )
uvr5_model = Inference(model_data, "cpu") uvr5_model.load_model(model_path, 1) return uvr5_model
def denoise(self, audio_info): print("denoise UVR5: ", audio_info) input_audio = load_wav(audio_info, sr=44100, channel=2) output_audio = self.model.demix_base({0: input_audio.squeeze()}, is_match_mix=False) return output_audio.squeeze().T.numpy(), 44100
denoise_model = UVR5( model_dir=PRETRAINED_ROOT / "uvr5", code_dir=REPO_ROOT / "uvr5",)
def load_wav(audio_info, sr=16000, channel=1): print("load audio:", audio_info) audio, raw_sr = torchaudio.load(audio_info) audio = audio.T if len(audio.shape) > 1 and audio.shape[1] == 2 else audio audio = audio / torch.max(torch.abs(audio)) audio = audio.squeeze().float() if channel == 1 and len(audio.shape) == 2: # stereo to mono audio = audio.mean(dim=0, keepdim=True) elif channel == 2 and len(audio.shape) == 1: audio = torch.stack((audio, audio)) # mono to stereo if raw_sr != sr: audio = torchaudio.functional.resample(audio.squeeze(), raw_sr, sr) audio = torch.clip(audio, -0.999, 0.999).squeeze() return audio
def denoise(audio_info): save_path = "./denoised_audio.wav" denoised_audio, sr = denoise_model.denoise(audio_info) sf.write(save_path, denoised_audio, sr, format='wav', subtype='PCM_24') print("save denoised audio:", save_path) return save_path
def cancel_denoise(audio_info): return audio_info
def get_checkpoints_project(project_name=None, is_gradio=True): """Get available checkpoint files""" checkpoint_dir = [str(CKPTS_ROOT)] # Remote ckpt locations on HF (used if local ckpts are not present) remote_ckpts = { "multilingual_grl": f"{HF_PRETRAINED_ROOT}/ckpts/multilingual_grl/multilingual_grl.safetensors", "multilingual_prosody": f"{HF_PRETRAINED_ROOT}/ckpts/multilingual_prosody/multilingual_prosody.safetensors", }
if project_name is None: # Look for checkpoints in local directory files_checkpoints = [] for path in checkpoint_dir: if os.path.isdir(path): files_checkpoints.extend(glob(os.path.join(path, "**/*.pt"), recursive=True)) files_checkpoints.extend(glob(os.path.join(path, "**/*.safetensors"), recursive=True)) break # Fallback: use HF ckpts if not files_checkpoints: files_checkpoints = list(remote_ckpts.values()) else: if os.path.isdir(checkpoint_dir[0]): files_checkpoints = glob(os.path.join(checkpoint_dir[0], project_name, "*.pt")) files_checkpoints.extend(glob(os.path.join(checkpoint_dir[0], project_name, "*.safetensors"))) else: ckpt = remote_ckpts.get(project_name) files_checkpoints = [ckpt] if ckpt is not None else [] print("files_checkpoints:", project_name, files_checkpoints) # Separate pretrained and regular checkpoints pretrained_checkpoints = [f for f in files_checkpoints if "pretrained_" in os.path.basename(f)] regular_checkpoints = [ f for f in files_checkpoints if "pretrained_" not in os.path.basename(f) and "model_last.pt" not in os.path.basename(f) ] last_checkpoint = [f for f in files_checkpoints if "model_last.pt" in os.path.basename(f)]
# Sort regular checkpoints by number try: regular_checkpoints = sorted( regular_checkpoints, key=lambda x: int(os.path.basename(x).split("_")[1].split(".")[0]) ) except (IndexError, ValueError): regular_checkpoints = sorted(regular_checkpoints)
# Combine in order: pretrained, regular, last files_checkpoints = pretrained_checkpoints + regular_checkpoints + last_checkpoint
select_checkpoint = None if not files_checkpoints else files_checkpoints[-1]
if is_gradio: return gr.update(choices=files_checkpoints, value=select_checkpoint)
return files_checkpoints, select_checkpoint
def get_available_projects(): """Get available project names from data directory""" data_paths = [ str(PRETRAINED_ROOT / "data"), ] project_list = [] for data_path in data_paths: if os.path.isdir(data_path): for folder in os.listdir(data_path): path_folder = os.path.join(data_path, folder) if "test" not in folder: project_list.append(folder) break # Fallback: if no local data dir, default to known HF projects if not project_list: project_list = ["multilingual_grl", "multilingual_prosody"] project_list.sort() print("project_list:", project_list) return project_list
def infer( project, file_checkpoint, exp_name, ref_text, ref_audio, denoise_audio, gen_text, nfe_step, use_ema, separate_langs, frontend, speed, cfg_strength, use_acc_grl, ref_ratio, no_ref_audio, sway_sampling_coef, use_prosody_encoder, seed): global last_checkpoint, last_device, tts_api, last_ema
# Resolve checkpoint path (local or HF) ckpt_path = file_checkpoint if isinstance(ckpt_path, str) and ckpt_path.startswith("hf://"): try: ckpt_resolved = str(cached_path(ckpt_path)) except Exception as e: traceback.print_exc() return None, f"Error downloading checkpoint: {str(e)}", "" else: ckpt_resolved = ckpt_path
if not os.path.isfile(ckpt_resolved): return None, "Checkpoint not found!", ""
if denoise_audio: ref_audio = denoise_audio
device_test = device # Use the global device
if last_checkpoint != ckpt_resolved or last_device != device_test or last_ema != use_ema or tts_api is None: if last_checkpoint != ckpt_resolved: last_checkpoint = ckpt_resolved
if last_device != device_test: last_device = device_test
if last_ema != use_ema: last_ema = use_ema
# Automatically enable prosody encoder when using the prosody checkpoint use_prosody_encoder = True if "prosody" in str(ckpt_resolved) else False
# Resolve vocab file (local or HF) local_vocab = Path(PRETRAINED_ROOT) / "data" / project / "vocab.txt" if local_vocab.is_file(): vocab_file = str(local_vocab) else: remote_vocab_map = { "multilingual_grl": f"{HF_PRETRAINED_ROOT}/data/multilingual_grl/vocab.txt", "multilingual_prosody": f"{HF_PRETRAINED_ROOT}/data/multilingual_prosody/vocab.txt", } remote_vocab = remote_vocab_map.get(project) if remote_vocab is None: return None, "Vocab file not found!", "" try: vocab_file = str(cached_path(remote_vocab)) except Exception as e: traceback.print_exc() return None, f"Error downloading vocab: {str(e)}", ""
# Resolve prosody encoder config & weights local_prosody_cfg = CKPTS_ROOT / "prosody_encoder" / "pretssel_cfg.json" local_prosody_ckpt = CKPTS_ROOT / "prosody_encoder" / "prosody_encoder_UnitY2.pt" if local_prosody_cfg.is_file(): prosody_cfg_path = str(local_prosody_cfg) else: prosody_cfg_path = str( cached_path(f"{HF_PRETRAINED_ROOT}/ckpts/prosody_encoder/pretssel_cfg.json") ) if local_prosody_ckpt.is_file(): prosody_ckpt_path = str(local_prosody_ckpt) else: prosody_ckpt_path = str( cached_path(f"{HF_PRETRAINED_ROOT}/ckpts/prosody_encoder/prosody_encoder_UnitY2.pt") )
try: tts_api = TTS( model=exp_name, ckpt_file=ckpt_resolved, vocab_file=vocab_file, device=device_test, use_ema=use_ema, frontend=frontend, use_prosody_encoder=use_prosody_encoder, prosody_cfg_path=prosody_cfg_path, prosody_ckpt_path=prosody_ckpt_path, ) except Exception as e: traceback.print_exc() return None, f"Error loading model: {str(e)}", ""
print("Model loaded >>", device_test, file_checkpoint, use_ema)
if seed == -1: # -1 used for random seed = None
try: with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f: tts_api.infer( ref_file=ref_audio, ref_text=ref_text.strip(), gen_text=gen_text.strip(), nfe_step=nfe_step, separate_langs=separate_langs, speed=speed, cfg_strength=cfg_strength, sway_sampling_coef=sway_sampling_coef, use_acc_grl=use_acc_grl, ref_ratio=ref_ratio, no_ref_audio=no_ref_audio, use_prosody_encoder=use_prosody_encoder, file_wave=f.name, seed=seed, ) return f.name, f"Device: {tts_api.device}", str(tts_api.seed) except Exception as e: traceback.print_exc() return None, f"Inference error: {str(e)}", ""
def get_gpu_stats(): """Get GPU statistics""" gpu_stats = ""
if torch.cuda.is_available(): gpu_count = torch.cuda.device_count() for i in range(gpu_count): gpu_name = torch.cuda.get_device_name(i) gpu_properties = torch.cuda.get_device_properties(i) total_memory = gpu_properties.total_memory / (1024**3) # in GB allocated_memory = torch.cuda.memory_allocated(i) / (1024**2) # in MB reserved_memory = torch.cuda.memory_reserved(i) / (1024**2) # in MB
gpu_stats += ( f"GPU {i} Name: {gpu_name}\n" f"Total GPU memory (GPU {i}): {total_memory:.2f} GB\n" f"Allocated GPU memory (GPU {i}): {allocated_memory:.2f} MB\n" f"Reserved GPU memory (GPU {i}): {reserved_memory:.2f} MB\n\n" ) elif torch.xpu.is_available(): gpu_count = torch.xpu.device_count() for i in range(gpu_count): gpu_name = torch.xpu.get_device_name(i) gpu_properties = torch.xpu.get_device_properties(i) total_memory = gpu_properties.total_memory / (1024**3) # in GB allocated_memory = torch.xpu.memory_allocated(i) / (1024**2) # in MB reserved_memory = torch.xpu.memory_reserved(i) / (1024**2) # in MB
gpu_stats += ( f"GPU {i} Name: {gpu_name}\n" f"Total GPU memory (GPU {i}): {total_memory:.2f} GB\n" f"Allocated GPU memory (GPU {i}): {allocated_memory:.2f} MB\n" f"Reserved GPU memory (GPU {i}): {reserved_memory:.2f} MB\n\n" ) elif torch.backends.mps.is_available(): gpu_count = 1 gpu_stats += "MPS GPU\n" total_memory = psutil.virtual_memory().total / ( 1024**3 ) # Total system memory (MPS doesn't have its own memory) allocated_memory = 0 reserved_memory = 0
gpu_stats += ( f"Total system memory: {total_memory:.2f} GB\n" f"Allocated GPU memory (MPS): {allocated_memory:.2f} MB\n" f"Reserved GPU memory (MPS): {reserved_memory:.2f} MB\n" )
else: gpu_stats = "No GPU available"
return gpu_stats
def get_cpu_stats(): """Get CPU statistics""" cpu_usage = psutil.cpu_percent(interval=1) memory_info = psutil.virtual_memory() memory_used = memory_info.used / (1024**2) memory_total = memory_info.total / (1024**2) memory_percent = memory_info.percent
pid = os.getpid() process = psutil.Process(pid) nice_value = process.nice()
cpu_stats = ( f"CPU Usage: {cpu_usage:.2f}%\n" f"System Memory: {memory_used:.2f} MB used / {memory_total:.2f} MB total ({memory_percent}% used)\n" f"Process Priority (Nice value): {nice_value}" )
return cpu_stats
def get_combined_stats(): """Get combined system stats""" gpu_stats = get_gpu_stats() cpu_stats = get_cpu_stats() combined_stats = f"### GPU Stats\n{gpu_stats}\n\n### CPU Stats\n{cpu_stats}" return combined_stats
# Create Gradio interfacewith gr.Blocks(title="LEMAS-TTS Inference") as app: gr.Markdown( """ # Zero-Shot TTS
Set seed to -1 for random generation. """ ) with gr.Accordion("Model configuration", open=False): # Model configuration with gr.Row(): exp_name = gr.Radio( label="Model", choices=["multilingual_grl", "multilingual_prosody"], value="multilingual_grl", visible=False, ) # Project selection available_projects = get_available_projects()
# Get initial checkpoints list_checkpoints, checkpoint_select = get_checkpoints_project(available_projects[0] if available_projects else None, False)
with gr.Row(): with gr.Column(scale=1): # load_models_btn = gr.Button(value="Load models") cm_project = gr.Dropdown( choices=available_projects, value=available_projects[0] if available_projects else None, label="Project", allow_custom_value=True, scale=4 ) with gr.Column(scale=5): cm_checkpoint = gr.Dropdown( choices=list_checkpoints, value=checkpoint_select, label="Checkpoints", allow_custom_value=True # scale=4, ) bt_checkpoint_refresh = gr.Button("Refresh", scale=1)
with gr.Row(): ch_use_ema = gr.Checkbox(label="Use EMA", visible=False, value=True, scale=2, info="Turn off at early stage might offer better results") frontend = gr.Radio(label="Frontend", visible=False, choices=["phone", "char", "bpe"], value="phone", scale=3) separate_langs = gr.Checkbox(label="Separate Languages", visible=False, value=True, scale=2, info="separate language tokens")
# Inference parameters with gr.Row(): nfe_step = gr.Number(label="NFE Step", scale=1, value=64) speed = gr.Slider(label="Speed", scale=3, value=1.0, minimum=0.5, maximum=1.5, step=0.1) cfg_strength = gr.Slider(label="CFG Strength", scale=2, value=5.0, minimum=0.0, maximum=10.0, step=1) sway_sampling_coef = gr.Slider(label="Sway Sampling Coef", scale=2, value=3, minimum=2, maximum=5, step=0.1) ref_ratio = gr.Slider(label="Ref Ratio", scale=2, value=1.0, minimum=0.0, maximum=1.0, step=0.1) no_ref_audio = gr.Checkbox(label="No Reference Audio", visible=False, value=False, scale=1, info="No mel condition") use_acc_grl = gr.Checkbox(label="Use accent grl condition", visible=False, value=True, scale=1, info="Use accent grl condition") use_prosody_encoder = gr.Checkbox(label="Use prosody encoder", visible=False, value=False, scale=1, info="Use prosody encoder") seed = gr.Number(label="Random Seed", scale=1, value=-1, minimum=-1)
# Input fields ref_text = gr.Textbox(label="Reference Text", placeholder="Enter the text for the reference audio...") ref_audio = gr.Audio(label="Reference Audio", type="filepath", interactive=True, show_download_button=True, editable=True)
with gr.Accordion("Denoise audio (Optional / Recommend)", open=True): with gr.Row(): denoise_btn = gr.Button(value="Denoise") cancel_btn = gr.Button(value="Cancel Denoise") denoise_audio = gr.Audio(label="Denoised Audio", value=None, type="filepath", interactive=True, show_download_button=True, editable=True)
gen_text = gr.Textbox(label="Text to Generate", placeholder="Enter the text you want to generate...")
# Inference button and outputs with gr.Row(): txt_info_gpu = gr.Textbox("", label="Device Info") seed_info = gr.Textbox(label="Used Random Seed") check_button_infer = gr.Button("Generate Audio", variant="primary")
gen_audio = gr.Audio(label="Generated Audio", type="filepath", interactive=True, show_download_button=True, editable=True)
# Examples def _resolve_example(name: str) -> str: local = PRETRAINED_ROOT / "data" / "test_examples" / name if local.is_file(): return str(local) remote_map = { "en.wav": f"{HF_PRETRAINED_ROOT}/data/test_examples/en.wav", "es.wav": f"{HF_PRETRAINED_ROOT}/data/test_examples/es.wav", "pt.wav": f"{HF_PRETRAINED_ROOT}/data/test_examples/pt.wav", } url = remote_map.get(name) return str(cached_path(url)) if url is not None else ""
examples = gr.Examples( examples=[ ["em, #1 I have a list of YouTubers, and I'm gonna be going to their houses and raiding them by.", _resolve_example("en.wav"), "我有一份 YouTuber 名单,我打算去他们家,对他们进行突袭。", ], ["Te voy a dar un tip #1 que le copia a John Rockefeller, uno de los empresarios más picudos de la historia.", _resolve_example("es.wav"), "我要给你一个从历史上最精明的商人之一约翰·洛克菲勒那里抄来的秘诀。", ], ["Nova, #1 dia 25 desse mês vai rolar operação the last Frontier.", _resolve_example("pt.wav"), "新消息,本月二十五日,'最后的边疆行动'将启动。", ], ], inputs=[ ref_text, ref_audio, gen_text, ], outputs=[gen_audio, txt_info_gpu, seed_info], fn=infer, cache_examples=False )
# System Info section at the bottom gr.Markdown("---") gr.Markdown("## System Information") with gr.Accordion("Update System Stats", open=False): update_button = gr.Button("Update System Stats", scale=1) output_box = gr.Textbox(label="GPU and CPU Information", lines=5, scale=5)
def update_stats(): return get_combined_stats() denoise_btn.click(fn=denoise, inputs=[ref_audio], outputs=[denoise_audio])
cancel_btn.click(fn=cancel_denoise, inputs=[ref_audio], outputs=[denoise_audio])
# Event handlers check_button_infer.click( fn=infer, inputs=[ cm_project, cm_checkpoint, exp_name, ref_text, ref_audio, denoise_audio, gen_text, nfe_step, ch_use_ema, separate_langs, frontend, speed, cfg_strength, use_acc_grl, ref_ratio, no_ref_audio, sway_sampling_coef, use_prosody_encoder, seed, ], outputs=[gen_audio, txt_info_gpu, seed_info], )
bt_checkpoint_refresh.click(fn=get_checkpoints_project, inputs=[cm_project], outputs=[cm_checkpoint]) cm_project.change(fn=get_checkpoints_project, inputs=[cm_project], outputs=[cm_checkpoint])
ref_audio.change( fn=lambda x: None, inputs=[ref_audio], outputs=[denoise_audio] )
update_button.click(fn=update_stats, outputs=output_box) # Auto-load system stats on startup app.load(fn=update_stats, outputs=output_box)
@click.command()@click.option("--port", "-p", default=7860, type=int, help="Port to run the app on")@click.option("--host", "-H", default="0.0.0.0", help="Host to run the app on")@click.option( "--share", "-s", default=False, is_flag=True, help="Share the app via Gradio share link",)@click.option("--api", "-a", default=True, is_flag=True, help="Allow API access")def main(port, host, share, api): global app print("Starting LEMAS-TTS Inference Interface...") print(f"Device: {device}") app.queue(api_open=api).launch( server_name=host, server_port=port, share=share, show_api=api, allowed_paths=[str(PRETRAINED_ROOT / "data")], )
if __name__ == "__main__": main()123456from .api import TTS
__all__ = ["TTS"]
__version__ = "0.1.0"
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252import randomimport sysfrom pathlib import Pathimport re, regeximport soundfile as sfimport tqdmfrom cached_path import cached_pathfrom hydra.utils import get_classfrom omegaconf import OmegaConf
from lemas_tts.infer.utils_infer import ( load_model, load_vocoder, transcribe, preprocess_ref_audio_text, infer_process, remove_silence_for_generated_wav, save_spectrogram,)from lemas_tts.model.utils import seed_everythingfrom lemas_tts.model.backbones.dit import DiT
# Resolve repository layout so we can find pretrained assets (ckpts, vocoder, etc.)THIS_FILE = Path(__file__).resolve()
def _find_repo_root(start: Path) -> Path: """Locate the repo root by looking for a `pretrained_models` folder upwards.""" for p in [start, *start.parents]: if (p / "pretrained_models").is_dir(): return p cwd = Path.cwd() if (cwd / "pretrained_models").is_dir(): return cwd return start
REPO_ROOT = _find_repo_root(THIS_FILE)# Local pretrained root (used when running from a repo / Space that bundles weights)PRETRAINED_ROOT = REPO_ROOT / "pretrained_models"# Remote pretrained root on Hugging Face Hub (fallback when local files are absent)HF_PRETRAINED_ROOT = "hf://LEMAS-Project/LEMAS-TTS/pretrained_models"CKPTS_ROOT = PRETRAINED_ROOT / "ckpts"
class TTS: def __init__( self, model="multilingual", ckpt_file="", vocab_file="", ode_method="euler", use_ema=False, vocoder_local_path=str(CKPTS_ROOT / "vocos-mel-24khz"), use_prosody_encoder=False, prosody_cfg_path="", prosody_ckpt_path="", device=None, hf_cache_dir=None, frontend="phone", ): # Load model architecture config from bundled yaml config_dir = THIS_FILE.parent / "configs" model_cfg = OmegaConf.load(config_dir / f"{model}.yaml") # model_cls = get_class(f"lemas_tts.model.dit.{model_cfg.model.backbone}") model_arc = model_cfg.model.arch
self.mel_spec_type = model_cfg.model.mel_spec.mel_spec_type self.target_sample_rate = model_cfg.model.mel_spec.target_sample_rate
self.ode_method = ode_method self.use_ema = use_ema self.langs = {"cmn":"zh", "zh":"zh", "en":"en-us", "it":"it", "es":"es", "pt":"pt-br", "fr":"fr-fr", "de":"de", "ru":"ru", "id":"id", "vi":"vi", "th":"th"} if device is not None: self.device = device else: import torch
self.device = ( "cuda" if torch.cuda.is_available() else "xpu" if torch.xpu.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" )
# # Load models # Prefer local vocoder directory if it exists; otherwise let `load_vocoder` # fall back to downloading from the default HF repo (charactr/vocos-mel-24khz). vocoder_is_local = False if vocoder_local_path is not None: try: vocoder_is_local = Path(vocoder_local_path).is_dir() except TypeError: vocoder_is_local = False
self.vocoder = load_vocoder( self.mel_spec_type, vocoder_is_local, vocoder_local_path, self.device, hf_cache_dir ) # self.vocoder = load_vocoder(vocoder_name="vocos", is_local=True, local_path=vocoder_local_path, device=self.device) if frontend is not None: from lemas_tts.infer.frontend import TextNorm self.frontend = TextNorm(dtype=frontend) else: self.frontend = None
self.ema_model = load_model( DiT, model_arc, ckpt_file, self.mel_spec_type, vocab_file, self.ode_method, self.use_ema, self.device, use_prosody_encoder=use_prosody_encoder, prosody_cfg_path=prosody_cfg_path, prosody_ckpt_path=prosody_ckpt_path, )
def transcribe(self, ref_audio, language=None): return transcribe(ref_audio, language)
def export_wav(self, wav, file_wave, remove_silence=False): sf.write(file_wave, wav, self.target_sample_rate)
if remove_silence: remove_silence_for_generated_wav(file_wave)
def export_spectrogram(self, spec, file_spec): save_spectrogram(spec, file_spec)
def infer( self, ref_file, ref_text, gen_text, show_info=print, progress=tqdm, target_rms=0.1, cross_fade_duration=0.15, use_acc_grl=False, ref_ratio=None, no_ref_audio=False, cfg_strength=2, nfe_step=32, speed=1.0, sway_sampling_coef=5, separate_langs=False, fix_duration=None, use_prosody_encoder=True, file_wave=None, file_spec=None, seed=None, ): if seed is None: seed = random.randint(0, sys.maxsize) seed_everything(seed) self.seed = seed
ref_file, ref_text = preprocess_ref_audio_text(ref_file, ref_text) print("preprocesss:\n", "ref_file:", ref_file, "\nref_text:", ref_text) if self.frontend.dtype == "phone": ref_text = self.frontend.text2phn(ref_text+". ").replace("(cmn)", "(zh)").split("|") gen_text = gen_text.split("\n") gen_text = [self.frontend.text2phn(x+". ").replace("(cmn)", "(zh)").split("|") for x in gen_text] elif self.frontend.dtype == "char": src_lang, ref_text = self.frontend.text2norm(ref_text+". ") ref_text = ["("+src_lang.replace("cmn", "zh")+")"] + list(ref_text) gen_text = gen_text.split("\n") gen_text = [self.frontend.text2norm(x+". ") for x in gen_text] gen_text = [["("+x[0].replace("cmn", "zh")+")"] + list(x[1]) for x in gen_text] print("after frontend:\n", "ref_text:", ref_text, "\ngen_text:", gen_text)
if separate_langs: ref_text = self.process_phone_list(ref_text) # Optional gen_text = [self.process_phone_list(x) for x in gen_text] print("gen_text:", gen_text, "\nref_text:", ref_text)
wav, sr, spec = infer_process( ref_file, ref_text, gen_text, self.ema_model, self.vocoder, self.mel_spec_type, show_info=show_info, progress=progress, target_rms=target_rms, cross_fade_duration=cross_fade_duration, nfe_step=nfe_step, cfg_strength=cfg_strength, sway_sampling_coef=sway_sampling_coef, use_prosody_encoder=use_prosody_encoder, use_acc_grl=use_acc_grl, ref_ratio=ref_ratio, no_ref_audio=no_ref_audio, speed=speed, fix_duration=fix_duration, device=self.device, )
if file_wave is not None: self.export_wav(wav, file_wave, remove_silence=False)
if file_spec is not None: self.export_spectrogram(spec, file_spec)
return wav, sr, spec
def process_phone_list(self, parts): puncs = {"#1", "#2", "#3", "#4", "_", "!", ",", ".", "?", '"', "'", "^", "。", ",", "?", "!"} """(vocab756 ver)处理phone list,给不带language id的phone添加当前language id前缀""" # parts = phn_str.split('|') processed = [] current_lang = "" for i in range(len(parts)): part = parts[i] if part.startswith('(') and part.endswith(')') and part[1:-1] in self.langs: # 这是一个language id current_lang = part # processed.append(part) elif part in puncs: # not bool(regex.search(r'\p{L}', part[0])): # 匹配非字母数字、非空格的字符 # 是停顿符或标点 if len(processed) > 0 and processed[-1] == "_": processed.pop() elif len(processed) > 0 and processed[-1] in puncs and part == "_": continue processed.append(part) # if i < len(parts) - 1 and parts[i+1] != "_": # processed.append("_") elif current_lang is not None: # 不是language id且有当前language id,添加前缀 processed.append(f"{current_lang}{part}") return processed
if __name__ == "__main__": f5tts = F5TTS()
wav, sr, spec = f5tts.infer( ref_file=str((THIS_FILE.parent / "infer" / "examples" / "basic" / "basic_ref_en.wav").resolve()), ref_text="some call me nature, others call me mother nature.", gen_text=( "I don't really care what you call me. I've been a silent spectator, watching species evolve, " "empires rise and fall. But always remember, I am mighty and enduring. Respect me and I'll nurture " "you; ignore me and you shall face the consequences." ), file_wave=str((REPO_ROOT / "outputs" / "api_out.wav").resolve()), file_spec=str((REPO_ROOT / "outputs" / "api_out.png").resolve()), seed=None, )
print("seed :", f5tts.seed)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778# compute_environment: LOCAL_MACHINE# debug: false# distributed_type: MULTI_GPU# downcast_bf16: 'no'# enable_cpu_affinity: true# gpu_ids: all# # machine_rank: 0# # main_training_function: main# mixed_precision: bf16# num_machines: 1# num_processes: 16# # rdzv_backend: static# same_network: true# use_cpu: false
hydra: run: dir: exp/${model.name}_${model.mel_spec.mel_spec_type}_${model.tokenizer}_${datasets.name}/${now:%Y-%m-%d}/${now:%H-%M-%S}
datasets: name: multilingual_vocab898_acc_grl_ctc_fix # dataset name batch_size_per_gpu: 40000 # 8 GPUs, 8 * 38400 = 307200 batch_size_type: frame # frame | sample max_samples: 64 # max sequences per batch if use frame-wise batch_size. we set 32 for small models, 64 for base models num_workers: 2 separate_langs: True
optim: epochs: 100 learning_rate: 2e-5 num_warmup_updates: 1000 # warmup updates grad_accumulation_steps: 1 # note: updates = steps / grad_accumulation_steps max_grad_norm: 1.0 # gradient clipping bnb_optimizer: False # use bnb 8bit AdamW optimizer or notmodel: name: multilingual # model name tokenizer: custom # tokenizer type tokenizer_path: "pretrained_models/data/multilingual_grl/vocab.txt" # if 'custom' tokenizer, define the path want to use (should be vocab.txt) audio_dir: "pretrained_models/data/multilingual_grl" use_ctc_loss: True # whether to use ctc loss use_spk_enc: False use_prosody_encoder: False prosody_cfg_path: "pretrained_models/ckpts/prosody_encoder/pretssel_cfg.json" # pretssel_cfg.json prosody_ckpt_path: "pretrained_models/ckpts/prosody_encoder/prosody_encoder_UnitY2.pt" # prosody_encoder_pretssel.pt
backbone: DiT arch: dim: 1024 depth: 22 heads: 16 ff_mult: 2 text_dim: 512 text_mask_padding: True qk_norm: null # null | rms_norm conv_layers: 4 pe_attn_head: null checkpoint_activations: False # recompute activations and save memory for extra compute mel_spec: target_sample_rate: 24000 n_mel_channels: 100 hop_length: 256 win_length: 1024 n_fft: 1024 mel_spec_type: vocos # vocos | bigvgan vocoder: is_local: True # use local offline ckpt or not # Path in the original training environment; kept here for reference only. # For the open-sourced LEMAS-TTS repo, use `pretrained_models/ckpts/vocos-mel-24khz`. local_path: "pretrained_models/ckpts/vocos-mel-24khz" # local vocoder path
ckpts: logger: tensorboard # wandb | tensorboard | null log_samples: True # infer random sample per save checkpoint. wip, normal to fail with extra long samples save_per_updates: 1000 # save checkpoint per updates keep_last_n_checkpoints: -1 # -1 to keep all, 0 to not save intermediate, > 0 to keep last N checkpoints last_per_updates: 1000 # save last checkpoint per updates save_dir: ckpts/${model.name}_${model.mel_spec.mel_spec_type}_${model.tokenizer}_${datasets.name}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778# compute_environment: LOCAL_MACHINE# debug: false# distributed_type: MULTI_GPU# downcast_bf16: 'no'# enable_cpu_affinity: true# gpu_ids: all# # machine_rank: 0# # main_training_function: main# mixed_precision: bf16# num_machines: 1# num_processes: 16# # rdzv_backend: static# same_network: true# use_cpu: false
hydra: run: dir: exp/${model.name}_${model.mel_spec.mel_spec_type}_${model.tokenizer}_${datasets.name}/${now:%Y-%m-%d}/${now:%H-%M-%S}
datasets: name: multilingual_vocab898_acc_grl_prosody_ctc_fix # dataset name batch_size_per_gpu: 40000 # 8 GPUs, 8 * 38400 = 307200 batch_size_type: frame # frame | sample max_samples: 64 # max sequences per batch if use frame-wise batch_size. we set 32 for small models, 64 for base models num_workers: 2 separate_langs: True
optim: epochs: 100 learning_rate: 2e-5 num_warmup_updates: 1000 # warmup updates grad_accumulation_steps: 1 # note: updates = steps / grad_accumulation_steps max_grad_norm: 1.0 # gradient clipping bnb_optimizer: False # use bnb 8bit AdamW optimizer or notmodel: name: multilingual # model name tokenizer: custom # tokenizer type tokenizer_path: "pretrained_models/data/multilingual_grl/vocab.txt" # if 'custom' tokenizer, define the path want to use (should be vocab.txt) audio_dir: "pretrained_models/data/multilingual_grl" use_ctc_loss: True # whether to use ctc loss use_spk_enc: False use_prosody_encoder: True prosody_cfg_path: "pretrained_models/ckpts/prosody_encoder/pretssel_cfg.json" # pretssel_cfg.json prosody_ckpt_path: "pretrained_models/ckpts/prosody_encoder/prosody_encoder_UnitY2.pt" # prosody_encoder_pretssel.pt
backbone: DiT arch: dim: 1024 depth: 22 heads: 16 ff_mult: 2 text_dim: 512 text_mask_padding: True qk_norm: null # null | rms_norm conv_layers: 4 pe_attn_head: null checkpoint_activations: False # recompute activations and save memory for extra compute mel_spec: target_sample_rate: 24000 n_mel_channels: 100 hop_length: 256 win_length: 1024 n_fft: 1024 mel_spec_type: vocos # vocos | bigvgan vocoder: is_local: True # use local offline ckpt or not # Path in the original training environment; kept here for reference only. # For the open-sourced LEMAS-TTS repo, use `pretrained_models/ckpts/vocos-mel-24khz`. local_path: "pretrained_models/ckpts/vocos-mel-24khz" # local vocoder path
ckpts: logger: tensorboard # wandb | tensorboard | null log_samples: True # infer random sample per save checkpoint. wip, normal to fail with extra long samples save_per_updates: 1000 # save checkpoint per updates keep_last_n_checkpoints: -1 # -1 to keep all, 0 to not save intermediate, > 0 to keep last N checkpoints last_per_updates: 1000 # save last checkpoint per updates save_dir: ckpts/${model.name}_${model.mel_spec.mel_spec_type}_${model.tokenizer}_${datasets.name}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251import os, re, regeximport langidimport uroman as urimport jieba, zhconvfrom num2words import num2words
jieba.set_dictionary(dictionary_path=os.path.join(os.path.dirname(__file__) + "/../infer/text_norm/jieba_dict.txt"))# from pypinyin.core import Pinyinfrom pypinyin import pinyin, lazy_pinyin, Style
from .text_norm.txt2pinyin import _PAUSE_SYMBOL, get_phoneme_from_char_and_pinyinfrom .text_norm.cn_tn import NSWNormalizerfrom .text_norm.tokenizer import TextTokenizer, txt2phonefrom pypinyin.contrib.tone_convert import to_initials, to_finals_tone3from pypinyin_dict.phrase_pinyin_data import large_pinyin # large_pinyin # cc_cedictlarge_pinyin.load()
class TextNorm(): def __init__(self, dtype="phone"): # my_pinyin = Pinyin(MyConverter()) # self.pinyin_parser = my_pinyin.pinyin cmn_lexicon = open(os.path.join(os.path.dirname(__file__)+'/../infer/text_norm/pinyin-lexicon-r.txt'),'r', encoding="utf-8").readlines() cmn_lexicon = [x.strip().split() for x in cmn_lexicon] self.cmn_dict = {x[0]:x[1:] for x in cmn_lexicon} langid.set_languages(['es','pt','zh','en','de','fr','it','ru', 'vi','id','th','ja','ko','ar']) langs = {"en":"en-us", "it":"it", "es":"es", "pt":"pt-br", "fr":"fr-fr", "de":"de", "ru":"ru", "vi":"vi", "id":"id", "th":"th", "ja":"ja", "ko":"ko"} # "zh":"cmn", "cmn":"cmn", "ar":"ar-sa"} text_tokenizer = {} for k,v in langs.items(): tokenizer = TextTokenizer(language=v, backend="espeak") lang = "zh" if k == "cmn" else k text_tokenizer[k] = (lang, tokenizer) self.text_tokenizer = text_tokenizer self.cn_tn = NSWNormalizer() self.dtype = dtype
def detect_lang(self, text): lang, _ = langid.classify(text)[0] return lang
def sil_type(self, time_s): if round(time_s) < 0.4: return "" elif round(time_s) >= 0.4 and round(time_s) < 0.8: return "#1" elif round(time_s) >= 0.8 and round(time_s) < 1.5: return "#2" elif round(time_s) >= 1.5 and round(time_s) < 3.0: return "#3" elif round(time_s) >= 3.0: return "#4"
def add_sil_raw(self, sub_list, start_time, end_time, target_transcript): txt = [] txt_list = [x["word"] for x in sub_list] sil = self.sil_type(sub_list[0]["start"]) if len(sil) > 0: txt.append(sil) txt.append(txt_list[0]) for i in range(1, len(sub_list)): if sub_list[i]["start"] >= start_time and sub_list[i]["end"] <= end_time: txt.append(target_transcript) target_transcript = "" else: sil = self.sil_type(sub_list[i]["start"] - sub_list[i-1]["end"]) if len(sil) > 0: txt.append(sil) txt.append(txt_list[i]) return ' '.join(txt)
def add_sil(self, sub_list, start_time, end_time, target_transcript, src_lang, tar_lang): txts = [] txt_list = [x["word"] for x in sub_list] sil = self.sil_type(sub_list[0]["start"]) if len(sil) > 0: txts.append([src_lang, sil])
if sub_list[0]["start"] < start_time: txts.append([src_lang, txt_list[0]]) for i in range(1, len(sub_list)): if sub_list[i]["start"] >= start_time and sub_list[i]["end"] <= end_time: txts.append([tar_lang, target_transcript]) target_transcript = "" else: sil = self.sil_type(sub_list[i]["start"] - sub_list[i-1]["end"]) if len(sil) > 0: txts.append([src_lang, sil]) txts.append([src_lang, txt_list[i]]) target_txt = [txts[0]] for txt in txts[1:]: if txt[1] == "": continue if txt[0] != target_txt[-1][0]: target_txt.append([txt[0], ""]) target_txt[-1][-1] += " " + txt[1] return target_txt
def replace_numbers_with_words(self, sentence, lang="en"): sentence = re.sub(r'(\d+)', r' \1 ', sentence) # add spaces around numbers def replace_with_words(match): num = match.group(0) try: return num2words(num, lang=lang) # Convert numbers to words except: return num # In case num2words fails (unlikely with digits but just to be safe) return re.sub(r'\b\d+\b', replace_with_words, sentence) # Regular expression that matches numbers
def get_prompt(self, sub_list, start_time, end_time, src_lang): txts = [] txt_list = [x["word"] for x in sub_list]
if start_time <= sub_list[0]["start"]: sil = self.sil_type(sub_list[0]["start"]) if len(sil) > 0: txts.append([src_lang, sil]) txts.append([src_lang, txt_list[0]]) for i in range(1, len(sub_list)): # if sub_list[i]["start"] <= start_time and sub_list[i]["end"] <= end_time: # txts.append([tar_lang, target_transcript]) # target_transcript = "" if sub_list[i]["start"] >= start_time and sub_list[i]["end"] <= end_time: sil = self.sil_type(sub_list[i]["start"] - sub_list[i-1]["end"]) if len(sil) > 0: txts.append([src_lang, sil]) txts.append([src_lang, txt_list[i]])
target_txt = [txts[0]] for txt in txts[1:]: if txt[1] == "": continue if txt[0] != target_txt[-1][0]: target_txt.append([txt[0], ""]) target_txt[-1][-1] += " " + txt[1] return target_txt
def txt2pinyin(self, text): txts, phonemes = [], [] texts = re.split(r"(#\d)", text) print("before norm: ", texts) for text in texts: if text in {'#1', '#2', '#3', '#4'}: txts.append(text) phonemes.append(text) continue text = self.cn_tn.normalize(text.strip()) text_list = list(jieba.cut(text)) print("jieba cut: ", text, text_list) for words in text_list: if words in _PAUSE_SYMBOL: # phonemes[-1] += _PAUSE_SYMBOL[words] phonemes.append(_PAUSE_SYMBOL[words]) # phonemes.append('#1') txts[-1] += words elif re.search("[\u4e00-\u9fa5]+", words): # pinyin = self.pinyin_parser(words, style=Style.TONE3, errors="ignore") pinyin = lazy_pinyin(words, style=Style.TONE3, tone_sandhi=True, neutral_tone_with_five=True) new_pinyin = [] for x in pinyin: x = "".join(x) if "#" not in x: new_pinyin.append(x) else: phonemes.append(words) continue # new_pinyin = change_tone_in_bu_or_yi(words, new_pinyin) if len(words)>1 and words[-1] not in {"一","不"} else new_pinyin phoneme = get_phoneme_from_char_and_pinyin(words, new_pinyin) phonemes += phoneme txts += list(words) elif re.search(r"[a-zA-Z]", words) or re.search(r"#[1-4]", words): phonemes.append(words.upper()) txts.append(words.upper()) # phonemes.append("#1") # phones = " ".join(phonemes) return txts, phonemes
def txt2pin_phns(self, text): text = re.sub(r'(?<! )(' + r'[^\w\s]' + r')', r' \1', text) text = re.sub(r'\s+', ' ', text).strip() # print(text.split(" ")) res_list = [] for txt in text.split(" "): if txt in self.cmn_dict: # res_list += ["(zh)" + x for x in self.cmn_dict[txt]] res_list.append("(zh)") res_list.append(to_initials(txt, strict=False)) res_list.append(to_finals_tone3(txt, neutral_tone_with_five=True)) elif txt == '': continue elif txt[0] in {"#1", "#2", "#3", "#4"} or not bool(regex.search(r'\p{L}', txt[0][0])): if len(res_list) > 0 and res_list[-1] == "_": res_list.pop() res_list += [txt] continue else: if len(res_list) > 0 and res_list[-1] == "_": res_list.pop() lang = langid.classify(txt)[0] lang = lang if lang in self.text_tokenizer else "en" tokenizer = self.text_tokenizer[lang][1] ipa = tokenizer.backend.phonemize([txt], separator=tokenizer.separator, strip=True, njobs=1) phns = ipa[0] if ipa[0][0] == "(" else f"({lang})_" + ipa[0] res_list += phns.replace("_", "|_|").split("|")
# lang = phns.split(")")[0][1:] # phns = phns[len(lang)+3:].replace("_", "|_|") # phns = phns.split("|") # for i in range(len(phns)): # if phns[i] not in {"#1", "#2", "#3", "#4", "_", ",", ".", "?", "!"}: # phns[i] = f"({lang})" + phns[i] # res_list += phns res_list.append("_") res = "|".join(res_list) res = re.sub(r'(\|_)+', '|_', res) return res
def text2phn(self, sentence, lang=None): if not lang: lang = langid.classify(sentence)[0] if re.search("[\u4e00-\u9fa5]+", sentence): txts, phones = self.txt2pinyin(sentence) transcript_norm = " ".join(phones) phones = self.txt2pin_phns(transcript_norm) # IPA mix Pinyin else: transcript = self.replace_numbers_with_words(sentence, lang=lang).split(' ') transcript_norm = sentence # All IPA phones = txt2phone(self.text_tokenizer[lang][1], transcript_norm.strip().replace(".", ",").replace("。", ",")) phones = f"({lang})|" + phones if phones[0] != "(" else phones return phones
def text2norm(self, sentence, lang=None): if not lang: lang = langid.classify(sentence)[0] if re.search("[\u4e00-\u9fa5]+", sentence): txts, phones = self.txt2pinyin(sentence) transcript_norm = " ".join(phones) else: transcript = self.replace_numbers_with_words(sentence, lang=lang).split(' ') transcript_norm = sentence return (lang, transcript_norm)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386import argparseimport codecsimport osimport refrom datetime import datetimefrom importlib.resources import filesfrom pathlib import Path
import numpy as npimport soundfile as sfimport tomlifrom cached_path import cached_pathfrom hydra.utils import get_classfrom omegaconf import OmegaConf
from lemas_tts.infer.utils_infer import ( mel_spec_type, target_rms, cross_fade_duration, nfe_step, cfg_strength, sway_sampling_coef, speed, fix_duration, device, infer_process, load_model, load_vocoder, preprocess_ref_audio_text, remove_silence_for_generated_wav,)
THIS_FILE = Path(__file__).resolve()
def _find_repo_root(start: Path) -> Path: """Locate the repo root by looking for a `pretrained_models` folder upwards.""" for p in [start, *start.parents]: if (p / "pretrained_models").is_dir(): return p cwd = Path.cwd() if (cwd / "pretrained_models").is_dir(): return cwd return start
REPO_ROOT = _find_repo_root(THIS_FILE)PRETRAINED_ROOT = REPO_ROOT / "pretrained_models"CKPTS_ROOT = PRETRAINED_ROOT / "ckpts"
parser = argparse.ArgumentParser( prog="python3 infer-cli.py", description="Commandline interface for E2/F5 TTS with Advanced Batch Processing.", epilog="Specify options above to override one or more settings from config.",)parser.add_argument( "-c", "--config", type=str, default=os.path.join(files("lemas_tts").joinpath("infer/examples/basic"), "basic.toml"), help="The configuration file, default see infer/examples/basic/basic.toml",)
# Note. Not to provide default value here in order to read default from config file
parser.add_argument( "-m", "--model", type=str, help="The model name: F5TTS_v1_Base | F5TTS_Base | E2TTS_Base | etc.",)parser.add_argument( "-mc", "--model_cfg", type=str, help="The path to F5-TTS model config file .yaml",)parser.add_argument( "-p", "--ckpt_file", type=str, help="The path to model checkpoint .pt, leave blank to use default",)parser.add_argument( "-v", "--vocab_file", type=str, help="The path to vocab file .txt, leave blank to use default",)parser.add_argument( "-r", "--ref_audio", type=str, help="The reference audio file.",)parser.add_argument( "-s", "--ref_text", type=str, help="The transcript/subtitle for the reference audio",)parser.add_argument( "-t", "--gen_text", type=str, help="The text to make model synthesize a speech",)parser.add_argument( "-f", "--gen_file", type=str, help="The file with text to generate, will ignore --gen_text",)parser.add_argument( "-o", "--output_dir", type=str, help="The path to output folder",)parser.add_argument( "-w", "--output_file", type=str, help="The name of output file",)parser.add_argument( "--save_chunk", action="store_true", help="To save each audio chunks during inference",)parser.add_argument( "--remove_silence", action="store_true", help="To remove long silence found in ouput",)parser.add_argument( "--load_vocoder_from_local", action="store_true", help="To load vocoder from local dir, default to ../checkpoints/vocos-mel-24khz",)parser.add_argument( "--vocoder_name", type=str, choices=["vocos", "bigvgan"], help=f"Used vocoder name: vocos | bigvgan, default {mel_spec_type}",)parser.add_argument( "--target_rms", type=float, help=f"Target output speech loudness normalization value, default {target_rms}",)parser.add_argument( "--cross_fade_duration", type=float, help=f"Duration of cross-fade between audio segments in seconds, default {cross_fade_duration}",)parser.add_argument( "--nfe_step", type=int, help=f"The number of function evaluation (denoising steps), default {nfe_step}",)parser.add_argument( "--cfg_strength", type=float, help=f"Classifier-free guidance strength, default {cfg_strength}",)parser.add_argument( "--sway_sampling_coef", type=float, help=f"Sway Sampling coefficient, default {sway_sampling_coef}",)parser.add_argument( "--speed", type=float, help=f"The speed of the generated audio, default {speed}",)parser.add_argument( "--fix_duration", type=float, help=f"Fix the total duration (ref and gen audios) in seconds, default {fix_duration}",)parser.add_argument( "--device", type=str, help="Specify the device to run on",)args = parser.parse_args()
# config file
config = tomli.load(open(args.config, "rb"))
# command-line interface parameters
model = args.model or config.get("model", "F5TTS_v1_Base")ckpt_file = args.ckpt_file or config.get("ckpt_file", "")vocab_file = args.vocab_file or config.get("vocab_file", "")
ref_audio = args.ref_audio or config.get("ref_audio", "infer/examples/basic/basic_ref_en.wav")ref_text = ( args.ref_text if args.ref_text is not None else config.get("ref_text", "Some call me nature, others call me mother nature."))gen_text = args.gen_text or config.get("gen_text", "Here we generate something just for test.")gen_file = args.gen_file or config.get("gen_file", "")
output_dir = args.output_dir or config.get("output_dir", "tests")output_file = args.output_file or config.get( "output_file", f"infer_cli_{datetime.now().strftime(r'%Y%m%d_%H%M%S')}.wav")
save_chunk = args.save_chunk or config.get("save_chunk", False)remove_silence = args.remove_silence or config.get("remove_silence", False)load_vocoder_from_local = args.load_vocoder_from_local or config.get("load_vocoder_from_local", False)
vocoder_name = args.vocoder_name or config.get("vocoder_name", mel_spec_type)target_rms = args.target_rms or config.get("target_rms", target_rms)cross_fade_duration = args.cross_fade_duration or config.get("cross_fade_duration", cross_fade_duration)nfe_step = args.nfe_step or config.get("nfe_step", nfe_step)cfg_strength = args.cfg_strength or config.get("cfg_strength", cfg_strength)sway_sampling_coef = args.sway_sampling_coef or config.get("sway_sampling_coef", sway_sampling_coef)speed = args.speed or config.get("speed", speed)fix_duration = args.fix_duration or config.get("fix_duration", fix_duration)device = args.device or config.get("device", device)
# patches for pip pkg userif "infer/examples/" in ref_audio: ref_audio = str(files("lemas_tts").joinpath(f"{ref_audio}"))if "infer/examples/" in gen_file: gen_file = str(files("lemas_tts").joinpath(f"{gen_file}"))if "voices" in config: for voice in config["voices"]: voice_ref_audio = config["voices"][voice]["ref_audio"] if "infer/examples/" in voice_ref_audio: config["voices"][voice]["ref_audio"] = str(files("lemas_tts").joinpath(f"{voice_ref_audio}"))
# ignore gen_text if gen_file provided
if gen_file: gen_text = codecs.open(gen_file, "r", "utf-8").read()
# output path
wave_path = Path(output_dir) / output_file# spectrogram_path = Path(output_dir) / "infer_cli_out.png"if save_chunk: output_chunk_dir = os.path.join(output_dir, f"{Path(output_file).stem}_chunks") if not os.path.exists(output_chunk_dir): os.makedirs(output_chunk_dir)
# load vocoder
if vocoder_name == "vocos": vocoder_local_path = str(CKPTS_ROOT / "vocos-mel-24khz")elif vocoder_name == "bigvgan": vocoder_local_path = "../checkpoints/bigvgan_v2_24khz_100band_256x"
vocoder = load_vocoder( vocoder_name=vocoder_name, is_local=load_vocoder_from_local, local_path=vocoder_local_path, device=device)
# load TTS model
model_cfg = OmegaConf.load( args.model_cfg or config.get("model_cfg", str(files("lemas_tts").joinpath(f"configs/{model}.yaml"))))model_cls = get_class(f"lemas_tts.model.{model_cfg.model.backbone}")model_arc = model_cfg.model.arch
repo_name, ckpt_step, ckpt_type = "F5-TTS", 1250000, "safetensors"
if model != "F5TTS_Base": assert vocoder_name == model_cfg.model.mel_spec.mel_spec_type
# override for previous modelsif model == "F5TTS_Base": if vocoder_name == "vocos": ckpt_step = 1200000 elif vocoder_name == "bigvgan": model = "F5TTS_Base_bigvgan" ckpt_type = "pt"elif model == "E2TTS_Base": repo_name = "E2-TTS" ckpt_step = 1200000
if not ckpt_file: ckpt_file = str(cached_path(f"hf://SWivid/{repo_name}/{model}/model_{ckpt_step}.{ckpt_type}"))
print(f"Using {model}...")ema_model = load_model( model_cls, model_arc, ckpt_file, mel_spec_type=vocoder_name, vocab_file=vocab_file, device=device)
# inference process
def main(): main_voice = {"ref_audio": ref_audio, "ref_text": ref_text} if "voices" not in config: voices = {"main": main_voice} else: voices = config["voices"] voices["main"] = main_voice for voice in voices: print("Voice:", voice) print("ref_audio ", voices[voice]["ref_audio"]) voices[voice]["ref_audio"], voices[voice]["ref_text"] = preprocess_ref_audio_text( voices[voice]["ref_audio"], voices[voice]["ref_text"] ) print("ref_audio_", voices[voice]["ref_audio"], "\n\n")
generated_audio_segments = [] reg1 = r"(?=\[\w+\])" chunks = re.split(reg1, gen_text) reg2 = r"\[(\w+)\]" for text in chunks: if not text.strip(): continue match = re.match(reg2, text) if match: voice = match[1] else: print("No voice tag found, using main.") voice = "main" if voice not in voices: print(f"Voice {voice} not found, using main.") voice = "main" text = re.sub(reg2, "", text) ref_audio_ = voices[voice]["ref_audio"] ref_text_ = voices[voice]["ref_text"] gen_text_ = text.strip() print(f"Voice: {voice}") audio_segment, final_sample_rate, spectragram = infer_process( ref_audio_, ref_text_, gen_text_, ema_model, vocoder, mel_spec_type=vocoder_name, target_rms=target_rms, cross_fade_duration=cross_fade_duration, nfe_step=nfe_step, cfg_strength=cfg_strength, sway_sampling_coef=sway_sampling_coef, speed=speed, fix_duration=fix_duration, device=device, ) generated_audio_segments.append(audio_segment)
if save_chunk: if len(gen_text_) > 200: gen_text_ = gen_text_[:200] + " ... " sf.write( os.path.join(output_chunk_dir, f"{len(generated_audio_segments) - 1}_{gen_text_}.wav"), audio_segment, final_sample_rate, )
if generated_audio_segments: final_wave = np.concatenate(generated_audio_segments)
if not os.path.exists(output_dir): os.makedirs(output_dir)
with open(wave_path, "wb") as f: sf.write(f.name, final_wave, final_sample_rate) # Remove silence if remove_silence: remove_silence_for_generated_wav(f.name) print(f.name)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824#!/usr/bin/env python3# coding=utf-8# Authors:# 2019.5 Zhiyang Zhou (https://github.com/Joee1995/chn_text_norm.git)# 2019.9 Jiayu DU## requirements:# - python 3.X# notes: python 2.X WILL fail or produce misleading results
import sys, os, argparse, codecs, string, re, unicodedata
# ================================================================================ ## basic constant# ================================================================================ #CHINESE_DIGIS = u'零一二三四五六七八九'BIG_CHINESE_DIGIS_SIMPLIFIED = u'零壹贰叁肆伍陆柒捌玖'BIG_CHINESE_DIGIS_TRADITIONAL = u'零壹貳參肆伍陸柒捌玖'SMALLER_BIG_CHINESE_UNITS_SIMPLIFIED = u'十百千万'SMALLER_BIG_CHINESE_UNITS_TRADITIONAL = u'拾佰仟萬'LARGER_CHINESE_NUMERING_UNITS_SIMPLIFIED = u'亿兆京垓秭穰沟涧正载'LARGER_CHINESE_NUMERING_UNITS_TRADITIONAL = u'億兆京垓秭穰溝澗正載'SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED = u'十百千万'SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL = u'拾佰仟萬'
ZERO_ALT = u'〇'ONE_ALT = u'幺'TWO_ALTS = [u'两', u'兩']
POSITIVE = [u'正', u'正']NEGATIVE = [u'负', u'負']POINT = [u'点', u'點']# PLUS = [u'加', u'加']# SIL = [u'杠', u'槓']
# 中文数字系统类型NUMBERING_TYPES = ['low', 'mid', 'high']
CURRENCY_NAMES = '(人民币|美元|日元|英镑|欧元|马克|法郎|加拿大元|澳元|港币|先令|芬兰马克|爱尔兰镑|' \ '里拉|荷兰盾|埃斯库多|比塞塔|印尼盾|林吉特|新西兰元|比索|卢布|新加坡元|韩元|泰铢)'CURRENCY_UNITS = '((亿|千万|百万|万|千|百)|(亿|千万|百万|万|千|百|)元|(亿|千万|百万|万|千|百|)块|角|毛|分)'COM_QUANTIFIERS = '(匹|张|座|回|场|尾|条|个|首|阙|阵|网|炮|顶|丘|棵|只|支|袭|辆|挑|担|颗|壳|窠|曲|墙|群|腔|' \ '砣|座|客|贯|扎|捆|刀|令|打|手|罗|坡|山|岭|江|溪|钟|队|单|双|对|出|口|头|脚|板|跳|枝|件|贴|' \ '针|线|管|名|位|身|堂|课|本|页|家|户|层|丝|毫|厘|分|钱|两|斤|担|铢|石|钧|锱|忽|(千|毫|微)克|' \ '毫|厘|分|寸|尺|丈|里|寻|常|铺|程|(千|分|厘|毫|微)米|撮|勺|合|升|斗|石|盘|碗|碟|叠|桶|笼|盆|' \ '盒|杯|钟|斛|锅|簋|篮|盘|桶|罐|瓶|壶|卮|盏|箩|箱|煲|啖|袋|钵|年|月|日|季|刻|时|周|天|秒|分|旬|' \ '纪|岁|世|更|夜|春|夏|秋|冬|代|伏|辈|丸|泡|粒|颗|幢|堆|条|根|支|道|面|片|张|颗|块)'
# punctuation information are based on Zhon project (https://github.com/tsroten/zhon.git)CHINESE_PUNC_STOP = '!?。。'CHINESE_PUNC_NON_STOP = '"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏'CHINESE_PUNC_OTHER = '·〈〉-'CHINESE_PUNC_LIST = CHINESE_PUNC_STOP + CHINESE_PUNC_NON_STOP + CHINESE_PUNC_OTHER
# ================================================================================ ## basic class# ================================================================================ #class ChineseChar(object): """ 中文字符 每个字符对应简体和繁体, e.g. 简体 = '负', 繁体 = '負' 转换时可转换为简体或繁体 """
def __init__(self, simplified, traditional): self.simplified = simplified self.traditional = traditional #self.__repr__ = self.__str__
def __str__(self): return self.simplified or self.traditional or None
def __repr__(self): return self.__str__()
class ChineseNumberUnit(ChineseChar): """ 中文数字/数位字符 每个字符除繁简体外还有一个额外的大写字符 e.g. '陆' 和 '陸' """
def __init__(self, power, simplified, traditional, big_s, big_t): super(ChineseNumberUnit, self).__init__(simplified, traditional) self.power = power self.big_s = big_s self.big_t = big_t
def __str__(self): return '10^{}'.format(self.power)
@classmethod def create(cls, index, value, numbering_type=NUMBERING_TYPES[1], small_unit=False):
if small_unit: return ChineseNumberUnit(power=index + 1, simplified=value[0], traditional=value[1], big_s=value[1], big_t=value[1]) elif numbering_type == NUMBERING_TYPES[0]: return ChineseNumberUnit(power=index + 8, simplified=value[0], traditional=value[1], big_s=value[0], big_t=value[1]) elif numbering_type == NUMBERING_TYPES[1]: return ChineseNumberUnit(power=(index + 2) * 4, simplified=value[0], traditional=value[1], big_s=value[0], big_t=value[1]) elif numbering_type == NUMBERING_TYPES[2]: return ChineseNumberUnit(power=pow(2, index + 3), simplified=value[0], traditional=value[1], big_s=value[0], big_t=value[1]) else: raise ValueError( 'Counting type should be in {0} ({1} provided).'.format(NUMBERING_TYPES, numbering_type))
class ChineseNumberDigit(ChineseChar): """ 中文数字字符 """
def __init__(self, value, simplified, traditional, big_s, big_t, alt_s=None, alt_t=None): super(ChineseNumberDigit, self).__init__(simplified, traditional) self.value = value self.big_s = big_s self.big_t = big_t self.alt_s = alt_s self.alt_t = alt_t
def __str__(self): return str(self.value)
@classmethod def create(cls, i, v): return ChineseNumberDigit(i, v[0], v[1], v[2], v[3])
class ChineseMath(ChineseChar): """ 中文数位字符 """
def __init__(self, simplified, traditional, symbol, expression=None): super(ChineseMath, self).__init__(simplified, traditional) self.symbol = symbol self.expression = expression self.big_s = simplified self.big_t = traditional
CC, CNU, CND, CM = ChineseChar, ChineseNumberUnit, ChineseNumberDigit, ChineseMath
class NumberSystem(object): """ 中文数字系统 """ pass
class MathSymbol(object): """ 用于中文数字系统的数学符号 (繁/简体), e.g. positive = ['正', '正'] negative = ['负', '負'] point = ['点', '點'] """
def __init__(self, positive, negative, point): self.positive = positive self.negative = negative self.point = point
def __iter__(self): for v in self.__dict__.values(): yield v
# class OtherSymbol(object):# """# 其他符号# """## def __init__(self, sil):# self.sil = sil## def __iter__(self):# for v in self.__dict__.values():# yield v
# ================================================================================ ## basic utils# ================================================================================ #def create_system(numbering_type=NUMBERING_TYPES[1]): """ 根据数字系统类型返回创建相应的数字系统,默认为 mid NUMBERING_TYPES = ['low', 'mid', 'high']: 中文数字系统类型 low: '兆' = '亿' * '十' = $10^{9}$, '京' = '兆' * '十', etc. mid: '兆' = '亿' * '万' = $10^{12}$, '京' = '兆' * '万', etc. high: '兆' = '亿' * '亿' = $10^{16}$, '京' = '兆' * '兆', etc. 返回对应的数字系统 """
# chinese number units of '亿' and larger all_larger_units = zip( LARGER_CHINESE_NUMERING_UNITS_SIMPLIFIED, LARGER_CHINESE_NUMERING_UNITS_TRADITIONAL) larger_units = [CNU.create(i, v, numbering_type, False) for i, v in enumerate(all_larger_units)] # chinese number units of '十, 百, 千, 万' all_smaller_units = zip( SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED, SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL) smaller_units = [CNU.create(i, v, small_unit=True) for i, v in enumerate(all_smaller_units)] # digis chinese_digis = zip(CHINESE_DIGIS, CHINESE_DIGIS, BIG_CHINESE_DIGIS_SIMPLIFIED, BIG_CHINESE_DIGIS_TRADITIONAL) digits = [CND.create(i, v) for i, v in enumerate(chinese_digis)] digits[0].alt_s, digits[0].alt_t = ZERO_ALT, ZERO_ALT digits[1].alt_s, digits[1].alt_t = ONE_ALT, ONE_ALT digits[2].alt_s, digits[2].alt_t = TWO_ALTS[0], TWO_ALTS[1]
# symbols positive_cn = CM(POSITIVE[0], POSITIVE[1], '+', lambda x: x) negative_cn = CM(NEGATIVE[0], NEGATIVE[1], '-', lambda x: -x) point_cn = CM(POINT[0], POINT[1], '.', lambda x, y: float(str(x) + '.' + str(y))) # sil_cn = CM(SIL[0], SIL[1], '-', lambda x, y: float(str(x) + '-' + str(y))) system = NumberSystem() system.units = smaller_units + larger_units system.digits = digits system.math = MathSymbol(positive_cn, negative_cn, point_cn) # system.symbols = OtherSymbol(sil_cn) return system
def chn2num(chinese_string, numbering_type=NUMBERING_TYPES[1]):
def get_symbol(char, system): for u in system.units: if char in [u.traditional, u.simplified, u.big_s, u.big_t]: return u for d in system.digits: if char in [d.traditional, d.simplified, d.big_s, d.big_t, d.alt_s, d.alt_t]: return d for m in system.math: if char in [m.traditional, m.simplified]: return m
def string2symbols(chinese_string, system): int_string, dec_string = chinese_string, '' for p in [system.math.point.simplified, system.math.point.traditional]: if p in chinese_string: int_string, dec_string = chinese_string.split(p) break return [get_symbol(c, system) for c in int_string], \ [get_symbol(c, system) for c in dec_string]
def correct_symbols(integer_symbols, system): """ 一百八 to 一百八十 一亿一千三百万 to 一亿 一千万 三百万 """
if integer_symbols and isinstance(integer_symbols[0], CNU): if integer_symbols[0].power == 1: integer_symbols = [system.digits[1]] + integer_symbols
if len(integer_symbols) > 1: if isinstance(integer_symbols[-1], CND) and isinstance(integer_symbols[-2], CNU): integer_symbols.append( CNU(integer_symbols[-2].power - 1, None, None, None, None))
result = [] unit_count = 0 for s in integer_symbols: if isinstance(s, CND): result.append(s) unit_count = 0 elif isinstance(s, CNU): current_unit = CNU(s.power, None, None, None, None) unit_count += 1
if unit_count == 1: result.append(current_unit) elif unit_count > 1: for i in range(len(result)): if isinstance(result[-i - 1], CNU) and result[-i - 1].power < current_unit.power: result[-i - 1] = CNU(result[-i - 1].power + current_unit.power, None, None, None, None) return result
def compute_value(integer_symbols): """ Compute the value. When current unit is larger than previous unit, current unit * all previous units will be used as all previous units. e.g. '两千万' = 2000 * 10000 not 2000 + 10000 """ value = [0] last_power = 0 for s in integer_symbols: if isinstance(s, CND): value[-1] = s.value elif isinstance(s, CNU): value[-1] *= pow(10, s.power) if s.power > last_power: value[:-1] = list(map(lambda v: v * pow(10, s.power), value[:-1])) last_power = s.power value.append(0) return sum(value)
system = create_system(numbering_type) int_part, dec_part = string2symbols(chinese_string, system) int_part = correct_symbols(int_part, system) int_str = str(compute_value(int_part)) dec_str = ''.join([str(d.value) for d in dec_part]) if dec_part: return '{0}.{1}'.format(int_str, dec_str) else: return int_str
def num2chn(number_string, numbering_type=NUMBERING_TYPES[1], big=False, traditional=False, alt_zero=False, alt_one=False, alt_two=True, use_zeros=True, use_units=True):
def get_value(value_string, use_zeros=True):
striped_string = value_string.lstrip('0')
# record nothing if all zeros if not striped_string: return []
# record one digits elif len(striped_string) == 1: if use_zeros and len(value_string) != len(striped_string): return [system.digits[0], system.digits[int(striped_string)]] else: return [system.digits[int(striped_string)]]
# recursively record multiple digits else: result_unit = next(u for u in reversed( system.units) if u.power < len(striped_string)) result_string = value_string[:-result_unit.power] return get_value(result_string) + [result_unit] + get_value(striped_string[-result_unit.power:])
system = create_system(numbering_type)
int_dec = number_string.split('.') if len(int_dec) == 1: int_string = int_dec[0] dec_string = "" elif len(int_dec) == 2: int_string = int_dec[0] dec_string = int_dec[1] else: raise ValueError( "invalid input num string with more than one dot: {}".format(number_string))
if use_units and len(int_string) > 1: result_symbols = get_value(int_string) else: result_symbols = [system.digits[int(c)] for c in int_string] dec_symbols = [system.digits[int(c)] for c in dec_string] if dec_string: result_symbols += [system.math.point] + dec_symbols
if alt_two: liang = CND(2, system.digits[2].alt_s, system.digits[2].alt_t, system.digits[2].big_s, system.digits[2].big_t) for i, v in enumerate(result_symbols): if isinstance(v, CND) and v.value == 2: next_symbol = result_symbols[i + 1] if i < len(result_symbols) - 1 else None previous_symbol = result_symbols[i - 1] if i > 0 else None if isinstance(next_symbol, CNU) and isinstance(previous_symbol, (CNU, type(None))): if next_symbol.power != 1 and ((previous_symbol is None) or (previous_symbol.power != 1)): result_symbols[i] = liang
# if big is True, '两' will not be used and `alt_two` has no impact on output if big: attr_name = 'big_' if traditional: attr_name += 't' else: attr_name += 's' else: if traditional: attr_name = 'traditional' else: attr_name = 'simplified'
result = ''.join([getattr(s, attr_name) for s in result_symbols])
# if not use_zeros: # result = result.strip(getattr(system.digits[0], attr_name))
if alt_zero: result = result.replace( getattr(system.digits[0], attr_name), system.digits[0].alt_s)
if alt_one: result = result.replace( getattr(system.digits[1], attr_name), system.digits[1].alt_s)
for i, p in enumerate(POINT): if result.startswith(p): return CHINESE_DIGIS[0] + result
# ^10, 11, .., 19 if len(result) >= 2 and result[1] in [SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED[0], SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL[0]] and \ result[0] in [CHINESE_DIGIS[1], BIG_CHINESE_DIGIS_SIMPLIFIED[1], BIG_CHINESE_DIGIS_TRADITIONAL[1]]: result = result[1:]
return result
# ================================================================================ ## different types of rewriters# ================================================================================ #class Cardinal: """ CARDINAL类 """
def __init__(self, cardinal=None, chntext=None): self.cardinal = cardinal self.chntext = chntext
def chntext2cardinal(self): return chn2num(self.chntext)
def cardinal2chntext(self): return num2chn(self.cardinal)
class Digit: """ DIGIT类 """
def __init__(self, digit=None, chntext=None): self.digit = digit self.chntext = chntext
# def chntext2digit(self): # return chn2num(self.chntext)
def digit2chntext(self): return num2chn(self.digit, alt_two=False, use_units=False)
class TelePhone: """ TELEPHONE类 """
def __init__(self, telephone=None, raw_chntext=None, chntext=None): self.telephone = telephone self.raw_chntext = raw_chntext self.chntext = chntext
# def chntext2telephone(self): # sil_parts = self.raw_chntext.split('<SIL>') # self.telephone = '-'.join([ # str(chn2num(p)) for p in sil_parts # ]) # return self.telephone
def telephone2chntext(self, fixed=False):
if fixed: sil_parts = self.telephone.split('-') self.raw_chntext = '<SIL>'.join([ num2chn(part, alt_two=False, use_units=False) for part in sil_parts ]) self.chntext = self.raw_chntext.replace('<SIL>', '') else: sp_parts = self.telephone.strip('+').split() self.raw_chntext = '<SP>'.join([ num2chn(part, alt_two=False, use_units=False) for part in sp_parts ]) self.chntext = self.raw_chntext.replace('<SP>', '') return self.chntext
class Fraction: """ FRACTION类 """
def __init__(self, fraction=None, chntext=None): self.fraction = fraction self.chntext = chntext
def chntext2fraction(self): denominator, numerator = self.chntext.split('分之') return chn2num(numerator) + '/' + chn2num(denominator)
def fraction2chntext(self): numerator, denominator = self.fraction.split('/') return num2chn(denominator) + '分之' + num2chn(numerator)
class Date: """ DATE类 """
def __init__(self, date=None, chntext=None): self.date = date self.chntext = chntext
# def chntext2date(self): # chntext = self.chntext # try: # year, other = chntext.strip().split('年', maxsplit=1) # year = Digit(chntext=year).digit2chntext() + '年' # except ValueError: # other = chntext # year = '' # if other: # try: # month, day = other.strip().split('月', maxsplit=1) # month = Cardinal(chntext=month).chntext2cardinal() + '月' # except ValueError: # day = chntext # month = '' # if day: # day = Cardinal(chntext=day[:-1]).chntext2cardinal() + day[-1] # else: # month = '' # day = '' # date = year + month + day # self.date = date # return self.date
def date2chntext(self): date = self.date try: year, other = date.strip().split('年', 1) year = Digit(digit=year).digit2chntext() + '年' except ValueError: other = date year = '' if other: try: month, day = other.strip().split('月', 1) month = Cardinal(cardinal=month).cardinal2chntext() + '月' except ValueError: day = date month = '' if day: day = Cardinal(cardinal=day[:-1]).cardinal2chntext() + day[-1] else: month = '' day = '' chntext = year + month + day self.chntext = chntext return self.chntext
class Time: """ MONEY类 """
def __init__(self, time=None, chntext=None): self.time = time self.chntext = chntext
# def chntext2money(self): # return self.money
def time2chntext(self): time = self.time.replace('-', '至') pattern = re.compile(r'(\d{1,2}:\d{1,2}(:)?(\d{1,2})?)') matchers = pattern.findall(time) if matchers: if len(matchers[0])>2: time = time.replace(':', '时', 1) time = time.replace(':', '分', 1) self.chntext = time return self.chntext
class Money: """ MONEY类 """
def __init__(self, money=None, chntext=None): self.money = money self.chntext = chntext
# def chntext2money(self): # return self.money
def money2chntext(self): money = self.money pattern = re.compile(r'(\d+(\.\d+)?)') matchers = pattern.findall(money) if matchers: for matcher in matchers: money = money.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext()) self.chntext = money return self.chntext
class Percentage: """ PERCENTAGE类 """
def __init__(self, percentage=None, chntext=None): self.percentage = percentage self.chntext = chntext
def chntext2percentage(self): return chn2num(self.chntext.strip().strip('百分之')) + '%'
def percentage2chntext(self): return '百分之' + num2chn(self.percentage.strip().strip('%'))
# ================================================================================ ## NSW Normalizer# ================================================================================ #class NSWNormalizer: def __init__(self): self.raw_text = ' ' # '^' + raw_text + '$' self.norm_text = ''
def _particular(self): text = self.norm_text pattern = re.compile(r"(([a-zA-Z]+)二([a-zA-Z]+))") matchers = pattern.findall(text) if matchers: # print('particular') for matcher in matchers: text = text.replace(matcher[0], matcher[1]+'2'+matcher[2], 1) self.norm_text = text return self.norm_text
def normalize(self, raw_text): self.raw_text = '^' + raw_text + '$' text = unicodedata.normalize("NFKC", self.raw_text) # 规范化日期 pattern = re.compile(r"\D+((([089]\d|(19|20)\d{2})年)?(\d{1,2}月(\d{1,2}[日号])?)?)") matchers = pattern.findall(text) if matchers: #print('date') for matcher in matchers: text = text.replace(matcher[0], Date(date=matcher[0]).date2chntext(), 1)
# 规范化时间 pattern = re.compile(r"\D+((\d{1,2}-)?\d{1,2}[时点:]((\d{1,2}-)?\d{1,2}[分:]((\d{1,2}-)?\d{1,2}秒)?)?)") matchers = pattern.findall(text) if matchers: #print('time') for matcher in matchers: text = text.replace(matcher[0], Time(time=matcher[0]).time2chntext(), 1)
# 规范化金钱 pattern = re.compile(r"\D+((\d+(\.\d+)?)[多余几]?" + CURRENCY_UNITS + r"(\d" + CURRENCY_UNITS + r"?)?)") matchers = pattern.findall(text) if matchers: #print('money') for matcher in matchers: text = text.replace(matcher[0], Money(money=matcher[0]).money2chntext(), 1)
# 规范化固话/手机号码 # 手机 # http://www.jihaoba.com/news/show/13680 # 移动:139、138、137、136、135、134、159、158、157、150、151、152、188、187、182、183、184、178、198 # 联通:130、131、132、156、155、186、185、176 # 电信:133、153、189、180、181、177 pattern = re.compile(r"\D((\+?86 ?)?1([38]\d|5[0-35-9]|7[678]|9[89])\d{8})\D") matchers = pattern.findall(text) if matchers: #print('telephone') for matcher in matchers: text = text.replace(matcher[0], TelePhone(telephone=matcher[0]).telephone2chntext(), 1) # 固话 pattern = re.compile(r"\D((0(10|2[1-3]|[3-9]\d{2})-?)?[1-9]\d{6,7})\D") matchers = pattern.findall(text) if matchers: # print('fixed telephone') for matcher in matchers: text = text.replace(matcher[0], TelePhone(telephone=matcher[0]).telephone2chntext(fixed=True), 1)
# 规范化分数 pattern = re.compile(r"(\d+/\d+)") matchers = pattern.findall(text) if matchers: #print('fraction') for matcher in matchers: text = text.replace(matcher, Fraction(fraction=matcher).fraction2chntext(), 1)
# 规范化百分数 text = text.replace('%', '%') pattern = re.compile(r"(\d+(\.\d+)?%)") matchers = pattern.findall(text) if matchers: #print('percentage') for matcher in matchers: text = text.replace(matcher[0], Percentage(percentage=matcher[0]).percentage2chntext(), 1)
# 规范化纯数+量词 pattern = re.compile(r"(\d+(\.\d+)?)[多余几]?" + COM_QUANTIFIERS) matchers = pattern.findall(text) if matchers: #print('cardinal+quantifier') for matcher in matchers: text = text.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext(), 1)
# 规范化数字编号 pattern = re.compile(r"(\d{2,32})") matchers = pattern.findall(text) if matchers: #print('digit') for matcher in matchers: text = text.replace(matcher, Digit(digit=matcher).digit2chntext(), 1)
# 规范化纯数 pattern = re.compile(r"(\d+(\.\d+)?)") matchers = pattern.findall(text) if matchers: #print('cardinal') for matcher in matchers: text = text.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext(), 1)
self.norm_text = text self._particular()
return self.norm_text.lstrip('^').rstrip('$')
def nsw_test_case(raw_text): print('I:' + raw_text) print('O:' + NSWNormalizer(raw_text).normalize()) print('')
def nsw_test(): nsw_test_case('固话:0595-23865596或23880880。') nsw_test_case('固话:0595-23865596或23880880。') nsw_test_case('手机:+86 19859213959或15659451527。') nsw_test_case('分数:32477/76391。') nsw_test_case('百分数:80.03%。') nsw_test_case('编号:31520181154418。') nsw_test_case('纯数:2983.07克或12345.60米。') nsw_test_case('日期:1999年2月20日或09年3月15号。') nsw_test_case('金钱:12块5,34.5元,20.1万') nsw_test_case('特殊:O2O或B2C。') nsw_test_case('3456万吨') nsw_test_case('2938个') nsw_test_case('938') nsw_test_case('今天吃了115个小笼包231个馒头') nsw_test_case('有62%的概率')
if __name__ == '__main__': #nsw_test()
p = argparse.ArgumentParser() p.add_argument('ifile', help='input filename, assume utf-8 encoding') p.add_argument('ofile', help='output filename') p.add_argument('--to_upper', action='store_true', help='convert to upper case') p.add_argument('--to_lower', action='store_true', help='convert to lower case') p.add_argument('--has_key', action='store_true', help="input text has Kaldi's key as first field.") p.add_argument('--log_interval', type=int, default=100000, help='log interval in number of processed lines') args = p.parse_args()
ifile = codecs.open(args.ifile, 'r', 'utf8') ofile = codecs.open(args.ofile, 'w+', 'utf8')
n = 0 for l in ifile: key = '' text = '' if args.has_key: cols = l.split(maxsplit=1) key = cols[0] if len(cols) == 2: text = cols[1].strip() else: text = '' else: text = l.strip()
# cases if args.to_upper and args.to_lower: sys.stderr.write('cn_tn.py: to_upper OR to_lower?') exit(1) if args.to_upper: text = text.upper() if args.to_lower: text = text.lower()
# NSW(Non-Standard-Word) normalization text = NSWNormalizer(text).normalize()
# Punctuations removal old_chars = CHINESE_PUNC_LIST + string.punctuation # includes all CN and EN punctuations new_chars = ' ' * len(old_chars) del_chars = '' text = text.translate(str.maketrans(old_chars, new_chars, del_chars))
# if args.has_key: ofile.write(key + '\t' + text + '\n') else: if text.strip() != '': # skip empty line in pure text format(without Kaldi's utt key) ofile.write(text + '\n')
n += 1 if n % args.log_interval == 0: sys.stderr.write("cn_tn.py: {} lines done.\n".format(n)) sys.stderr.flush()
sys.stderr.write("cn_tn.py: {} lines done in total.\n".format(n)) sys.stderr.flush()
ifile.close() ofile.close()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178# -*- coding: utf-8 -*-# Copyright (c) 2017 Keith Ito## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.
import refrom unidecode import unidecodeimport inflect
_inflect = inflect.engine()_comma_number_re = re.compile(r"([0-9][0-9\,]+[0-9])")_decimal_number_re = re.compile(r"([0-9]+\.[0-9]+)")_pounds_re = re.compile(r"£([0-9\,]*[0-9]+)")_dollars_re = re.compile(r"\$([0-9\.\,]*[0-9]+)")_ordinal_re = re.compile(r"[0-9]+(st|nd|rd|th)")_number_re = re.compile(r"[0-9]+")
def _remove_commas(m): return m.group(1).replace(",", "")
def _expand_decimal_point(m): return m.group(1).replace(".", " point ")
def _expand_dollars(m): match = m.group(1) parts = match.split(".") if len(parts) > 2: return match + " dollars" # Unexpected format dollars = int(parts[0]) if parts[0] else 0 cents = int(parts[1]) if len(parts) > 1 and parts[1] else 0 if dollars and cents: dollar_unit = "dollar" if dollars == 1 else "dollars" cent_unit = "cent" if cents == 1 else "cents" return "%s %s, %s %s" % (dollars, dollar_unit, cents, cent_unit) elif dollars: dollar_unit = "dollar" if dollars == 1 else "dollars" return "%s %s" % (dollars, dollar_unit) elif cents: cent_unit = "cent" if cents == 1 else "cents" return "%s %s" % (cents, cent_unit) else: return "zero dollars"
def _expand_ordinal(m): return _inflect.number_to_words(m.group(0))
def _expand_number(m): num = int(m.group(0)) if num > 1000 and num < 3000: if num == 2000: return "two thousand" elif num > 2000 and num < 2010: return "two thousand " + _inflect.number_to_words(num % 100) elif num % 100 == 0: return _inflect.number_to_words(num // 100) + " hundred" else: return _inflect.number_to_words( num, andword="", zero="oh", group=2 ).replace(", ", " ") else: return _inflect.number_to_words(num, andword="")
def normalize_numbers(text): text = re.sub(_comma_number_re, _remove_commas, text) text = re.sub(_pounds_re, r"\1 pounds", text) text = re.sub(_dollars_re, _expand_dollars, text) text = re.sub(_decimal_number_re, _expand_decimal_point, text) text = re.sub(_ordinal_re, _expand_ordinal, text) text = re.sub(_number_re, _expand_number, text) return text
# Regular expression matching whitespace:_whitespace_re = re.compile(r"\s+")
# List of (regular expression, replacement) pairs for abbreviations:_abbreviations = [ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1]) for x in [ ("mrs", "misess"), ("mr", "mister"), ("dr", "doctor"), ("st", "saint"), ("co", "company"), ("jr", "junior"), ("maj", "major"), ("gen", "general"), ("drs", "doctors"), ("rev", "reverend"), ("lt", "lieutenant"), ("hon", "honorable"), ("sgt", "sergeant"), ("capt", "captain"), ("esq", "esquire"), ("ltd", "limited"), ("col", "colonel"), ("ft", "fort"), ]]
def expand_abbreviations(text): for regex, replacement in _abbreviations: text = re.sub(regex, replacement, text) return text
def expand_numbers(text): return normalize_numbers(text)
def lowercase(text): return text.lower()
def collapse_whitespace(text): return re.sub(_whitespace_re, " ", text)
def convert_to_ascii(text): return unidecode(text)
def basic_cleaners(text): """Basic pipeline that lowercases and collapses whitespace without transliteration.""" text = lowercase(text) text = collapse_whitespace(text) return text
def transliteration_cleaners(text): """Pipeline for non-English text that transliterates to ASCII.""" text = convert_to_ascii(text) text = lowercase(text) text = collapse_whitespace(text) return text
def english_cleaners(text): """Pipeline for English text, including number and abbreviation expansion.""" text = convert_to_ascii(text) text = lowercase(text) text = expand_numbers(text) text = expand_abbreviations(text) text = collapse_whitespace(text) return text
def read_lexicon(lex_path): lexicon = {} with open(lex_path) as f: for line in f: temp = re.split(r"\s+", line.strip("\n")) word = temp[0] phones = temp[1:] if word not in lexicon: lexicon[word] = phones return lexicon123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148import argparseimport copyimport osfrom typing import List
import jiebaimport pypinyin
SPECIAL_NOTES = '。?!?!.;;:,,:'
def read_vocab(file: os.PathLike) -> List[str]: with open(file) as f: vocab = f.read().split('\n') vocab = [v for v in vocab if len(v) > 0 and v != '\n'] return vocab
class TextNormal: def __init__(self, gp_vocab_file: os.PathLike, py_vocab_file: os.PathLike, add_sp1=False, fix_er=False, add_sil=True): if gp_vocab_file is not None: self.gp_vocab = read_vocab(gp_vocab_file) if py_vocab_file is not None: self.py_vocab = read_vocab(py_vocab_file) self.in_py_vocab = dict([(p, True) for p in self.py_vocab]) self.add_sp1 = add_sp1 self.add_sil = add_sil self.fix_er = fix_er
# gp2idx = dict([(c, i) for i, c in enumerate(self.gp_vocab)]) # idx2gp = dict([(i, c) for i, c in enumerate(self.gp_vocab)])
def _split2sent(self, text): new_sub = [text] while True: sub = copy.deepcopy(new_sub) new_sub = [] for s in sub: sp = False for t in SPECIAL_NOTES: if t in s: new_sub += s.split(t) sp = True break
if not sp and len(s) > 0: new_sub += [s] if len(new_sub) == len(sub): break tokens = [a for a in text if a in SPECIAL_NOTES]
return new_sub, tokens
def _correct_tone3(self, pys: List[str]) -> List[str]: """Fix the continuous tone3 pronunciation problem""" for i in range(2, len(pys)): if pys[i][-1] == '3' and pys[i - 1][-1] == '3' and pys[i - 2][-1] == '3': pys[i - 1] = pys[i - 1][:-1] + '2' # change the middle one for i in range(1, len(pys)): if pys[i][-1] == '3': if pys[i - 1][-1] == '3': pys[i - 1] = pys[i - 1][:-1] + '2' return pys
def _correct_tone4(self, pys: List[str]) -> List[str]: """Fixed the problem of pronouncing 不 bu2 yao4 / bu4 neng2""" for i in range(len(pys) - 1): if pys[i] == 'bu4': if pys[i + 1][-1] == '4': pys[i] = 'bu2' return pys
def _replace_with_sp(self, pys: List[str]) -> List[str]: for i, p in enumerate(pys): if p in ',,、': pys[i] = 'sp1' return pys
def _correct_tone5(self, pys: List[str]) -> List[str]: for i in range(len(pys)): if pys[i][-1] not in '1234': pys[i] += '5' return pys
def gp2py(self, gp_text: str) -> List[str]:
gp_sent_list, tokens = self._split2sent(gp_text) py_sent_list = [] for sent in gp_sent_list: pys = [] for words in list(jieba.cut(sent)): py = pypinyin.pinyin(words, pypinyin.TONE3) py = [p[0] for p in py] pys += py if self.add_sp1: pys = self._replace_with_sp(pys) pys = self._correct_tone3(pys) pys = self._correct_tone4(pys) pys = self._correct_tone5(pys) if self.add_sil: py_sent_list += [' '.join(['sil'] + pys + ['sil'])] else: py_sent_list += [' '.join(pys)]
if self.add_sil: gp_sent_list = ['sil ' + ' '.join(list(gp)) + ' sil' for gp in gp_sent_list] else: gp_sent_list = [' '.join(list(gp)) for gp in gp_sent_list]
if self.fix_er: new_py_sent_list = [] for py, gp in zip(py_sent_list, gp_sent_list): py = self._convert_er2(py, gp) new_py_sent_list += [py] py_sent_list = new_py_sent_list print(new_py_sent_list)
return py_sent_list, gp_sent_list
def _convert_er2(self, py, gp): py2hz = dict([(p, h) for p, h in zip(py.split(), gp.split())]) py_list = py.split() for i, p in enumerate(py_list): if (p == 'er2' and py2hz[p] == '儿' and i > 1 and len(py_list[i - 1]) > 2 and py_list[i - 1][-1] in '1234'):
py_er = py_list[i - 1][:-1] + 'r' + py_list[i - 1][-1]
if self.in_py_vocab.get(py_er, False): # must in vocab py_list[i - 1] = py_er py_list[i] = 'r' py = ' '.join(py_list) return py
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-t', '--text', type=str) args = parser.parse_args() text = args.text tn = TextNormal('gp.vocab', 'py.vocab', add_sp1=True, fix_er=True) py_list, gp_list = tn.gp2py(text) for py, gp in zip(py_list, gp_list): print(py + '|' + gp)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275# Indonesian TTS Text Normalization for YouTube subtitles# Requirements: pip install num2wordsimport refrom num2words import num2words
# --- small slang map (expandable) ---SLANG_MAP = { "gpp": "nggak apa-apa", "gak": "nggak", "ga": "nggak", "gk": "nggak", "sy": "saya", "sya": "saya", "km": "kamu", "tp": "tapi", "tpi": "tapi", "jd": "jadi", "bgt": "banget", "blm": "belum", "trs": "terus", "sm": "sama", "wkwk": "wkwk", # keep as-is (laugh token) or strip later "wkwkwk": "wkwk"}
# emoji pattern: removes most emoji blocksEMOJI_PATTERN = re.compile( "[" "\U0001F600-\U0001F64F" # emoticons "\U0001F300-\U0001F5FF" # symbols & pictographs "\U0001F680-\U0001F6FF" # transport & map symbols "\U0001F1E0-\U0001F1FF" # flags (iOS) "\U00002700-\U000027BF" # dingbats "\U000024C2-\U0001F251" "]+", flags=re.UNICODE)
# units mapUNITS = { "kg": "kilogram","g": "gram","km": "kilometer", "m": "meter","cm": "sentimeter","mm": "milimeter", "l": "liter"}
# helper: safe num2words for Indonesiandef num_to_words_ind(num_str): """Convert numeric string to Indonesian words. - Handles integers and simple decimals like '1.5' (reads digits after decimal). - Removes grouping dots in Indonesian numbers (e.g. '10.000'). """ num_str = num_str.strip() # remove thousand separators commonly used in Indonesian (dot) # but if decimal point (like '1,5' or '1.5'), assume '.' is decimal point (we expect '.' used) # We'll treat commas as thousand separators too if no decimal comma present. if re.match(r'^\d+[.,]\d+$', num_str): # decimal number: normalize to use '.' then split s = num_str.replace(',', '.') left, right = s.split('.', 1) try: left_w = num2words(int(left), lang='id') except: left_w = left # read each decimal digit separately right_w = " ".join(num2words(int(d), lang='id') for d in right if d.isdigit()) return f"{left_w} koma {right_w}" else: # remove non-digit separators like dots or commas used as thousand separators cleaned = re.sub(r'[.,]', '', num_str) try: return num2words(int(cleaned), lang='id') except: return num_str
# helper: per-digit reader for phone numbers (default)def read_digits_per_digit(number_str, prefix_plus=False): digits = re.findall(r'\d', number_str) words = " ".join(num2words(int(d), lang='id') for d in digits) if prefix_plus: return "plus " + words return words
# noise removal rule for tokens like 'yyy6yy' or other long mixed garbage:def is_noise_token(tok): # remove tokens that: # - length >=4 and contain at least one digit and at least one letter (typical ASR/keyboard noise) # - or tokens of a single repeated char length >=4 (e.g., 'aaaa', '!!!!!!' but punctuation handled earlier) if len(tok) < 4: return False if re.search(r'[A-Za-z]', tok) and re.search(r'\d', tok): return True if re.fullmatch(r'(.)\1{3,}', tok): # same char repeated >=4 return True return False
# --- 新增:标点规范化函数 ---def punctuation_normalize(text): """ - 替换除 . , ! ? 之外的所有标点为逗号 - 统一多重逗号为单逗号 - 去掉开头多余逗号、省略号 - 逗号后空格规范化 """ # 替换括号、引号、冒号、分号、破折号、省略号等为逗号 text = re.sub(r'[:;()\[\]{}"“”«»…—–/\\]', ',', text) # 多个逗号替换成一个 text = re.sub(r',+', ',', text) # 开头去掉逗号和省略号 text = re.sub(r'^(,|\.\.\.|…)+\s*', '', text) # 逗号后空格规范 text = re.sub(r'\s*,\s*', ', ', text) # 多余空白合并 text = re.sub(r'\s+', ' ', text).strip() return text
def normalize_id_tts(text): """ Main normalization pipeline tailored for: - Indonesian YouTube subtitles (mostly ASR/MT) - TTS frontend requirements: * Remove emojis * Keep . , ! ? as sentence/phrase delimiters * Replace other punctuation with comma * Expand numbers, percents, currency, units, times, dates * Remove keyboard noise like 'yyy6yy' * Keep English words as-is * Keep repeated words (do not collapse) """ if not text: return text
# 1) Normalize whitespace and trim text = text.strip() text = re.sub(r'\s+', ' ', text)
# 2) Remove emojis text = EMOJI_PATTERN.sub('', text)
# 3) 标点规范化(替代原有 PUNCT_TO_COMMA 替换) text = punctuation_normalize(text)
# 保护时间和日期的代码(防止被逗号破坏) text = re.sub(r'(\d{1,2}):(\d{2})', lambda m: f"__TIME_{m.group(1)}_{m.group(2)}__", text) text = re.sub(r'(\d{1,4})[\/-](\d{1,2})[\/-](\d{1,4})', lambda m: f"__DATE_{m.group(1)}_{m.group(2)}_{m.group(3)}__", text)
# 恢复时间日期标记 text = re.sub(r'__TIME_(\d{1,2})_(\d{2})__', lambda m: f"{m.group(1)}:{m.group(2)}", text) text = re.sub(r'__DATE_(\d{1,4})_(\d{1,2})_(\d{1,4})__', lambda m: f"{m.group(1)}/{m.group(2)}/{m.group(3)}", text)
# 4) Tokenize loosely by spaces and punctuation tokens = re.split(r'(\s+|[,.!?])', text) # keep delimiters
out_tokens = [] for tok in tokens: if not tok or tok.isspace(): out_tokens.append(tok) continue
# keep punctuation .,!? as-is if tok in ['.', ',', '!', '?']: out_tokens.append(tok) continue
# remove any remaining emojis or control chars if EMOJI_PATTERN.search(tok): continue
# slang normalization lower_tok = tok.lower() if lower_tok in SLANG_MAP: out_tokens.append(SLANG_MAP[lower_tok]) continue
# remove noise tokens if is_noise_token(tok): continue
# currency: Rp 10.000 or rp10.000 m = re.match(r'^(Rp|rp)\s*([0-9\.,]+)$', tok) if m: num = m.group(2) cleaned = re.sub(r'[.,]', '', num) out_tokens.append(f"{num_to_words_ind(cleaned)} rupiah") continue
# percent like 30% m = re.match(r'^(\d+)%$', tok) if m: out_tokens.append(f"{num_to_words_ind(m.group(1))} persen") continue
# phone numbers +62..., 0812... m = re.match(r'^\+?\d[\d\-\s]{6,}\d$', tok) if m: prefix_plus = tok.startswith('+') out_tokens.append(read_digits_per_digit(tok, prefix_plus=prefix_plus)) continue
# time hh:mm m = re.match(r'^(\d{1,2}):(\d{2})$', tok) if m: h, mi = m.group(1), m.group(2) h_w = num_to_words_ind(h.lstrip('0') or '0') mi_w = num_to_words_ind(mi.lstrip('0') or '0') out_tokens.append(f"pukul {h_w} lewat {mi_w} menit") continue
# date yyyy/mm/dd or dd/mm/yyyy m = re.match(r'^(\d{1,4})\/(\d{1,2})\/(\d{1,4})$', tok) if m: a,b,c = m.group(1), m.group(2).zfill(2), m.group(3) if len(a) == 4: year, month, day = a, b, c elif len(c) == 4: day, month, year = a, b, c else: day, month, year = a, b, c MONTHS = { "01": "Januari","02": "Februari","03": "Maret","04": "April", "05": "Mei","06": "Juni","07": "Juli","08": "Agustus", "09": "September","10": "Oktober","11": "November","12": "Desember" } day_w = num_to_words_ind(day.lstrip('0') or '0') year_w = num_to_words_ind(year) month_name = MONTHS.get(month, month) out_tokens.append(f"{day_w} {month_name} {year_w}") continue
# units like 30kg m = re.match(r'^(\d+)\s*(kg|g|km|m|cm|mm|l)$', tok, flags=re.I) if m: num, unit = m.group(1), m.group(2).lower() unit_word = UNITS.get(unit, unit) out_tokens.append(f"{num_to_words_ind(num)} {unit_word}") continue
# plain integers if re.fullmatch(r'\d+', tok): out_tokens.append(num_to_words_ind(tok)) continue
# numbers with separators if re.fullmatch(r'[\d\.,]+', tok) and re.search(r'[.,]', tok): out_tokens.append(num_to_words_ind(tok)) continue
# keep English/as-is tokens out_tokens.append(tok)
normalized = "".join(out_tokens)
# final cleanup: spacing around punctuation normalized = re.sub(r'\s+,', ',', normalized) normalized = re.sub(r',\s*', ', ', normalized) normalized = re.sub(r'\s+\.', '.', normalized) normalized = re.sub(r'\s+!', '!', normalized) normalized = re.sub(r'\s+\?', '?', normalized) normalized = re.sub(r'\s+', ' ', normalized).strip()
# 如果你不想全部小写,注释掉下面这行 normalized = normalized.lower()
return normalized
# -------------------------# Example usage and tests# -------------------------if __name__ == "__main__": examples = [ "kita cek Project nadi PHP pemberi harapan palsu tuh yyy6yy 46 ini ini usernya ini di bagian user", "Harga Rp 10.000, diskon 30%! Buka jam 09:30 (hari 2025/11/28).", "Call +62 812-3456-7890 sekarang!", "angka kecil 3.14 dan 1,234 serta 1000", "[musik]", "... atau mungkin juga jumlah anggota keluarga mereka." ] for ex in examples: print("IN: ", ex) print("OUT:", normalize_id_tts(ex)) print("-"*60)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120a1 a1a2 a2a3 a3a4 a4a5 a5ai1 ai1ai2 ai2ai3 ai3ai4 ai4ai5 ai5an1 an1an2 an2an3 an3an4 an4an5 an5ang1 ang1ang2 ang2ang3 ang3ang4 ang4ang5 ang5ao1 ao1ao2 ao2ao3 ao3ao4 ao4ao5 ao5ba1 b a1ba2 b a2ba3 b a3ba4 b a4ba5 b a5bai1 b ai1bai2 b ai2bai3 b ai3bai4 b ai4bai5 b ai5ban1 b an1ban2 b an2ban3 b an3ban4 b an4ban5 b an5bang1 b ang1bang2 b ang2bang3 b ang3bang4 b ang4bang5 b ang5bao1 b ao1bao2 b ao2bao3 b ao3bao4 b ao4bao5 b ao5bei1 b ei1bei2 b ei2bei3 b ei3bei4 b ei4bei5 b ei5ben1 b en1ben2 b en2ben3 b en3ben4 b en4ben5 b en5beng1 b eng1beng2 b eng2beng3 b eng3beng4 b eng4beng5 b eng5bi1 b i1bi2 b i2bi3 b i3bi4 b i4bi5 b i5bian1 b ian1bian2 b ian2bian3 b ian3bian4 b ian4bian5 b ian5biao1 b iao1biao2 b iao2biao3 b iao3biao4 b iao4biao5 b iao5bie1 b ie1bie2 b ie2bie3 b ie3bie4 b ie4bie5 b ie5bin1 b in1bin2 b in2bin3 b in3bin4 b in4bin5 b in5bing1 b ing1bing2 b ing2bing3 b ing3bing4 b ing4bing5 b ing5bo1 b o1bo2 b o2bo3 b o3bo4 b o4bo5 b o5bu1 b u1bu2 b u2bu3 b u3bu4 b u4bu5 b u5ca1 c a1ca2 c a2ca3 c a3ca4 c a4ca5 c a5cai1 c ai1cai2 c ai2cai3 c ai3cai4 c ai4cai5 c ai5can1 c an1can2 c an2can3 c an3can4 c an4can5 c an5cang1 c ang1cang2 c ang2cang3 c ang3cang4 c ang4cang5 c ang5cao1 c ao1cao2 c ao2cao3 c ao3cao4 c ao4cao5 c ao5ce1 c e1ce2 c e2ce3 c e3ce4 c e4ce5 c e5cen1 c en1cen2 c en2cen3 c en3cen4 c en4cen5 c en5ceng1 c eng1ceng2 c eng2ceng3 c eng3ceng4 c eng4ceng5 c eng5cha1 ch a1cha2 ch a2cha3 ch a3cha4 ch a4cha5 ch a5chai1 ch ai1chai2 ch ai2chai3 ch ai3chai4 ch ai4chai5 ch ai5chan1 ch an1chan2 ch an2chan3 ch an3chan4 ch an4chan5 ch an5chang1 ch ang1chang2 ch ang2chang3 ch ang3chang4 ch ang4chang5 ch ang5chao1 ch ao1chao2 ch ao2chao3 ch ao3chao4 ch ao4chao5 ch ao5che1 ch e1che2 ch e2che3 ch e3che4 ch e4che5 ch e5chen1 ch en1chen2 ch en2chen3 ch en3chen4 ch en4chen5 ch en5cheng1 ch eng1cheng2 ch eng2cheng3 ch eng3cheng4 ch eng4cheng5 ch eng5chi1 ch iii1chi2 ch iii2chi3 ch iii3chi4 ch iii4chi5 ch iii5chong1 ch ong1chong2 ch ong2chong3 ch ong3chong4 ch ong4chong5 ch ong5chou1 ch ou1chou2 ch ou2chou3 ch ou3chou4 ch ou4chou5 ch ou5chu1 ch u1chu2 ch u2chu3 ch u3chu4 ch u4chu5 ch u5chuai1 ch uai1chuai2 ch uai2chuai3 ch uai3chuai4 ch uai4chuai5 ch uai5chuan1 ch uan1chuan2 ch uan2chuan3 ch uan3chuan4 ch uan4chuan5 ch uan5chuang1 ch uang1chuang2 ch uang2chuang3 ch uang3chuang4 ch uang4chuang5 ch uang5chui1 ch uei1chui2 ch uei2chui3 ch uei3chui4 ch uei4chui5 ch uei5chun1 ch uen1chun2 ch uen2chun3 ch uen3chun4 ch uen4chun5 ch uen5chuo1 ch uo1chuo2 ch uo2chuo3 ch uo3chuo4 ch uo4chuo5 ch uo5ci1 c ii1ci2 c ii2ci3 c ii3ci4 c ii4ci5 c ii5cong1 c ong1cong2 c ong2cong3 c ong3cong4 c ong4cong5 c ong5cou1 c ou1cou2 c ou2cou3 c ou3cou4 c ou4cou5 c ou5cu1 c u1cu2 c u2cu3 c u3cu4 c u4cu5 c u5cuan1 c uan1cuan2 c uan2cuan3 c uan3cuan4 c uan4cuan5 c uan5cui1 c uei1cui2 c uei2cui3 c uei3cui4 c uei4cui5 c uei5cun1 c uen1cun2 c uen2cun3 c uen3cun4 c uen4cun5 c uen5cuo1 c uo1cuo2 c uo2cuo3 c uo3cuo4 c uo4cuo5 c uo5da1 d a1da2 d a2da3 d a3da4 d a4da5 d a5dai1 d ai1dai2 d ai2dai3 d ai3dai4 d ai4dai5 d ai5dan1 d an1dan2 d an2dan3 d an3dan4 d an4dan5 d an5dang1 d ang1dang2 d ang2dang3 d ang3dang4 d ang4dang5 d ang5dao1 d ao1dao2 d ao2dao3 d ao3dao4 d ao4dao5 d ao5de1 d e1de2 d e2de3 d e3de4 d e4de5 d e5dei1 d ei1dei2 d ei2dei3 d ei3dei4 d ei4dei5 d ei5den1 d en1den2 d en2den3 d en3den4 d en4den5 d en5deng1 d eng1deng2 d eng2deng3 d eng3deng4 d eng4deng5 d eng5di1 d i1di2 d i2di3 d i3di4 d i4di5 d i5dia1 d ia1dia2 d ia2dia3 d ia3dia4 d ia4dia5 d ia5dian1 d ian1dian2 d ian2dian3 d ian3dian4 d ian4dian5 d ian5diao1 d iao1diao2 d iao2diao3 d iao3diao4 d iao4diao5 d iao5die1 d ie1die2 d ie2die3 d ie3die4 d ie4die5 d ie5ding1 d ing1ding2 d ing2ding3 d ing3ding4 d ing4ding5 d ing5diu1 d iou1diu2 d iou2diu3 d iou3diu4 d iou4diu5 d iou5dong1 d ong1dong2 d ong2dong3 d ong3dong4 d ong4dong5 d ong5dou1 d ou1dou2 d ou2dou3 d ou3dou4 d ou4dou5 d ou5du1 d u1du2 d u2du3 d u3du4 d u4du5 d u5duan1 d uan1duan2 d uan2duan3 d uan3duan4 d uan4duan5 d uan5dui1 d uei1dui2 d uei2dui3 d uei3dui4 d uei4dui5 d uei5dun1 d uen1dun2 d uen2dun3 d uen3dun4 d uen4dun5 d uen5duo1 d uo1duo2 d uo2duo3 d uo3duo4 d uo4duo5 d uo5e1 e1e2 e2e3 e3e4 e4e5 e5ei1 ei1ei2 ei2ei3 ei3ei4 ei4ei5 ei5en1 en1en2 en2en3 en3en4 en4en5 en5eng1 eng1eng2 eng2eng3 eng3eng4 eng4eng5 eng5r1 er1r2 er2r3 er3r4 er4r5 er5er1 er1er2 er2er3 er3er4 er4er5 er5fa1 f a1fa2 f a2fa3 f a3fa4 f a4fa5 f a5fan1 f an1fan2 f an2fan3 f an3fan4 f an4fan5 f an5fang1 f ang1fang2 f ang2fang3 f ang3fang4 f ang4fang5 f ang5fei1 f ei1fei2 f ei2fei3 f ei3fei4 f ei4fei5 f ei5fen1 f en1fen2 f en2fen3 f en3fen4 f en4fen5 f en5feng1 f eng1feng2 f eng2feng3 f eng3feng4 f eng4feng5 f eng5fo1 f o1fo2 f o2fo3 f o3fo4 f o4fo5 f o5fou1 f ou1fou2 f ou2fou3 f ou3fou4 f ou4fou5 f ou5fu1 f u1fu2 f u2fu3 f u3fu4 f u4fu5 f u5ga1 g a1ga2 g a2ga3 g a3ga4 g a4ga5 g a5gai1 g ai1gai2 g ai2gai3 g ai3gai4 g ai4gai5 g ai5gan1 g an1gan2 g an2gan3 g an3gan4 g an4gan5 g an5gang1 g ang1gang2 g ang2gang3 g ang3gang4 g ang4gang5 g ang5gao1 g ao1gao2 g ao2gao3 g ao3gao4 g ao4gao5 g ao5ge1 g e1ge2 g e2ge3 g e3ge4 g e4ge5 g e5gei1 g ei1gei2 g ei2gei3 g ei3gei4 g ei4gei5 g ei5gen1 g en1gen2 g en2gen3 g en3gen4 g en4gen5 g en5geng1 g eng1geng2 g eng2geng3 g eng3geng4 g eng4geng5 g eng5gong1 g ong1gong2 g ong2gong3 g ong3gong4 g ong4gong5 g ong5gou1 g ou1gou2 g ou2gou3 g ou3gou4 g ou4gou5 g ou5gu1 g u1gu2 g u2gu3 g u3gu4 g u4gu5 g u5gua1 g ua1gua2 g ua2gua3 g ua3gua4 g ua4gua5 g ua5guai1 g uai1guai2 g uai2guai3 g uai3guai4 g uai4guai5 g uai5guan1 g uan1guan2 g uan2guan3 g uan3guan4 g uan4guan5 g uan5guang1 g uang1guang2 g uang2guang3 g uang3guang4 g uang4guang5 g uang5gui1 g uei1gui2 g uei2gui3 g uei3gui4 g uei4gui5 g uei5gun1 g uen1gun2 g uen2gun3 g uen3gun4 g uen4gun5 g uen5guo1 g uo1guo2 g uo2guo3 g uo3guo4 g uo4guo5 g uo5ha1 h a1ha2 h a2ha3 h a3ha4 h a4ha5 h a5hai1 h ai1hai2 h ai2hai3 h ai3hai4 h ai4hai5 h ai5han1 h an1han2 h an2han3 h an3han4 h an4han5 h an5hang1 h ang1hang2 h ang2hang3 h ang3hang4 h ang4hang5 h ang5hao1 h ao1hao2 h ao2hao3 h ao3hao4 h ao4hao5 h ao5he1 h e1he2 h e2he3 h e3he4 h e4he5 h e5hei1 h ei1hei2 h ei2hei3 h ei3hei4 h ei4hei5 h ei5hen1 h en1hen2 h en2hen3 h en3hen4 h en4hen5 h en5heng1 h eng1heng2 h eng2heng3 h eng3heng4 h eng4heng5 h eng5hong1 h ong1hong2 h ong2hong3 h ong3hong4 h ong4hong5 h ong5hou1 h ou1hou2 h ou2hou3 h ou3hou4 h ou4hou5 h ou5hu1 h u1hu2 h u2hu3 h u3hu4 h u4hu5 h u5hua1 h ua1hua2 h ua2hua3 h ua3hua4 h ua4hua5 h ua5huai1 h uai1huai2 h uai2huai3 h uai3huai4 h uai4huai5 h uai5huan1 h uan1huan2 h uan2huan3 h uan3huan4 h uan4huan5 h uan5huang1 h uang1huang2 h uang2huang3 h uang3huang4 h uang4huang5 h uang5hui1 h uei1hui2 h uei2hui3 h uei3hui4 h uei4hui5 h uei5hun1 h uen1hun2 h uen2hun3 h uen3hun4 h uen4hun5 h uen5huo1 h uo1huo2 h uo2huo3 h uo3huo4 h uo4huo5 h uo5ji1 j i1ji2 j i2ji3 j i3ji4 j i4ji5 j i5jia1 j ia1jia2 j ia2jia3 j ia3jia4 j ia4jia5 j ia5jian1 j ian1jian2 j ian2jian3 j ian3jian4 j ian4jian5 j ian5jiang1 j iang1jiang2 j iang2jiang3 j iang3jiang4 j iang4jiang5 j iang5jiao1 j iao1jiao2 j iao2jiao3 j iao3jiao4 j iao4jiao5 j iao5jie1 j ie1jie2 j ie2jie3 j ie3jie4 j ie4jie5 j ie5jin1 j in1jin2 j in2jin3 j in3jin4 j in4jin5 j in5jing1 j ing1jing2 j ing2jing3 j ing3jing4 j ing4jing5 j ing5jiong1 j iong1jiong2 j iong2jiong3 j iong3jiong4 j iong4jiong5 j iong5jiu1 j iou1jiu2 j iou2jiu3 j iou3jiu4 j iou4jiu5 j iou5ju1 j v1ju2 j v2ju3 j v3ju4 j v4ju5 j v5juan1 j van1juan2 j van2juan3 j van3juan4 j van4juan5 j van5jue1 j ve1jue2 j ve2jue3 j ve3jue4 j ve4jue5 j ve5jun1 j vn1jun2 j vn2jun3 j vn3jun4 j vn4jun5 j vn5ka1 k a1ka2 k a2ka3 k a3ka4 k a4ka5 k a5kai1 k ai1kai2 k ai2kai3 k ai3kai4 k ai4kai5 k ai5kan1 k an1kan2 k an2kan3 k an3kan4 k an4kan5 k an5kang1 k ang1kang2 k ang2kang3 k ang3kang4 k ang4kang5 k ang5kao1 k ao1kao2 k ao2kao3 k ao3kao4 k ao4kao5 k ao5ke1 k e1ke2 k e2ke3 k e3ke4 k e4ke5 k e5kei1 k ei1kei2 k ei2kei3 k ei3kei4 k ei4kei5 k ei5ken1 k en1ken2 k en2ken3 k en3ken4 k en4ken5 k en5keng1 k eng1keng2 k eng2keng3 k eng3keng4 k eng4keng5 k eng5kong1 k ong1kong2 k ong2kong3 k ong3kong4 k ong4kong5 k ong5kou1 k ou1kou2 k ou2kou3 k ou3kou4 k ou4kou5 k ou5ku1 k u1ku2 k u2ku3 k u3ku4 k u4ku5 k u5kua1 k ua1kua2 k ua2kua3 k ua3kua4 k ua4kua5 k ua5kuai1 k uai1kuai2 k uai2kuai3 k uai3kuai4 k uai4kuai5 k uai5kuan1 k uan1kuan2 k uan2kuan3 k uan3kuan4 k uan4kuan5 k uan5kuang1 k uang1kuang2 k uang2kuang3 k uang3kuang4 k uang4kuang5 k uang5kui1 k uei1kui2 k uei2kui3 k uei3kui4 k uei4kui5 k uei5kun1 k uen1kun2 k uen2kun3 k uen3kun4 k uen4kun5 k uen5kuo1 k uo1kuo2 k uo2kuo3 k uo3kuo4 k uo4kuo5 k uo5la1 l a1la2 l a2la3 l a3la4 l a4la5 l a5lai1 l ai1lai2 l ai2lai3 l ai3lai4 l ai4lai5 l ai5lan1 l an1lan2 l an2lan3 l an3lan4 l an4lan5 l an5lang1 l ang1lang2 l ang2lang3 l ang3lang4 l ang4lang5 l ang5lao1 l ao1lao2 l ao2lao3 l ao3lao4 l ao4lao5 l ao5le1 l e1le2 l e2le3 l e3le4 l e4le5 l e5lei1 l ei1lei2 l ei2lei3 l ei3lei4 l ei4lei5 l ei5leng1 l eng1leng2 l eng2leng3 l eng3leng4 l eng4leng5 l eng5li1 l i1li2 l i2li3 l i3li4 l i4li5 l i5lia1 l ia1lia2 l ia2lia3 l ia3lia4 l ia4lia5 l ia5lian1 l ian1lian2 l ian2lian3 l ian3lian4 l ian4lian5 l ian5liang1 l iang1liang2 l iang2liang3 l iang3liang4 l iang4liang5 l iang5liao1 l iao1liao2 l iao2liao3 l iao3liao4 l iao4liao5 l iao5lie1 l ie1lie2 l ie2lie3 l ie3lie4 l ie4lie5 l ie5lin1 l in1lin2 l in2lin3 l in3lin4 l in4lin5 l in5ling1 l ing1ling2 l ing2ling3 l ing3ling4 l ing4ling5 l ing5liu1 l iou1liu2 l iou2liu3 l iou3liu4 l iou4liu5 l iou5lo1 l o1lo2 l o2lo3 l o3lo4 l o4lo5 l o5long1 l ong1long2 l ong2long3 l ong3long4 l ong4long5 l ong5lou1 l ou1lou2 l ou2lou3 l ou3lou4 l ou4lou5 l ou5lu1 l u1lu2 l u2lu3 l u3lu4 l u4lu5 l u5luan1 l uan1luan2 l uan2luan3 l uan3luan4 l uan4luan5 l uan5lue1 l ve1lue2 l ve2lue3 l ve3lue4 l ve4lue5 l ve5lve1 l ve1lve2 l ve2lve3 l ve3lve4 l ve4lve5 l ve5lun1 l uen1lun2 l uen2lun3 l uen3lun4 l uen4lun5 l uen5luo1 l uo1luo2 l uo2luo3 l uo3luo4 l uo4luo5 l uo5lv1 l v1lv2 l v2lv3 l v3lv4 l v4lv5 l v5ma1 m a1ma2 m a2ma3 m a3ma4 m a4ma5 m a5mai1 m ai1mai2 m ai2mai3 m ai3mai4 m ai4mai5 m ai5man1 m an1man2 m an2man3 m an3man4 m an4man5 m an5mang1 m ang1mang2 m ang2mang3 m ang3mang4 m ang4mang5 m ang5mao1 m ao1mao2 m ao2mao3 m ao3mao4 m ao4mao5 m ao5me1 m e1me2 m e2me3 m e3me4 m e4me5 m e5mei1 m ei1mei2 m ei2mei3 m ei3mei4 m ei4mei5 m ei5men1 m en1men2 m en2men3 m en3men4 m en4men5 m en5meng1 m eng1meng2 m eng2meng3 m eng3meng4 m eng4meng5 m eng5mi1 m i1mi2 m i2mi3 m i3mi4 m i4mi5 m i5mian1 m ian1mian2 m ian2mian3 m ian3mian4 m ian4mian5 m ian5miao1 m iao1miao2 m iao2miao3 m iao3miao4 m iao4miao5 m iao5mie1 m ie1mie2 m ie2mie3 m ie3mie4 m ie4mie5 m ie5min1 m in1min2 m in2min3 m in3min4 m in4min5 m in5ming1 m ing1ming2 m ing2ming3 m ing3ming4 m ing4ming5 m ing5miu1 m iou1miu2 m iou2miu3 m iou3miu4 m iou4miu5 m iou5mo1 m o1mo2 m o2mo3 m o3mo4 m o4mo5 m o5mou1 m ou1mou2 m ou2mou3 m ou3mou4 m ou4mou5 m ou5mu1 m u1mu2 m u2mu3 m u3mu4 m u4mu5 m u5na1 n a1na2 n a2na3 n a3na4 n a4na5 n a5nai1 n ai1nai2 n ai2nai3 n ai3nai4 n ai4nai5 n ai5nan1 n an1nan2 n an2nan3 n an3nan4 n an4nan5 n an5nang1 n ang1nang2 n ang2nang3 n ang3nang4 n ang4nang5 n ang5nao1 n ao1nao2 n ao2nao3 n ao3nao4 n ao4nao5 n ao5ne1 n e1ne2 n e2ne3 n e3ne4 n e4ne5 n e5nei1 n ei1nei2 n ei2nei3 n ei3nei4 n ei4nei5 n ei5nen1 n en1nen2 n en2nen3 n en3nen4 n en4nen5 n en5neng1 n eng1neng2 n eng2neng3 n eng3neng4 n eng4neng5 n eng5ni1 n i1ni2 n i2ni3 n i3ni4 n i4ni5 n i5nian1 n ian1nian2 n ian2nian3 n ian3nian4 n ian4nian5 n ian5niang1 n iang1niang2 n iang2niang3 n iang3niang4 n iang4niang5 n iang5niao1 n iao1niao2 n iao2niao3 n iao3niao4 n iao4niao5 n iao5nie1 n ie1nie2 n ie2nie3 n ie3nie4 n ie4nie5 n ie5nin1 n in1nin2 n in2nin3 n in3nin4 n in4nin5 n in5ning1 n ing1ning2 n ing2ning3 n ing3ning4 n ing4ning5 n ing5niu1 n iou1niu2 n iou2niu3 n iou3niu4 n iou4niu5 n iou5nong1 n ong1nong2 n ong2nong3 n ong3nong4 n ong4nong5 n ong5nou1 n ou1nou2 n ou2nou3 n ou3nou4 n ou4nou5 n ou5nu1 n u1nu2 n u2nu3 n u3nu4 n u4nu5 n u5nuan1 n uan1nuan2 n uan2nuan3 n uan3nuan4 n uan4nuan5 n uan5nue1 n ve1nue2 n ve2nue3 n ve3nue4 n ve4nue5 n ve5nve1 n ve1nve2 n ve2nve3 n ve3nve4 n ve4nve5 n ve5nuo1 n uo1nuo2 n uo2nuo3 n uo3nuo4 n uo4nuo5 n uo5nv1 n v1nv2 n v2nv3 n v3nv4 n v4nv5 n v5o1 o1o2 o2o3 o3o4 o4o5 o5ou1 ou1ou2 ou2ou3 ou3ou4 ou4ou5 ou5pa1 p a1pa2 p a2pa3 p a3pa4 p a4pa5 p a5pai1 p ai1pai2 p ai2pai3 p ai3pai4 p ai4pai5 p ai5pan1 p an1pan2 p an2pan3 p an3pan4 p an4pan5 p an5pang1 p ang1pang2 p ang2pang3 p ang3pang4 p ang4pang5 p ang5pao1 p ao1pao2 p ao2pao3 p ao3pao4 p ao4pao5 p ao5pei1 p ei1pei2 p ei2pei3 p ei3pei4 p ei4pei5 p ei5pen1 p en1pen2 p en2pen3 p en3pen4 p en4pen5 p en5peng1 p eng1peng2 p eng2peng3 p eng3peng4 p eng4peng5 p eng5pi1 p i1pi2 p i2pi3 p i3pi4 p i4pi5 p i5pian1 p ian1pian2 p ian2pian3 p ian3pian4 p ian4pian5 p ian5piao1 p iao1piao2 p iao2piao3 p iao3piao4 p iao4piao5 p iao5pie1 p ie1pie2 p ie2pie3 p ie3pie4 p ie4pie5 p ie5pin1 p in1pin2 p in2pin3 p in3pin4 p in4pin5 p in5ping1 p ing1ping2 p ing2ping3 p ing3ping4 p ing4ping5 p ing5po1 p o1po2 p o2po3 p o3po4 p o4po5 p o5pou1 p ou1pou2 p ou2pou3 p ou3pou4 p ou4pou5 p ou5pu1 p u1pu2 p u2pu3 p u3pu4 p u4pu5 p u5qi1 q i1qi2 q i2qi3 q i3qi4 q i4qi5 q i5qia1 q ia1qia2 q ia2qia3 q ia3qia4 q ia4qia5 q ia5qian1 q ian1qian2 q ian2qian3 q ian3qian4 q ian4qian5 q ian5qiang1 q iang1qiang2 q iang2qiang3 q iang3qiang4 q iang4qiang5 q iang5qiao1 q iao1qiao2 q iao2qiao3 q iao3qiao4 q iao4qiao5 q iao5qie1 q ie1qie2 q ie2qie3 q ie3qie4 q ie4qie5 q ie5qin1 q in1qin2 q in2qin3 q in3qin4 q in4qin5 q in5qing1 q ing1qing2 q ing2qing3 q ing3qing4 q ing4qing5 q ing5qiong1 q iong1qiong2 q iong2qiong3 q iong3qiong4 q iong4qiong5 q iong5qiu1 q iou1qiu2 q iou2qiu3 q iou3qiu4 q iou4qiu5 q iou5qu1 q v1qu2 q v2qu3 q v3qu4 q v4qu5 q v5quan1 q van1quan2 q van2quan3 q van3quan4 q van4quan5 q van5que1 q ve1que2 q ve2que3 q ve3que4 q ve4que5 q ve5qun1 q vn1qun2 q vn2qun3 q vn3qun4 q vn4qun5 q vn5ran1 r an1ran2 r an2ran3 r an3ran4 r an4ran5 r an5rang1 r ang1rang2 r ang2rang3 r ang3rang4 r ang4rang5 r ang5rao1 r ao1rao2 r ao2rao3 r ao3rao4 r ao4rao5 r ao5re1 r e1re2 r e2re3 r e3re4 r e4re5 r e5ren1 r en1ren2 r en2ren3 r en3ren4 r en4ren5 r en5reng1 r eng1reng2 r eng2reng3 r eng3reng4 r eng4reng5 r eng5ri1 r iii1ri2 r iii2ri3 r iii3ri4 r iii4ri5 r iii5rong1 r ong1rong2 r ong2rong3 r ong3rong4 r ong4rong5 r ong5rou1 r ou1rou2 r ou2rou3 r ou3rou4 r ou4rou5 r ou5ru1 r u1ru2 r u2ru3 r u3ru4 r u4ru5 r u5rua1 r ua1rua2 r ua2rua3 r ua3rua4 r ua4rua5 r ua5ruan1 r uan1ruan2 r uan2ruan3 r uan3ruan4 r uan4ruan5 r uan5rui1 r uei1rui2 r uei2rui3 r uei3rui4 r uei4rui5 r uei5run1 r uen1run2 r uen2run3 r uen3run4 r uen4run5 r uen5ruo1 r uo1ruo2 r uo2ruo3 r uo3ruo4 r uo4ruo5 r uo5sa1 s a1sa2 s a2sa3 s a3sa4 s a4sa5 s a5sai1 s ai1sai2 s ai2sai3 s ai3sai4 s ai4sai5 s ai5san1 s an1san2 s an2san3 s an3san4 s an4san5 s an5sang1 s ang1sang2 s ang2sang3 s ang3sang4 s ang4sang5 s ang5sao1 s ao1sao2 s ao2sao3 s ao3sao4 s ao4sao5 s ao5se1 s e1se2 s e2se3 s e3se4 s e4se5 s e5sen1 s en1sen2 s en2sen3 s en3sen4 s en4sen5 s en5seng1 s eng1seng2 s eng2seng3 s eng3seng4 s eng4seng5 s eng5sha1 sh a1sha2 sh a2sha3 sh a3sha4 sh a4sha5 sh a5shai1 sh ai1shai2 sh ai2shai3 sh ai3shai4 sh ai4shai5 sh ai5shan1 sh an1shan2 sh an2shan3 sh an3shan4 sh an4shan5 sh an5shang1 sh ang1shang2 sh ang2shang3 sh ang3shang4 sh ang4shang5 sh ang5shao1 sh ao1shao2 sh ao2shao3 sh ao3shao4 sh ao4shao5 sh ao5she1 sh e1she2 sh e2she3 sh e3she4 sh e4she5 sh e5shei1 sh ei1shei2 sh ei2shei3 sh ei3shei4 sh ei4shei5 sh ei5shen1 sh en1shen2 sh en2shen3 sh en3shen4 sh en4shen5 sh en5sheng1 sh eng1sheng2 sh eng2sheng3 sh eng3sheng4 sh eng4sheng5 sh eng5shi1 sh iii1shi2 sh iii2shi3 sh iii3shi4 sh iii4shi5 sh iii5shou1 sh ou1shou2 sh ou2shou3 sh ou3shou4 sh ou4shou5 sh ou5shu1 sh u1shu2 sh u2shu3 sh u3shu4 sh u4shu5 sh u5shua1 sh ua1shua2 sh ua2shua3 sh ua3shua4 sh ua4shua5 sh ua5shuai1 sh uai1shuai2 sh uai2shuai3 sh uai3shuai4 sh uai4shuai5 sh uai5shuan1 sh uan1shuan2 sh uan2shuan3 sh uan3shuan4 sh uan4shuan5 sh uan5shuang1 sh uang1shuang2 sh uang2shuang3 sh uang3shuang4 sh uang4shuang5 sh uang5shui1 sh uei1shui2 sh uei2shui3 sh uei3shui4 sh uei4shui5 sh uei5shun1 sh uen1shun2 sh uen2shun3 sh uen3shun4 sh uen4shun5 sh uen5shuo1 sh uo1shuo2 sh uo2shuo3 sh uo3shuo4 sh uo4shuo5 sh uo5si1 s ii1si2 s ii2si3 s ii3si4 s ii4si5 s ii5song1 s ong1song2 s ong2song3 s ong3song4 s ong4song5 s ong5sou1 s ou1sou2 s ou2sou3 s ou3sou4 s ou4sou5 s ou5su1 s u1su2 s u2su3 s u3su4 s u4su5 s u5suan1 s uan1suan2 s uan2suan3 s uan3suan4 s uan4suan5 s uan5sui1 s uei1sui2 s uei2sui3 s uei3sui4 s uei4sui5 s uei5sun1 s uen1sun2 s uen2sun3 s uen3sun4 s uen4sun5 s uen5suo1 s uo1suo2 s uo2suo3 s uo3suo4 s uo4suo5 s uo5ta1 t a1ta2 t a2ta3 t a3ta4 t a4ta5 t a5tai1 t ai1tai2 t ai2tai3 t ai3tai4 t ai4tai5 t ai5tan1 t an1tan2 t an2tan3 t an3tan4 t an4tan5 t an5tang1 t ang1tang2 t ang2tang3 t ang3tang4 t ang4tang5 t ang5tao1 t ao1tao2 t ao2tao3 t ao3tao4 t ao4tao5 t ao5te1 t e1te2 t e2te3 t e3te4 t e4te5 t e5tei1 t ei1tei2 t ei2tei3 t ei3tei4 t ei4tei5 t ei5teng1 t eng1teng2 t eng2teng3 t eng3teng4 t eng4teng5 t eng5ti1 t i1ti2 t i2ti3 t i3ti4 t i4ti5 t i5tian1 t ian1tian2 t ian2tian3 t ian3tian4 t ian4tian5 t ian5tiao1 t iao1tiao2 t iao2tiao3 t iao3tiao4 t iao4tiao5 t iao5tie1 t ie1tie2 t ie2tie3 t ie3tie4 t ie4tie5 t ie5ting1 t ing1ting2 t ing2ting3 t ing3ting4 t ing4ting5 t ing5tong1 t ong1tong2 t ong2tong3 t ong3tong4 t ong4tong5 t ong5tou1 t ou1tou2 t ou2tou3 t ou3tou4 t ou4tou5 t ou5tu1 t u1tu2 t u2tu3 t u3tu4 t u4tu5 t u5tuan1 t uan1tuan2 t uan2tuan3 t uan3tuan4 t uan4tuan5 t uan5tui1 t uei1tui2 t uei2tui3 t uei3tui4 t uei4tui5 t uei5tun1 t uen1tun2 t uen2tun3 t uen3tun4 t uen4tun5 t uen5tuo1 t uo1tuo2 t uo2tuo3 t uo3tuo4 t uo4tuo5 t uo5wa1 w ua1wa2 w ua2wa3 w ua3wa4 w ua4wa5 w ua5wai1 w uai1wai2 w uai2wai3 w uai3wai4 w uai4wai5 w uai5wan1 w uan1wan2 w uan2wan3 w uan3wan4 w uan4wan5 w uan5wang1 w uang1wang2 w uang2wang3 w uang3wang4 w uang4wang5 w uang5wei1 w uei1wei2 w uei2wei3 w uei3wei4 w uei4wei5 w uei5wen1 w uen1wen2 w uen2wen3 w uen3wen4 w uen4wen5 w uen5weng1 w uen1weng2 w uen2weng3 w uen3weng4 w uen4weng5 w uen5wo1 w uo1wo2 w uo2wo3 w uo3wo4 w uo4wo5 w uo5wu1 w u1wu2 w u2wu3 w u3wu4 w u4wu5 w u5xi1 x i1xi2 x i2xi3 x i3xi4 x i4xi5 x i5xia1 x ia1xia2 x ia2xia3 x ia3xia4 x ia4xia5 x ia5xian1 x ian1xian2 x ian2xian3 x ian3xian4 x ian4xian5 x ian5xiang1 x iang1xiang2 x iang2xiang3 x iang3xiang4 x iang4xiang5 x iang5xiao1 x iao1xiao2 x iao2xiao3 x iao3xiao4 x iao4xiao5 x iao5xie1 x ie1xie2 x ie2xie3 x ie3xie4 x ie4xie5 x ie5xin1 x in1xin2 x in2xin3 x in3xin4 x in4xin5 x in5xing1 x ing1xing2 x ing2xing3 x ing3xing4 x ing4xing5 x ing5xiong1 x iong1xiong2 x iong2xiong3 x iong3xiong4 x iong4xiong5 x iong5xiu1 x iou1xiu2 x iou2xiu3 x iou3xiu4 x iou4xiu5 x iou5xu1 x v1xu2 x v2xu3 x v3xu4 x v4xu5 x v5xuan1 x van1xuan2 x van2xuan3 x van3xuan4 x van4xuan5 x van5xue1 x ve1xue2 x ve2xue3 x ve3xue4 x ve4xue5 x ve5xun1 x vn1xun2 x vn2xun3 x vn3xun4 x vn4xun5 x vn5ya1 y ia1ya2 y ia2ya3 y ia3ya4 y ia4ya5 y ia5yan1 y ian1yan2 y ian2yan3 y ian3yan4 y ian4yan5 y ian5yang1 y iang1yang2 y iang2yang3 y iang3yang4 y iang4yang5 y iang5yao1 y iao1yao2 y iao2yao3 y iao3yao4 y iao4yao5 y iao5ye1 y ie1ye2 y ie2ye3 y ie3ye4 y ie4ye5 y ie5yi1 y i1yi2 y i2yi3 y i3yi4 y i4yi5 y i5yin1 y in1yin2 y in2yin3 y in3yin4 y in4yin5 y in5ying1 y ing1ying2 y ing2ying3 y ing3ying4 y ing4ying5 y ing5yo1 y iou1yo2 y iou2yo3 y iou3yo4 y iou4yo5 y iou5yong1 y iong1yong2 y iong2yong3 y iong3yong4 y iong4yong5 y iong5you1 y iou1you2 y iou2you3 y iou3you4 y iou4you5 y iou5yu1 y v1yu2 y v2yu3 y v3yu4 y v4yu5 y v5yuan1 y van1yuan2 y van2yuan3 y van3yuan4 y van4yuan5 y van5yue1 y ve1yue2 y ve2yue3 y ve3yue4 y ve4yue5 y ve5yun1 y vn1yun2 y vn2yun3 y vn3yun4 y vn4yun5 y vn5za1 z a1za2 z a2za3 z a3za4 z a4za5 z a5zai1 z ai1zai2 z ai2zai3 z ai3zai4 z ai4zai5 z ai5zan1 z an1zan2 z an2zan3 z an3zan4 z an4zan5 z an5zang1 z ang1zang2 z ang2zang3 z ang3zang4 z ang4zang5 z ang5zao1 z ao1zao2 z ao2zao3 z ao3zao4 z ao4zao5 z ao5ze1 z e1ze2 z e2ze3 z e3ze4 z e4ze5 z e5zei1 z ei1zei2 z ei2zei3 z ei3zei4 z ei4zei5 z ei5zen1 z en1zen2 z en2zen3 z en3zen4 z en4zen5 z en5zeng1 z eng1zeng2 z eng2zeng3 z eng3zeng4 z eng4zeng5 z eng5zha1 zh a1zha2 zh a2zha3 zh a3zha4 zh a4zha5 zh a5zhai1 zh ai1zhai2 zh ai2zhai3 zh ai3zhai4 zh ai4zhai5 zh ai5zhan1 zh an1zhan2 zh an2zhan3 zh an3zhan4 zh an4zhan5 zh an5zhang1 zh ang1zhang2 zh ang2zhang3 zh ang3zhang4 zh ang4zhang5 zh ang5zhao1 zh ao1zhao2 zh ao2zhao3 zh ao3zhao4 zh ao4zhao5 zh ao5zhe1 zh e1zhe2 zh e2zhe3 zh e3zhe4 zh e4zhe5 zh e5zhei1 zh ei1zhei2 zh ei2zhei3 zh ei3zhei4 zh ei4zhei5 zh ei5zhen1 zh en1zhen2 zh en2zhen3 zh en3zhen4 zh en4zhen5 zh en5zheng1 zh eng1zheng2 zh eng2zheng3 zh eng3zheng4 zh eng4zheng5 zh eng5zhi1 zh iii1zhi2 zh iii2zhi3 zh iii3zhi4 zh iii4zhi5 zh iii5zhong1 zh ong1zhong2 zh ong2zhong3 zh ong3zhong4 zh ong4zhong5 zh ong5zhou1 zh ou1zhou2 zh ou2zhou3 zh ou3zhou4 zh ou4zhou5 zh ou5zhu1 zh u1zhu2 zh u2zhu3 zh u3zhu4 zh u4zhu5 zh u5zhua1 zh ua1zhua2 zh ua2zhua3 zh ua3zhua4 zh ua4zhua5 zh ua5zhuai1 zh uai1zhuai2 zh uai2zhuai3 zh uai3zhuai4 zh uai4zhuai5 zh uai5zhuan1 zh uan1zhuan2 zh uan2zhuan3 zh uan3zhuan4 zh uan4zhuan5 zh uan5zhuang1 zh uang1zhuang2 zh uang2zhuang3 zh uang3zhuang4 zh uang4zhuang5 zh uang5zhui1 zh uei1zhui2 zh uei2zhui3 zh uei3zhui4 zh uei4zhui5 zh uei5zhun1 zh uen1zhun2 zh uen2zhun3 zh uen3zhun4 zh uen4zhun5 zh uen5zhuo1 zh uo1zhuo2 zh uo2zhuo3 zh uo3zhuo4 zh uo4zhuo5 zh uo5zi1 z ii1zi2 z ii2zi3 z ii3zi4 z ii4zi5 z ii5zong1 z ong1zong2 z ong2zong3 z ong3zong4 z ong4zong5 z ong5zou1 z ou1zou2 z ou2zou3 z ou3zou4 z ou4zou5 z ou5zu1 z u1zu2 z u2zu3 z u3zu4 z u4zu5 z u5zuan1 z uan1zuan2 z uan2zuan3 z uan3zuan4 z uan4zuan5 z uan5zui1 z uei1zui2 z uei2zui3 z uei3zui4 z uei4zui5 z uei5zun1 z uen1zun2 z uen2zun3 z uen3zun4 z uen4zun5 z uen5zuo1 z uo1zuo2 z uo2zuo3 z uo3zuo4 z uo4zuo5 z uo5ar1 a1 rrar2 a2 rrar3 a3 rrar4 a4 rrar5 a5 rrair1 ai1 rrair2 ai2 rrair3 ai3 rrair4 ai4 rrair5 ai5 rranr1 an1 rranr2 an2 rranr3 an3 rranr4 an4 rranr5 an5 rrangr1 ang1 rrangr2 ang2 rrangr3 ang3 rrangr4 ang4 rrangr5 ang5 rraor1 ao1 rraor2 ao2 rraor3 ao3 rraor4 ao4 rraor5 ao5 rrbar1 b a1 rrbar2 b a2 rrbar3 b a3 rrbar4 b a4 rrbar5 b a5 rrbair1 b ai1 rrbair2 b ai2 rrbair3 b ai3 rrbair4 b ai4 rrbair5 b ai5 rrbanr1 b an1 rrbanr2 b an2 rrbanr3 b an3 rrbanr4 b an4 rrbanr5 b an5 rrbangr1 b ang1 rrbangr2 b ang2 rrbangr3 b ang3 rrbangr4 b ang4 rrbangr5 b ang5 rrbaor1 b ao1 rrbaor2 b ao2 rrbaor3 b ao3 rrbaor4 b ao4 rrbaor5 b ao5 rrbeir1 b ei1 rrbeir2 b ei2 rrbeir3 b ei3 rrbeir4 b ei4 rrbeir5 b ei5 rrbenr1 b en1 rrbenr2 b en2 rrbenr3 b en3 rrbenr4 b en4 rrbenr5 b en5 rrbengr1 b eng1 rrbengr2 b eng2 rrbengr3 b eng3 rrbengr4 b eng4 rrbengr5 b eng5 rrbir1 b i1 rrbir2 b i2 rrbir3 b i3 rrbir4 b i4 rrbir5 b i5 rrbianr1 b ian1 rrbianr2 b ian2 rrbianr3 b ian3 rrbianr4 b ian4 rrbianr5 b ian5 rrbiaor1 b iao1 rrbiaor2 b iao2 rrbiaor3 b iao3 rrbiaor4 b iao4 rrbiaor5 b iao5 rrbier1 b ie1 rrbier2 b ie2 rrbier3 b ie3 rrbier4 b ie4 rrbier5 b ie5 rrbinr1 b in1 rrbinr2 b in2 rrbinr3 b in3 rrbinr4 b in4 rrbinr5 b in5 rrbingr1 b ing1 rrbingr2 b ing2 rrbingr3 b ing3 rrbingr4 b ing4 rrbingr5 b ing5 rrbor1 b o1 rrbor2 b o2 rrbor3 b o3 rrbor4 b o4 rrbor5 b o5 rrbur1 b u1 rrbur2 b u2 rrbur3 b u3 rrbur4 b u4 rrbur5 b u5 rrcar1 c a1 rrcar2 c a2 rrcar3 c a3 rrcar4 c a4 rrcar5 c a5 rrcair1 c ai1 rrcair2 c ai2 rrcair3 c ai3 rrcair4 c ai4 rrcair5 c ai5 rrcanr1 c an1 rrcanr2 c an2 rrcanr3 c an3 rrcanr4 c an4 rrcanr5 c an5 rrcangr1 c ang1 rrcangr2 c ang2 rrcangr3 c ang3 rrcangr4 c ang4 rrcangr5 c ang5 rrcaor1 c ao1 rrcaor2 c ao2 rrcaor3 c ao3 rrcaor4 c ao4 rrcaor5 c ao5 rrcer1 c e1 rrcer2 c e2 rrcer3 c e3 rrcer4 c e4 rrcer5 c e5 rrcenr1 c en1 rrcenr2 c en2 rrcenr3 c en3 rrcenr4 c en4 rrcenr5 c en5 rrcengr1 c eng1 rrcengr2 c eng2 rrcengr3 c eng3 rrcengr4 c eng4 rrcengr5 c eng5 rrchar1 ch a1 rrchar2 ch a2 rrchar3 ch a3 rrchar4 ch a4 rrchar5 ch a5 rrchair1 ch ai1 rrchair2 ch ai2 rrchair3 ch ai3 rrchair4 ch ai4 rrchair5 ch ai5 rrchanr1 ch an1 rrchanr2 ch an2 rrchanr3 ch an3 rrchanr4 ch an4 rrchanr5 ch an5 rrchangr1 ch ang1 rrchangr2 ch ang2 rrchangr3 ch ang3 rrchangr4 ch ang4 rrchangr5 ch ang5 rrchaor1 ch ao1 rrchaor2 ch ao2 rrchaor3 ch ao3 rrchaor4 ch ao4 rrchaor5 ch ao5 rrcher1 ch e1 rrcher2 ch e2 rrcher3 ch e3 rrcher4 ch e4 rrcher5 ch e5 rrchenr1 ch en1 rrchenr2 ch en2 rrchenr3 ch en3 rrchenr4 ch en4 rrchenr5 ch en5 rrchengr1 ch eng1 rrchengr2 ch eng2 rrchengr3 ch eng3 rrchengr4 ch eng4 rrchengr5 ch eng5 rrchir1 ch iii1 rrchir2 ch iii2 rrchir3 ch iii3 rrchir4 ch iii4 rrchir5 ch iii5 rrchongr1 ch ong1 rrchongr2 ch ong2 rrchongr3 ch ong3 rrchongr4 ch ong4 rrchongr5 ch ong5 rrchour1 ch ou1 rrchour2 ch ou2 rrchour3 ch ou3 rrchour4 ch ou4 rrchour5 ch ou5 rrchur1 ch u1 rrchur2 ch u2 rrchur3 ch u3 rrchur4 ch u4 rrchur5 ch u5 rrchuair1 ch uai1 rrchuair2 ch uai2 rrchuair3 ch uai3 rrchuair4 ch uai4 rrchuair5 ch uai5 rrchuanr1 ch uan1 rrchuanr2 ch uan2 rrchuanr3 ch uan3 rrchuanr4 ch uan4 rrchuanr5 ch uan5 rrchuangr1 ch uang1 rrchuangr2 ch uang2 rrchuangr3 ch uang3 rrchuangr4 ch uang4 rrchuangr5 ch uang5 rrchuir1 ch uei1 rrchuir2 ch uei2 rrchuir3 ch uei3 rrchuir4 ch uei4 rrchuir5 ch uei5 rrchunr1 ch uen1 rrchunr2 ch uen2 rrchunr3 ch uen3 rrchunr4 ch uen4 rrchunr5 ch uen5 rrchuor1 ch uo1 rrchuor2 ch uo2 rrchuor3 ch uo3 rrchuor4 ch uo4 rrchuor5 ch uo5 rrcir1 c ii1 rrcir2 c ii2 rrcir3 c ii3 rrcir4 c ii4 rrcir5 c ii5 rrcongr1 c ong1 rrcongr2 c ong2 rrcongr3 c ong3 rrcongr4 c ong4 rrcongr5 c ong5 rrcour1 c ou1 rrcour2 c ou2 rrcour3 c ou3 rrcour4 c ou4 rrcour5 c ou5 rrcur1 c u1 rrcur2 c u2 rrcur3 c u3 rrcur4 c u4 rrcur5 c u5 rrcuanr1 c uan1 rrcuanr2 c uan2 rrcuanr3 c uan3 rrcuanr4 c uan4 rrcuanr5 c uan5 rrcuir1 c uei1 rrcuir2 c uei2 rrcuir3 c uei3 rrcuir4 c uei4 rrcuir5 c uei5 rrcunr1 c uen1 rrcunr2 c uen2 rrcunr3 c uen3 rrcunr4 c uen4 rrcunr5 c uen5 rrcuor1 c uo1 rrcuor2 c uo2 rrcuor3 c uo3 rrcuor4 c uo4 rrcuor5 c uo5 rrdar1 d a1 rrdar2 d a2 rrdar3 d a3 rrdar4 d a4 rrdar5 d a5 rrdair1 d ai1 rrdair2 d ai2 rrdair3 d ai3 rrdair4 d ai4 rrdair5 d ai5 rrdanr1 d an1 rrdanr2 d an2 rrdanr3 d an3 rrdanr4 d an4 rrdanr5 d an5 rrdangr1 d ang1 rrdangr2 d ang2 rrdangr3 d ang3 rrdangr4 d ang4 rrdangr5 d ang5 rrdaor1 d ao1 rrdaor2 d ao2 rrdaor3 d ao3 rrdaor4 d ao4 rrdaor5 d ao5 rrder1 d e1 rrder2 d e2 rrder3 d e3 rrder4 d e4 rrder5 d e5 rrdeir1 d ei1 rrdeir2 d ei2 rrdeir3 d ei3 rrdeir4 d ei4 rrdeir5 d ei5 rrdenr1 d en1 rrdenr2 d en2 rrdenr3 d en3 rrdenr4 d en4 rrdenr5 d en5 rrdengr1 d eng1 rrdengr2 d eng2 rrdengr3 d eng3 rrdengr4 d eng4 rrdengr5 d eng5 rrdir1 d i1 rrdir2 d i2 rrdir3 d i3 rrdir4 d i4 rrdir5 d i5 rrdiar1 d ia1 rrdiar2 d ia2 rrdiar3 d ia3 rrdiar4 d ia4 rrdiar5 d ia5 rrdianr1 d ian1 rrdianr2 d ian2 rrdianr3 d ian3 rrdianr4 d ian4 rrdianr5 d ian5 rrdiaor1 d iao1 rrdiaor2 d iao2 rrdiaor3 d iao3 rrdiaor4 d iao4 rrdiaor5 d iao5 rrdier1 d ie1 rrdier2 d ie2 rrdier3 d ie3 rrdier4 d ie4 rrdier5 d ie5 rrdingr1 d ing1 rrdingr2 d ing2 rrdingr3 d ing3 rrdingr4 d ing4 rrdingr5 d ing5 rrdiur1 d iou1 rrdiur2 d iou2 rrdiur3 d iou3 rrdiur4 d iou4 rrdiur5 d iou5 rrdongr1 d ong1 rrdongr2 d ong2 rrdongr3 d ong3 rrdongr4 d ong4 rrdongr5 d ong5 rrdour1 d ou1 rrdour2 d ou2 rrdour3 d ou3 rrdour4 d ou4 rrdour5 d ou5 rrdur1 d u1 rrdur2 d u2 rrdur3 d u3 rrdur4 d u4 rrdur5 d u5 rrduanr1 d uan1 rrduanr2 d uan2 rrduanr3 d uan3 rrduanr4 d uan4 rrduanr5 d uan5 rrduir1 d uei1 rrduir2 d uei2 rrduir3 d uei3 rrduir4 d uei4 rrduir5 d uei5 rrdunr1 d uen1 rrdunr2 d uen2 rrdunr3 d uen3 rrdunr4 d uen4 rrdunr5 d uen5 rrduor1 d uo1 rrduor2 d uo2 rrduor3 d uo3 rrduor4 d uo4 rrduor5 d uo5 rrer1 e1 rrer2 e2 rrer3 e3 rrer4 e4 rrer5 e5 rreir1 ei1 rreir2 ei2 rreir3 ei3 rreir4 ei4 rreir5 ei5 rrenr1 en1 rrenr2 en2 rrenr3 en3 rrenr4 en4 rrenr5 en5 rrengr1 eng1 rrengr2 eng2 rrengr3 eng3 rrengr4 eng4 rrengr5 eng5 rrfar1 f a1 rrfar2 f a2 rrfar3 f a3 rrfar4 f a4 rrfar5 f a5 rrfanr1 f an1 rrfanr2 f an2 rrfanr3 f an3 rrfanr4 f an4 rrfanr5 f an5 rrfangr1 f ang1 rrfangr2 f ang2 rrfangr3 f ang3 rrfangr4 f ang4 rrfangr5 f ang5 rrfeir1 f ei1 rrfeir2 f ei2 rrfeir3 f ei3 rrfeir4 f ei4 rrfeir5 f ei5 rrfenr1 f en1 rrfenr2 f en2 rrfenr3 f en3 rrfenr4 f en4 rrfenr5 f en5 rrfengr1 f eng1 rrfengr2 f eng2 rrfengr3 f eng3 rrfengr4 f eng4 rrfengr5 f eng5 rrfor1 f o1 rrfor2 f o2 rrfor3 f o3 rrfor4 f o4 rrfor5 f o5 rrfour1 f ou1 rrfour2 f ou2 rrfour3 f ou3 rrfour4 f ou4 rrfour5 f ou5 rrfur1 f u1 rrfur2 f u2 rrfur3 f u3 rrfur4 f u4 rrfur5 f u5 rrgar1 g a1 rrgar2 g a2 rrgar3 g a3 rrgar4 g a4 rrgar5 g a5 rrgair1 g ai1 rrgair2 g ai2 rrgair3 g ai3 rrgair4 g ai4 rrgair5 g ai5 rrganr1 g an1 rrganr2 g an2 rrganr3 g an3 rrganr4 g an4 rrganr5 g an5 rrgangr1 g ang1 rrgangr2 g ang2 rrgangr3 g ang3 rrgangr4 g ang4 rrgangr5 g ang5 rrgaor1 g ao1 rrgaor2 g ao2 rrgaor3 g ao3 rrgaor4 g ao4 rrgaor5 g ao5 rrger1 g e1 rrger2 g e2 rrger3 g e3 rrger4 g e4 rrger5 g e5 rrgeir1 g ei1 rrgeir2 g ei2 rrgeir3 g ei3 rrgeir4 g ei4 rrgeir5 g ei5 rrgenr1 g en1 rrgenr2 g en2 rrgenr3 g en3 rrgenr4 g en4 rrgenr5 g en5 rrgengr1 g eng1 rrgengr2 g eng2 rrgengr3 g eng3 rrgengr4 g eng4 rrgengr5 g eng5 rrgongr1 g ong1 rrgongr2 g ong2 rrgongr3 g ong3 rrgongr4 g ong4 rrgongr5 g ong5 rrgour1 g ou1 rrgour2 g ou2 rrgour3 g ou3 rrgour4 g ou4 rrgour5 g ou5 rrgur1 g u1 rrgur2 g u2 rrgur3 g u3 rrgur4 g u4 rrgur5 g u5 rrguar1 g ua1 rrguar2 g ua2 rrguar3 g ua3 rrguar4 g ua4 rrguar5 g ua5 rrguair1 g uai1 rrguair2 g uai2 rrguair3 g uai3 rrguair4 g uai4 rrguair5 g uai5 rrguanr1 g uan1 rrguanr2 g uan2 rrguanr3 g uan3 rrguanr4 g uan4 rrguanr5 g uan5 rrguangr1 g uang1 rrguangr2 g uang2 rrguangr3 g uang3 rrguangr4 g uang4 rrguangr5 g uang5 rrguir1 g uei1 rrguir2 g uei2 rrguir3 g uei3 rrguir4 g uei4 rrguir5 g uei5 rrgunr1 g uen1 rrgunr2 g uen2 rrgunr3 g uen3 rrgunr4 g uen4 rrgunr5 g uen5 rrguor1 g uo1 rrguor2 g uo2 rrguor3 g uo3 rrguor4 g uo4 rrguor5 g uo5 rrhar1 h a1 rrhar2 h a2 rrhar3 h a3 rrhar4 h a4 rrhar5 h a5 rrhair1 h ai1 rrhair2 h ai2 rrhair3 h ai3 rrhair4 h ai4 rrhair5 h ai5 rrhanr1 h an1 rrhanr2 h an2 rrhanr3 h an3 rrhanr4 h an4 rrhanr5 h an5 rrhangr1 h ang1 rrhangr2 h ang2 rrhangr3 h ang3 rrhangr4 h ang4 rrhangr5 h ang5 rrhaor1 h ao1 rrhaor2 h ao2 rrhaor3 h ao3 rrhaor4 h ao4 rrhaor5 h ao5 rrher1 h e1 rrher2 h e2 rrher3 h e3 rrher4 h e4 rrher5 h e5 rrheir1 h ei1 rrheir2 h ei2 rrheir3 h ei3 rrheir4 h ei4 rrheir5 h ei5 rrhenr1 h en1 rrhenr2 h en2 rrhenr3 h en3 rrhenr4 h en4 rrhenr5 h en5 rrhengr1 h eng1 rrhengr2 h eng2 rrhengr3 h eng3 rrhengr4 h eng4 rrhengr5 h eng5 rrhongr1 h ong1 rrhongr2 h ong2 rrhongr3 h ong3 rrhongr4 h ong4 rrhongr5 h ong5 rrhour1 h ou1 rrhour2 h ou2 rrhour3 h ou3 rrhour4 h ou4 rrhour5 h ou5 rrhur1 h u1 rrhur2 h u2 rrhur3 h u3 rrhur4 h u4 rrhur5 h u5 rrhuar1 h ua1 rrhuar2 h ua2 rrhuar3 h ua3 rrhuar4 h ua4 rrhuar5 h ua5 rrhuair1 h uai1 rrhuair2 h uai2 rrhuair3 h uai3 rrhuair4 h uai4 rrhuair5 h uai5 rrhuanr1 h uan1 rrhuanr2 h uan2 rrhuanr3 h uan3 rrhuanr4 h uan4 rrhuanr5 h uan5 rrhuangr1 h uang1 rrhuangr2 h uang2 rrhuangr3 h uang3 rrhuangr4 h uang4 rrhuangr5 h uang5 rrhuir1 h uei1 rrhuir2 h uei2 rrhuir3 h uei3 rrhuir4 h uei4 rrhuir5 h uei5 rrhunr1 h uen1 rrhunr2 h uen2 rrhunr3 h uen3 rrhunr4 h uen4 rrhunr5 h uen5 rrhuor1 h uo1 rrhuor2 h uo2 rrhuor3 h uo3 rrhuor4 h uo4 rrhuor5 h uo5 rrjir1 j i1 rrjir2 j i2 rrjir3 j i3 rrjir4 j i4 rrjir5 j i5 rrjiar1 j ia1 rrjiar2 j ia2 rrjiar3 j ia3 rrjiar4 j ia4 rrjiar5 j ia5 rrjianr1 j ian1 rrjianr2 j ian2 rrjianr3 j ian3 rrjianr4 j ian4 rrjianr5 j ian5 rrjiangr1 j iang1 rrjiangr2 j iang2 rrjiangr3 j iang3 rrjiangr4 j iang4 rrjiangr5 j iang5 rrjiaor1 j iao1 rrjiaor2 j iao2 rrjiaor3 j iao3 rrjiaor4 j iao4 rrjiaor5 j iao5 rrjier1 j ie1 rrjier2 j ie2 rrjier3 j ie3 rrjier4 j ie4 rrjier5 j ie5 rrjinr1 j in1 rrjinr2 j in2 rrjinr3 j in3 rrjinr4 j in4 rrjinr5 j in5 rrjingr1 j ing1 rrjingr2 j ing2 rrjingr3 j ing3 rrjingr4 j ing4 rrjingr5 j ing5 rrjiongr1 j iong1 rrjiongr2 j iong2 rrjiongr3 j iong3 rrjiongr4 j iong4 rrjiongr5 j iong5 rrjiur1 j iou1 rrjiur2 j iou2 rrjiur3 j iou3 rrjiur4 j iou4 rrjiur5 j iou5 rrjur1 j v1 rrjur2 j v2 rrjur3 j v3 rrjur4 j v4 rrjur5 j v5 rrjuanr1 j van1 rrjuanr2 j van2 rrjuanr3 j van3 rrjuanr4 j van4 rrjuanr5 j van5 rrjuer1 j ve1 rrjuer2 j ve2 rrjuer3 j ve3 rrjuer4 j ve4 rrjuer5 j ve5 rrjunr1 j vn1 rrjunr2 j vn2 rrjunr3 j vn3 rrjunr4 j vn4 rrjunr5 j vn5 rrkar1 k a1 rrkar2 k a2 rrkar3 k a3 rrkar4 k a4 rrkar5 k a5 rrkair1 k ai1 rrkair2 k ai2 rrkair3 k ai3 rrkair4 k ai4 rrkair5 k ai5 rrkanr1 k an1 rrkanr2 k an2 rrkanr3 k an3 rrkanr4 k an4 rrkanr5 k an5 rrkangr1 k ang1 rrkangr2 k ang2 rrkangr3 k ang3 rrkangr4 k ang4 rrkangr5 k ang5 rrkaor1 k ao1 rrkaor2 k ao2 rrkaor3 k ao3 rrkaor4 k ao4 rrkaor5 k ao5 rrker1 k e1 rrker2 k e2 rrker3 k e3 rrker4 k e4 rrker5 k e5 rrkeir1 k ei1 rrkeir2 k ei2 rrkeir3 k ei3 rrkeir4 k ei4 rrkeir5 k ei5 rrkenr1 k en1 rrkenr2 k en2 rrkenr3 k en3 rrkenr4 k en4 rrkenr5 k en5 rrkengr1 k eng1 rrkengr2 k eng2 rrkengr3 k eng3 rrkengr4 k eng4 rrkengr5 k eng5 rrkongr1 k ong1 rrkongr2 k ong2 rrkongr3 k ong3 rrkongr4 k ong4 rrkongr5 k ong5 rrkour1 k ou1 rrkour2 k ou2 rrkour3 k ou3 rrkour4 k ou4 rrkour5 k ou5 rrkur1 k u1 rrkur2 k u2 rrkur3 k u3 rrkur4 k u4 rrkur5 k u5 rrkuar1 k ua1 rrkuar2 k ua2 rrkuar3 k ua3 rrkuar4 k ua4 rrkuar5 k ua5 rrkuair1 k uai1 rrkuair2 k uai2 rrkuair3 k uai3 rrkuair4 k uai4 rrkuair5 k uai5 rrkuanr1 k uan1 rrkuanr2 k uan2 rrkuanr3 k uan3 rrkuanr4 k uan4 rrkuanr5 k uan5 rrkuangr1 k uang1 rrkuangr2 k uang2 rrkuangr3 k uang3 rrkuangr4 k uang4 rrkuangr5 k uang5 rrkuir1 k uei1 rrkuir2 k uei2 rrkuir3 k uei3 rrkuir4 k uei4 rrkuir5 k uei5 rrkunr1 k uen1 rrkunr2 k uen2 rrkunr3 k uen3 rrkunr4 k uen4 rrkunr5 k uen5 rrkuor1 k uo1 rrkuor2 k uo2 rrkuor3 k uo3 rrkuor4 k uo4 rrkuor5 k uo5 rrlar1 l a1 rrlar2 l a2 rrlar3 l a3 rrlar4 l a4 rrlar5 l a5 rrlair1 l ai1 rrlair2 l ai2 rrlair3 l ai3 rrlair4 l ai4 rrlair5 l ai5 rrlanr1 l an1 rrlanr2 l an2 rrlanr3 l an3 rrlanr4 l an4 rrlanr5 l an5 rrlangr1 l ang1 rrlangr2 l ang2 rrlangr3 l ang3 rrlangr4 l ang4 rrlangr5 l ang5 rrlaor1 l ao1 rrlaor2 l ao2 rrlaor3 l ao3 rrlaor4 l ao4 rrlaor5 l ao5 rrler1 l e1 rrler2 l e2 rrler3 l e3 rrler4 l e4 rrler5 l e5 rrleir1 l ei1 rrleir2 l ei2 rrleir3 l ei3 rrleir4 l ei4 rrleir5 l ei5 rrlengr1 l eng1 rrlengr2 l eng2 rrlengr3 l eng3 rrlengr4 l eng4 rrlengr5 l eng5 rrlir1 l i1 rrlir2 l i2 rrlir3 l i3 rrlir4 l i4 rrlir5 l i5 rrliar1 l ia1 rrliar2 l ia2 rrliar3 l ia3 rrliar4 l ia4 rrliar5 l ia5 rrlianr1 l ian1 rrlianr2 l ian2 rrlianr3 l ian3 rrlianr4 l ian4 rrlianr5 l ian5 rrliangr1 l iang1 rrliangr2 l iang2 rrliangr3 l iang3 rrliangr4 l iang4 rrliangr5 l iang5 rrliaor1 l iao1 rrliaor2 l iao2 rrliaor3 l iao3 rrliaor4 l iao4 rrliaor5 l iao5 rrlier1 l ie1 rrlier2 l ie2 rrlier3 l ie3 rrlier4 l ie4 rrlier5 l ie5 rrlinr1 l in1 rrlinr2 l in2 rrlinr3 l in3 rrlinr4 l in4 rrlinr5 l in5 rrlingr1 l ing1 rrlingr2 l ing2 rrlingr3 l ing3 rrlingr4 l ing4 rrlingr5 l ing5 rrliur1 l iou1 rrliur2 l iou2 rrliur3 l iou3 rrliur4 l iou4 rrliur5 l iou5 rrlor1 l o1 rrlor2 l o2 rrlor3 l o3 rrlor4 l o4 rrlor5 l o5 rrlongr1 l ong1 rrlongr2 l ong2 rrlongr3 l ong3 rrlongr4 l ong4 rrlongr5 l ong5 rrlour1 l ou1 rrlour2 l ou2 rrlour3 l ou3 rrlour4 l ou4 rrlour5 l ou5 rrlur1 l u1 rrlur2 l u2 rrlur3 l u3 rrlur4 l u4 rrlur5 l u5 rrluanr1 l uan1 rrluanr2 l uan2 rrluanr3 l uan3 rrluanr4 l uan4 rrluanr5 l uan5 rrluer1 l ve1 rrluer2 l ve2 rrluer3 l ve3 rrluer4 l ve4 rrluer5 l ve5 rrlver1 l ve1 rrlver2 l ve2 rrlver3 l ve3 rrlver4 l ve4 rrlver5 l ve5 rrlunr1 l uen1 rrlunr2 l uen2 rrlunr3 l uen3 rrlunr4 l uen4 rrlunr5 l uen5 rrluor1 l uo1 rrluor2 l uo2 rrluor3 l uo3 rrluor4 l uo4 rrluor5 l uo5 rrlvr1 l v1 rrlvr2 l v2 rrlvr3 l v3 rrlvr4 l v4 rrlvr5 l v5 rrmar1 m a1 rrmar2 m a2 rrmar3 m a3 rrmar4 m a4 rrmar5 m a5 rrmair1 m ai1 rrmair2 m ai2 rrmair3 m ai3 rrmair4 m ai4 rrmair5 m ai5 rrmanr1 m an1 rrmanr2 m an2 rrmanr3 m an3 rrmanr4 m an4 rrmanr5 m an5 rrmangr1 m ang1 rrmangr2 m ang2 rrmangr3 m ang3 rrmangr4 m ang4 rrmangr5 m ang5 rrmaor1 m ao1 rrmaor2 m ao2 rrmaor3 m ao3 rrmaor4 m ao4 rrmaor5 m ao5 rrmer1 m e1 rrmer2 m e2 rrmer3 m e3 rrmer4 m e4 rrmer5 m e5 rrmeir1 m ei1 rrmeir2 m ei2 rrmeir3 m ei3 rrmeir4 m ei4 rrmeir5 m ei5 rrmenr1 m en1 rrmenr2 m en2 rrmenr3 m en3 rrmenr4 m en4 rrmenr5 m en5 rrmengr1 m eng1 rrmengr2 m eng2 rrmengr3 m eng3 rrmengr4 m eng4 rrmengr5 m eng5 rrmir1 m i1 rrmir2 m i2 rrmir3 m i3 rrmir4 m i4 rrmir5 m i5 rrmianr1 m ian1 rrmianr2 m ian2 rrmianr3 m ian3 rrmianr4 m ian4 rrmianr5 m ian5 rrmiaor1 m iao1 rrmiaor2 m iao2 rrmiaor3 m iao3 rrmiaor4 m iao4 rrmiaor5 m iao5 rrmier1 m ie1 rrmier2 m ie2 rrmier3 m ie3 rrmier4 m ie4 rrmier5 m ie5 rrminr1 m in1 rrminr2 m in2 rrminr3 m in3 rrminr4 m in4 rrminr5 m in5 rrmingr1 m ing1 rrmingr2 m ing2 rrmingr3 m ing3 rrmingr4 m ing4 rrmingr5 m ing5 rrmiur1 m iou1 rrmiur2 m iou2 rrmiur3 m iou3 rrmiur4 m iou4 rrmiur5 m iou5 rrmor1 m o1 rrmor2 m o2 rrmor3 m o3 rrmor4 m o4 rrmor5 m o5 rrmour1 m ou1 rrmour2 m ou2 rrmour3 m ou3 rrmour4 m ou4 rrmour5 m ou5 rrmur1 m u1 rrmur2 m u2 rrmur3 m u3 rrmur4 m u4 rrmur5 m u5 rrnar1 n a1 rrnar2 n a2 rrnar3 n a3 rrnar4 n a4 rrnar5 n a5 rrnair1 n ai1 rrnair2 n ai2 rrnair3 n ai3 rrnair4 n ai4 rrnair5 n ai5 rrnanr1 n an1 rrnanr2 n an2 rrnanr3 n an3 rrnanr4 n an4 rrnanr5 n an5 rrnangr1 n ang1 rrnangr2 n ang2 rrnangr3 n ang3 rrnangr4 n ang4 rrnangr5 n ang5 rrnaor1 n ao1 rrnaor2 n ao2 rrnaor3 n ao3 rrnaor4 n ao4 rrnaor5 n ao5 rrner1 n e1 rrner2 n e2 rrner3 n e3 rrner4 n e4 rrner5 n e5 rrneir1 n ei1 rrneir2 n ei2 rrneir3 n ei3 rrneir4 n ei4 rrneir5 n ei5 rrnenr1 n en1 rrnenr2 n en2 rrnenr3 n en3 rrnenr4 n en4 rrnenr5 n en5 rrnengr1 n eng1 rrnengr2 n eng2 rrnengr3 n eng3 rrnengr4 n eng4 rrnengr5 n eng5 rrnir1 n i1 rrnir2 n i2 rrnir3 n i3 rrnir4 n i4 rrnir5 n i5 rrnianr1 n ian1 rrnianr2 n ian2 rrnianr3 n ian3 rrnianr4 n ian4 rrnianr5 n ian5 rrniangr1 n iang1 rrniangr2 n iang2 rrniangr3 n iang3 rrniangr4 n iang4 rrniangr5 n iang5 rrniaor1 n iao1 rrniaor2 n iao2 rrniaor3 n iao3 rrniaor4 n iao4 rrniaor5 n iao5 rrnier1 n ie1 rrnier2 n ie2 rrnier3 n ie3 rrnier4 n ie4 rrnier5 n ie5 rrninr1 n in1 rrninr2 n in2 rrninr3 n in3 rrninr4 n in4 rrninr5 n in5 rrningr1 n ing1 rrningr2 n ing2 rrningr3 n ing3 rrningr4 n ing4 rrningr5 n ing5 rrniur1 n iou1 rrniur2 n iou2 rrniur3 n iou3 rrniur4 n iou4 rrniur5 n iou5 rrnongr1 n ong1 rrnongr2 n ong2 rrnongr3 n ong3 rrnongr4 n ong4 rrnongr5 n ong5 rrnour1 n ou1 rrnour2 n ou2 rrnour3 n ou3 rrnour4 n ou4 rrnour5 n ou5 rrnur1 n u1 rrnur2 n u2 rrnur3 n u3 rrnur4 n u4 rrnur5 n u5 rrnuanr1 n uan1 rrnuanr2 n uan2 rrnuanr3 n uan3 rrnuanr4 n uan4 rrnuanr5 n uan5 rrnuer1 n ve1 rrnuer2 n ve2 rrnuer3 n ve3 rrnuer4 n ve4 rrnuer5 n ve5 rrnver1 n ve1 rrnver2 n ve2 rrnver3 n ve3 rrnver4 n ve4 rrnver5 n ve5 rrnuor1 n uo1 rrnuor2 n uo2 rrnuor3 n uo3 rrnuor4 n uo4 rrnuor5 n uo5 rrnvr1 n v1 rrnvr2 n v2 rrnvr3 n v3 rrnvr4 n v4 rrnvr5 n v5 rror1 o1 rror2 o2 rror3 o3 rror4 o4 rror5 o5 rrour1 ou1 rrour2 ou2 rrour3 ou3 rrour4 ou4 rrour5 ou5 rrpar1 p a1 rrpar2 p a2 rrpar3 p a3 rrpar4 p a4 rrpar5 p a5 rrpair1 p ai1 rrpair2 p ai2 rrpair3 p ai3 rrpair4 p ai4 rrpair5 p ai5 rrpanr1 p an1 rrpanr2 p an2 rrpanr3 p an3 rrpanr4 p an4 rrpanr5 p an5 rrpangr1 p ang1 rrpangr2 p ang2 rrpangr3 p ang3 rrpangr4 p ang4 rrpangr5 p ang5 rrpaor1 p ao1 rrpaor2 p ao2 rrpaor3 p ao3 rrpaor4 p ao4 rrpaor5 p ao5 rrpeir1 p ei1 rrpeir2 p ei2 rrpeir3 p ei3 rrpeir4 p ei4 rrpeir5 p ei5 rrpenr1 p en1 rrpenr2 p en2 rrpenr3 p en3 rrpenr4 p en4 rrpenr5 p en5 rrpengr1 p eng1 rrpengr2 p eng2 rrpengr3 p eng3 rrpengr4 p eng4 rrpengr5 p eng5 rrpir1 p i1 rrpir2 p i2 rrpir3 p i3 rrpir4 p i4 rrpir5 p i5 rrpianr1 p ian1 rrpianr2 p ian2 rrpianr3 p ian3 rrpianr4 p ian4 rrpianr5 p ian5 rrpiaor1 p iao1 rrpiaor2 p iao2 rrpiaor3 p iao3 rrpiaor4 p iao4 rrpiaor5 p iao5 rrpier1 p ie1 rrpier2 p ie2 rrpier3 p ie3 rrpier4 p ie4 rrpier5 p ie5 rrpinr1 p in1 rrpinr2 p in2 rrpinr3 p in3 rrpinr4 p in4 rrpinr5 p in5 rrpingr1 p ing1 rrpingr2 p ing2 rrpingr3 p ing3 rrpingr4 p ing4 rrpingr5 p ing5 rrpor1 p o1 rrpor2 p o2 rrpor3 p o3 rrpor4 p o4 rrpor5 p o5 rrpour1 p ou1 rrpour2 p ou2 rrpour3 p ou3 rrpour4 p ou4 rrpour5 p ou5 rrpur1 p u1 rrpur2 p u2 rrpur3 p u3 rrpur4 p u4 rrpur5 p u5 rrqir1 q i1 rrqir2 q i2 rrqir3 q i3 rrqir4 q i4 rrqir5 q i5 rrqiar1 q ia1 rrqiar2 q ia2 rrqiar3 q ia3 rrqiar4 q ia4 rrqiar5 q ia5 rrqianr1 q ian1 rrqianr2 q ian2 rrqianr3 q ian3 rrqianr4 q ian4 rrqianr5 q ian5 rrqiangr1 q iang1 rrqiangr2 q iang2 rrqiangr3 q iang3 rrqiangr4 q iang4 rrqiangr5 q iang5 rrqiaor1 q iao1 rrqiaor2 q iao2 rrqiaor3 q iao3 rrqiaor4 q iao4 rrqiaor5 q iao5 rrqier1 q ie1 rrqier2 q ie2 rrqier3 q ie3 rrqier4 q ie4 rrqier5 q ie5 rrqinr1 q in1 rrqinr2 q in2 rrqinr3 q in3 rrqinr4 q in4 rrqinr5 q in5 rrqingr1 q ing1 rrqingr2 q ing2 rrqingr3 q ing3 rrqingr4 q ing4 rrqingr5 q ing5 rrqiongr1 q iong1 rrqiongr2 q iong2 rrqiongr3 q iong3 rrqiongr4 q iong4 rrqiongr5 q iong5 rrqiur1 q iou1 rrqiur2 q iou2 rrqiur3 q iou3 rrqiur4 q iou4 rrqiur5 q iou5 rrqur1 q v1 rrqur2 q v2 rrqur3 q v3 rrqur4 q v4 rrqur5 q v5 rrquanr1 q van1 rrquanr2 q van2 rrquanr3 q van3 rrquanr4 q van4 rrquanr5 q van5 rrquer1 q ve1 rrquer2 q ve2 rrquer3 q ve3 rrquer4 q ve4 rrquer5 q ve5 rrqunr1 q vn1 rrqunr2 q vn2 rrqunr3 q vn3 rrqunr4 q vn4 rrqunr5 q vn5 rrranr1 r an1 rrranr2 r an2 rrranr3 r an3 rrranr4 r an4 rrranr5 r an5 rrrangr1 r ang1 rrrangr2 r ang2 rrrangr3 r ang3 rrrangr4 r ang4 rrrangr5 r ang5 rrraor1 r ao1 rrraor2 r ao2 rrraor3 r ao3 rrraor4 r ao4 rrraor5 r ao5 rrrer1 r e1 rrrer2 r e2 rrrer3 r e3 rrrer4 r e4 rrrer5 r e5 rrrenr1 r en1 rrrenr2 r en2 rrrenr3 r en3 rrrenr4 r en4 rrrenr5 r en5 rrrengr1 r eng1 rrrengr2 r eng2 rrrengr3 r eng3 rrrengr4 r eng4 rrrengr5 r eng5 rrrir1 r iii1 rrrir2 r iii2 rrrir3 r iii3 rrrir4 r iii4 rrrir5 r iii5 rrrongr1 r ong1 rrrongr2 r ong2 rrrongr3 r ong3 rrrongr4 r ong4 rrrongr5 r ong5 rrrour1 r ou1 rrrour2 r ou2 rrrour3 r ou3 rrrour4 r ou4 rrrour5 r ou5 rrrur1 r u1 rrrur2 r u2 rrrur3 r u3 rrrur4 r u4 rrrur5 r u5 rrruar1 r ua1 rrruar2 r ua2 rrruar3 r ua3 rrruar4 r ua4 rrruar5 r ua5 rrruanr1 r uan1 rrruanr2 r uan2 rrruanr3 r uan3 rrruanr4 r uan4 rrruanr5 r uan5 rrruir1 r uei1 rrruir2 r uei2 rrruir3 r uei3 rrruir4 r uei4 rrruir5 r uei5 rrrunr1 r uen1 rrrunr2 r uen2 rrrunr3 r uen3 rrrunr4 r uen4 rrrunr5 r uen5 rrruor1 r uo1 rrruor2 r uo2 rrruor3 r uo3 rrruor4 r uo4 rrruor5 r uo5 rrsar1 s a1 rrsar2 s a2 rrsar3 s a3 rrsar4 s a4 rrsar5 s a5 rrsair1 s ai1 rrsair2 s ai2 rrsair3 s ai3 rrsair4 s ai4 rrsair5 s ai5 rrsanr1 s an1 rrsanr2 s an2 rrsanr3 s an3 rrsanr4 s an4 rrsanr5 s an5 rrsangr1 s ang1 rrsangr2 s ang2 rrsangr3 s ang3 rrsangr4 s ang4 rrsangr5 s ang5 rrsaor1 s ao1 rrsaor2 s ao2 rrsaor3 s ao3 rrsaor4 s ao4 rrsaor5 s ao5 rrser1 s e1 rrser2 s e2 rrser3 s e3 rrser4 s e4 rrser5 s e5 rrsenr1 s en1 rrsenr2 s en2 rrsenr3 s en3 rrsenr4 s en4 rrsenr5 s en5 rrsengr1 s eng1 rrsengr2 s eng2 rrsengr3 s eng3 rrsengr4 s eng4 rrsengr5 s eng5 rrshar1 sh a1 rrshar2 sh a2 rrshar3 sh a3 rrshar4 sh a4 rrshar5 sh a5 rrshair1 sh ai1 rrshair2 sh ai2 rrshair3 sh ai3 rrshair4 sh ai4 rrshair5 sh ai5 rrshanr1 sh an1 rrshanr2 sh an2 rrshanr3 sh an3 rrshanr4 sh an4 rrshanr5 sh an5 rrshangr1 sh ang1 rrshangr2 sh ang2 rrshangr3 sh ang3 rrshangr4 sh ang4 rrshangr5 sh ang5 rrshaor1 sh ao1 rrshaor2 sh ao2 rrshaor3 sh ao3 rrshaor4 sh ao4 rrshaor5 sh ao5 rrsher1 sh e1 rrsher2 sh e2 rrsher3 sh e3 rrsher4 sh e4 rrsher5 sh e5 rrsheir1 sh ei1 rrsheir2 sh ei2 rrsheir3 sh ei3 rrsheir4 sh ei4 rrsheir5 sh ei5 rrshenr1 sh en1 rrshenr2 sh en2 rrshenr3 sh en3 rrshenr4 sh en4 rrshenr5 sh en5 rrshengr1 sh eng1 rrshengr2 sh eng2 rrshengr3 sh eng3 rrshengr4 sh eng4 rrshengr5 sh eng5 rrshir1 sh iii1 rrshir2 sh iii2 rrshir3 sh iii3 rrshir4 sh iii4 rrshir5 sh iii5 rrshour1 sh ou1 rrshour2 sh ou2 rrshour3 sh ou3 rrshour4 sh ou4 rrshour5 sh ou5 rrshur1 sh u1 rrshur2 sh u2 rrshur3 sh u3 rrshur4 sh u4 rrshur5 sh u5 rrshuar1 sh ua1 rrshuar2 sh ua2 rrshuar3 sh ua3 rrshuar4 sh ua4 rrshuar5 sh ua5 rrshuair1 sh uai1 rrshuair2 sh uai2 rrshuair3 sh uai3 rrshuair4 sh uai4 rrshuair5 sh uai5 rrshuanr1 sh uan1 rrshuanr2 sh uan2 rrshuanr3 sh uan3 rrshuanr4 sh uan4 rrshuanr5 sh uan5 rrshuangr1 sh uang1 rrshuangr2 sh uang2 rrshuangr3 sh uang3 rrshuangr4 sh uang4 rrshuangr5 sh uang5 rrshuir1 sh uei1 rrshuir2 sh uei2 rrshuir3 sh uei3 rrshuir4 sh uei4 rrshuir5 sh uei5 rrshunr1 sh uen1 rrshunr2 sh uen2 rrshunr3 sh uen3 rrshunr4 sh uen4 rrshunr5 sh uen5 rrshuor1 sh uo1 rrshuor2 sh uo2 rrshuor3 sh uo3 rrshuor4 sh uo4 rrshuor5 sh uo5 rrsir1 s ii1 rrsir2 s ii2 rrsir3 s ii3 rrsir4 s ii4 rrsir5 s ii5 rrsongr1 s ong1 rrsongr2 s ong2 rrsongr3 s ong3 rrsongr4 s ong4 rrsongr5 s ong5 rrsour1 s ou1 rrsour2 s ou2 rrsour3 s ou3 rrsour4 s ou4 rrsour5 s ou5 rrsur1 s u1 rrsur2 s u2 rrsur3 s u3 rrsur4 s u4 rrsur5 s u5 rrsuanr1 s uan1 rrsuanr2 s uan2 rrsuanr3 s uan3 rrsuanr4 s uan4 rrsuanr5 s uan5 rrsuir1 s uei1 rrsuir2 s uei2 rrsuir3 s uei3 rrsuir4 s uei4 rrsuir5 s uei5 rrsunr1 s uen1 rrsunr2 s uen2 rrsunr3 s uen3 rrsunr4 s uen4 rrsunr5 s uen5 rrsuor1 s uo1 rrsuor2 s uo2 rrsuor3 s uo3 rrsuor4 s uo4 rrsuor5 s uo5 rrtar1 t a1 rrtar2 t a2 rrtar3 t a3 rrtar4 t a4 rrtar5 t a5 rrtair1 t ai1 rrtair2 t ai2 rrtair3 t ai3 rrtair4 t ai4 rrtair5 t ai5 rrtanr1 t an1 rrtanr2 t an2 rrtanr3 t an3 rrtanr4 t an4 rrtanr5 t an5 rrtangr1 t ang1 rrtangr2 t ang2 rrtangr3 t ang3 rrtangr4 t ang4 rrtangr5 t ang5 rrtaor1 t ao1 rrtaor2 t ao2 rrtaor3 t ao3 rrtaor4 t ao4 rrtaor5 t ao5 rrter1 t e1 rrter2 t e2 rrter3 t e3 rrter4 t e4 rrter5 t e5 rrteir1 t ei1 rrteir2 t ei2 rrteir3 t ei3 rrteir4 t ei4 rrteir5 t ei5 rrtengr1 t eng1 rrtengr2 t eng2 rrtengr3 t eng3 rrtengr4 t eng4 rrtengr5 t eng5 rrtir1 t i1 rrtir2 t i2 rrtir3 t i3 rrtir4 t i4 rrtir5 t i5 rrtianr1 t ian1 rrtianr2 t ian2 rrtianr3 t ian3 rrtianr4 t ian4 rrtianr5 t ian5 rrtiaor1 t iao1 rrtiaor2 t iao2 rrtiaor3 t iao3 rrtiaor4 t iao4 rrtiaor5 t iao5 rrtier1 t ie1 rrtier2 t ie2 rrtier3 t ie3 rrtier4 t ie4 rrtier5 t ie5 rrtingr1 t ing1 rrtingr2 t ing2 rrtingr3 t ing3 rrtingr4 t ing4 rrtingr5 t ing5 rrtongr1 t ong1 rrtongr2 t ong2 rrtongr3 t ong3 rrtongr4 t ong4 rrtongr5 t ong5 rrtour1 t ou1 rrtour2 t ou2 rrtour3 t ou3 rrtour4 t ou4 rrtour5 t ou5 rrtur1 t u1 rrtur2 t u2 rrtur3 t u3 rrtur4 t u4 rrtur5 t u5 rrtuanr1 t uan1 rrtuanr2 t uan2 rrtuanr3 t uan3 rrtuanr4 t uan4 rrtuanr5 t uan5 rrtuir1 t uei1 rrtuir2 t uei2 rrtuir3 t uei3 rrtuir4 t uei4 rrtuir5 t uei5 rrtunr1 t uen1 rrtunr2 t uen2 rrtunr3 t uen3 rrtunr4 t uen4 rrtunr5 t uen5 rrtuor1 t uo1 rrtuor2 t uo2 rrtuor3 t uo3 rrtuor4 t uo4 rrtuor5 t uo5 rrwar1 w ua1 rrwar2 w ua2 rrwar3 w ua3 rrwar4 w ua4 rrwar5 w ua5 rrwair1 w uai1 rrwair2 w uai2 rrwair3 w uai3 rrwair4 w uai4 rrwair5 w uai5 rrwanr1 w uan1 rrwanr2 w uan2 rrwanr3 w uan3 rrwanr4 w uan4 rrwanr5 w uan5 rrwangr1 w uang1 rrwangr2 w uang2 rrwangr3 w uang3 rrwangr4 w uang4 rrwangr5 w uang5 rrweir1 w uei1 rrweir2 w uei2 rrweir3 w uei3 rrweir4 w uei4 rrweir5 w uei5 rrwenr1 w uen1 rrwenr2 w uen2 rrwenr3 w uen3 rrwenr4 w uen4 rrwenr5 w uen5 rrwengr1 w uen1 rrwengr2 w uen2 rrwengr3 w uen3 rrwengr4 w uen4 rrwengr5 w uen5 rrwor1 w uo1 rrwor2 w uo2 rrwor3 w uo3 rrwor4 w uo4 rrwor5 w uo5 rrwur1 w u1 rrwur2 w u2 rrwur3 w u3 rrwur4 w u4 rrwur5 w u5 rrxir1 x i1 rrxir2 x i2 rrxir3 x i3 rrxir4 x i4 rrxir5 x i5 rrxiar1 x ia1 rrxiar2 x ia2 rrxiar3 x ia3 rrxiar4 x ia4 rrxiar5 x ia5 rrxianr1 x ian1 rrxianr2 x ian2 rrxianr3 x ian3 rrxianr4 x ian4 rrxianr5 x ian5 rrxiangr1 x iang1 rrxiangr2 x iang2 rrxiangr3 x iang3 rrxiangr4 x iang4 rrxiangr5 x iang5 rrxiaor1 x iao1 rrxiaor2 x iao2 rrxiaor3 x iao3 rrxiaor4 x iao4 rrxiaor5 x iao5 rrxier1 x ie1 rrxier2 x ie2 rrxier3 x ie3 rrxier4 x ie4 rrxier5 x ie5 rrxinr1 x in1 rrxinr2 x in2 rrxinr3 x in3 rrxinr4 x in4 rrxinr5 x in5 rrxingr1 x ing1 rrxingr2 x ing2 rrxingr3 x ing3 rrxingr4 x ing4 rrxingr5 x ing5 rrxiongr1 x iong1 rrxiongr2 x iong2 rrxiongr3 x iong3 rrxiongr4 x iong4 rrxiongr5 x iong5 rrxiur1 x iou1 rrxiur2 x iou2 rrxiur3 x iou3 rrxiur4 x iou4 rrxiur5 x iou5 rrxur1 x v1 rrxur2 x v2 rrxur3 x v3 rrxur4 x v4 rrxur5 x v5 rrxuanr1 x van1 rrxuanr2 x van2 rrxuanr3 x van3 rrxuanr4 x van4 rrxuanr5 x van5 rrxuer1 x ve1 rrxuer2 x ve2 rrxuer3 x ve3 rrxuer4 x ve4 rrxuer5 x ve5 rrxunr1 x vn1 rrxunr2 x vn2 rrxunr3 x vn3 rrxunr4 x vn4 rrxunr5 x vn5 rryar1 y ia1 rryar2 y ia2 rryar3 y ia3 rryar4 y ia4 rryar5 y ia5 rryanr1 y ian1 rryanr2 y ian2 rryanr3 y ian3 rryanr4 y ian4 rryanr5 y ian5 rryangr1 y iang1 rryangr2 y iang2 rryangr3 y iang3 rryangr4 y iang4 rryangr5 y iang5 rryaor1 y iao1 rryaor2 y iao2 rryaor3 y iao3 rryaor4 y iao4 rryaor5 y iao5 rryer1 y ie1 rryer2 y ie2 rryer3 y ie3 rryer4 y ie4 rryer5 y ie5 rryir1 y i1 rryir2 y i2 rryir3 y i3 rryir4 y i4 rryir5 y i5 rryinr1 y in1 rryinr2 y in2 rryinr3 y in3 rryinr4 y in4 rryinr5 y in5 rryingr1 y ing1 rryingr2 y ing2 rryingr3 y ing3 rryingr4 y ing4 rryingr5 y ing5 rryor1 y iou1 rryor2 y iou2 rryor3 y iou3 rryor4 y iou4 rryor5 y iou5 rryongr1 y iong1 rryongr2 y iong2 rryongr3 y iong3 rryongr4 y iong4 rryongr5 y iong5 rryour1 y iou1 rryour2 y iou2 rryour3 y iou3 rryour4 y iou4 rryour5 y iou5 rryur1 y v1 rryur2 y v2 rryur3 y v3 rryur4 y v4 rryur5 y v5 rryuanr1 y van1 rryuanr2 y van2 rryuanr3 y van3 rryuanr4 y van4 rryuanr5 y van5 rryuer1 y ve1 rryuer2 y ve2 rryuer3 y ve3 rryuer4 y ve4 rryuer5 y ve5 rryunr1 y vn1 rryunr2 y vn2 rryunr3 y vn3 rryunr4 y vn4 rryunr5 y vn5 rrzar1 z a1 rrzar2 z a2 rrzar3 z a3 rrzar4 z a4 rrzar5 z a5 rrzair1 z ai1 rrzair2 z ai2 rrzair3 z ai3 rrzair4 z ai4 rrzair5 z ai5 rrzanr1 z an1 rrzanr2 z an2 rrzanr3 z an3 rrzanr4 z an4 rrzanr5 z an5 rrzangr1 z ang1 rrzangr2 z ang2 rrzangr3 z ang3 rrzangr4 z ang4 rrzangr5 z ang5 rrzaor1 z ao1 rrzaor2 z ao2 rrzaor3 z ao3 rrzaor4 z ao4 rrzaor5 z ao5 rrzer1 z e1 rrzer2 z e2 rrzer3 z e3 rrzer4 z e4 rrzer5 z e5 rrzeir1 z ei1 rrzeir2 z ei2 rrzeir3 z ei3 rrzeir4 z ei4 rrzeir5 z ei5 rrzenr1 z en1 rrzenr2 z en2 rrzenr3 z en3 rrzenr4 z en4 rrzenr5 z en5 rrzengr1 z eng1 rrzengr2 z eng2 rrzengr3 z eng3 rrzengr4 z eng4 rrzengr5 z eng5 rrzhar1 zh a1 rrzhar2 zh a2 rrzhar3 zh a3 rrzhar4 zh a4 rrzhar5 zh a5 rrzhair1 zh ai1 rrzhair2 zh ai2 rrzhair3 zh ai3 rrzhair4 zh ai4 rrzhair5 zh ai5 rrzhanr1 zh an1 rrzhanr2 zh an2 rrzhanr3 zh an3 rrzhanr4 zh an4 rrzhanr5 zh an5 rrzhangr1 zh ang1 rrzhangr2 zh ang2 rrzhangr3 zh ang3 rrzhangr4 zh ang4 rrzhangr5 zh ang5 rrzhaor1 zh ao1 rrzhaor2 zh ao2 rrzhaor3 zh ao3 rrzhaor4 zh ao4 rrzhaor5 zh ao5 rrzher1 zh e1 rrzher2 zh e2 rrzher3 zh e3 rrzher4 zh e4 rrzher5 zh e5 rrzheir1 zh ei1 rrzheir2 zh ei2 rrzheir3 zh ei3 rrzheir4 zh ei4 rrzheir5 zh ei5 rrzhenr1 zh en1 rrzhenr2 zh en2 rrzhenr3 zh en3 rrzhenr4 zh en4 rrzhenr5 zh en5 rrzhengr1 zh eng1 rrzhengr2 zh eng2 rrzhengr3 zh eng3 rrzhengr4 zh eng4 rrzhengr5 zh eng5 rrzhir1 zh iii1 rrzhir2 zh iii2 rrzhir3 zh iii3 rrzhir4 zh iii4 rrzhir5 zh iii5 rrzhongr1 zh ong1 rrzhongr2 zh ong2 rrzhongr3 zh ong3 rrzhongr4 zh ong4 rrzhongr5 zh ong5 rrzhour1 zh ou1 rrzhour2 zh ou2 rrzhour3 zh ou3 rrzhour4 zh ou4 rrzhour5 zh ou5 rrzhur1 zh u1 rrzhur2 zh u2 rrzhur3 zh u3 rrzhur4 zh u4 rrzhur5 zh u5 rrzhuar1 zh ua1 rrzhuar2 zh ua2 rrzhuar3 zh ua3 rrzhuar4 zh ua4 rrzhuar5 zh ua5 rrzhuair1 zh uai1 rrzhuair2 zh uai2 rrzhuair3 zh uai3 rrzhuair4 zh uai4 rrzhuair5 zh uai5 rrzhuanr1 zh uan1 rrzhuanr2 zh uan2 rrzhuanr3 zh uan3 rrzhuanr4 zh uan4 rrzhuanr5 zh uan5 rrzhuangr1 zh uang1 rrzhuangr2 zh uang2 rrzhuangr3 zh uang3 rrzhuangr4 zh uang4 rrzhuangr5 zh uang5 rrzhuir1 zh uei1 rrzhuir2 zh uei2 rrzhuir3 zh uei3 rrzhuir4 zh uei4 rrzhuir5 zh uei5 rrzhunr1 zh uen1 rrzhunr2 zh uen2 rrzhunr3 zh uen3 rrzhunr4 zh uen4 rrzhunr5 zh uen5 rrzhuor1 zh uo1 rrzhuor2 zh uo2 rrzhuor3 zh uo3 rrzhuor4 zh uo4 rrzhuor5 zh uo5 rrzir1 z ii1 rrzir2 z ii2 rrzir3 z ii3 rrzir4 z ii4 rrzir5 z ii5 rrzongr1 z ong1 rrzongr2 z ong2 rrzongr3 z ong3 rrzongr4 z ong4 rrzongr5 z ong5 rrzour1 z ou1 rrzour2 z ou2 rrzour3 z ou3 rrzour4 z ou4 rrzour5 z ou5 rrzur1 z u1 rrzur2 z u2 rrzur3 z u3 rrzur4 z u4 rrzur5 z u5 rrzuanr1 z uan1 rrzuanr2 z uan2 rrzuanr3 z uan3 rrzuanr4 z uan4 rrzuanr5 z uan5 rrzuir1 z uei1 rrzuir2 z uei2 rrzuir3 z uei3 rrzuir4 z uei4 rrzuir5 z uei5 rrzunr1 z uen1 rrzunr2 z uen2 rrzunr3 z uen3 rrzunr4 z uen4 rrzunr5 z uen5 rrzuor1 z uo1 rrzuor2 z uo2 rrzuor3 z uo3 rrzuor4 z uo4 rrzuor5 z uo5 rr123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419pinyin_dict = { "a": ("^", "a"), "ai": ("^", "ai"), "an": ("^", "an"), "ang": ("^", "ang"), "ao": ("^", "ao"), "ba": ("b", "a"), "bai": ("b", "ai"), "ban": ("b", "an"), "bang": ("b", "ang"), "bao": ("b", "ao"), "be": ("b", "e"), "bei": ("b", "ei"), "ben": ("b", "en"), "beng": ("b", "eng"), "bi": ("b", "i"), "bian": ("b", "ian"), "biao": ("b", "iao"), "bie": ("b", "ie"), "bin": ("b", "in"), "bing": ("b", "ing"), "bo": ("b", "o"), "bu": ("b", "u"), "ca": ("c", "a"), "cai": ("c", "ai"), "can": ("c", "an"), "cang": ("c", "ang"), "cao": ("c", "ao"), "ce": ("c", "e"), "cen": ("c", "en"), "ceng": ("c", "eng"), "cha": ("ch", "a"), "chai": ("ch", "ai"), "chan": ("ch", "an"), "chang": ("ch", "ang"), "chao": ("ch", "ao"), "che": ("ch", "e"), "chen": ("ch", "en"), "cheng": ("ch", "eng"), "chi": ("ch", "iii"), "chong": ("ch", "ong"), "chou": ("ch", "ou"), "chu": ("ch", "u"), "chua": ("ch", "ua"), "chuai": ("ch", "uai"), "chuan": ("ch", "uan"), "chuang": ("ch", "uang"), "chui": ("ch", "uei"), "chun": ("ch", "uen"), "chuo": ("ch", "uo"), "ci": ("c", "ii"), "cong": ("c", "ong"), "cou": ("c", "ou"), "cu": ("c", "u"), "cuan": ("c", "uan"), "cui": ("c", "uei"), "cun": ("c", "uen"), "cuo": ("c", "uo"), "da": ("d", "a"), "dai": ("d", "ai"), "dan": ("d", "an"), "dang": ("d", "ang"), "dao": ("d", "ao"), "de": ("d", "e"), "dei": ("d", "ei"), "den": ("d", "en"), "deng": ("d", "eng"), "di": ("d", "i"), "dia": ("d", "ia"), "dian": ("d", "ian"), "diao": ("d", "iao"), "die": ("d", "ie"), "ding": ("d", "ing"), "diu": ("d", "iou"), "dong": ("d", "ong"), "dou": ("d", "ou"), "du": ("d", "u"), "duan": ("d", "uan"), "dui": ("d", "uei"), "dun": ("d", "uen"), "duo": ("d", "uo"), "e": ("^", "e"), "ei": ("^", "ei"), "en": ("^", "en"), "ng": ("^", "en"), "eng": ("^", "eng"), "er": ("^", "er"), "fa": ("f", "a"), "fan": ("f", "an"), "fang": ("f", "ang"), "fei": ("f", "ei"), "fen": ("f", "en"), "feng": ("f", "eng"), "fo": ("f", "o"), "fou": ("f", "ou"), "fu": ("f", "u"), "ga": ("g", "a"), "gai": ("g", "ai"), "gan": ("g", "an"), "gang": ("g", "ang"), "gao": ("g", "ao"), "ge": ("g", "e"), "gei": ("g", "ei"), "gen": ("g", "en"), "geng": ("g", "eng"), "gong": ("g", "ong"), "gou": ("g", "ou"), "gu": ("g", "u"), "gua": ("g", "ua"), "guai": ("g", "uai"), "guan": ("g", "uan"), "guang": ("g", "uang"), "gui": ("g", "uei"), "gun": ("g", "uen"), "guo": ("g", "uo"), "ha": ("h", "a"), "hai": ("h", "ai"), "han": ("h", "an"), "hang": ("h", "ang"), "hao": ("h", "ao"), "he": ("h", "e"), "hei": ("h", "ei"), "hen": ("h", "en"), "heng": ("h", "eng"), "hong": ("h", "ong"), "hou": ("h", "ou"), "hu": ("h", "u"), "hua": ("h", "ua"), "huai": ("h", "uai"), "huan": ("h", "uan"), "huang": ("h", "uang"), "hui": ("h", "uei"), "hun": ("h", "uen"), "huo": ("h", "uo"), "ji": ("j", "i"), "jia": ("j", "ia"), "jian": ("j", "ian"), "jiang": ("j", "iang"), "jiao": ("j", "iao"), "jie": ("j", "ie"), "jin": ("j", "in"), "jing": ("j", "ing"), "jiong": ("j", "iong"), "jiu": ("j", "iou"), "ju": ("j", "v"), "juan": ("j", "van"), "jue": ("j", "ve"), "jun": ("j", "vn"), "ka": ("k", "a"), "kai": ("k", "ai"), "kan": ("k", "an"), "kang": ("k", "ang"), "kao": ("k", "ao"), "ke": ("k", "e"), "kei": ("k", "ei"), "ken": ("k", "en"), "keng": ("k", "eng"), "kong": ("k", "ong"), "kou": ("k", "ou"), "ku": ("k", "u"), "kua": ("k", "ua"), "kuai": ("k", "uai"), "kuan": ("k", "uan"), "kuang": ("k", "uang"), "kui": ("k", "uei"), "kun": ("k", "uen"), "kuo": ("k", "uo"), "la": ("l", "a"), "lai": ("l", "ai"), "lan": ("l", "an"), "lang": ("l", "ang"), "lao": ("l", "ao"), "le": ("l", "e"), "lei": ("l", "ei"), "leng": ("l", "eng"), "li": ("l", "i"), "lia": ("l", "ia"), "lian": ("l", "ian"), "liang": ("l", "iang"), "liao": ("l", "iao"), "lie": ("l", "ie"), "lin": ("l", "in"), "ling": ("l", "ing"), "liu": ("l", "iou"), "lo": ("l", "o"), "long": ("l", "ong"), "lou": ("l", "ou"), "lu": ("l", "u"), "lv": ("l", "v"), "luan": ("l", "uan"), "lve": ("l", "ve"), "lue": ("l", "ve"), "lun": ("l", "uen"), "luo": ("l", "uo"), "ma": ("m", "a"), "mai": ("m", "ai"), "man": ("m", "an"), "mang": ("m", "ang"), "mao": ("m", "ao"), "me": ("m", "e"), "mei": ("m", "ei"), "men": ("m", "en"), "meng": ("m", "eng"), "mi": ("m", "i"), "mian": ("m", "ian"), "miao": ("m", "iao"), "mie": ("m", "ie"), "min": ("m", "in"), "ming": ("m", "ing"), "miu": ("m", "iou"), "mo": ("m", "o"), "mou": ("m", "ou"), "mu": ("m", "u"), "na": ("n", "a"), "nai": ("n", "ai"), "nan": ("n", "an"), "nang": ("n", "ang"), "nao": ("n", "ao"), "ne": ("n", "e"), "nei": ("n", "ei"), "nen": ("n", "en"), "neng": ("n", "eng"), "ni": ("n", "i"), "nia": ("n", "ia"), "nian": ("n", "ian"), "niang": ("n", "iang"), "niao": ("n", "iao"), "nie": ("n", "ie"), "nin": ("n", "in"), "ning": ("n", "ing"), "niu": ("n", "iou"), "nong": ("n", "ong"), "nou": ("n", "ou"), "nu": ("n", "u"), "nv": ("n", "v"), "nuan": ("n", "uan"), "nve": ("n", "ve"), "nue": ("n", "ve"), "nuo": ("n", "uo"), "o": ("^", "o"), "ou": ("^", "ou"), "pa": ("p", "a"), "pai": ("p", "ai"), "pan": ("p", "an"), "pang": ("p", "ang"), "pao": ("p", "ao"), "pe": ("p", "e"), "pei": ("p", "ei"), "pen": ("p", "en"), "peng": ("p", "eng"), "pi": ("p", "i"), "pian": ("p", "ian"), "piao": ("p", "iao"), "pie": ("p", "ie"), "pin": ("p", "in"), "ping": ("p", "ing"), "po": ("p", "o"), "pou": ("p", "ou"), "pu": ("p", "u"), "qi": ("q", "i"), "qia": ("q", "ia"), "qian": ("q", "ian"), "qiang": ("q", "iang"), "qiao": ("q", "iao"), "qie": ("q", "ie"), "qin": ("q", "in"), "qing": ("q", "ing"), "qiong": ("q", "iong"), "qiu": ("q", "iou"), "qu": ("q", "v"), "quan": ("q", "van"), "que": ("q", "ve"), "qun": ("q", "vn"), "ran": ("r", "an"), "rang": ("r", "ang"), "rao": ("r", "ao"), "re": ("r", "e"), "ren": ("r", "en"), "reng": ("r", "eng"), "ri": ("r", "iii"), "rong": ("r", "ong"), "rou": ("r", "ou"), "ru": ("r", "u"), "rua": ("r", "ua"), "ruan": ("r", "uan"), "rui": ("r", "uei"), "run": ("r", "uen"), "ruo": ("r", "uo"), "sa": ("s", "a"), "sai": ("s", "ai"), "san": ("s", "an"), "sang": ("s", "ang"), "sao": ("s", "ao"), "se": ("s", "e"), "sen": ("s", "en"), "seng": ("s", "eng"), "sha": ("sh", "a"), "shai": ("sh", "ai"), "shan": ("sh", "an"), "shang": ("sh", "ang"), "shao": ("sh", "ao"), "she": ("sh", "e"), "shei": ("sh", "ei"), "shen": ("sh", "en"), "sheng": ("sh", "eng"), "shi": ("sh", "iii"), "shou": ("sh", "ou"), "shu": ("sh", "u"), "shua": ("sh", "ua"), "shuai": ("sh", "uai"), "shuan": ("sh", "uan"), "shuang": ("sh", "uang"), "shui": ("sh", "uei"), "shun": ("sh", "uen"), "shuo": ("sh", "uo"), "si": ("s", "ii"), "song": ("s", "ong"), "sou": ("s", "ou"), "su": ("s", "u"), "suan": ("s", "uan"), "sui": ("s", "uei"), "sun": ("s", "uen"), "suo": ("s", "uo"), "ta": ("t", "a"), "tai": ("t", "ai"), "tan": ("t", "an"), "tang": ("t", "ang"), "tao": ("t", "ao"), "te": ("t", "e"), "tei": ("t", "ei"), "teng": ("t", "eng"), "ti": ("t", "i"), "tian": ("t", "ian"), "tiao": ("t", "iao"), "tie": ("t", "ie"), "ting": ("t", "ing"), "tong": ("t", "ong"), "tou": ("t", "ou"), "tu": ("t", "u"), "tuan": ("t", "uan"), "tui": ("t", "uei"), "tun": ("t", "uen"), "tuo": ("t", "uo"), "wa": ("^", "ua"), "wai": ("^", "uai"), "wan": ("^", "uan"), "wang": ("^", "uang"), "wei": ("^", "uei"), "wen": ("^", "uen"), "weng": ("^", "ueng"), "wo": ("^", "uo"), "wu": ("^", "u"), "xi": ("x", "i"), "xia": ("x", "ia"), "xian": ("x", "ian"), "xiang": ("x", "iang"), "xiao": ("x", "iao"), "xie": ("x", "ie"), "xin": ("x", "in"), "xing": ("x", "ing"), "xiong": ("x", "iong"), "xiu": ("x", "iou"), "xu": ("x", "v"), "xuan": ("x", "van"), "xue": ("x", "ve"), "xun": ("x", "vn"), "ya": ("^", "ia"), "yan": ("^", "ian"), "yang": ("^", "iang"), "yao": ("^", "iao"), "ye": ("^", "ie"), "yi": ("^", "i"), "yin": ("^", "in"), "ying": ("^", "ing"), "yo": ("^", "iou"), "yong": ("^", "iong"), "you": ("^", "iou"), "yu": ("^", "v"), "yuan": ("^", "van"), "yue": ("^", "ve"), "yun": ("^", "vn"), "za": ("z", "a"), "zai": ("z", "ai"), "zan": ("z", "an"), "zang": ("z", "ang"), "zao": ("z", "ao"), "ze": ("z", "e"), "zei": ("z", "ei"), "zen": ("z", "en"), "zeng": ("z", "eng"), "zha": ("zh", "a"), "zhai": ("zh", "ai"), "zhan": ("zh", "an"), "zhang": ("zh", "ang"), "zhao": ("zh", "ao"), "zhe": ("zh", "e"), "zhei": ("zh", "ei"), "zhen": ("zh", "en"), "zheng": ("zh", "eng"), "zhi": ("zh", "iii"), "zhong": ("zh", "ong"), "zhou": ("zh", "ou"), "zhu": ("zh", "u"), "zhua": ("zh", "ua"), "zhuai": ("zh", "uai"), "zhuan": ("zh", "uan"), "zhuang": ("zh", "uang"), "zhui": ("zh", "uei"), "zhun": ("zh", "uen"), "zhuo": ("zh", "uo"), "zi": ("z", "ii"), "zong": ("z", "ong"), "zou": ("z", "ou"), "zu": ("z", "u"), "zuan": ("z", "uan"), "zui": ("z", "uei"), "zun": ("z", "uen"), "zuo": ("z", "uo"),}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219# cp from https://github.com/lifeiteng/vall-e/blob/main/valle/data/tokenizer.py# Copyright 2023 (authors: Feiteng Li)## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
import re, loggingfrom dataclasses import asdict, dataclassfrom typing import Any, Dict, List, Optional, Pattern, Unionimport mathimport numpy as npimport torchimport torchaudio# from lhotse.features import FeatureExtractor# from lhotse.utils import Seconds, compute_num_framesfrom phonemizer.backend import EspeakBackendfrom phonemizer.backend.espeak.language_switch import LanguageSwitchfrom phonemizer.backend.espeak.words_mismatch import WordMismatchfrom phonemizer.punctuation import Punctuationfrom phonemizer.separator import Separator
class TextTokenizer: """Phonemize Text."""
def __init__( self, language="en-us", backend="espeak", separator=Separator(word="_", syllable="-", phone="|"), preserve_punctuation=True, punctuation_marks: Union[str, Pattern] = Punctuation.default_marks(), with_stress: bool = False, tie: Union[bool, str] = False, language_switch: LanguageSwitch = "keep-flags", words_mismatch: WordMismatch = "ignore", ) -> None: phonemizer = EspeakBackend( language, punctuation_marks=punctuation_marks, preserve_punctuation=preserve_punctuation, with_stress=with_stress, tie=tie, language_switch=language_switch, words_mismatch=words_mismatch, ) self.backend = phonemizer self.separator = separator
def to_list(self, phonemized: str) -> List[str]: fields = [] for word in phonemized.split(self.separator.word): # "ɐ m|iː|n?" ɹ|ɪ|z|ɜː|v; h|ɪ|z. pp = re.findall(r"\w+|[^\w\s]", word, re.UNICODE) fields.extend( [p for p in pp if p != self.separator.phone] + [self.separator.word] ) assert len("".join(fields[:-1])) == len(phonemized) - phonemized.count( self.separator.phone ) return fields[:-1]
def __call__(self, text, strip=True) -> List[List[str]]: if isinstance(text, str): text = [text] phones = [] for txt in text: if txt == '': continue if txt[0] == '#': phones.append(txt) else: ipa = text_tokenizer.backend.phonemize([txt], separator=text_tokenizer.separator, strip=True, njobs=1, logger=logging.basicConfig(level=logging.ERROR)) phones += text_tokenizer.to_list(ipa[0]) return phones
def tokenize_text(tokenizer: TextTokenizer, text: str) -> List[str]: phonemes = tokenizer([text.strip()]) return phonemes[0] # k2symbols
_PAUSE_SYMBOL = {'、':',', ',':',', '。':',', '!':'!', '?':'?', ':':':'}def _replace(match): word = match.group(0) return _PAUSE_SYMBOL[word]
def txt2phone(tokenizer: TextTokenizer, text: str): text = re.sub('|'.join(_PAUSE_SYMBOL.keys()), _replace, text) text = re.split(r"(#\d)", text) phones = [] for txt in text: if txt == '': continue if txt[0] == '#': phones.append(txt) else: ipa = tokenizer.backend.phonemize([txt], separator=tokenizer.separator, strip=True, njobs=1) phones += tokenizer.to_list(ipa[0]) phones = "|".join(phones).replace("(|", "(").replace("|)", ")") # phones = ["(cmn)"] + phones.split("|") return phones
def convert_audio(wav: torch.Tensor, sr: int, target_sr: int, target_channels: int): assert wav.shape[0] in [1, 2], "Audio must be mono or stereo." if target_channels == 1: wav = wav.mean(0, keepdim=True) elif target_channels == 2: *shape, _, length = wav.shape wav = wav.expand(*shape, target_channels, length) elif wav.shape[0] == 1: wav = wav.expand(target_channels, -1) wav = torchaudio.transforms.Resample(sr, target_sr)(wav) return wav
class AudioTokenizer: """EnCodec audio."""
def __init__( self, device: Any = None, signature = None ) -> None: from audiocraft.solvers import CompressionSolver model = CompressionSolver.model_from_checkpoint(signature) self.sample_rate = model.sample_rate self.channels = model.channels if not device: device = torch.device("cpu") if torch.cuda.is_available(): device = torch.device("cuda:0")
self._device = device
self.codec = model.to(device)
@property def device(self): return self._device
def encode(self, wav: torch.Tensor) -> torch.Tensor: codes = self.codec.encode(wav.to(self.device)) return [(codes[0], None)]
def decode(self, frames: torch.Tensor) -> torch.Tensor: frames = frames[0][0] # [1,4,T] return self.codec.decode(frames)
def tokenize_audio(tokenizer: AudioTokenizer, audio, offset = -1, num_frames=-1): # Load and pre-process the audio waveform if type(audio) == str: if offset != -1 and num_frames!=-1: wav, sr = torchaudio.load(audio, frame_offset=offset, num_frames=num_frames) else: wav, sr = torchaudio.load(audio) wav = convert_audio(wav, sr, tokenizer.sample_rate, tokenizer.channels) wav = wav.unsqueeze(0) else: wav = audio.unsqueeze(0).unsqueeze(0) # Extract discrete codes from EnCodec with torch.no_grad(): encoded_frames = tokenizer.encode(wav) return encoded_frames
class AudioSR: """EnCodec audio."""
def __init__( self, model_path, device = "cpu", ) -> None: import dac self.codec = dac.DAC.load(model_path) self.codec.to(device) self.codec.eval()
self.sample_rate = self.codec.sample_rate self.channels = 1 self._device = device
@property def device(self): return self._device
def encode(self, wav: torch.Tensor) -> torch.Tensor: length = wav.shape[-1] right_pad = math.ceil(length / self.codec.hop_length) * self.codec.hop_length - length wav = torch.nn.functional.pad(wav, (0, right_pad)) z, codes, _, _, _ = self.codec.encode(wav.to(self._device)) return [(codes, z)]
def decode(self, frames: torch.Tensor) -> torch.Tensor: # frames = frames[0][0] # [1,4,T] # with torch.no_grad(): # z = self.codec.quantizer.from_codes(frames)[0] # y = self.codec.decode(z) z = frames[0][1] # [1, 2048, T] with torch.no_grad(): y = self.codec.decode(z) return y123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225import multiprocessingfrom concurrent.futures import ProcessPoolExecutorimport argparseimport os, sys, refrom random import shufflefrom tqdm import tqdmfrom pypinyin import Stylefrom pypinyin.contrib.neutral_tone import NeutralToneWith5Mixinfrom pypinyin.converter import DefaultConverterfrom pypinyin.core import Pinyinimport jiebajieba.set_dictionary(dictionary_path=os.path.join(os.path.dirname(__file__)+'/jieba_dict.txt'))
from .symbols import pinyin_dictfrom .cn_tn import NSWNormalizer
zh_pattern = re.compile("[\u4e00-\u9fa5]")alpha_pattern = re.compile(r"[a-zA-Z]")
def is_zh(word): global zh_pattern match = zh_pattern.search(word) return match is not None
def is_alpha(word): global alpha_pattern match = alpha_pattern.search(word) return match is not None
def get_phoneme_from_char_and_pinyin(chn_char, pinyin): # we do not need #4, use sil to replace it chn_char = chn_char.replace("#4", "") char_len = len(chn_char) i, j = 0, 0 result = [] # print(pinyin) while i < char_len: cur_char = chn_char[i] if is_zh(cur_char): if pinyin[j][:-1] == 'n': # 处理特殊“嗯” 特殊拼音 pinyin[j] = 'en' + pinyin[j][-1] if i < len(chn_char)-2 and is_zh(chn_char[i:i+3]) and pinyin[j][-1] == pinyin[j+1][-1] == pinyin[j+2][-1] == '3': # 处理连续三个三声变调 pinyin[j+1] = pinyin[j+1][:-1] + '2' # print(chn_char[i:i+3], pinyin[j:j+3]) if i < len(chn_char)-1 and pinyin[j][:-1] in pinyin_dict and is_zh(chn_char[i]) and is_zh(chn_char[i+1]) and pinyin[j][-1] == pinyin[j+1][-1] == '3': # 处理连续两个三声变调 pinyin[j] = pinyin[j][:-1] + '2' # print('change tone ', chn_char[i:i+2], pinyin[j:j + 2]) if pinyin[j][:-1] not in pinyin_dict: # 处理儿化音 assert chn_char[i + 1] == "儿", f"current_char : {cur_char}, next_char: {chn_char[i+1]}, cur_pinyin: {pinyin[j]}" assert pinyin[j][-2] == "r" tone = pinyin[j][-1] a = pinyin[j][:-2] # a1, a2 = pinyin_dict[a] # result += [a1, a2 + tone, "er5"] result += [a + tone, er5] if i + 2 < char_len and chn_char[i + 2] != "#": result.append("#0") i += 2 j += 1 else: tone = pinyin[j][-1] a = pinyin[j][:-1] a1, a2 = pinyin_dict[a] # a="wen" a1="^", a2="en" # result += [a1, a2 + tone] # result = [zh, ong1, ^,en2] result.append(a+tone) # if i + 1 < char_len and chn_char[i + 1] != "#": # 每个字后面接一个#0 # result.append("#0")
i += 1 j += 1
# TODO support English alpha # elif is_alpha(cur_char): # result += ALPHA_PHONE_DICT[cur_char.upper()] # if i + 1 < char_len and chn_char[i + 1] not in "#、,。!?:" : # 每个字后面接一个#0 # result.append("#0") # i += 1 # j += 1 # baker alpha dataset "ABC" in pinyin elif cur_char == "#": result.append(chn_char[i : i + 2]) i += 2 elif cur_char in _PAUSE_SYMBOL: # 遇到标点符号,添加停顿 result.pop() # 去掉#0 result.append("#3") i += 1 else: # ignore the unknown char # result.append(chn_char[i]) i += 1 if result[-1] == "#0": # 去掉最后的#0,改为sil result = result[:-1] # if result[-1] != "sil": # result.append("sil") assert j == len(pinyin) return result
# _PAUSE_SYMBOL = {'、', ',', '。', ',', '!', '!', '?', ':', ':', '《', '》', '·', '(', ')', '(', ')'}_PAUSE_SYMBOL = {'.':'.', '、':',', ',':',', '。':'.', ',':',', '!':'!', '!':'!', '?':'?', '?':'?', ':':',', ':':',', '——':','}
class MyConverter(NeutralToneWith5Mixin, DefaultConverter): pass
def checkErHuaYin(text, GT_pinyin): new_pinyin = [] check_pattern = re.compile("[\\t\.\!\?\/_,$%^*(+\"\']+|[+——!,。?、~@#¥%……&*()“”:;]+") check_text = check_pattern.sub('', text) if len(check_text) > len(GT_pinyin) and '儿' in check_text: # print('Size mismatch: ', check_text, len(check_text), '\n', GT_pinyin, len(GT_pinyin)) for i in range(len(GT_pinyin)): if GT_pinyin[i][-2] == 'r' and GT_pinyin[i][:2] != 'er' and check_text[i + 1] == '儿': new_pinyin.append(GT_pinyin[i][:-2] + GT_pinyin[i][-1]) new_pinyin.append('er5') replace_word = check_text[i:i + 2] replace_pattern = re.compile(replace_word) # text = replace_pattern.sub(replace_word[:-1], text) check_text = replace_pattern.sub(replace_word[:-1], check_text, count=1) else: new_pinyin.append(GT_pinyin[i]) GT_pinyin = new_pinyin return GT_pinyin
def change_tone_in_bu_or_yi(chars, pinyin_list): location_yi = [m.start() for m in re.finditer(r'一', chars)] location_bu = [m.start() for m in re.finditer(r'不', chars)] # print('data: ', chars, pinyin_list, location_yi, location_bu) for l in location_yi: if l > 0 and l<len(chars) and chars[l-1]==chars[l+1]: pinyin_list[l] = 'yi5' elif l<len(chars) and pinyin_list[l+1][-1] == '4': pinyin_list[l] = 'yi2' for l in location_bu: if l<len(chars) and pinyin_list[l+1][-1] == '4': pinyin_list[l] = 'bu2' return pinyin_list
def txt2pinyin(text, pinyin_parser): phonemes = [] text = NSWNormalizer(text.strip()).normalize().upper() texts = text.split(' ') for text in texts: text_list = list(jieba.cut(text)) for words in text_list: # print('words: ', words) if words in _PAUSE_SYMBOL: # phonemes.append('#2') phonemes[-1] += _PAUSE_SYMBOL[words] elif re.search("[\u4e00-\u9fa5]+", words): pinyin = pinyin_parser(words, style=Style.TONE3, errors="ignore") new_pinyin = [] for x in pinyin: x = "".join(x) if "#" not in x: new_pinyin.append(x) new_pinyin = change_tone_in_bu_or_yi(words, new_pinyin) if len(words)>1 and words[-1] not in {"一","不"} else new_pinyin phoneme = get_phoneme_from_char_and_pinyin(words, new_pinyin) # phoneme seq: [sil c e4 #0 sh iii4 #0 ^ uen2 #0 b en3 sil] string 的list phonemes += phoneme elif re.search(r"[a-zA-Z]", words): phonemes.append(words.upper()) # phonemes.append("#1") phones = " ".join(phonemes) return phones
def process_batch(text_list, save_dir): my_pinyin = Pinyin(MyConverter()) pinyin_parser = my_pinyin.pinyin
for text_info in tqdm(text_list): try: name, text = text_info save_path = os.path.join(save_dir, name+".txt") phones = txt2pinyin(text, pinyin_parser) open(save_path, 'w', encoding='utf-8').write(phones) except Exception as e: print(text_info, e)
def parallel_process(filenames, num_processes, save_dir): with ProcessPoolExecutor(max_workers=num_processes) as executor: tasks = [] for i in range(num_processes): start = int(i * len(filenames) / num_processes) end = int((i + 1) * len(filenames) / num_processes) chunk = filenames[start:end] tasks.append(executor.submit(process_batch, chunk, save_dir))
for task in tqdm(tasks): task.result()
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--text_file", type=str, default="", help="path to input text file") parser.add_argument( "--save_dir", type=str, default="", help="path to output text file") parser.add_argument( '--workers', type=int, default=4, help='You are advised to set the number of processes to the same as the number of CPU cores') args = parser.parse_args()
sampling_rate = 16000
os.makedirs(args.save_dir, exist_ok=True)
filenames = open(args.text_file, 'r', encoding='utf-8').readlines() filenames = [x.strip().split('\t') for x in tqdm(filenames)] filenames = [[x[0], x[-1]] for x in tqdm(filenames)] # shuffle(filenames) print(len(filenames)) multiprocessing.set_start_method("spawn", force=True)
if args.workers == 0: args.workers = os.cpu_count() parallel_process(filenames, args.workers, args.save_dir)
#################################################################################
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651# A unified script for inference process# Make adjustments inside functions, and consider both gradio and cli scripts if need to change func output formatimport osimport sysfrom pathlib import Pathfrom concurrent.futures import ThreadPoolExecutor
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1" # for MPS device compatibilitysys.path.append(f"{os.path.dirname(os.path.abspath(__file__))}/../../third_party/BigVGAN/")
import hashlibimport reimport tempfilefrom importlib.resources import files
import matplotlib
matplotlib.use("Agg")
import matplotlib.pylab as pltimport numpy as npimport torchimport torchaudioimport tqdmfrom huggingface_hub import hf_hub_downloadfrom pydub import AudioSegment, silencefrom transformers import pipelinefrom vocos import Vocos
from lemas_tts.model.cfm import CFMfrom lemas_tts.model.utils import ( get_tokenizer, convert_char_to_pinyin,)
def _find_repo_root(start: Path) -> Path: """Locate the repo root by looking for a `pretrained_models` folder upwards.""" for p in [start, *start.parents]: if (p / "pretrained_models").is_dir(): return p cwd = Path.cwd() if (cwd / "pretrained_models").is_dir(): return cwd return start
# Resolve repository layout for pretrained assets when running from source treeTHIS_FILE = Path(__file__).resolve()REPO_ROOT = _find_repo_root(THIS_FILE)PRETRAINED_ROOT = REPO_ROOT / "pretrained_models"CKPTS_ROOT = PRETRAINED_ROOT / "ckpts"
_ref_audio_cache = {}
device = ( "cuda" if torch.cuda.is_available() else "xpu" if torch.xpu.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
# -----------------------------------------
target_sample_rate = 24000n_mel_channels = 100hop_length = 256win_length = 1024n_fft = 1024mel_spec_type = "vocos"target_rms = 0.1cross_fade_duration = 0.15ode_method = "euler"nfe_step = 32 # 16, 32cfg_strength = 3.0sway_sampling_coef = 1speed = 1.0fix_duration = None
# -----------------------------------------
# chunk text into smaller pieces
def chunk_text(text, max_chars=135): """ Splits the input text into chunks, each with a maximum number of characters.
Args: text (str): The text to be split. max_chars (int): The maximum number of characters per chunk.
Returns: List[str]: A list of text chunks. """ chunks = [] current_chunk = "" # Split the text into sentences based on punctuation followed by whitespace sentences = re.split(r"(?<=[;:,.!?])\s+|(?<=[;:,。!?])", text)
for sentence in sentences: if len(current_chunk.encode("utf-8")) + len(sentence.encode("utf-8")) <= max_chars: current_chunk += sentence + " " if sentence and len(sentence[-1].encode("utf-8")) == 1 else sentence else: if current_chunk: chunks.append(current_chunk.strip()) current_chunk = sentence + " " if sentence and len(sentence[-1].encode("utf-8")) == 1 else sentence
if current_chunk: chunks.append(current_chunk.strip())
return chunks
# load vocoderdef load_vocoder(vocoder_name="vocos", is_local=False, local_path="", device=device, hf_cache_dir=None): if vocoder_name == "vocos": # vocoder = Vocos.from_pretrained("charactr/vocos-mel-24khz").to(device) if is_local: print(f"Load vocos from local path {local_path}") config_path = f"{local_path}/config.yaml" model_path = f"{local_path}/pytorch_model.bin" else: print("Download Vocos from huggingface charactr/vocos-mel-24khz") repo_id = "charactr/vocos-mel-24khz" config_path = hf_hub_download(repo_id=repo_id, cache_dir=hf_cache_dir, filename="config.yaml") model_path = hf_hub_download(repo_id=repo_id, cache_dir=hf_cache_dir, filename="pytorch_model.bin") vocoder = Vocos.from_hparams(config_path) state_dict = torch.load(model_path, map_location="cpu", weights_only=True) from vocos.feature_extractors import EncodecFeatures
if isinstance(vocoder.feature_extractor, EncodecFeatures): encodec_parameters = { "feature_extractor.encodec." + key: value for key, value in vocoder.feature_extractor.encodec.state_dict().items() } state_dict.update(encodec_parameters) vocoder.load_state_dict(state_dict) vocoder = vocoder.eval().to(device) elif vocoder_name == "bigvgan": try: from third_party.BigVGAN import bigvgan except ImportError: print("You need to follow the README to init submodule and change the BigVGAN source code.") if is_local: # download generator from https://huggingface.co/nvidia/bigvgan_v2_24khz_100band_256x/tree/main vocoder = bigvgan.BigVGAN.from_pretrained(local_path, use_cuda_kernel=False) else: vocoder = bigvgan.BigVGAN.from_pretrained( "nvidia/bigvgan_v2_24khz_100band_256x", use_cuda_kernel=False, cache_dir=hf_cache_dir )
vocoder.remove_weight_norm() vocoder = vocoder.eval().to(device) return vocoder
# load asr pipeline
asr_pipe = None
def initialize_asr_pipeline(device: str = device, dtype=None): if dtype is None: dtype = ( torch.float16 if "cuda" in device and torch.cuda.get_device_properties(device).major >= 7 and not torch.cuda.get_device_name().endswith("[ZLUDA]") else torch.float32 ) global asr_pipe asr_pipe = pipeline( "automatic-speech-recognition", model="openai/whisper-large-v3-turbo", torch_dtype=dtype, device=device, )
# transcribe
def transcribe(ref_audio, language=None): global asr_pipe if asr_pipe is None: initialize_asr_pipeline(device=device) return asr_pipe( ref_audio, chunk_length_s=30, batch_size=128, generate_kwargs={"task": "transcribe", "language": language} if language else {"task": "transcribe"}, return_timestamps=False, )["text"].strip()
# load model checkpoint for inference
def load_checkpoint(model, ckpt_path, device: str, dtype=None, use_ema=True): if dtype is None: dtype = ( torch.float16 if "cuda" in device and torch.cuda.get_device_properties(device).major >= 7 and not torch.cuda.get_device_name().endswith("[ZLUDA]") else torch.float32 ) model = model.to(dtype)
ckpt_type = ckpt_path.split(".")[-1] if ckpt_type == "safetensors": from safetensors.torch import load_file
checkpoint = load_file(ckpt_path, device=device) else: checkpoint = torch.load(ckpt_path, map_location=device, weights_only=True)
if use_ema: if ckpt_type == "safetensors": checkpoint = {"ema_model_state_dict": checkpoint} checkpoint["model_state_dict"] = { k.replace("ema_model.", ""): v for k, v in checkpoint["ema_model_state_dict"].items() if k not in ["initted", "step"] }
# patch for backward compatibility, 305e3ea for key in ["mel_spec.mel_stft.mel_scale.fb", "mel_spec.mel_stft.spectrogram.window", "ctc.proj.0.weight", "ctc.proj.0.bias", "ctc.ctc_proj.weight", "ctc.ctc_proj.bias"]: if key in checkpoint["model_state_dict"]: del checkpoint["model_state_dict"][key]
model.load_state_dict(checkpoint["model_state_dict"]) else: if ckpt_type == "safetensors": checkpoint = {"model_state_dict": checkpoint} model.load_state_dict(checkpoint["model_state_dict"])
del checkpoint torch.cuda.empty_cache()
return model.to(device)
# load model for inference
def load_model( model_cls, model_cfg, ckpt_path, mel_spec_type=mel_spec_type, vocab_file="", ode_method=ode_method, use_ema=True, device=device, use_prosody_encoder=False, prosody_cfg_path="", prosody_ckpt_path="",): if vocab_file == "": vocab_file = str(files("lemas_tts").joinpath("infer/examples/vocab.txt")) tokenizer = "custom"
print("\nvocab : ", vocab_file) print("token : ", tokenizer) print("model : ", ckpt_path, "\n")
vocab_char_map, vocab_size = get_tokenizer(vocab_file, tokenizer)
# Resolve prosody encoder assets if requested but paths not provided if use_prosody_encoder: if not prosody_cfg_path: prosody_cfg_path = str(CKPTS_ROOT / "prosody_encoder" / "pretssel_cfg.json") if not prosody_ckpt_path: prosody_ckpt_path = str(CKPTS_ROOT / "prosody_encoder" / "prosody_encoder_UnitY2.pt") model = CFM( transformer=model_cls(**model_cfg, text_num_embeds=vocab_size, mel_dim=n_mel_channels, use_prosody_encoder=use_prosody_encoder), mel_spec_kwargs=dict( n_fft=n_fft, hop_length=hop_length, win_length=win_length, n_mel_channels=n_mel_channels, target_sample_rate=target_sample_rate, mel_spec_type=mel_spec_type, ), odeint_kwargs=dict( method=ode_method, ), vocab_char_map=vocab_char_map, use_prosody_encoder=use_prosody_encoder, prosody_cfg_path=prosody_cfg_path, prosody_ckpt_path=prosody_ckpt_path, ).to(device)
dtype = torch.float32 if mel_spec_type == "bigvgan" else None model = load_checkpoint(model, ckpt_path, device, dtype=dtype, use_ema=use_ema)
return model
def remove_silence_edges(audio, silence_threshold=-42): # Remove silence from the start non_silent_start_idx = silence.detect_leading_silence(audio, silence_threshold=silence_threshold) audio = audio[non_silent_start_idx:]
# Remove silence from the end non_silent_end_duration = audio.duration_seconds for ms in reversed(audio): if ms.dBFS > silence_threshold: break non_silent_end_duration -= 0.001 trimmed_audio = audio[: int(non_silent_end_duration * 1000)]
return trimmed_audio
# preprocess reference audio and text
def preprocess_ref_audio_text(ref_audio_orig, ref_text, clip_short=True, show_info=print): show_info("Converting audio...") with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f: aseg = AudioSegment.from_file(ref_audio_orig)
if clip_short: # 1. try to find long silence for clipping non_silent_segs = silence.split_on_silence( aseg, min_silence_len=1000, silence_thresh=-50, keep_silence=1000, seek_step=10 ) non_silent_wave = AudioSegment.silent(duration=0) for non_silent_seg in non_silent_segs: if len(non_silent_wave) > 6000 and len(non_silent_wave + non_silent_seg) > 12000: show_info("Audio is over 12s, clipping short. (1)") break non_silent_wave += non_silent_seg
# 2. try to find short silence for clipping if 1. failed if len(non_silent_wave) > 12000: non_silent_segs = silence.split_on_silence( aseg, min_silence_len=100, silence_thresh=-40, keep_silence=1000, seek_step=10 ) non_silent_wave = AudioSegment.silent(duration=0) for non_silent_seg in non_silent_segs: if len(non_silent_wave) > 6000 and len(non_silent_wave + non_silent_seg) > 12000: show_info("Audio is over 12s, clipping short. (2)") break non_silent_wave += non_silent_seg
aseg = non_silent_wave
# 3. if no proper silence found for clipping if len(aseg) > 12000: aseg = aseg[:12000] show_info("Audio is over 12s, clipping short. (3)")
aseg = remove_silence_edges(aseg) + AudioSegment.silent(duration=50) aseg.export(f.name, format="wav") ref_audio = f.name
# Compute a hash of the reference audio file with open(ref_audio, "rb") as audio_file: audio_data = audio_file.read() audio_hash = hashlib.md5(audio_data).hexdigest()
if not ref_text.strip(): global _ref_audio_cache if audio_hash in _ref_audio_cache: # Use cached asr transcription show_info("Using cached reference text...") ref_text = _ref_audio_cache[audio_hash] else: show_info("No reference text provided, transcribing reference audio...") ref_text = transcribe(ref_audio) # Cache the transcribed text (not caching custom ref_text, enabling users to do manual tweak) _ref_audio_cache[audio_hash] = ref_text else: show_info("Using custom reference text...")
# Ensure ref_text ends with a proper sentence-ending punctuation if not ref_text.endswith(". ") and not ref_text.endswith("。"): if ref_text.endswith("."): ref_text += " " else: ref_text += ". "
print("\nref_text ", ref_text)
return ref_audio, ref_text
# infer process: chunk text -> infer batches [i.e. infer_batch_process()]
def infer_process( ref_audio, ref_text, gen_text, model_obj, vocoder, mel_spec_type=mel_spec_type, show_info=print, progress=tqdm, target_rms=target_rms, cross_fade_duration=cross_fade_duration, nfe_step=nfe_step, cfg_strength=cfg_strength, sway_sampling_coef=sway_sampling_coef, use_acc_grl=True, use_prosody_encoder=True, ref_ratio=None, no_ref_audio=False, speed=speed, fix_duration=fix_duration, device=device,): # Split the input text into batches audio, sr = torchaudio.load(ref_audio)
if type(ref_text) == str: max_chars = int(len(ref_text.encode("utf-8")) / (audio.shape[-1] / sr) * (22 - audio.shape[-1] / sr)) gen_text_batches = chunk_text(gen_text, max_chars=max_chars) else: gen_text_batches = gen_text
print(f"ref_text:", ref_text) for i, gen_text in enumerate(gen_text_batches): print(f"gen_text {i}", gen_text) print("\n")
show_info(f"Generating audio in {len(gen_text_batches)} batches...") return next( infer_batch_process( (audio, sr), ref_text, gen_text_batches, model_obj, vocoder, mel_spec_type=mel_spec_type, progress=progress, target_rms=target_rms, cross_fade_duration=cross_fade_duration, nfe_step=nfe_step, cfg_strength=cfg_strength, sway_sampling_coef=sway_sampling_coef, use_acc_grl=use_acc_grl, use_prosody_encoder=use_prosody_encoder, ref_ratio=ref_ratio, no_ref_audio=no_ref_audio, speed=speed, fix_duration=fix_duration, device=device, ) )
# infer batches
def infer_batch_process( ref_audio, ref_text, gen_text_batches, model_obj, vocoder, mel_spec_type="vocos", progress=tqdm, target_rms=0.1, cross_fade_duration=0.15, nfe_step=32, cfg_strength=2.0, sway_sampling_coef=-1, use_acc_grl=True, use_prosody_encoder=True, ref_ratio=None, no_ref_audio=False, speed=1, fix_duration=None, device=None, streaming=False, chunk_size=2048,): audio, sr = ref_audio if audio.shape[0] > 1: audio = torch.mean(audio, dim=0, keepdim=True)
rms = torch.sqrt(torch.mean(torch.square(audio))) if rms < target_rms: audio = audio * target_rms / rms if sr != target_sample_rate: resampler = torchaudio.transforms.Resample(sr, target_sample_rate) audio = resampler(audio) audio = audio.to(device)
generated_waves = [] spectrograms = [] if type(ref_text) == str: if len(ref_text[-1].encode("utf-8")) == 1: ref_text = ref_text + " "
def process_batch(gen_text): local_speed = speed
if type(ref_text) == str: if len(gen_text.encode("utf-8")) < 10: local_speed = 0.3
# Prepare the text text_list = [ref_text + gen_text] final_text_list = convert_char_to_pinyin(text_list) else: final_text_list = [ref_text + gen_text] print("final_text_list:", final_text_list)
ref_audio_len = audio.shape[-1] // hop_length if fix_duration is not None: duration = int(fix_duration * target_sample_rate / hop_length) else: # Calculate duration ref_text_len = len(ref_text) # .encode("utf-8") gen_text_len = len(gen_text) # .encode("utf-8") duration = ref_audio_len + int(ref_audio_len / ref_text_len * gen_text_len / local_speed) # inference with torch.inference_mode(): generated, _ = model_obj.sample( cond=audio, text=final_text_list, duration=duration, steps=nfe_step, cfg_strength=cfg_strength, sway_sampling_coef=sway_sampling_coef, use_acc_grl=use_acc_grl, use_prosody_encoder=use_prosody_encoder, ref_ratio=ref_ratio, no_ref_audio=no_ref_audio, ) del _
generated = generated.to(torch.float32) # generated mel spectrogram generated = generated[:, ref_audio_len:, :] generated = generated.permute(0, 2, 1) if mel_spec_type == "vocos": generated_wave = vocoder.decode(generated) elif mel_spec_type == "bigvgan": generated_wave = vocoder(generated) if rms < target_rms: generated_wave = generated_wave * rms / target_rms
# wav -> numpy # generated_wave = torch.clip(generated_wave, -0.999, 0.999) generated_wave = generated_wave.squeeze().cpu().numpy()
if streaming: for j in range(0, len(generated_wave), chunk_size): yield generated_wave[j : j + chunk_size], target_sample_rate else: generated_cpu = generated[0].cpu().numpy() del generated yield generated_wave, generated_cpu
if streaming: for gen_text in progress.tqdm(gen_text_batches) if progress is not None else gen_text_batches: for chunk in process_batch(gen_text): yield chunk else: with ThreadPoolExecutor() as executor: futures = [executor.submit(process_batch, gen_text) for gen_text in gen_text_batches] for future in progress.tqdm(futures) if progress is not None else futures: result = future.result() if result: generated_wave, generated_mel_spec = next(result) generated_waves.append(generated_wave) spectrograms.append(generated_mel_spec)
if generated_waves: if cross_fade_duration <= 0: # Simply concatenate final_wave = np.concatenate(generated_waves) else: # Combine all generated waves with cross-fading final_wave = generated_waves[0] for i in range(1, len(generated_waves)): prev_wave = final_wave next_wave = generated_waves[i]
# Calculate cross-fade samples, ensuring it does not exceed wave lengths cross_fade_samples = int(cross_fade_duration * target_sample_rate) cross_fade_samples = min(cross_fade_samples, len(prev_wave), len(next_wave))
if cross_fade_samples <= 0: # No overlap possible, concatenate final_wave = np.concatenate([prev_wave, next_wave]) continue
# Overlapping parts prev_overlap = prev_wave[-cross_fade_samples:] next_overlap = next_wave[:cross_fade_samples]
# Fade out and fade in fade_out = np.linspace(1, 0, cross_fade_samples) fade_in = np.linspace(0, 1, cross_fade_samples)
# Cross-faded overlap cross_faded_overlap = prev_overlap * fade_out + next_overlap * fade_in
# Combine new_wave = np.concatenate( [prev_wave[:-cross_fade_samples], cross_faded_overlap, next_wave[cross_fade_samples:]] )
final_wave = new_wave
# Create a combined spectrogram combined_spectrogram = np.concatenate(spectrograms, axis=1) final_wave = np.clip(final_wave, -0.999, 0.999) yield final_wave, target_sample_rate, combined_spectrogram
else: yield None, target_sample_rate, None
# remove silence from generated wav
def remove_silence_for_generated_wav(filename): aseg = AudioSegment.from_file(filename) non_silent_segs = silence.split_on_silence( aseg, min_silence_len=1000, silence_thresh=-50, keep_silence=500, seek_step=10 ) non_silent_wave = AudioSegment.silent(duration=0) for non_silent_seg in non_silent_segs: non_silent_wave += non_silent_seg aseg = non_silent_wave aseg.export(filename, format="wav")
# save spectrogram
def save_spectrogram(spectrogram, path): plt.figure(figsize=(12, 4)) plt.imshow(spectrogram, origin="lower", aspect="auto") plt.colorbar() plt.savefig(path) plt.close()1234567891011121314151617181920## Backbones quick introduction
### unett.py- flat unet transformer- structure same as in e2-tts & voicebox paper except using rotary pos emb- possible abs pos emb & convnextv2 blocks for embedded text before concat
### dit.py- adaln-zero dit- embedded timestep as condition- concatted noised_input + masked_cond + embedded_text, linear proj in- possible abs pos emb & convnextv2 blocks for embedded text before concat- possible long skip connection (first layer to last layer)
### mmdit.py- stable diffusion 3 block structure- timestep as condition- left stream: text embedded and applied a abs pos emb- right stream: masked_cond & noised_input concatted and with same conv pos emb as unett123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254"""ein notation:b - batchn - sequencent - text sequencenw - raw wave lengthd - dimension"""
from __future__ import annotations
from typing import Optional
import torchfrom torch import nnimport torch.nn.functional as F
from x_transformers.x_transformers import RotaryEmbedding
from lemas_tts.model.modules import ( TimestepEmbedding, ConvNeXtV2Block, ConvPositionEmbedding, DiTBlock, AdaLayerNorm_Final, precompute_freqs_cis, get_pos_embed_indices,)from lemas_tts.model.backbones.ecapa_tdnn import ECAPA_TDNN
# Text embedding
class TextEmbedding(nn.Module): def __init__(self, text_num_embeds, text_dim, mask_padding=True, conv_layers=0, conv_mult=2): super().__init__() self.text_embed = nn.Embedding(text_num_embeds + 1, text_dim) # use 0 as filler token
self.mask_padding = mask_padding # mask filler and batch padding tokens or not
if conv_layers > 0: self.extra_modeling = True self.precompute_max_pos = 4096 # ~44s of 24khz audio self.register_buffer("freqs_cis", precompute_freqs_cis(text_dim, self.precompute_max_pos), persistent=False) self.text_blocks = nn.Sequential( *[ConvNeXtV2Block(text_dim, text_dim * conv_mult) for _ in range(conv_layers)] ) else: self.extra_modeling = False
def forward(self, text: int["b nt"], seq_len, drop_text=False): # noqa: F722 text = text + 1 # use 0 as filler token. preprocess of batch pad -1, see list_str_to_idx() text = text[:, :seq_len] # curtail if character tokens are more than the mel spec tokens batch, text_len = text.shape[0], text.shape[1] text = F.pad(text, (0, seq_len - text_len), value=0) if self.mask_padding: text_mask = text == 0
if drop_text: # cfg for text text = torch.zeros_like(text)
text = self.text_embed(text) # b n -> b n d
# possible extra modeling if self.extra_modeling: # sinus pos emb batch_start = torch.zeros((batch,), dtype=torch.long) pos_idx = get_pos_embed_indices(batch_start, seq_len, max_pos=self.precompute_max_pos) text_pos_embed = self.freqs_cis[pos_idx] text = text + text_pos_embed
# convnextv2 blocks if self.mask_padding: text = text.masked_fill(text_mask.unsqueeze(-1).expand(-1, -1, text.size(-1)), 0.0) for block in self.text_blocks: text = block(text) text = text.masked_fill(text_mask.unsqueeze(-1).expand(-1, -1, text.size(-1)), 0.0) else: text = self.text_blocks(text)
return text
# noised input audio and context mixing embedding
class InputEmbedding(nn.Module): def __init__(self, mel_dim, text_dim, out_dim): super().__init__() self.proj = nn.Linear(mel_dim * 2 + text_dim, out_dim) self.conv_pos_embed = ConvPositionEmbedding(dim=out_dim)
def forward(self, x: float["b n d"], cond: float["b n d"], text_embed: float["b n d"], drop_audio_cond=False): # noqa: F722 if drop_audio_cond: # cfg for cond audio cond = torch.zeros_like(cond)
x = self.proj(torch.cat((x, cond, text_embed), dim=-1)) x = self.conv_pos_embed(x) + x return x
# Transformer backbone using DiT blocks
class DiT(nn.Module): def __init__( self, *, dim, depth=8, heads=8, dim_head=64, dropout=0.1, ff_mult=4, mel_dim=100, text_num_embeds=256, text_dim=None, text_mask_padding=True, qk_norm=None, conv_layers=0, pe_attn_head=None, long_skip_connection=False, checkpoint_activations=False, use_prosody_encoder=False, ): super().__init__()
self.time_embed = TimestepEmbedding(dim) if text_dim is None: text_dim = mel_dim self.text_embed = TextEmbedding( text_num_embeds, text_dim, mask_padding=text_mask_padding, conv_layers=conv_layers ) # project prosody embeddings (512-dim) to text_dim for conditioning self.use_prosody_encoder = use_prosody_encoder if use_prosody_encoder: self.prosody_text_proj = nn.Linear(512, text_dim) else: self.prosody_text_proj = None self.text_cond, self.text_uncond = None, None # text cache self.input_embed = InputEmbedding(mel_dim, text_dim, dim)
self.rotary_embed = RotaryEmbedding(dim_head)
self.dim = dim self.depth = depth
self.transformer_blocks = nn.ModuleList( [ DiTBlock( dim=dim, heads=heads, dim_head=dim_head, ff_mult=ff_mult, dropout=dropout, qk_norm=qk_norm, pe_attn_head=pe_attn_head, ) for _ in range(depth) ] ) self.long_skip_connection = nn.Linear(dim * 2, dim, bias=False) if long_skip_connection else None
self.norm_out = AdaLayerNorm_Final(dim) # final modulation self.proj_out = nn.Linear(dim, mel_dim)
self.checkpoint_activations = checkpoint_activations
self.initialize_weights()
def initialize_weights(self): # Zero-out AdaLN layers in DiT blocks: for block in self.transformer_blocks: nn.init.constant_(block.attn_norm.linear.weight, 0) nn.init.constant_(block.attn_norm.linear.bias, 0)
# Zero-out output layers: nn.init.constant_(self.norm_out.linear.weight, 0) nn.init.constant_(self.norm_out.linear.bias, 0) nn.init.constant_(self.proj_out.weight, 0) nn.init.constant_(self.proj_out.bias, 0)
def ckpt_wrapper(self, module): # https://github.com/chuanyangjin/fast-DiT/blob/main/models.py def ckpt_forward(*inputs): outputs = module(*inputs) return outputs
return ckpt_forward
def clear_cache(self): self.text_cond, self.text_uncond = None, None
def forward( self, x: float["b n d"], # nosied input audio # noqa: F722 cond: float["b n d"], # masked cond audio # noqa: F722 text: int["b nt"], # text # noqa: F722 time: float["b"] | float[""], # time step # noqa: F821 F722 drop_audio_cond, # cfg for cond audio drop_text, # cfg for text mask: bool["b n"] | None = None, # noqa: F722 cache=False, prosody_text: Optional[torch.Tensor] = None, ): batch, seq_len = x.shape[0], x.shape[1] if time.ndim == 0: time = time.repeat(batch)
# t: conditioning time, text: text, x: noised audio + cond audio + text t = self.time_embed(time) if cache: if drop_text: if self.text_uncond is None: self.text_uncond = self.text_embed(text, seq_len, drop_text=True) text_embed = self.text_uncond else: if self.text_cond is None: self.text_cond = self.text_embed(text, seq_len, drop_text=False) text_embed = self.text_cond else: text_embed = self.text_embed(text, seq_len, drop_text=drop_text)
# optional prosody conditioning on text side if prosody_text is not None and self.use_prosody_encoder: # prosody_text: (B, T_text, 512) -> project to text_dim and align to seq_len pt = self.prosody_text_proj(prosody_text) if pt.size(1) < seq_len: pad_len = seq_len - pt.size(1) pt = F.pad(pt, (0, 0, 0, pad_len)) elif pt.size(1) > seq_len: pt = pt[:, :seq_len] text_embed = text_embed + pt x = self.input_embed(x, cond, text_embed, drop_audio_cond=drop_audio_cond)
rope = self.rotary_embed.forward_from_seq_len(seq_len)
if self.long_skip_connection is not None: residual = x
for block in self.transformer_blocks: if self.checkpoint_activations: # https://pytorch.org/docs/stable/checkpoint.html#torch.utils.checkpoint.checkpoint x = torch.utils.checkpoint.checkpoint(self.ckpt_wrapper(block), x, t, mask, rope, use_reentrant=False) else: x = block(x, t, mask=mask, rope=rope)
if self.long_skip_connection is not None: x = self.long_skip_connection(torch.cat((x, residual), dim=-1))
x = self.norm_out(x, t) output = self.proj_out(x)
return output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931"""A popular speaker recognition and diarization model.
Authors * Hwidong Na 2020"""
import mathimport osimport torch # noqa: F401import torch.nn as nnimport torch.nn.functional as F
def length_to_mask(length, max_len=None, dtype=None, device=None): """Creates a binary mask for each sequence.
Reference: https://discuss.pytorch.org/t/how-to-generate-variable-length-mask/23397/3
Arguments --------- length : torch.LongTensor Containing the length of each sequence in the batch. Must be 1D. max_len : int Max length for the mask, also the size of the second dimension. dtype : torch.dtype, default: None The dtype of the generated mask. device: torch.device, default: None The device to put the mask variable.
Returns ------- mask : tensor The binary mask.
Example ------- >>> length=torch.Tensor([1,2,3]) >>> mask=length_to_mask(length) >>> mask tensor([[1., 0., 0.], [1., 1., 0.], [1., 1., 1.]]) """ assert len(length.shape) == 1
if max_len is None: max_len = length.max().long().item() # using arange to generate mask mask = torch.arange(max_len, device=length.device, dtype=length.dtype).expand( len(length), max_len ) < length.unsqueeze(1)
if dtype is None: dtype = length.dtype
if device is None: device = length.device
mask = torch.as_tensor(mask, dtype=dtype, device=device) return mask
def get_padding_elem(L_in: int, stride: int, kernel_size: int, dilation: int): """This function computes the number of elements to add for zero-padding.
Arguments --------- L_in : int stride: int kernel_size : int dilation : int """ if stride > 1: n_steps = math.ceil(((L_in - kernel_size * dilation) / stride) + 1) L_out = stride * (n_steps - 1) + kernel_size * dilation padding = [kernel_size // 2, kernel_size // 2]
else: L_out = (L_in - dilation * (kernel_size - 1) - 1) // stride + 1
padding = [(L_in - L_out) // 2, (L_in - L_out) // 2] return padding
class Conv1d(nn.Module): """This function implements 1d convolution.
Arguments --------- out_channels : int It is the number of output channels. kernel_size : int Kernel size of the convolutional filters. input_shape : tuple The shape of the input. Alternatively use ``in_channels``. in_channels : int The number of input channels. Alternatively use ``input_shape``. stride : int Stride factor of the convolutional filters. When the stride factor > 1, a decimation in time is performed. dilation : int Dilation factor of the convolutional filters. padding : str (same, valid, causal). If "valid", no padding is performed. If "same" and stride is 1, output shape is the same as the input shape. "causal" results in causal (dilated) convolutions. padding_mode : str This flag specifies the type of padding. See torch.nn documentation for more information. skip_transpose : bool If False, uses batch x time x channel convention of speechbrain. If True, uses batch x channel x time convention.
Example ------- >>> inp_tensor = torch.rand([10, 40, 16]) >>> cnn_1d = Conv1d( ... input_shape=inp_tensor.shape, out_channels=8, kernel_size=5 ... ) >>> out_tensor = cnn_1d(inp_tensor) >>> out_tensor.shape torch.Size([10, 40, 8]) """
def __init__( self, out_channels, kernel_size, input_shape=None, in_channels=None, stride=1, dilation=1, padding="same", groups=1, bias=True, padding_mode="reflect", skip_transpose=True, ): super().__init__() self.kernel_size = kernel_size self.stride = stride self.dilation = dilation self.padding = padding self.padding_mode = padding_mode self.unsqueeze = False self.skip_transpose = skip_transpose
if input_shape is None and in_channels is None: raise ValueError("Must provide one of input_shape or in_channels")
if in_channels is None: in_channels = self._check_input_shape(input_shape)
self.conv = nn.Conv1d( in_channels, out_channels, self.kernel_size, stride=self.stride, dilation=self.dilation, padding=0, groups=groups, bias=bias, )
def forward(self, x): """Returns the output of the convolution.
Arguments --------- x : torch.Tensor (batch, time, channel) input to convolve. 2d or 4d tensors are expected. """
if not self.skip_transpose: x = x.transpose(1, -1)
if self.unsqueeze: x = x.unsqueeze(1)
if self.padding == "same": x = self._manage_padding(x, self.kernel_size, self.dilation, self.stride)
elif self.padding == "causal": num_pad = (self.kernel_size - 1) * self.dilation x = F.pad(x, (num_pad, 0))
elif self.padding == "valid": pass
else: raise ValueError( "Padding must be 'same', 'valid' or 'causal'. Got " + self.padding )
wx = self.conv(x.to(self.conv.weight.dtype))
if self.unsqueeze: wx = wx.squeeze(1)
if not self.skip_transpose: wx = wx.transpose(1, -1)
return wx
def _manage_padding( self, x, kernel_size: int, dilation: int, stride: int, ): """This function performs zero-padding on the time axis such that their lengths is unchanged after the convolution.
Arguments --------- x : torch.Tensor Input tensor. kernel_size : int Size of kernel. dilation : int Dilation used. stride : int Stride. """
# Detecting input shape L_in = x.shape[-1]
# Time padding padding = get_padding_elem(L_in, stride, kernel_size, dilation)
# Applying padding x = F.pad(x, padding, mode=self.padding_mode)
return x
def _check_input_shape(self, shape): """Checks the input shape and returns the number of input channels."""
if len(shape) == 2: self.unsqueeze = True in_channels = 1 elif self.skip_transpose: in_channels = shape[1] elif len(shape) == 3: in_channels = shape[2] else: raise ValueError("conv1d expects 2d, 3d inputs. Got " + str(len(shape)))
# Kernel size must be odd if self.kernel_size % 2 == 0: raise ValueError( "The field kernel size must be an odd number. Got %s." % (self.kernel_size) ) return in_channels
class Fp32BatchNorm(nn.Module): def __init__(self, sync=True, *args, **kwargs): super().__init__()
if ( not torch.distributed.is_initialized() or torch.distributed.get_world_size() == 1 ): sync = False
if sync: self.bn = nn.SyncBatchNorm(*args, **kwargs) else: self.bn = nn.BatchNorm1d(*args, **kwargs)
self.sync = sync
def forward(self, input): if self.bn.running_mean.dtype != torch.float: if self.sync: self.bn.running_mean = self.bn.running_mean.float() self.bn.running_var = self.bn.running_var.float() if self.bn.affine: try: self.bn.weight = self.bn.weight.float() self.bn.bias = self.bn.bias.float() except: self.bn.float() else: self.bn.float()
output = self.bn(input.float()) return output.type_as(input)
class BatchNorm1d(nn.Module): """Applies 1d batch normalization to the input tensor.
Arguments --------- input_shape : tuple The expected shape of the input. Alternatively, use ``input_size``. input_size : int The expected size of the input. Alternatively, use ``input_shape``. eps : float This value is added to std deviation estimation to improve the numerical stability. momentum : float It is a value used for the running_mean and running_var computation. affine : bool When set to True, the affine parameters are learned. track_running_stats : bool When set to True, this module tracks the running mean and variance, and when set to False, this module does not track such statistics. combine_batch_time : bool When true, it combines batch an time axis.
Example ------- >>> input = torch.randn(100, 10) >>> norm = BatchNorm1d(input_shape=input.shape) >>> output = norm(input) >>> output.shape torch.Size([100, 10]) """
def __init__( self, input_shape=None, input_size=None, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, combine_batch_time=False, skip_transpose=True, enabled=True, ): super().__init__() self.combine_batch_time = combine_batch_time self.skip_transpose = skip_transpose
if input_size is None and skip_transpose: input_size = input_shape[1] elif input_size is None: input_size = input_shape[-1]
if enabled: self.norm = Fp32BatchNorm( num_features=input_size, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats, ) else: self.norm = nn.Identity()
def forward(self, x): """Returns the normalized input tensor.
Arguments --------- x : torch.Tensor (batch, time, [channels]) input to normalize. 2d or 3d tensors are expected in input 4d tensors can be used when combine_dims=True. """ shape_or = x.shape if self.combine_batch_time: if x.ndim == 3: x = x.reshape(shape_or[0] * shape_or[1], shape_or[2]) else: x = x.reshape(shape_or[0] * shape_or[1], shape_or[3], shape_or[2])
elif not self.skip_transpose: x = x.transpose(-1, 1)
x_n = self.norm(x)
if self.combine_batch_time: x_n = x_n.reshape(shape_or) elif not self.skip_transpose: x_n = x_n.transpose(1, -1)
return x_n
class Linear(torch.nn.Module): """Computes a linear transformation y = wx + b.
Arguments --------- n_neurons : int It is the number of output neurons (i.e, the dimensionality of the output). bias : bool If True, the additive bias b is adopted. combine_dims : bool If True and the input is 4D, combine 3rd and 4th dimensions of input.
Example ------- >>> inputs = torch.rand(10, 50, 40) >>> lin_t = Linear(input_shape=(10, 50, 40), n_neurons=100) >>> output = lin_t(inputs) >>> output.shape torch.Size([10, 50, 100]) """
def __init__( self, n_neurons, input_shape=None, input_size=None, bias=True, combine_dims=False, ): super().__init__() self.combine_dims = combine_dims
if input_shape is None and input_size is None: raise ValueError("Expected one of input_shape or input_size")
if input_size is None: input_size = input_shape[-1] if len(input_shape) == 4 and self.combine_dims: input_size = input_shape[2] * input_shape[3]
# Weights are initialized following pytorch approach self.w = nn.Linear(input_size, n_neurons, bias=bias)
def forward(self, x): """Returns the linear transformation of input tensor.
Arguments --------- x : torch.Tensor Input to transform linearly. """ if x.ndim == 4 and self.combine_dims: x = x.reshape(x.shape[0], x.shape[1], x.shape[2] * x.shape[3])
wx = self.w(x)
return wx
class TDNNBlock(nn.Module): """An implementation of TDNN.
Arguments ---------- in_channels : int Number of input channels. out_channels : int The number of output channels. kernel_size : int The kernel size of the TDNN blocks. dilation : int The dilation of the Res2Net block. activation : torch class A class for constructing the activation layers.
Example ------- >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2) >>> layer = TDNNBlock(64, 64, kernel_size=3, dilation=1) >>> out_tensor = layer(inp_tensor).transpose(1, 2) >>> out_tensor.shape torch.Size([8, 120, 64]) """
def __init__( self, in_channels, out_channels, kernel_size, dilation, activation=nn.ReLU, batch_norm=True, ): super(TDNNBlock, self).__init__() self.conv = Conv1d( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, dilation=dilation, ) self.activation = activation() self.norm = BatchNorm1d(input_size=out_channels, enabled=batch_norm)
def forward(self, x): return self.norm(self.activation(self.conv(x)))
class Res2NetBlock(torch.nn.Module): """An implementation of Res2NetBlock w/ dilation.
Arguments --------- in_channels : int The number of channels expected in the input. out_channels : int The number of output channels. scale : int The scale of the Res2Net block. kernel_size: int The kernel size of the Res2Net block. dilation : int The dilation of the Res2Net block.
Example ------- >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2) >>> layer = Res2NetBlock(64, 64, scale=4, dilation=3) >>> out_tensor = layer(inp_tensor).transpose(1, 2) >>> out_tensor.shape torch.Size([8, 120, 64]) """
def __init__( self, in_channels, out_channels, scale=8, kernel_size=3, dilation=1, batch_norm=True, ): super(Res2NetBlock, self).__init__() assert in_channels % scale == 0 assert out_channels % scale == 0
in_channel = in_channels // scale hidden_channel = out_channels // scale
self.blocks = nn.ModuleList( [ TDNNBlock( in_channel, hidden_channel, kernel_size=kernel_size, dilation=dilation, batch_norm=batch_norm, ) for i in range(scale - 1) ] ) self.scale = scale
def forward(self, x): y = [] for i, x_i in enumerate(torch.chunk(x, self.scale, dim=1)): if i == 0: y_i = x_i elif i == 1: y_i = self.blocks[i - 1](x_i) else: y_i = self.blocks[i - 1](x_i + y_i) y.append(y_i) y = torch.cat(y, dim=1) return y
class SEBlock(nn.Module): """An implementation of squeeze-and-excitation block.
Arguments --------- in_channels : int The number of input channels. se_channels : int The number of output channels after squeeze. out_channels : int The number of output channels.
Example ------- >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2) >>> se_layer = SEBlock(64, 16, 64) >>> lengths = torch.rand((8,)) >>> out_tensor = se_layer(inp_tensor, lengths).transpose(1, 2) >>> out_tensor.shape torch.Size([8, 120, 64]) """
def __init__(self, in_channels, se_channels, out_channels): super(SEBlock, self).__init__()
self.conv1 = Conv1d( in_channels=in_channels, out_channels=se_channels, kernel_size=1 ) self.relu = torch.nn.ReLU(inplace=True) self.conv2 = Conv1d( in_channels=se_channels, out_channels=out_channels, kernel_size=1 ) self.sigmoid = torch.nn.Sigmoid()
def forward(self, x, lengths=None): L = x.shape[-1] if lengths is not None: mask = length_to_mask(lengths * L, max_len=L, device=x.device) mask = mask.unsqueeze(1) total = mask.sum(dim=2, keepdim=True) s = (x * mask).sum(dim=2, keepdim=True) / total else: s = x.mean(dim=2, keepdim=True)
s = self.relu(self.conv1(s)) s = self.sigmoid(self.conv2(s))
return s * x
class AttentiveStatisticsPooling(nn.Module): """This class implements an attentive statistic pooling layer for each channel. It returns the concatenated mean and std of the input tensor.
Arguments --------- channels: int The number of input channels. attention_channels: int The number of attention channels.
Example ------- >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2) >>> asp_layer = AttentiveStatisticsPooling(64) >>> lengths = torch.rand((8,)) >>> out_tensor = asp_layer(inp_tensor, lengths).transpose(1, 2) >>> out_tensor.shape torch.Size([8, 1, 128]) """
def __init__( self, channels, attention_channels=128, global_context=True, batch_norm=True ): super().__init__()
self.eps = 1e-12 self.global_context = global_context if global_context: self.tdnn = TDNNBlock( channels * 3, attention_channels, 1, 1, batch_norm=batch_norm ) else: self.tdnn = TDNNBlock( channels, attention_channels, 1, 1, batch_norm, batch_norm ) self.tanh = nn.Tanh() self.conv = Conv1d( in_channels=attention_channels, out_channels=channels, kernel_size=1 )
def forward(self, x, lengths=None): """Calculates mean and std for a batch (input tensor).
Arguments --------- x : torch.Tensor Tensor of shape [N, C, L]. """ L = x.shape[-1]
def _compute_statistics(x, m, dim=2, eps=self.eps): mean = (m * x).sum(dim) std = torch.sqrt((m * (x - mean.unsqueeze(dim)).pow(2)).sum(dim).clamp(eps)) return mean, std
if lengths is None: lengths = torch.ones(x.shape[0], device=x.device)
# Make binary mask of shape [N, 1, L] mask = length_to_mask(lengths * L, max_len=L, device=x.device) mask = mask.unsqueeze(1)
# Expand the temporal context of the pooling layer by allowing the # self-attention to look at global properties of the utterance. if self.global_context: # torch.std is unstable for backward computation # https://github.com/pytorch/pytorch/issues/4320 total = mask.sum(dim=2, keepdim=True).float() mean, std = _compute_statistics(x, mask / total) mean = mean.unsqueeze(2).repeat(1, 1, L) std = std.unsqueeze(2).repeat(1, 1, L) attn = torch.cat([x, mean, std], dim=1) else: attn = x
# Apply layers attn = self.conv(self.tanh(self.tdnn(attn)))
# Filter out zero-paddings attn = attn.masked_fill(mask == 0, float("-inf"))
attn = F.softmax(attn, dim=2) mean, std = _compute_statistics(x, attn) # Append mean and std of the batch pooled_stats = torch.cat((mean, std), dim=1) pooled_stats = pooled_stats.unsqueeze(2)
return pooled_stats
class SERes2NetBlock(nn.Module): """An implementation of building block in ECAPA-TDNN, i.e., TDNN-Res2Net-TDNN-SEBlock.
Arguments ---------- out_channels: int The number of output channels. res2net_scale: int The scale of the Res2Net block. kernel_size: int The kernel size of the TDNN blocks. dilation: int The dilation of the Res2Net block. activation : torch class A class for constructing the activation layers.
Example ------- >>> x = torch.rand(8, 120, 64).transpose(1, 2) >>> conv = SERes2NetBlock(64, 64, res2net_scale=4) >>> out = conv(x).transpose(1, 2) >>> out.shape torch.Size([8, 120, 64]) """
def __init__( self, in_channels, out_channels, res2net_scale=8, se_channels=128, kernel_size=1, dilation=1, activation=torch.nn.ReLU, batch_norm=True, ): super().__init__() self.out_channels = out_channels self.tdnn1 = TDNNBlock( in_channels, out_channels, kernel_size=1, dilation=1, activation=activation, batch_norm=batch_norm, ) self.res2net_block = Res2NetBlock( out_channels, out_channels, res2net_scale, kernel_size, dilation, batch_norm=batch_norm, ) self.tdnn2 = TDNNBlock( out_channels, out_channels, kernel_size=1, dilation=1, activation=activation, batch_norm=batch_norm, ) self.se_block = SEBlock(out_channels, se_channels, out_channels)
self.shortcut = None if in_channels != out_channels: self.shortcut = Conv1d( in_channels=in_channels, out_channels=out_channels, kernel_size=1, )
def forward(self, x, lengths=None): residual = x if self.shortcut: residual = self.shortcut(x)
x = self.tdnn1(x) x = self.res2net_block(x) x = self.tdnn2(x) x = self.se_block(x, lengths)
return x + residual
class ECAPA_TDNN(torch.nn.Module): """An implementation of the speaker embedding model in a paper. "ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in TDNN Based Speaker Verification" (https://arxiv.org/abs/2005.07143).
Arguments --------- device : str Device used, e.g., "cpu" or "cuda". activation : torch class A class for constructing the activation layers. channels : list of ints Output channels for TDNN/SERes2Net layer. kernel_sizes : list of ints List of kernel sizes for each layer. dilations : list of ints List of dilations for kernels in each layer. lin_neurons : int Number of neurons in linear layers.
Example ------- >>> input_feats = torch.rand([5, 120, 80]) >>> compute_embedding = ECAPA_TDNN(80, lin_neurons=192) >>> outputs = compute_embedding(input_feats) >>> outputs.shape torch.Size([5, 1, 192]) """
def __init__( self, input_size, lin_neurons=192, activation=torch.nn.ReLU, channels=[512, 512, 512, 512, 1536], kernel_sizes=[5, 3, 3, 3, 1], dilations=[1, 2, 3, 4, 1], attention_channels=128, res2net_scale=8, se_channels=128, global_context=True, batch_norm=True, ):
super().__init__() assert len(channels) == len(kernel_sizes) assert len(channels) == len(dilations) self.channels = channels self.blocks = nn.ModuleList()
# The initial TDNN layer self.blocks.append( TDNNBlock( input_size, channels[0], kernel_sizes[0], dilations[0], activation, batch_norm=batch_norm, ) )
# SE-Res2Net layers for i in range(1, len(channels) - 1): self.blocks.append( SERes2NetBlock( channels[i - 1], channels[i], res2net_scale=res2net_scale, se_channels=se_channels, kernel_size=kernel_sizes[i], dilation=dilations[i], activation=activation, batch_norm=batch_norm, ) )
# Multi-layer feature aggregation self.mfa = TDNNBlock( channels[-1], channels[-1], kernel_sizes[-1], dilations[-1], activation, batch_norm=batch_norm, )
# Attentive Statistical Pooling self.asp = AttentiveStatisticsPooling( channels[-1], attention_channels=attention_channels, global_context=global_context, batch_norm=batch_norm, ) self.asp_bn = BatchNorm1d(input_size=channels[-1] * 2, enabled=batch_norm)
# Final linear transformation self.fc = Conv1d( in_channels=channels[-1] * 2, out_channels=input_size, # lin_neurons, kernel_size=1, )
# @torch.cuda.amp.autocast(enabled=True, dtype=torch.float32) def forward(self, x, lengths=None): """Returns the embedding vector.
Arguments --------- x : torch.Tensor Tensor of shape (batch, time, channel). """ # Minimize transpose for efficiency x = x.transpose(1, 2)
xl = [] for layer in self.blocks: try: x = layer(x, lengths=lengths) except TypeError: x = layer(x) xl.append(x)
# Multi-layer feature aggregation x = torch.cat(xl[1:], dim=1) x = self.mfa(x)
# Attentive Statistical Pooling x = self.asp(x, lengths=lengths) x = self.asp_bn(x)
# Final linear transformation x = self.fc(x)
x = x.squeeze(-1) return x
if __name__ == "__main__": model = ECAPA_TDNN(128, batch_norm=False) # print(model)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189"""ein notation:b - batchn - sequencent - text sequencenw - raw wave lengthd - dimension"""
from __future__ import annotations
import torchfrom torch import nn
from x_transformers.x_transformers import RotaryEmbedding
from lemas_tts.model.modules import ( TimestepEmbedding, ConvPositionEmbedding, MMDiTBlock, AdaLayerNorm_Final, precompute_freqs_cis, get_pos_embed_indices,)
# text embedding
class TextEmbedding(nn.Module): def __init__(self, out_dim, text_num_embeds, mask_padding=True): super().__init__() self.text_embed = nn.Embedding(text_num_embeds + 1, out_dim) # will use 0 as filler token
self.mask_padding = mask_padding # mask filler and batch padding tokens or not
self.precompute_max_pos = 1024 self.register_buffer("freqs_cis", precompute_freqs_cis(out_dim, self.precompute_max_pos), persistent=False)
def forward(self, text: int["b nt"], drop_text=False) -> int["b nt d"]: # noqa: F722 text = text + 1 # use 0 as filler token. preprocess of batch pad -1, see list_str_to_idx() if self.mask_padding: text_mask = text == 0
if drop_text: # cfg for text text = torch.zeros_like(text)
text = self.text_embed(text) # b nt -> b nt d
# sinus pos emb batch_start = torch.zeros((text.shape[0],), dtype=torch.long) batch_text_len = text.shape[1] pos_idx = get_pos_embed_indices(batch_start, batch_text_len, max_pos=self.precompute_max_pos) text_pos_embed = self.freqs_cis[pos_idx]
text = text + text_pos_embed
if self.mask_padding: text = text.masked_fill(text_mask.unsqueeze(-1).expand(-1, -1, text.size(-1)), 0.0)
return text
# noised input & masked cond audio embedding
class AudioEmbedding(nn.Module): def __init__(self, in_dim, out_dim): super().__init__() self.linear = nn.Linear(2 * in_dim, out_dim) self.conv_pos_embed = ConvPositionEmbedding(out_dim)
def forward(self, x: float["b n d"], cond: float["b n d"], drop_audio_cond=False): # noqa: F722 if drop_audio_cond: cond = torch.zeros_like(cond) x = torch.cat((x, cond), dim=-1) x = self.linear(x) x = self.conv_pos_embed(x) + x return x
# Transformer backbone using MM-DiT blocks
class MMDiT(nn.Module): def __init__( self, *, dim, depth=8, heads=8, dim_head=64, dropout=0.1, ff_mult=4, mel_dim=100, text_num_embeds=256, text_mask_padding=True, qk_norm=None, ): super().__init__()
self.time_embed = TimestepEmbedding(dim) self.text_embed = TextEmbedding(dim, text_num_embeds, mask_padding=text_mask_padding) self.text_cond, self.text_uncond = None, None # text cache self.audio_embed = AudioEmbedding(mel_dim, dim)
self.rotary_embed = RotaryEmbedding(dim_head)
self.dim = dim self.depth = depth
self.transformer_blocks = nn.ModuleList( [ MMDiTBlock( dim=dim, heads=heads, dim_head=dim_head, dropout=dropout, ff_mult=ff_mult, context_pre_only=i == depth - 1, qk_norm=qk_norm, ) for i in range(depth) ] ) self.norm_out = AdaLayerNorm_Final(dim) # final modulation self.proj_out = nn.Linear(dim, mel_dim)
self.initialize_weights()
def initialize_weights(self): # Zero-out AdaLN layers in MMDiT blocks: for block in self.transformer_blocks: nn.init.constant_(block.attn_norm_x.linear.weight, 0) nn.init.constant_(block.attn_norm_x.linear.bias, 0) nn.init.constant_(block.attn_norm_c.linear.weight, 0) nn.init.constant_(block.attn_norm_c.linear.bias, 0)
# Zero-out output layers: nn.init.constant_(self.norm_out.linear.weight, 0) nn.init.constant_(self.norm_out.linear.bias, 0) nn.init.constant_(self.proj_out.weight, 0) nn.init.constant_(self.proj_out.bias, 0)
def clear_cache(self): self.text_cond, self.text_uncond = None, None
def forward( self, x: float["b n d"], # nosied input audio # noqa: F722 cond: float["b n d"], # masked cond audio # noqa: F722 text: int["b nt"], # text # noqa: F722 time: float["b"] | float[""], # time step # noqa: F821 F722 drop_audio_cond, # cfg for cond audio drop_text, # cfg for text mask: bool["b n"] | None = None, # noqa: F722 cache=False, ): batch = x.shape[0] if time.ndim == 0: time = time.repeat(batch)
# t: conditioning (time), c: context (text + masked cond audio), x: noised input audio t = self.time_embed(time) if cache: if drop_text: if self.text_uncond is None: self.text_uncond = self.text_embed(text, drop_text=True) c = self.text_uncond else: if self.text_cond is None: self.text_cond = self.text_embed(text, drop_text=False) c = self.text_cond else: c = self.text_embed(text, drop_text=drop_text) x = self.audio_embed(x, cond, drop_audio_cond=drop_audio_cond)
seq_len = x.shape[1] text_len = text.shape[1] rope_audio = self.rotary_embed.forward_from_seq_len(seq_len) rope_text = self.rotary_embed.forward_from_seq_len(text_len)
for block in self.transformer_blocks: c, x = block(x, c, t, mask=mask, rope=rope_audio, c_rope=rope_text)
x = self.norm_out(x, t) output = self.proj_out(x)
return output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433"""Prosody encoder backbone based on the Pretssel ECAPA-TDNN architecture.
This module provides: - ProsodyEncoder: wraps an ECAPA-TDNN model to produce utterance-level prosody embeddings from 80-dim FBANK features. - extract_fbank_16k: utility to compute 80-bin FBANK from 16kHz audio.
It is self-contained (no fairseq2 dependency) and can be used insideCFM or other models as a conditioning network."""
from __future__ import annotations
from pathlib import Pathfrom typing import List, Optional, Tupleimport json
import torchimport torchaudiofrom torch import Tensorfrom torch import nnfrom torch.nn import Conv1d, LayerNorm, Module, ModuleList, ReLU, Sigmoid, Tanh, initimport torch.nn.functional as F
AUDIO_SAMPLE_RATE = 16_000
class ECAPA_TDNN(Module): """ ECAPA-TDNN core used in Pretssel prosody encoder.
Expects input features of shape (B, T, C) with C=80 and returns a normalized embedding of shape (B, embed_dim). """
def __init__( self, channels: List[int], kernel_sizes: List[int], dilations: List[int], attention_channels: int, res2net_scale: int, se_channels: int, global_context: bool, groups: List[int], embed_dim: int, input_dim: int, ): super().__init__() assert len(channels) == len(kernel_sizes) == len(dilations) self.channels = channels self.embed_dim = embed_dim self.blocks = ModuleList()
self.blocks.append( TDNNBlock( input_dim, channels[0], kernel_sizes[0], dilations[0], groups[0], ) )
for i in range(1, len(channels) - 1): self.blocks.append( SERes2NetBlock( channels[i - 1], channels[i], res2net_scale=res2net_scale, se_channels=se_channels, kernel_size=kernel_sizes[i], dilation=dilations[i], groups=groups[i], ) )
self.mfa = TDNNBlock( channels[-1], channels[-1], kernel_sizes[-1], dilations[-1], groups=groups[-1], )
self.asp = AttentiveStatisticsPooling( channels[-1], attention_channels=attention_channels, global_context=global_context, ) self.asp_norm = LayerNorm(channels[-1] * 2, eps=1e-12)
self.fc = Conv1d( in_channels=channels[-1] * 2, out_channels=embed_dim, kernel_size=1, )
self.reset_parameters()
def reset_parameters(self) -> None: def encoder_init(m: Module) -> None: if isinstance(m, Conv1d): init.xavier_uniform_(m.weight, init.calculate_gain("relu"))
self.apply(encoder_init)
def forward( self, x: Tensor, padding_mask: Optional[Tensor] = None, ) -> Tensor: # x: (B, T, C) x = x.transpose(1, 2) # (B, C, T)
xl = [] for layer in self.blocks: x = layer(x, padding_mask=padding_mask) xl.append(x)
x = torch.cat(xl[1:], dim=1) x = self.mfa(x)
x = self.asp(x, padding_mask=padding_mask) x = self.asp_norm(x.transpose(1, 2)).transpose(1, 2)
x = self.fc(x)
x = x.transpose(1, 2).squeeze(1) # (B, embed_dim) return F.normalize(x, dim=-1)
class TDNNBlock(Module): def __init__( self, in_channels: int, out_channels: int, kernel_size: int, dilation: int, groups: int = 1, ): super().__init__() self.conv = Conv1d( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, dilation=dilation, padding=dilation * (kernel_size - 1) // 2, groups=groups, ) self.activation = ReLU() self.norm = LayerNorm(out_channels, eps=1e-12)
def forward(self, x: Tensor, padding_mask: Optional[Tensor] = None) -> Tensor: x = self.activation(self.conv(x)) return self.norm(x.transpose(1, 2)).transpose(1, 2)
class Res2NetBlock(Module): def __init__( self, in_channels: int, out_channels: int, scale: int = 8, kernel_size: int = 3, dilation: int = 1, ): super().__init__() assert in_channels % scale == 0 assert out_channels % scale == 0
in_channel = in_channels // scale hidden_channel = out_channels // scale self.blocks = ModuleList( [ TDNNBlock( in_channel, hidden_channel, kernel_size=kernel_size, dilation=dilation, ) for _ in range(scale - 1) ] ) self.scale = scale
def forward(self, x: Tensor) -> Tensor: y = [] for i, x_i in enumerate(torch.chunk(x, self.scale, dim=1)): if i == 0: y_i = x_i elif i == 1: y_i = self.blocks[i - 1](x_i) else: y_i = self.blocks[i - 1](x_i + y_i) y.append(y_i) return torch.cat(y, dim=1)
class SEBlock(Module): def __init__( self, in_channels: int, se_channels: int, out_channels: int, ): super().__init__() self.conv1 = Conv1d(in_channels=in_channels, out_channels=se_channels, kernel_size=1) self.relu = ReLU(inplace=True) self.conv2 = Conv1d(in_channels=se_channels, out_channels=out_channels, kernel_size=1) self.sigmoid = Sigmoid()
def forward(self, x: Tensor, padding_mask: Optional[Tensor] = None) -> Tensor: if padding_mask is not None: # padding_mask: (B, T) with 1 for valid, 0 for pad mask = padding_mask.unsqueeze(1) # (B, 1, T) lengths = mask.sum(dim=2, keepdim=True) s = (x * mask).sum(dim=2, keepdim=True) / torch.clamp(lengths, min=1.0) else: s = x.mean(dim=2, keepdim=True)
s = self.relu(self.conv1(s)) s = self.sigmoid(self.conv2(s)) return s * x
class AttentiveStatisticsPooling(Module): def __init__( self, channels: int, attention_channels: int = 128, global_context: bool = True ): super().__init__() self.eps = 1e-12 self.global_context = global_context if global_context: self.tdnn = TDNNBlock(channels * 3, attention_channels, 1, 1) else: self.tdnn = TDNNBlock(channels, attention_channels, 1, 1)
self.tanh = Tanh() self.conv = Conv1d(in_channels=attention_channels, out_channels=channels, kernel_size=1)
def forward(self, x: Tensor, padding_mask: Optional[Tensor] = None) -> Tensor: # x: (N, C, L) N, C, L = x.shape
def _compute_statistics( x: Tensor, m: Tensor, dim: int = 2, eps: float = 1e-12 ) -> Tuple[Tensor, Tensor]: mean = (m * x).sum(dim) std = torch.sqrt((m * (x - mean.unsqueeze(dim)).pow(2)).sum(dim).clamp(eps)) return mean, std
if padding_mask is not None: mask = padding_mask else: mask = torch.ones(N, L, device=x.device, dtype=x.dtype) mask = mask.unsqueeze(1) # (N, 1, L)
if self.global_context: total = mask.sum(dim=2, keepdim=True).to(x) mean, std = _compute_statistics(x, mask / total) mean = mean.unsqueeze(2).repeat(1, 1, L) std = std.unsqueeze(2).repeat(1, 1, L) attn = torch.cat([x, mean, std], dim=1) else: attn = x
attn = self.conv(self.tanh(self.tdnn(attn)))
attn = attn.masked_fill(mask == 0, float("-inf"))
attn = F.softmax(attn, dim=2) mean, std = _compute_statistics(x, attn) pooled_stats = torch.cat((mean, std), dim=1) pooled_stats = pooled_stats.unsqueeze(2) return pooled_stats
class SERes2NetBlock(Module): def __init__( self, in_channels: int, out_channels: int, res2net_scale: int = 8, se_channels: int = 128, kernel_size: int = 1, dilation: int = 1, groups: int = 1, ): super().__init__() self.out_channels = out_channels self.tdnn1 = TDNNBlock( in_channels, out_channels, kernel_size=1, dilation=1, groups=groups, ) self.res2net_block = Res2NetBlock( out_channels, out_channels, res2net_scale, kernel_size, dilation, ) self.tdnn2 = TDNNBlock( out_channels, out_channels, kernel_size=1, dilation=1, groups=groups, ) self.se_block = SEBlock(out_channels, se_channels, out_channels)
self.shortcut = None if in_channels != out_channels: self.shortcut = Conv1d( in_channels=in_channels, out_channels=out_channels, kernel_size=1, )
def forward(self, x: Tensor, padding_mask: Optional[Tensor] = None) -> Tensor: residual = x if self.shortcut: residual = self.shortcut(x)
x = self.tdnn1(x) x = self.res2net_block(x) x = self.tdnn2(x) x = self.se_block(x, padding_mask=padding_mask) return x + residual
def extract_fbank_16k(audio_16k: Tensor) -> Tensor: """ Compute 80-dim FBANK features from 16kHz audio.
Args: audio_16k: Tensor of shape (T,) or (1, T) Returns: fbank: Tensor of shape (T_fbank, 80) """ if audio_16k.ndim == 1: audio_16k = audio_16k.unsqueeze(0)
# Ensure minimum length for kaldi.fbank window (default 25ms @16k -> 400 samples) min_len = 400 if audio_16k.shape[-1] < min_len: repeat_times = (min_len // audio_16k.shape[-1]) + 1 audio_16k = audio_16k.repeat(1, repeat_times) if audio_16k.dim() > 1 else audio_16k.repeat(repeat_times)
fbank = torchaudio.compliance.kaldi.fbank( audio_16k, num_mel_bins=80, sample_frequency=AUDIO_SAMPLE_RATE, ) return fbank
class ProsodyEncoder(nn.Module): """ High-level wrapper for the Pretssel prosody encoder.
Usage: encoder = ProsodyEncoder(cfg_path, ckpt_path, freeze=True) emb = encoder(fbank_batch) # (B, 512) """
def __init__(self, cfg_path: Path, ckpt_path: Path, freeze: bool = True): super().__init__() model_cfg = self._load_pretssel_model_cfg(cfg_path) self.encoder = self._build_prosody_encoder(model_cfg) self._load_prosody_encoder_state(self.encoder, ckpt_path) if freeze: for p in self.encoder.parameters(): p.requires_grad = False
@staticmethod def _load_pretssel_model_cfg(cfg_path: Path) -> dict: cfg = json.loads(cfg_path.read_text()) if "model" not in cfg: raise ValueError(f"{cfg_path} does not contain a top-level 'model' key.") return cfg["model"]
@staticmethod def _build_prosody_encoder(model_cfg: dict) -> ECAPA_TDNN: encoder = ECAPA_TDNN( channels=model_cfg["prosody_channels"], kernel_sizes=model_cfg["prosody_kernel_sizes"], dilations=model_cfg["prosody_dilations"], attention_channels=model_cfg["prosody_attention_channels"], res2net_scale=model_cfg["prosody_res2net_scale"], se_channels=model_cfg["prosody_se_channels"], global_context=model_cfg["prosody_global_context"], groups=model_cfg["prosody_groups"], embed_dim=model_cfg["prosody_embed_dim"], input_dim=model_cfg["input_feat_per_channel"], ) return encoder
@staticmethod def _load_prosody_encoder_state(model: Module, ckpt_path: Path) -> None: state = torch.load(ckpt_path, map_location="cpu") if isinstance(state, dict): if all(isinstance(k, str) for k in state.keys()) and ( any(k.startswith("prosody_encoder.") for k in state.keys()) or any(k.startswith("prosody_encoder_model.") for k in state.keys()) ): state = { k.replace("prosody_encoder_model.", "", 1).replace("prosody_encoder.", "", 1): v for k, v in state.items() if k.startswith("prosody_encoder.") or k.startswith("prosody_encoder_model.") } missing, unexpected = model.load_state_dict(state, strict=False) if missing or unexpected: raise RuntimeError( f"Error loading checkpoint {ckpt_path}: missing keys={missing}, " f"unexpected keys={unexpected}" )
def forward(self, fbank: Tensor, padding_mask: Optional[Tensor] = None) -> Tensor: """ Args: fbank: Tensor of shape (B, T, 80) padding_mask: Optional tensor of shape (B, T) with 1 for valid. Returns: emb: Tensor of shape (B, 512) """ return self.encoder(fbank, padding_mask=padding_mask)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250"""ein notation:b - batchn - sequencent - text sequencenw - raw wave lengthd - dimension"""
from __future__ import annotationsfrom typing import Literal
import torchfrom torch import nnimport torch.nn.functional as F
from x_transformers import RMSNormfrom x_transformers.x_transformers import RotaryEmbedding
from lemas_tts.model.modules import ( TimestepEmbedding, ConvNeXtV2Block, ConvPositionEmbedding, Attention, AttnProcessor, FeedForward, precompute_freqs_cis, get_pos_embed_indices,)
# Text embedding
class TextEmbedding(nn.Module): def __init__(self, text_num_embeds, text_dim, mask_padding=True, conv_layers=0, conv_mult=2): super().__init__() self.text_embed = nn.Embedding(text_num_embeds + 1, text_dim) # use 0 as filler token
self.mask_padding = mask_padding # mask filler and batch padding tokens or not
if conv_layers > 0: self.extra_modeling = True self.precompute_max_pos = 4096 # ~44s of 24khz audio self.register_buffer("freqs_cis", precompute_freqs_cis(text_dim, self.precompute_max_pos), persistent=False) self.text_blocks = nn.Sequential( *[ConvNeXtV2Block(text_dim, text_dim * conv_mult) for _ in range(conv_layers)] ) else: self.extra_modeling = False
def forward(self, text: int["b nt"], seq_len, drop_text=False): # noqa: F722 text = text + 1 # use 0 as filler token. preprocess of batch pad -1, see list_str_to_idx() text = text[:, :seq_len] # curtail if character tokens are more than the mel spec tokens batch, text_len = text.shape[0], text.shape[1] text = F.pad(text, (0, seq_len - text_len), value=0) if self.mask_padding: text_mask = text == 0
if drop_text: # cfg for text text = torch.zeros_like(text)
text = self.text_embed(text) # b n -> b n d
# possible extra modeling if self.extra_modeling: # sinus pos emb batch_start = torch.zeros((batch,), dtype=torch.long) pos_idx = get_pos_embed_indices(batch_start, seq_len, max_pos=self.precompute_max_pos) text_pos_embed = self.freqs_cis[pos_idx] text = text + text_pos_embed
# convnextv2 blocks if self.mask_padding: text = text.masked_fill(text_mask.unsqueeze(-1).expand(-1, -1, text.size(-1)), 0.0) for block in self.text_blocks: text = block(text) text = text.masked_fill(text_mask.unsqueeze(-1).expand(-1, -1, text.size(-1)), 0.0) else: text = self.text_blocks(text)
return text
# noised input audio and context mixing embedding
class InputEmbedding(nn.Module): def __init__(self, mel_dim, text_dim, out_dim): super().__init__() self.proj = nn.Linear(mel_dim * 2 + text_dim, out_dim) self.conv_pos_embed = ConvPositionEmbedding(dim=out_dim)
def forward(self, x: float["b n d"], cond: float["b n d"], text_embed: float["b n d"], drop_audio_cond=False): # noqa: F722 if drop_audio_cond: # cfg for cond audio cond = torch.zeros_like(cond)
x = self.proj(torch.cat((x, cond, text_embed), dim=-1)) x = self.conv_pos_embed(x) + x return x
# Flat UNet Transformer backbone
class UNetT(nn.Module): def __init__( self, *, dim, depth=8, heads=8, dim_head=64, dropout=0.1, ff_mult=4, mel_dim=100, text_num_embeds=256, text_dim=None, text_mask_padding=True, qk_norm=None, conv_layers=0, pe_attn_head=None, skip_connect_type: Literal["add", "concat", "none"] = "concat", ): super().__init__() assert depth % 2 == 0, "UNet-Transformer's depth should be even."
self.time_embed = TimestepEmbedding(dim) if text_dim is None: text_dim = mel_dim self.text_embed = TextEmbedding( text_num_embeds, text_dim, mask_padding=text_mask_padding, conv_layers=conv_layers ) self.text_cond, self.text_uncond = None, None # text cache self.input_embed = InputEmbedding(mel_dim, text_dim, dim)
self.rotary_embed = RotaryEmbedding(dim_head)
# transformer layers & skip connections
self.dim = dim self.skip_connect_type = skip_connect_type needs_skip_proj = skip_connect_type == "concat"
self.depth = depth self.layers = nn.ModuleList([])
for idx in range(depth): is_later_half = idx >= (depth // 2)
attn_norm = RMSNorm(dim) attn = Attention( processor=AttnProcessor(pe_attn_head=pe_attn_head), dim=dim, heads=heads, dim_head=dim_head, dropout=dropout, qk_norm=qk_norm, )
ff_norm = RMSNorm(dim) ff = FeedForward(dim=dim, mult=ff_mult, dropout=dropout, approximate="tanh")
skip_proj = nn.Linear(dim * 2, dim, bias=False) if needs_skip_proj and is_later_half else None
self.layers.append( nn.ModuleList( [ skip_proj, attn_norm, attn, ff_norm, ff, ] ) )
self.norm_out = RMSNorm(dim) self.proj_out = nn.Linear(dim, mel_dim)
def clear_cache(self): self.text_cond, self.text_uncond = None, None
def forward( self, x: float["b n d"], # nosied input audio # noqa: F722 cond: float["b n d"], # masked cond audio # noqa: F722 text: int["b nt"], # text # noqa: F722 time: float["b"] | float[""], # time step # noqa: F821 F722 drop_audio_cond, # cfg for cond audio drop_text, # cfg for text mask: bool["b n"] | None = None, # noqa: F722 cache=False, ): batch, seq_len = x.shape[0], x.shape[1] if time.ndim == 0: time = time.repeat(batch)
# t: conditioning time, c: context (text + masked cond audio), x: noised input audio t = self.time_embed(time) if cache: if drop_text: if self.text_uncond is None: self.text_uncond = self.text_embed(text, seq_len, drop_text=True) text_embed = self.text_uncond else: if self.text_cond is None: self.text_cond = self.text_embed(text, seq_len, drop_text=False) text_embed = self.text_cond else: text_embed = self.text_embed(text, seq_len, drop_text=drop_text) x = self.input_embed(x, cond, text_embed, drop_audio_cond=drop_audio_cond)
# postfix time t to input x, [b n d] -> [b n+1 d] x = torch.cat([t.unsqueeze(1), x], dim=1) # pack t to x if mask is not None: mask = F.pad(mask, (1, 0), value=1)
rope = self.rotary_embed.forward_from_seq_len(seq_len + 1)
# flat unet transformer skip_connect_type = self.skip_connect_type skips = [] for idx, (maybe_skip_proj, attn_norm, attn, ff_norm, ff) in enumerate(self.layers): layer = idx + 1
# skip connection logic is_first_half = layer <= (self.depth // 2) is_later_half = not is_first_half
if is_first_half: skips.append(x)
if is_later_half: skip = skips.pop() if skip_connect_type == "concat": x = torch.cat((x, skip), dim=-1) x = maybe_skip_proj(x) elif skip_connect_type == "add": x = x + skip
# attention and feedforward blocks x = attn(attn_norm(x), rope=rope, mask=mask) + x x = ff(ff_norm(x)) + x
assert len(skips) == 0
x = self.norm_out(x)[:, 1:, :] # unpack t from x
return self.proj_out(x)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899"""ein notation:b - batchn - sequencent - text sequencenw - raw wave lengthd - dimension"""
from __future__ import annotations
from random import randomimport random as _randomfrom typing import Callable, Dict, OrderedDictimport mathfrom pathlib import Path
import torchimport torch.nn.functional as Fimport torchaudiofrom torch import nnfrom torch.nn.utils.rnn import pad_sequencefrom torchdiffeq import odeint
from lemas_tts.model.modules import MelSpecfrom lemas_tts.model.modules import MIEsitmator, AccentClassifier, grad_reversefrom lemas_tts.model.backbones.ecapa_tdnn import ECAPA_TDNNfrom lemas_tts.model.backbones.prosody_encoder import ProsodyEncoder, extract_fbank_16kfrom lemas_tts.model.utils import ( default, exists, lens_to_mask, list_str_to_idx, list_str_to_tensor, mask_from_frac_lengths,)
def clip_and_shuffle(mel, mel_len, sample_rate=24000, hop_length=256, ratio=None): """ Randomly clip a mel-spectrogram segment and shuffle 1-second chunks to create an accent-invariant conditioning segment.
This is a inference-time utility used by the accent GRL path.
Args: mel: [n_mels, T] mel_len: int, original mel length (T) """ frames_per_second = int(sample_rate / hop_length) # ≈ 94 frames / second
# ---- 1. Randomly crop 25%~75% of the original length (or ratio * length) ---- total_len = mel_len if not ratio: seg_len = _random.randint(int(0.25 * total_len), int(0.75 * total_len)) else: seg_len = int(total_len * ratio) start = _random.randint(0, max(0, total_len - seg_len)) mel_seg = mel[:, start : start + seg_len]
# ---- 2. Split into ~1-second chunks ---- n_chunks = (mel_seg.size(1) + frames_per_second - 1) // frames_per_second chunks = [] for i in range(n_chunks): chunk = mel_seg[:, i * frames_per_second : (i + 1) * frames_per_second] chunks.append(chunk)
# ---- 3. Shuffle chunk order ---- _random.shuffle(chunks) shuffled_mel = torch.cat(chunks, dim=1)
# ---- 4. Repeat random chunks until reaching original length ---- if shuffled_mel.size(1) < total_len: repeat_chunks = [] while sum(c.size(1) for c in repeat_chunks) < total_len: repeat_chunks.append(_random.choice(chunks)) shuffled_mel = torch.cat([shuffled_mel] + repeat_chunks, dim=1)
# ---- 5. Trim to exactly mel_len ---- shuffled_mel = shuffled_mel[:, :total_len] assert shuffled_mel.shape == mel.shape, f"shuffled_mel.shape != mel.shape: {shuffled_mel.shape} != {mel.shape}"
return shuffled_mel
class CFM(nn.Module): def __init__( self, transformer: nn.Module, sigma=0.0, odeint_kwargs: dict = dict( # atol = 1e-5, # rtol = 1e-5, method="euler" # 'midpoint' ), audio_drop_prob=0.3, text_drop_prob=0.1, num_channels=None, mel_spec_module: nn.Module | None = None, mel_spec_kwargs: dict = dict(), frac_lengths_mask: tuple[float, float] = (0.7, 1.0), vocab_char_map: dict[str:int] | None = None, use_ctc_loss: bool = False, use_spk_enc: bool = False, use_prosody_encoder: bool = False, prosody_cfg_path: str | None = None, prosody_ckpt_path: str | None = None, ): super().__init__()
self.frac_lengths_mask = frac_lengths_mask
# mel spec self.mel_spec = default(mel_spec_module, MelSpec(**mel_spec_kwargs)) num_channels = default(num_channels, self.mel_spec.n_mel_channels) self.num_channels = num_channels
# classifier-free guidance self.audio_drop_prob = audio_drop_prob self.text_drop_prob = text_drop_prob
# transformer self.transformer = transformer dim = transformer.dim self.dim = dim
# conditional flow related self.sigma = sigma
# sampling related self.odeint_kwargs = odeint_kwargs
# vocab map for tokenization self.vocab_char_map = vocab_char_map
# Prosody encoder (Pretssel ECAPA-TDNN) self.use_prosody_encoder = ( use_prosody_encoder and prosody_cfg_path is not None and prosody_ckpt_path is not None ) if self.use_prosody_encoder: cfg_path = Path(prosody_cfg_path) ckpt_path = Path(prosody_ckpt_path) self.prosody_encoder = ProsodyEncoder(cfg_path, ckpt_path, freeze=True) # 512-d prosody -> mel channel dimension self.prosody_to_mel = nn.Linear(512, self.num_channels) self.prosody_dropout = nn.Dropout(p=0.2) else: self.prosody_encoder = None # Speaker encoder self.use_spk_enc = use_spk_enc if use_spk_enc: self.speaker_encoder = ECAPA_TDNN( self.num_channels, self.dim, channels=[512, 512, 512, 512, 1536], kernel_sizes=[5, 3, 3, 3, 1], dilations=[1, 2, 3, 4, 1], attention_channels=128, res2net_scale=4, se_channels=128, global_context=True, batch_norm=True, ) # self.load_partial_weights(self.speaker_encoder, "/cto_labs/vistring/zhaozhiyuan/outputs/F5-TTS/pretrain/speaker.bin", device="cpu")
self.use_ctc_loss = use_ctc_loss if use_ctc_loss: # print("vocab_char_map:", len(vocab_char_map)+1, "dim:", dim, "mel_spec_kwargs:",mel_spec_kwargs) self.ctc = MIEsitmator(len(self.vocab_char_map), self.num_channels, self.dim, dropout=self.text_drop_prob)
self.accent_classifier = AccentClassifier(input_dim=self.num_channels, hidden_dim=self.dim, num_accents=12) self.accent_criterion = nn.CrossEntropyLoss()
def load_partial_weights(self, model: nn.Module, ckpt_path: str, device="cpu", verbose=True) -> int: """ 仅加载形状匹配的参数,其余跳过。 返回成功加载的参数数量。 """ state_dict = torch.load(ckpt_path, map_location=device) model_dict = model.state_dict()
ok_count = 0 new_dict: OrderedDict[str, torch.Tensor] = OrderedDict()
for k, v in state_dict.items(): if k in model_dict and v.shape == model_dict[k].shape: new_dict[k] = v ok_count += 1 else: if verbose: print(f"[SKIP] {k} ckpt:{v.shape} model:{model_dict[k].shape if k in model_dict else 'N/A'}")
model_dict.update(new_dict) model.load_state_dict(model_dict) if verbose: print(f"=> 成功加载 {ok_count}/{len(state_dict)} 个参数") return ok_count
@property def device(self): return next(self.parameters()).device
@torch.no_grad() def sample( self, cond: float["b n d"] | float["b nw"], # noqa: F722 text: int["b nt"] | list[str], # noqa: F722 duration: int | int["b"], # noqa: F821 *, lens: int["b"] | None = None, # noqa: F821 steps=32, cfg_strength=1.0, sway_sampling_coef=None, seed: int | None = None, max_duration=4096, vocoder: Callable[[float["b d n"]], float["b nw"]] | None = None, # noqa: F722 no_ref_audio=False, duplicate_test=False, t_inter=0.1, edit_mask=None, use_acc_grl = True, use_prosody_encoder = True, ref_ratio = 1, ): self.eval()
# raw wave -> mel, keep a copy for prosody encoder if available raw_audio = None if cond.ndim == 2: raw_audio = cond.clone() # (B, nw) cond = self.mel_spec(cond) cond = cond.permute(0, 2, 1) assert cond.shape[-1] == self.num_channels
cond = cond.to(next(self.parameters()).dtype) cond_mean = cond.mean(dim=1, keepdim=True) batch, cond_seq_len, device = *cond.shape[:2], cond.device if not exists(lens): lens = torch.full((batch,), cond_seq_len, device=device, dtype=torch.long)
# optional global prosody conditioning at inference (one embedding per sample) prosody_mel_cond = None prosody_text_cond = None prosody_embeds = None if self.prosody_encoder is not None and raw_audio is not None and use_prosody_encoder: embeds = [] for b in range(batch): audio_b = raw_audio[b].unsqueeze(0) # (1, nw) src_sr = self.mel_spec.target_sample_rate if src_sr != 16_000: audio_16k = torchaudio.functional.resample( audio_b, src_sr, 16_000 ).squeeze(0) else: audio_16k = audio_b.squeeze(0) fbank = extract_fbank_16k(audio_16k) fbank = fbank.unsqueeze(0).to(device=device, dtype=cond.dtype) emb = self.prosody_encoder(fbank, padding_mask=None)[0] # (512,) embeds.append(emb) prosody_embeds = torch.stack(embeds, dim=0) # (B, 512) # broadcast along mel and text prosody_mel_cond = prosody_embeds[:, None, :].expand(-1, cond_seq_len, -1)
if use_acc_grl: # rand_mel = clip_and_shuffle(cond.permute(0, 2, 1).squeeze(0), cond.shape[1]) # rand_mel = rand_mel.unsqueeze(0).permute(0, 2, 1) # assert rand_mel.shape == cond.shape, f"Shape diff: rand_mel.shape: {rand_mel.shape}, cond.shape: {cond.shape}" # cond_grl = grad_reverse(rand_mel, lambda_=1.0)
if ref_ratio < 1: rand_mel = clip_and_shuffle(cond.permute(0, 2, 1).squeeze(0), cond.shape[1], ratio=ref_ratio) rand_mel = rand_mel.unsqueeze(0).permute(0, 2, 1) assert rand_mel.shape == cond.shape, f"Shape diff: rand_mel.shape: {rand_mel.shape}, cond.shape: {cond.shape}" cond_grl = grad_reverse(rand_mel, lambda_=1.0) else: cond_grl = grad_reverse(cond, lambda_=1.0) # print("cond:", cond.shape, cond.mean(), cond.max(), cond.min(), "rand_mel:", rand_mel.mean(), rand_mel.max(), rand_mel.min(), "cond_grl:", cond_grl.mean(), cond_grl.max(), cond_grl.min())
# text
if isinstance(text, list): if exists(self.vocab_char_map): text = list_str_to_idx(text, self.vocab_char_map).to(device) else: text = list_str_to_tensor(text).to(device) assert text.shape[0] == batch
# duration
cond_mask = lens_to_mask(lens) if edit_mask is not None: cond_mask = cond_mask & edit_mask
if isinstance(duration, int): duration = torch.full((batch,), duration, device=device, dtype=torch.long)
duration = torch.maximum( torch.maximum((text != -1).sum(dim=-1), lens) + 1, duration ) # duration at least text/audio prompt length plus one token, so something is generated # clamp and convert max_duration to python int for padding ops duration = duration.clamp(max=max_duration) max_duration = int(duration.amax().item())
# duplicate test corner for inner time step oberservation if duplicate_test: test_cond = F.pad(cond, (0, 0, cond_seq_len, max_duration - 2 * cond_seq_len), value=0.0)
cond = F.pad(cond, (0, 0, 0, max_duration - cond_seq_len), value=0.0)
if prosody_mel_cond is not None: prosody_mel_cond = F.pad( prosody_mel_cond, (0, 0, 0, max_duration - cond_seq_len), value=0.0 ) prosody_mel_proj = self.prosody_to_mel(prosody_mel_cond) cond = cond + prosody_mel_proj
if no_ref_audio: random_cond = torch.randn_like(cond) * 0.1 + cond_mean random_cond = random_cond / random_cond.mean(dim=1, keepdim=True) * cond_mean print("cond:", cond.mean(), cond.max(), cond.min(), "random_cond:", random_cond.mean(), random_cond.max(), random_cond.min(), "mean_cond:", cond_mean.shape) cond = random_cond cond_mask = F.pad(cond_mask, (0, max_duration - cond_mask.shape[-1]), value=False) cond_mask = cond_mask.unsqueeze(-1)
if use_acc_grl: cond_grl = F.pad(cond_grl, (0, 0, 0, max_duration - cond_seq_len), value=0.0)
step_cond = torch.where(cond_mask, cond, torch.zeros_like(cond)) # allow direct control (cut cond audio) with lens passed in
if batch > 1: mask = lens_to_mask(duration) else: # save memory and speed up, as single inference need no mask currently mask = None
# neural ode
def compute_sway_max(steps: int, t_start: float = 0.0, dtype=torch.float32, min_ratio: float | None = None, safety_factor: float = 0.5) -> float: """ Compute a safe upper bound for sway_sampling_coef given steps and t_start.
- steps: number of ODE steps - t_start: start time in [0,1) - dtype: torch dtype (for machine eps) - min_ratio: smallest distinguishable dt^p (if None, use conservative default) - safety_factor: scale down the theoretical maximum to be safe """ assert 0.0 <= t_start < 1.0 dt = (1.0 - t_start) / max(1, steps) eps = torch.finfo(dtype).eps
if min_ratio is None: # conservative default: ~100 * eps (float32 -> ~1e-5) min_ratio = max(1e-9, 1e2 * float(eps))
if dt >= 0.9: p_max = 1.0 + 10.0 else: # solve dt^p >= min_ratio => p <= log(min_ratio)/log(dt) p_max = math.log(min_ratio) / math.log(dt)
sway_max = max(0.0, p_max - 1.0) sway_max = sway_max * float(safety_factor) return torch.tensor(sway_max, device=device, dtype=dtype)
# prepare text-side prosody conditioning if embeddings available if prosody_embeds is not None: text_len = text.shape[1] prosody_text_cond = prosody_embeds[:, None, :].expand(-1, text_len, -1) else: prosody_text_cond = None
def fn(t, x): # at each step, conditioning is fixed # if use_spk_enc: # mix_cond = t * cond + (1-t) * spk_emb # step_cond = torch.where(cond_mask, mix_cond, torch.zeros_like(mix_cond)) if use_acc_grl: step_cond = torch.where(cond_mask, cond_grl, torch.zeros_like(cond_grl)) else: step_cond = torch.where(cond_mask, cond, torch.zeros_like(cond)) # predict flow pred = self.transformer( x=x, cond=step_cond, text=text, time=t, mask=mask, drop_audio_cond=False, drop_text=False, cache=True, prosody_text=prosody_text_cond, ) if cfg_strength < 1e-5: return pred
null_pred = self.transformer( x=x, cond=step_cond, text=text, time=t, mask=mask, drop_audio_cond=True, drop_text=True, cache=True, prosody_text=prosody_text_cond, ) # cfg_t = cfg_strength * torch.cos(0.5 * torch.pi * t) # cfg_t = cfg_strength * (1 - t) cfg_t = cfg_strength * ((1 - t) ** 2) # print("t:", t, "cfg_t:", cfg_t) res = pred + (pred - null_pred) * cfg_t # print("t:", t.item(), "\tres:", res.shape, res.mean().item(), res.max().item(), res.min().item(), "\tpred:", pred.mean().item(), pred.max().item(), pred.min().item(), "\tnull_pred:", null_pred.mean().item(), null_pred.max().item(), null_pred.min().item(), "\tcfg_t:", cfg_t.item()) res = res.clamp(-20, 20) return res
# noise input # to make sure batch inference result is same with different batch size, and for sure single inference # still some difference maybe due to convolutional layers y0 = [] for dur in duration: if exists(seed): torch.manual_seed(seed) y0.append(torch.randn(dur, self.num_channels, device=self.device, dtype=step_cond.dtype)) y0 = pad_sequence(y0, padding_value=0, batch_first=True)
t_start = 0
# duplicate test corner for inner time step oberservation if duplicate_test: t_start = t_inter y0 = (1 - t_start) * y0 + t_start * test_cond steps = int(steps * (1 - t_start))
t = torch.linspace(t_start, 1, int(steps + 1), device=self.device, dtype=step_cond.dtype)
sway_max = compute_sway_max(steps, t_start=t_start, dtype=step_cond.dtype, min_ratio=1e-9, safety_factor=0.7) if sway_sampling_coef is not None: sway_sampling_coef = min(sway_max, sway_sampling_coef) # t = t + sway_sampling_coef * (torch.cos(torch.pi / 2 * t) - 1 + t) t = t ** (1 + sway_sampling_coef) else: t = t ** (1 + sway_max) # print("t:",t, "sway_max:", sway_max, "sway_sampling_coef:", sway_sampling_coef) trajectory = odeint(fn, y0, t, **self.odeint_kwargs) self.transformer.clear_cache()
sampled = trajectory[-1] out = sampled out = torch.where(cond_mask, cond, out)
# out生成的部分,或者说pad补0的部分,单独计算mean, 然后和cond的mean做对齐(乘以系数,两个的均值要差不多) if no_ref_audio: out_mean = out[:,cond_seq_len:,:].mean(dim=1, keepdim=True) out[:,cond_seq_len:,:] = out[:,cond_seq_len:,:] - (out_mean - cond_mean) # print("out_mean:", out_mean.shape, out_mean.mean(), "cond_mean:", cond_mean.shape, cond_mean.mean(), "out:", out[:,cond_seq_len:,:].shape, out[:,cond_seq_len:,:].mean().item(), out[:,cond_seq_len:,:].max().item(), out[:,cond_seq_len:,:].min().item())
if exists(vocoder): out = out.permute(0, 2, 1) out = vocoder(out) # print("out:", out.shape, "trajectory:", trajectory.shape) return out, trajectory
def info_nce_speaker(self, e_gt: torch.Tensor, e_pred: torch.Tensor, temperature: float = 0.1): """ InfoNCE loss for speaker encoder training. 同一条样本的 e_gt 与 e_pred 互为正例,其余均为负例。
Args: temperature: 温度缩放 τ
Returns: loss: 标量 tensor,可 backward """ B = e_gt.size(0) # 2. L2 归一化 e_gt = F.normalize(e_gt, dim=1) e_pred = F.normalize(e_pred, dim=1)
# 3. 计算 B×B 相似度矩阵(pred 对 gt) logits = torch.einsum('bd,cd->bc', e_pred, e_gt) / temperature # [B, B]
# 4. 正例标签正好是对角线 labels = torch.arange(B, device=logits.device)
# 5. InfoNCE = cross-entropy over in-batch negatives loss = F.cross_entropy(logits, labels) return loss
def forward_old( self, batchs: Dict[str, torch.Tensor], # inp: float["b n d"] | float["b nw"], # mel or raw wave # noqa: F722 # text: int["b nt"] | list[str], # noqa: F722 *, # lens: int["b"] | None = None, # noqa: F821 noise_scheduler: str | None = None, ):
inp = batchs["mel"].permute(0, 2, 1) lens = batchs["mel_lengths"]
rand_mel = batchs["rand_mel"].permute(0, 2, 1)
text = batchs["text"] target_text_lengths = torch.tensor([len(x) for x in text], device=inp.device)
langs = batchs["langs"]
# print("inp:", inp.shape, "rand_mel:", rand_mel.shape, "lens:", lens, "target_text_lengths:", target_text_lengths, "langs:", langs)
# handle raw wave if inp.ndim == 2: inp = self.mel_spec(inp) inp = inp.permute(0, 2, 1) assert inp.shape[-1] == self.num_channels
batch, seq_len, dtype, device, _σ1 = *inp.shape[:2], inp.dtype, self.device, self.sigma # print("inp_shape:", inp.shape, inp.max(), inp.min(), "dtype:", dtype, "device:", device, "σ1:", _σ1)
# handle text as string if isinstance(text, list): if exists(self.vocab_char_map): text = list_str_to_idx(text, self.vocab_char_map).to(device) else: text = list_str_to_tensor(text).to(device) assert text.shape[0] == batch
# lens and mask if not exists(lens): lens = torch.full((batch,), seq_len, device=device)
mask = lens_to_mask(lens, length=seq_len) # useless here, as collate_fn will pad to max length in batch
# get a random span to mask out for training conditionally frac_lengths = torch.zeros((batch,), device=self.device).float().uniform_(*self.frac_lengths_mask) rand_span_mask = mask_from_frac_lengths(lens, frac_lengths)
if exists(mask): rand_span_mask &= mask
# mel is x1 x1 = inp
# x0 is gaussian noise x0 = torch.randn_like(x1)
# time step time = torch.rand((batch,), dtype=dtype, device=self.device) # TODO. noise_scheduler
# sample xt (φ_t(x) in the paper) t = time.unsqueeze(-1).unsqueeze(-1) φ = (1 - t) * x0 + t * x1 flow = x1 - x0
# cond = torch.where(rand_span_mask[..., None], torch.zeros_like(rand_mel), rand_mel) cond = torch.where(rand_span_mask[..., None], torch.zeros_like(x1), x1)
# print("seq_len:", seq_len, "lens:", lens) if self.use_spk_enc: # 50%的概率使用spk_emb spk_emb = self.speaker_encoder(rand_mel, lens) # global_emb: [batch, 1, dim] -> 重复扩展到 [batch, seq_len, dim] spk_emb = spk_emb.unsqueeze(1).expand_as(x1) # print("spk_emb_shape:", spk_emb.shape) # 应用mask操作 cond = torch.where(rand_span_mask[..., None], torch.zeros_like(spk_emb), spk_emb) # assert cond.shape[0] == batch, "speaker encoder batch size mismatch" # print("x1.shape:", x1.shape, "cond_shape:", cond.shape) # 给一个随机数,把spk_emb * 随机数,再加上原来的cond *(1-随机数) rand_num = torch.rand((batch, 1, 1), dtype=dtype, device=self.device) cond = cond * rand_num + spk_emb * (1 - rand_num) cond_grl = grad_reverse(cond, lambda_=1.0) # print("inp_shape:", inp.shape, "rand_span_mask:", rand_span_mask.shape)
# # # transformer and cfg training with a drop rate # drop_audio_cond = random() < self.audio_drop_prob # p_drop in voicebox paper # drop_text_cond = random() < self.text_drop_prob # p_drop in voicebox paper drop_audio_cond = random() < self.audio_drop_prob # p_drop in voicebox paper if random() < self.text_drop_prob: # p_uncond in voicebox paper drop_audio_cond = True drop_text_cond = True else: drop_text_cond = False
# print("drop_audio_cond:", drop_audio_cond, "drop_text_cond:", drop_text_cond) # if want rigorously mask out padding, record in collate_fn in dataset.py, and pass in here # adding mask will use more memory, thus also need to adjust batchsampler with scaled down threshold for long sequences pred = self.transformer(x=φ, cond=cond_grl, text=text, time=time, drop_audio_cond=drop_audio_cond, drop_text=drop_text_cond)
# flow matching loss pred_clamp = pred.float().clamp(-20, 20) loss = F.mse_loss(pred_clamp, flow, reduction="none") loss = loss[rand_span_mask] # [N]
# # # 1. 全局截断:>2 或 NaN → 0(全局) # print("mse loss shape:", loss.shape, "loss max:", loss.max(), "loss min:", loss.min(), target_text_lengths[0])
# # 2. 统计非NaN值的百分比 # valid_mask = ~torch.isnan(loss) # total_count = loss.numel() # 总元素数量(所有维度) # valid_count = valid_mask.sum().item() # 非NaN元素数量 # valid_percentage = (valid_count / total_count) * 100 # print(f"mse loss: total_count: {total_count}", f"valid_count: {valid_count}", f"valid_percentage: {valid_percentage:.2f}%")
# valid_loss = loss[~torch.isnan(loss)] loss = torch.where(torch.isnan(loss) | (loss > 300.0), 300.0, loss) loss = loss.mean() # loss = torch.tanh(torch.log1p(loss.mean())) # 对数缩放 # if len(valid_loss) > 0: # clipped_loss = torch.clamp(valid_loss, max=150) # loss = torch.tanh(torch.log1p(clipped_loss.mean())) # 对数缩放 # else: # loss = torch.tensor(0.0, device=pred.device)
accent_logits = self.accent_classifier(cond_grl) accent_logits_mean = accent_logits.mean(dim=1) lang_labels = langs.to(accent_logits.device).long() # print("langs:", lang_labels, "accent_logits:", accent_logits.shape, "accent_logits_mean:", accent_logits_mean.shape) accent_loss = self.accent_criterion(accent_logits_mean, lang_labels) # guard against NaN / Inf in accent_loss if not torch.isfinite(accent_loss): accent_loss = torch.zeros_like(accent_loss, device=accent_loss.device) # accent_loss = torch.zeros_like(loss, device=loss.device, requires_grad=True) loss += 0.1 * accent_loss
valid_indices = torch.where(time > 0.5)[0] # print("torch.where(time > 0.5):", valid_indices, torch.where(time > 0.5)) if valid_indices.size(0) > 2: # 动态选择符合条件的sample selected_gt = inp[valid_indices] selected_pred = pred[valid_indices] selected_text = text[valid_indices] selected_lens = lens[valid_indices] selected_target_lengths = target_text_lengths[valid_indices] # print("pred:", selected_pred.shape, "valid_indices:", valid_indices, "lens:", selected_lens, "target_lengths:", selected_target_lengths)
if self.use_spk_enc and valid_indices.size(0) > 2: # speaker encoder loss e_gt = self.speaker_encoder(selected_gt, selected_lens) e_pred = self.speaker_encoder(selected_pred, selected_lens) spk_loss = self.info_nce_speaker(e_gt, e_pred) if not torch.isnan(spk_loss).any(): # and spk_loss.item() > 1e-6: loss = loss + spk_loss * 10.0 else: spk_loss = torch.zeros_like(loss, device=loss.device, requires_grad=False) else: spk_loss = torch.zeros_like(loss, device=loss.device, requires_grad=False) # print("spk_loss:", spk_loss)
# ctc loss if self.use_ctc_loss and valid_indices.size(0) > 2: # 如果t大于0.5 则计算ctc loss ctc_loss = self.ctc( decoder_outputs=selected_pred, target_phones=selected_text, decoder_lengths=selected_lens, target_lengths=selected_target_lengths, ) # print("loss:", loss, "ctc_loss:", ctc_loss, "time: ", time.shape, time[valid_indices].mean()) # 如果ctc loss没有nan,才加上ctc loss if not torch.isnan(ctc_loss).any() and ctc_loss.item() > 1e-6: # ctc_scaled = torch.tanh(torch.log1p(ctc_loss)) ctc_scaled = ctc_loss loss = loss + 0.1 * ctc_scaled else: ctc_scaled = torch.zeros_like(loss, device=loss.device, requires_grad=False) # print("loss:", loss, "ctc_scaled:", ctc_scaled) else: ctc_scaled = torch.zeros_like(loss, device=loss.device, requires_grad=False)
# 在计算完 total loss 之前 total_loss = loss # base flow loss + others you added # note: we intentionally do NOT add 0.0 * pred.sum() etc. here, to avoid # propagating NaNs from intermediate tensors into the loss scalar.
return total_loss, ctc_scaled, accent_loss, len(valid_indices), cond, pred # accent_loss,
def forward(self, batchs: Dict[str, torch.Tensor], *, noise_scheduler: str | None = None): """ Simplified forward version for accent-invariant flow matching. Removes speaker encoder and CTC parts, keeps accent GRL. """ inp = batchs["mel"].permute(0, 2, 1) # [B, T_mel, D] lens = batchs["mel_lengths"] text = batchs["text"] langs = batchs["langs"] audio_16k_list = batchs.get("audio_16k", None) prosody_idx_list = batchs.get("prosody_idx", None)
# # ---- 4. 随机截取并打乱 segment ---- # rand_mel = [clip_and_shuffle(spec, spec.shape[-1]) for spec in batchs["mel"]] # padded_rand_mel = [] # for spec in rand_mel: # padding = (0, batchs["mel"].shape[-1] - spec.size(-1)) # padded_spec = F.pad(spec, padding, value=0) # padded_rand_mel.append(padded_spec) # rand_mel = torch.stack(padded_rand_mel).permute(0, 2, 1) # assert rand_mel.shape == inp.shape, f"shape diff: rand_mel.shape: {rand_mel.shape}, inp.shape: {inp.shape}"
if inp.ndim == 2: inp = self.mel_spec(inp).permute(0, 2, 1) assert inp.shape[-1] == self.num_channels
batch, seq_len, dtype, device = *inp.shape[:2], inp.dtype, self.device
# --- handle text if isinstance(text, list): if exists(self.vocab_char_map): text = list_str_to_idx(text, self.vocab_char_map).to(device) else: text = list_str_to_tensor(text).to(device) assert text.shape[0] == batch # print("text:", batchs["text"][0], text.shape, text[0], batchs["text_lengths"][0]) # --- prosody conditioning (compute embeddings per sub-utterance) prosody_mel_cond = None prosody_text_cond = None if ( self.prosody_encoder is not None and audio_16k_list is not None and prosody_idx_list is not None ): # prepare zero tensors for each sample T_mel = seq_len T_text = text.shape[1] prosody_mel_cond = torch.zeros(batch, T_mel, 512, device=device, dtype=dtype) prosody_text_cond = torch.zeros(batch, T_text, 512, device=device, dtype=dtype)
# collect all segments, run encoder per segment seg_embeds: list[Tensor] = [] seg_meta: list[tuple[int, int, int, int, int, int]] = [] for b in range(batch): audio_b = audio_16k_list[b] idx_list = prosody_idx_list[b] if audio_b is None or idx_list is None: continue audio_b = audio_b.to(device=device, dtype=dtype) for seg in idx_list: text_start, text_end, mel_start, mel_end, audio_start, audio_end = seg # clamp audio indices audio_start = max(0, min(audio_start, audio_b.shape[0] - 1)) audio_end = max(audio_start + 1, min(audio_end, audio_b.shape[0])) audio_seg = audio_b[audio_start:audio_end] if audio_seg.numel() == 0: continue fbank = extract_fbank_16k(audio_seg) # (T_fbank, 80) fbank = fbank.unsqueeze(0).to(device=device, dtype=dtype) # (1, T_fbank, 80) with torch.no_grad(): emb = self.prosody_encoder(fbank, padding_mask=None)[0] # (512,) seg_embeds.append(emb) seg_meta.append( (b, text_start, text_end, mel_start, mel_end) )
if seg_embeds: seg_embeds_tensor = torch.stack(seg_embeds, dim=0) # (N_seg, 512) # scatter embeddings back to per-sample tensors for emb, meta in zip(seg_embeds_tensor, seg_meta): b, ts, te, ms, me = meta emb_exp = emb.to(device=device, dtype=dtype) prosody_mel_cond[b, ms:me, :] = emb_exp prosody_text_cond[b, ts:te, :] = emb_exp
# dropout on prosody conditioning prosody_mel_cond = self.prosody_dropout(prosody_mel_cond) prosody_text_cond = self.prosody_dropout(prosody_text_cond)
# --- mask & random span mask = lens_to_mask(lens, length=seq_len) frac_lengths = torch.zeros((batch,), device=device).float().uniform_(*self.frac_lengths_mask) rand_span_mask = mask_from_frac_lengths(lens, frac_lengths) if exists(mask): rand_span_mask &= mask
# --- flow setup x1 = inp x0 = torch.randn_like(x1) time = torch.rand((batch,), dtype=dtype, device=device) t = time[:, None, None] φ = (1 - t) * x0 + t * x1 flow = x1 - x0
# --- conditional input (masked mel) + optional prosody cond = torch.where(rand_span_mask[..., None], torch.zeros_like(x1), x1) # x1 # rand_mel if prosody_mel_cond is not None: prosody_mel_proj = self.prosody_to_mel(prosody_mel_cond) # (B, T_mel, num_channels) # if needed, pad/crop to seq_len if prosody_mel_proj.size(1) < seq_len: pad_len = seq_len - prosody_mel_proj.size(1) prosody_mel_proj = F.pad(prosody_mel_proj, (0, 0, 0, pad_len)) elif prosody_mel_proj.size(1) > seq_len: prosody_mel_proj = prosody_mel_proj[:, :seq_len, :] cond = cond + prosody_mel_proj # --- Gradient reversal: encourage accent-invariant cond cond_grl = grad_reverse(cond, lambda_=1.0)
# # --- random drop condition for CFG-like robustness # drop_audio_cond = random() < self.audio_drop_prob # drop_text_cond = random() < self.text_drop_prob if not drop_audio_cond else True
# safe per-batch random (tensor) rand_for_drop = torch.rand(1, device=device) drop_audio_cond = (rand_for_drop.item() < self.audio_drop_prob) rand_for_text = torch.rand(1, device=device) drop_text_cond = (rand_for_text.item() < self.text_drop_prob)
# --- main prediction pred = self.transformer( x=φ, cond=cond_grl, text=text, time=time, drop_audio_cond=drop_audio_cond, drop_text=drop_text_cond, prosody_text=prosody_text_cond, )
# === FLOW LOSS (robust mask-weighted) === pred_clamp = pred.float().clamp(-20, 20) per_elem_loss = F.mse_loss(pred_clamp, flow, reduction="none") # [B, T, D]
mask_exp = rand_span_mask.unsqueeze(-1).to(dtype=per_elem_loss.dtype) # [B, T, 1] masked_loss = per_elem_loss * mask_exp # zeros where mask False
# total selected scalar (frames * dim) n_selected = mask_exp.sum() * per_elem_loss.size(-1) # scalar denom = torch.clamp(n_selected, min=1.0)
loss_sum = masked_loss.sum() loss = loss_sum / denom # numeric safety loss = torch.where(torch.isnan(loss) | (loss > 300.0), torch.tensor(300.0, device=loss.device, dtype=loss.dtype), loss)
# === ACCENT LOSS === accent_logits = self.accent_classifier(cond_grl) # pool across time -> [B, C] accent_logits_mean = accent_logits.mean(dim=1) lang_labels = langs.to(accent_logits_mean.device).long() accent_loss = self.accent_criterion(accent_logits_mean, lang_labels) # guard against NaN / Inf in accent_loss if not torch.isfinite(accent_loss): accent_loss = torch.zeros_like(accent_loss, device=accent_loss.device)
base_loss = loss + 0.1 * accent_loss
# === OPTIONAL CTC LOSS (robust, only on valid samples) === ctc_scaled = torch.tensor(0.0, device=device, dtype=dtype) if getattr(self, "use_ctc_loss", False) and getattr(self, "ctc", None) is not None: # select samples with larger t for CTC supervision (similar to forward_old) valid_indices = torch.where(time > 0.5)[0] if valid_indices.size(0) > 2: selected_pred = pred[valid_indices] selected_text = text[valid_indices] selected_lens = lens[valid_indices] # text was tokenized from list_str_to_idx, where padding is -1 selected_target_lengths = (selected_text != -1).sum(dim=-1)
ctc_loss = self.ctc( decoder_outputs=selected_pred, target_phones=selected_text, decoder_lengths=selected_lens, target_lengths=selected_target_lengths, ) if torch.isfinite(ctc_loss) and ctc_loss.item() > 1e-6: ctc_scaled = ctc_loss base_loss = base_loss + 0.1 * ctc_scaled
total_loss = base_loss
# note: we intentionally do NOT add 0.0 * pred.sum() etc. here, to avoid # propagating NaNs from intermediate tensors into the loss scalar.
return total_loss, accent_loss, ctc_scaled, cond, pred123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802"""ein notation:b - batchn - sequencent - text sequencenw - raw wave lengthd - dimension"""
from __future__ import annotations
import mathfrom typing import Optional
import torchimport torch.nn.functional as Fimport torchaudiofrom librosa.filters import mel as librosa_mel_fnfrom torch import nnfrom x_transformers.x_transformers import apply_rotary_pos_embfrom torch.autograd import Function
# raw wav to mel spec
mel_basis_cache = {}hann_window_cache = {}
def get_bigvgan_mel_spectrogram( waveform, n_fft=1024, n_mel_channels=100, target_sample_rate=24000, hop_length=256, win_length=1024, fmin=0, fmax=None, center=False,): # Copy from https://github.com/NVIDIA/BigVGAN/tree/main device = waveform.device key = f"{n_fft}_{n_mel_channels}_{target_sample_rate}_{hop_length}_{win_length}_{fmin}_{fmax}_{device}"
if key not in mel_basis_cache: mel = librosa_mel_fn(sr=target_sample_rate, n_fft=n_fft, n_mels=n_mel_channels, fmin=fmin, fmax=fmax) mel_basis_cache[key] = torch.from_numpy(mel).float().to(device) # TODO: why they need .float()? hann_window_cache[key] = torch.hann_window(win_length).to(device)
mel_basis = mel_basis_cache[key] hann_window = hann_window_cache[key]
padding = (n_fft - hop_length) // 2 waveform = torch.nn.functional.pad(waveform.unsqueeze(1), (padding, padding), mode="reflect").squeeze(1)
spec = torch.stft( waveform, n_fft, hop_length=hop_length, win_length=win_length, window=hann_window, center=center, pad_mode="reflect", normalized=False, onesided=True, return_complex=True, ) spec = torch.sqrt(torch.view_as_real(spec).pow(2).sum(-1) + 1e-9)
mel_spec = torch.matmul(mel_basis, spec) mel_spec = torch.log(torch.clamp(mel_spec, min=1e-5))
return mel_spec
def get_vocos_mel_spectrogram( waveform, n_fft=1024, n_mel_channels=100, target_sample_rate=24000, hop_length=256, win_length=1024,): mel_stft = torchaudio.transforms.MelSpectrogram( sample_rate=target_sample_rate, n_fft=n_fft, win_length=win_length, hop_length=hop_length, n_mels=n_mel_channels, power=1, center=True, normalized=False, norm=None, ).to(waveform.device) if len(waveform.shape) == 3: waveform = waveform.squeeze(1) # 'b 1 nw -> b nw'
assert len(waveform.shape) == 2
mel = mel_stft(waveform) mel = mel.clamp(min=1e-5).log() return mel
class MelSpec(nn.Module): def __init__( self, n_fft=1024, hop_length=256, win_length=1024, n_mel_channels=100, target_sample_rate=24_000, mel_spec_type="vocos", ): super().__init__() assert mel_spec_type in ["vocos", "bigvgan"], print("We only support two extract mel backend: vocos or bigvgan")
self.n_fft = n_fft self.hop_length = hop_length self.win_length = win_length self.n_mel_channels = n_mel_channels self.target_sample_rate = target_sample_rate
if mel_spec_type == "vocos": self.extractor = get_vocos_mel_spectrogram elif mel_spec_type == "bigvgan": self.extractor = get_bigvgan_mel_spectrogram
self.register_buffer("dummy", torch.tensor(0), persistent=False)
def forward(self, wav): if self.dummy.device != wav.device: self.to(wav.device)
mel = self.extractor( waveform=wav, n_fft=self.n_fft, n_mel_channels=self.n_mel_channels, target_sample_rate=self.target_sample_rate, hop_length=self.hop_length, win_length=self.win_length, )
return mel
# sinusoidal position embedding
class SinusPositionEmbedding(nn.Module): def __init__(self, dim): super().__init__() self.dim = dim
def forward(self, x, scale=1000): device = x.device half_dim = self.dim // 2 emb = math.log(10000) / (half_dim - 1) emb = torch.exp(torch.arange(half_dim, device=device).float() * -emb) emb = scale * x.unsqueeze(1) * emb.unsqueeze(0) emb = torch.cat((emb.sin(), emb.cos()), dim=-1) return emb
# convolutional position embedding
class ConvPositionEmbedding(nn.Module): def __init__(self, dim, kernel_size=31, groups=16): super().__init__() assert kernel_size % 2 != 0 self.conv1d = nn.Sequential( nn.Conv1d(dim, dim, kernel_size, groups=groups, padding=kernel_size // 2), nn.Mish(), nn.Conv1d(dim, dim, kernel_size, groups=groups, padding=kernel_size // 2), nn.Mish(), )
def forward(self, x: float["b n d"], mask: bool["b n"] | None = None): # noqa: F722 if mask is not None: mask = mask[..., None] x = x.masked_fill(~mask, 0.0)
x = x.permute(0, 2, 1) x = self.conv1d(x) out = x.permute(0, 2, 1)
if mask is not None: out = out.masked_fill(~mask, 0.0)
return out
# rotary positional embedding related
def precompute_freqs_cis(dim: int, end: int, theta: float = 10000.0, theta_rescale_factor=1.0): # proposed by reddit user bloc97, to rescale rotary embeddings to longer sequence length without fine-tuning # has some connection to NTK literature # https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/ # https://github.com/lucidrains/rotary-embedding-torch/blob/main/rotary_embedding_torch/rotary_embedding_torch.py theta *= theta_rescale_factor ** (dim / (dim - 2)) freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim)) t = torch.arange(end, device=freqs.device) # type: ignore freqs = torch.outer(t, freqs).float() # type: ignore freqs_cos = torch.cos(freqs) # real part freqs_sin = torch.sin(freqs) # imaginary part return torch.cat([freqs_cos, freqs_sin], dim=-1)
def get_pos_embed_indices(start, length, max_pos, scale=1.0): # length = length if isinstance(length, int) else length.max() scale = scale * torch.ones_like(start, dtype=torch.float32) # in case scale is a scalar pos = ( start.unsqueeze(1) + (torch.arange(length, device=start.device, dtype=torch.float32).unsqueeze(0) * scale.unsqueeze(1)).long() ) # avoid extra long error. pos = torch.where(pos < max_pos, pos, max_pos - 1) return pos
# Global Response Normalization layer (Instance Normalization ?)
class GRN(nn.Module): def __init__(self, dim): super().__init__() self.gamma = nn.Parameter(torch.zeros(1, 1, dim)) self.beta = nn.Parameter(torch.zeros(1, 1, dim))
def forward(self, x): Gx = torch.norm(x, p=2, dim=1, keepdim=True) Nx = Gx / (Gx.mean(dim=-1, keepdim=True) + 1e-6) return self.gamma * (x * Nx) + self.beta + x
# ConvNeXt-V2 Block https://github.com/facebookresearch/ConvNeXt-V2/blob/main/models/convnextv2.py# ref: https://github.com/bfs18/e2_tts/blob/main/rfwave/modules.py#L108
class ConvNeXtV2Block(nn.Module): def __init__( self, dim: int, intermediate_dim: int, dilation: int = 1, ): super().__init__() padding = (dilation * (7 - 1)) // 2 self.dwconv = nn.Conv1d( dim, dim, kernel_size=7, padding=padding, groups=dim, dilation=dilation ) # depthwise conv self.norm = nn.LayerNorm(dim, eps=1e-6) self.pwconv1 = nn.Linear(dim, intermediate_dim) # pointwise/1x1 convs, implemented with linear layers self.act = nn.GELU() self.grn = GRN(intermediate_dim) self.pwconv2 = nn.Linear(intermediate_dim, dim)
def forward(self, x: torch.Tensor) -> torch.Tensor: residual = x x = x.transpose(1, 2) # b n d -> b d n x = self.dwconv(x) x = x.transpose(1, 2) # b d n -> b n d x = self.norm(x) x = self.pwconv1(x) x = self.act(x) x = self.grn(x) x = self.pwconv2(x) return residual + x
# RMSNorm
class RMSNorm(nn.Module): def __init__(self, dim: int, eps: float): super().__init__() self.eps = eps self.weight = nn.Parameter(torch.ones(dim)) self.native_rms_norm = float(torch.__version__[:3]) >= 2.4
def forward(self, x): if self.native_rms_norm: if self.weight.dtype in [torch.float16, torch.bfloat16]: x = x.to(self.weight.dtype) x = F.rms_norm(x, normalized_shape=(x.shape[-1],), weight=self.weight, eps=self.eps) else: variance = x.to(torch.float32).pow(2).mean(-1, keepdim=True) x = x * torch.rsqrt(variance + self.eps) if self.weight.dtype in [torch.float16, torch.bfloat16]: x = x.to(self.weight.dtype) x = x * self.weight
return x
# AdaLayerNorm# return with modulated x for attn input, and params for later mlp modulation
class AdaLayerNorm(nn.Module): def __init__(self, dim): super().__init__()
self.silu = nn.SiLU() self.linear = nn.Linear(dim, dim * 6)
self.norm = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
def forward(self, x, emb=None): emb = self.linear(self.silu(emb)) shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = torch.chunk(emb, 6, dim=1)
x = self.norm(x) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp
# AdaLayerNorm for final layer# return only with modulated x for attn input, cuz no more mlp modulation
class AdaLayerNorm_Final(nn.Module): def __init__(self, dim): super().__init__()
self.silu = nn.SiLU() self.linear = nn.Linear(dim, dim * 2)
self.norm = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
def forward(self, x, emb): emb = self.linear(self.silu(emb)) scale, shift = torch.chunk(emb, 2, dim=1)
x = self.norm(x) * (1 + scale)[:, None, :] + shift[:, None, :] return x
# FeedForward
class FeedForward(nn.Module): def __init__(self, dim, dim_out=None, mult=4, dropout=0.0, approximate: str = "none"): super().__init__() inner_dim = int(dim * mult) dim_out = dim_out if dim_out is not None else dim
activation = nn.GELU(approximate=approximate) project_in = nn.Sequential(nn.Linear(dim, inner_dim), activation) self.ff = nn.Sequential(project_in, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out))
def forward(self, x): return self.ff(x)
# Attention with possible joint part# modified from diffusers/src/diffusers/models/attention_processor.py
class Attention(nn.Module): def __init__( self, processor: JointAttnProcessor | AttnProcessor, dim: int, heads: int = 8, dim_head: int = 64, dropout: float = 0.0, context_dim: Optional[int] = None, # if not None -> joint attention context_pre_only: bool = False, qk_norm: Optional[str] = None, ): super().__init__()
if not hasattr(F, "scaled_dot_product_attention"): raise ImportError("Attention equires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")
self.processor = processor
self.dim = dim self.heads = heads self.inner_dim = dim_head * heads self.dropout = dropout
self.context_dim = context_dim self.context_pre_only = context_pre_only
self.to_q = nn.Linear(dim, self.inner_dim) self.to_k = nn.Linear(dim, self.inner_dim) self.to_v = nn.Linear(dim, self.inner_dim)
if qk_norm is None: self.q_norm = None self.k_norm = None elif qk_norm == "rms_norm": self.q_norm = RMSNorm(dim_head, eps=1e-6) self.k_norm = RMSNorm(dim_head, eps=1e-6) else: raise ValueError(f"Unimplemented qk_norm: {qk_norm}")
if self.context_dim is not None: self.to_q_c = nn.Linear(context_dim, self.inner_dim) self.to_k_c = nn.Linear(context_dim, self.inner_dim) self.to_v_c = nn.Linear(context_dim, self.inner_dim) if qk_norm is None: self.c_q_norm = None self.c_k_norm = None elif qk_norm == "rms_norm": self.c_q_norm = RMSNorm(dim_head, eps=1e-6) self.c_k_norm = RMSNorm(dim_head, eps=1e-6)
self.to_out = nn.ModuleList([]) self.to_out.append(nn.Linear(self.inner_dim, dim)) self.to_out.append(nn.Dropout(dropout))
if self.context_dim is not None and not self.context_pre_only: self.to_out_c = nn.Linear(self.inner_dim, context_dim)
def forward( self, x: float["b n d"], # noised input x # noqa: F722 c: float["b n d"] = None, # context c # noqa: F722 mask: bool["b n"] | None = None, # noqa: F722 rope=None, # rotary position embedding for x c_rope=None, # rotary position embedding for c ) -> torch.Tensor: if c is not None: return self.processor(self, x, c=c, mask=mask, rope=rope, c_rope=c_rope) else: return self.processor(self, x, mask=mask, rope=rope)
# Attention processor
class AttnProcessor: def __init__( self, pe_attn_head: int | None = None, # number of attention head to apply rope, None for all ): self.pe_attn_head = pe_attn_head
def __call__( self, attn: Attention, x: float["b n d"], # noised input x # noqa: F722 mask: bool["b n"] | None = None, # noqa: F722 rope=None, # rotary position embedding ) -> torch.FloatTensor: batch_size = x.shape[0]
# `sample` projections query = attn.to_q(x) key = attn.to_k(x) value = attn.to_v(x)
# attention inner_dim = key.shape[-1] head_dim = inner_dim // attn.heads query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
# qk norm if attn.q_norm is not None: query = attn.q_norm(query) if attn.k_norm is not None: key = attn.k_norm(key)
# apply rotary position embedding if rope is not None: freqs, xpos_scale = rope q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale**-1.0) if xpos_scale is not None else (1.0, 1.0)
if self.pe_attn_head is not None: pn = self.pe_attn_head query[:, :pn, :, :] = apply_rotary_pos_emb(query[:, :pn, :, :], freqs, q_xpos_scale) key[:, :pn, :, :] = apply_rotary_pos_emb(key[:, :pn, :, :], freqs, k_xpos_scale) else: query = apply_rotary_pos_emb(query, freqs, q_xpos_scale) key = apply_rotary_pos_emb(key, freqs, k_xpos_scale)
# mask. e.g. inference got a batch with different target durations, mask out the padding if mask is not None: attn_mask = mask attn_mask = attn_mask.unsqueeze(1).unsqueeze(1) # 'b n -> b 1 1 n' attn_mask = attn_mask.expand(batch_size, attn.heads, query.shape[-2], key.shape[-2]) else: attn_mask = None
x = F.scaled_dot_product_attention(query, key, value, attn_mask=attn_mask, dropout_p=0.0, is_causal=False) x = x.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim) x = x.to(query.dtype)
# linear proj x = attn.to_out[0](x) # dropout x = attn.to_out[1](x)
if mask is not None: mask = mask.unsqueeze(-1) x = x.masked_fill(~mask, 0.0)
return x
# Joint Attention processor for MM-DiT# modified from diffusers/src/diffusers/models/attention_processor.py
class JointAttnProcessor: def __init__(self): pass
def __call__( self, attn: Attention, x: float["b n d"], # noised input x # noqa: F722 c: float["b nt d"] = None, # context c, here text # noqa: F722 mask: bool["b n"] | None = None, # noqa: F722 rope=None, # rotary position embedding for x c_rope=None, # rotary position embedding for c ) -> torch.FloatTensor: residual = x
batch_size = c.shape[0]
# `sample` projections query = attn.to_q(x) key = attn.to_k(x) value = attn.to_v(x)
# `context` projections c_query = attn.to_q_c(c) c_key = attn.to_k_c(c) c_value = attn.to_v_c(c)
# attention inner_dim = key.shape[-1] head_dim = inner_dim // attn.heads query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) c_query = c_query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) c_key = c_key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) c_value = c_value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
# qk norm if attn.q_norm is not None: query = attn.q_norm(query) if attn.k_norm is not None: key = attn.k_norm(key) if attn.c_q_norm is not None: c_query = attn.c_q_norm(c_query) if attn.c_k_norm is not None: c_key = attn.c_k_norm(c_key)
# apply rope for context and noised input independently if rope is not None: freqs, xpos_scale = rope q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale**-1.0) if xpos_scale is not None else (1.0, 1.0) query = apply_rotary_pos_emb(query, freqs, q_xpos_scale) key = apply_rotary_pos_emb(key, freqs, k_xpos_scale) if c_rope is not None: freqs, xpos_scale = c_rope q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale**-1.0) if xpos_scale is not None else (1.0, 1.0) c_query = apply_rotary_pos_emb(c_query, freqs, q_xpos_scale) c_key = apply_rotary_pos_emb(c_key, freqs, k_xpos_scale)
# joint attention query = torch.cat([query, c_query], dim=2) key = torch.cat([key, c_key], dim=2) value = torch.cat([value, c_value], dim=2)
# mask. e.g. inference got a batch with different target durations, mask out the padding if mask is not None: attn_mask = F.pad(mask, (0, c.shape[1]), value=True) # no mask for c (text) attn_mask = attn_mask.unsqueeze(1).unsqueeze(1) # 'b n -> b 1 1 n' attn_mask = attn_mask.expand(batch_size, attn.heads, query.shape[-2], key.shape[-2]) else: attn_mask = None
x = F.scaled_dot_product_attention(query, key, value, attn_mask=attn_mask, dropout_p=0.0, is_causal=False) x = x.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim) x = x.to(query.dtype)
# Split the attention outputs. x, c = ( x[:, : residual.shape[1]], x[:, residual.shape[1] :], )
# linear proj x = attn.to_out[0](x) # dropout x = attn.to_out[1](x) if not attn.context_pre_only: c = attn.to_out_c(c)
if mask is not None: mask = mask.unsqueeze(-1) x = x.masked_fill(~mask, 0.0) # c = c.masked_fill(~mask, 0.) # no mask for c (text)
return x, c
# DiT Block
class DiTBlock(nn.Module): def __init__(self, dim, heads, dim_head, ff_mult=4, dropout=0.1, qk_norm=None, pe_attn_head=None): super().__init__()
self.attn_norm = AdaLayerNorm(dim) self.attn = Attention( processor=AttnProcessor(pe_attn_head=pe_attn_head), dim=dim, heads=heads, dim_head=dim_head, dropout=dropout, qk_norm=qk_norm, )
self.ff_norm = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6) self.ff = FeedForward(dim=dim, mult=ff_mult, dropout=dropout, approximate="tanh")
def forward(self, x, t, mask=None, rope=None): # x: noised input, t: time embedding # pre-norm & modulation for attention input norm, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.attn_norm(x, emb=t)
# attention attn_output = self.attn(x=norm, mask=mask, rope=rope)
# process attention output for input x x = x + gate_msa.unsqueeze(1) * attn_output
norm = self.ff_norm(x) * (1 + scale_mlp[:, None]) + shift_mlp[:, None] ff_output = self.ff(norm) x = x + gate_mlp.unsqueeze(1) * ff_output
return x
# MMDiT Block https://arxiv.org/abs/2403.03206
class MMDiTBlock(nn.Module): r""" modified from diffusers/src/diffusers/models/attention.py
notes. _c: context related. text, cond, etc. (left part in sd3 fig2.b) _x: noised input related. (right part) context_pre_only: last layer only do prenorm + modulation cuz no more ffn """
def __init__( self, dim, heads, dim_head, ff_mult=4, dropout=0.1, context_dim=None, context_pre_only=False, qk_norm=None ): super().__init__() if context_dim is None: context_dim = dim self.context_pre_only = context_pre_only
self.attn_norm_c = AdaLayerNorm_Final(context_dim) if context_pre_only else AdaLayerNorm(context_dim) self.attn_norm_x = AdaLayerNorm(dim) self.attn = Attention( processor=JointAttnProcessor(), dim=dim, heads=heads, dim_head=dim_head, dropout=dropout, context_dim=context_dim, context_pre_only=context_pre_only, qk_norm=qk_norm, )
if not context_pre_only: self.ff_norm_c = nn.LayerNorm(context_dim, elementwise_affine=False, eps=1e-6) self.ff_c = FeedForward(dim=context_dim, mult=ff_mult, dropout=dropout, approximate="tanh") else: self.ff_norm_c = None self.ff_c = None self.ff_norm_x = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6) self.ff_x = FeedForward(dim=dim, mult=ff_mult, dropout=dropout, approximate="tanh")
def forward(self, x, c, t, mask=None, rope=None, c_rope=None): # x: noised input, c: context, t: time embedding # pre-norm & modulation for attention input if self.context_pre_only: norm_c = self.attn_norm_c(c, t) else: norm_c, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.attn_norm_c(c, emb=t) norm_x, x_gate_msa, x_shift_mlp, x_scale_mlp, x_gate_mlp = self.attn_norm_x(x, emb=t)
# attention x_attn_output, c_attn_output = self.attn(x=norm_x, c=norm_c, mask=mask, rope=rope, c_rope=c_rope)
# process attention output for context c if self.context_pre_only: c = None else: # if not last layer c = c + c_gate_msa.unsqueeze(1) * c_attn_output
norm_c = self.ff_norm_c(c) * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None] c_ff_output = self.ff_c(norm_c) c = c + c_gate_mlp.unsqueeze(1) * c_ff_output
# process attention output for input x x = x + x_gate_msa.unsqueeze(1) * x_attn_output
norm_x = self.ff_norm_x(x) * (1 + x_scale_mlp[:, None]) + x_shift_mlp[:, None] x_ff_output = self.ff_x(norm_x) x = x + x_gate_mlp.unsqueeze(1) * x_ff_output
return c, x
# time step conditioning embedding
class TimestepEmbedding(nn.Module): def __init__(self, dim, freq_embed_dim=256): super().__init__() self.time_embed = SinusPositionEmbedding(freq_embed_dim) self.time_mlp = nn.Sequential(nn.Linear(freq_embed_dim, dim), nn.SiLU(), nn.Linear(dim, dim))
def forward(self, timestep: float["b"]): # noqa: F821 time_hidden = self.time_embed(timestep) time_hidden = time_hidden.to(timestep.dtype) time = self.time_mlp(time_hidden) # b d return time
class MIEsitmator(nn.Module): def __init__(self, vocab_size, decoder_dim, hidden_size, dropout=0.5): super(MIEsitmator, self).__init__() self.proj = nn.Sequential( torch.nn.Linear(decoder_dim, hidden_size, bias=True), nn.ReLU(), nn.Dropout(p=dropout) ) self.ctc_proj = torch.nn.Linear(hidden_size, vocab_size + 1, bias=True) self.ctc = nn.CTCLoss(blank=vocab_size, reduction='mean', zero_infinity=True)
def forward(self, decoder_outputs, target_phones, decoder_lengths, target_lengths): out = self.proj(decoder_outputs.type(self.ctc_proj.weight.dtype)) log_probs = self.ctc_proj(out).log_softmax(dim=2) log_probs = log_probs.transpose(1, 0) ctc_loss = self.ctc(log_probs.float(), target_phones, decoder_lengths, target_lengths) ctc_loss = ctc_loss / decoder_lengths.float()
# print("ctc_loss:", ctc_loss.shape, "ctc_max:", torch.max(ctc_loss), "ctc_min:", torch.min(ctc_loss), decoder_lengths[0]) # # 2. 统计非NaN值的百分比 # mask = ~torch.isnan(ctc_loss) # total_count = ctc_loss.numel() # 总元素数量(所有维度) # valid_count = mask.sum().item() # 非NaN元素数量 # valid_percentage = (valid_count / total_count) * 100 # print(f"ctc loss: total_count: {total_count}", f"valid_count: {valid_count}", f"valid_percentage: {valid_percentage:.2f}%")
# 3. 将NaN或大于150的值替换为150 # ctc_loss = torch.where(torch.isnan(ctc_loss), 150.0, ctc_loss) ctc_loss = torch.where((ctc_loss > 300.0) | torch.isnan(ctc_loss), 300.0, ctc_loss) # ctc_loss = torch.nan_to_num(ctc_loss, nan=0.0, posinf=0.0, neginf=0.0) # average by number of frames since taco_loss is averaged. ctc_loss = ctc_loss.mean() return ctc_loss
def inference(self, decoder_output): out = self.proj(decoder_output.type(self.ctc_proj.weight.dtype)) log_probs = self.ctc_proj(out).log_softmax(dim=2) log_probs = log_probs.transpose(1, 0) return log_probs.item()
class AccentClassifier(nn.Module): def __init__(self, input_dim, hidden_dim, num_accents, dropout=0.3): super().__init__() self.net = nn.Sequential( nn.Linear(input_dim, hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, num_accents) )
def forward(self, x): return self.net(x)
class GradientReversalFunction(Function): @staticmethod def forward(ctx, x, lambda_): ctx.lambda_ = lambda_ return x.view_as(x)
@staticmethod def backward(ctx, grad_output): return grad_output.neg() * ctx.lambda_, None
def grad_reverse(x, lambda_=1.0): return GradientReversalFunction.apply(x, lambda_)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190from __future__ import annotations
import osimport randomfrom collections import defaultdictfrom importlib.resources import files
import torchfrom torch.nn.utils.rnn import pad_sequence
import jiebafrom pypinyin import lazy_pinyin, Styleimport sys
# seed everything
def seed_everything(seed=0): random.seed(seed) os.environ["PYTHONHASHSEED"] = str(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False
# helpers
def exists(v): return v is not None
def default(v, d): return v if exists(v) else d
# tensor helpers
def lens_to_mask(t: int["b"], length: int | None = None) -> bool["b n"]: # noqa: F722 F821 if not exists(length): length = t.amax()
seq = torch.arange(length, device=t.device) return seq[None, :] < t[:, None]
def mask_from_start_end_indices(seq_len: int["b"], start: int["b"], end: int["b"]): # noqa: F722 F821 max_seq_len = seq_len.max().item() seq = torch.arange(max_seq_len, device=start.device).long() start_mask = seq[None, :] >= start[:, None] end_mask = seq[None, :] < end[:, None] return start_mask & end_mask
def mask_from_frac_lengths(seq_len: int["b"], frac_lengths: float["b"]): # noqa: F722 F821 lengths = (frac_lengths * seq_len).long() max_start = seq_len - lengths
rand = torch.rand_like(frac_lengths) start = (max_start * rand).long().clamp(min=0) end = start + lengths
return mask_from_start_end_indices(seq_len, start, end)
def maybe_masked_mean(t: float["b n d"], mask: bool["b n"] = None) -> float["b d"]: # noqa: F722 if not exists(mask): return t.mean(dim=1)
t = torch.where(mask[:, :, None], t, torch.tensor(0.0, device=t.device)) num = t.sum(dim=1) den = mask.float().sum(dim=1)
return num / den.clamp(min=1.0)
# simple utf-8 tokenizer, since paper went character baseddef list_str_to_tensor(text: list[str], padding_value=-1) -> int["b nt"]: # noqa: F722 list_tensors = [torch.tensor([*bytes(t, "UTF-8")]) for t in text] # ByT5 style text = pad_sequence(list_tensors, padding_value=padding_value, batch_first=True) return text
# char tokenizer, based on custom dataset's extracted .txt filedef list_str_to_idx( text: list[str] | list[list[str]], vocab_char_map: dict[str, int], # {char: idx} padding_value=-1,) -> int["b nt"]: # noqa: F722 list_idx_tensors = [torch.tensor([vocab_char_map.get(c, 0) for c in t]) for t in text] # pinyin or char style text = pad_sequence(list_idx_tensors, padding_value=padding_value, batch_first=True) return text
# Get tokenizerdef get_tokenizer(dataset_name, tokenizer: str = "pinyin"): """ tokenizer - "pinyin" do g2p for only chinese characters, need .txt vocab_file - "char" for char-wise tokenizer, need .txt vocab_file - "byte" for utf-8 tokenizer - "custom" if you're directly passing in a path to the vocab.txt you want to use vocab_size - if use "pinyin", all available pinyin types, common alphabets (also those with accent) and symbols - if use "char", derived from unfiltered character & symbol counts of custom dataset - if use "byte", set to 256 (unicode byte range) """ if tokenizer in ["pinyin", "char"]: tokenizer_path = os.path.join(files("lemas_tts").joinpath("../../data"), f"{dataset_name}_{tokenizer}/vocab.txt") with open(tokenizer_path, "r", encoding="utf-8") as f: vocab_char_map = {} for i, char in enumerate(f): vocab_char_map[char[:-1]] = i vocab_size = len(vocab_char_map) assert vocab_char_map[" "] == 0, "make sure space is of idx 0 in vocab.txt, cuz 0 is used for unknown char"
elif tokenizer == "byte": vocab_char_map = None vocab_size = 256
elif tokenizer == "custom": with open(dataset_name, "r", encoding="utf-8") as f: vocab_char_map = {} for i, char in enumerate(f): vocab_char_map[char[:-1]] = i vocab_size = len(vocab_char_map)
return vocab_char_map, vocab_size
# convert char to pinyindef convert_char_to_pinyin(text_list, polyphone=True): if jieba.dt.initialized is False: jieba.default_logger.setLevel(50) # CRITICAL jieba.initialize()
final_text_list = [] custom_trans = str.maketrans( {";": ",", "“": '"', "”": '"', "‘": "'", "’": "'"} ) # add custom trans here, to address oov
def is_chinese(c): return ( "\u3100" <= c <= "\u9fff" # common chinese characters )
for text in text_list: char_list = [] text = text.translate(custom_trans) from lemas_tts.infer.cn_tn import NSWNormalizer text = NSWNormalizer(text.strip()).normalize() text = list(jieba.cut(text)) for seg in text: seg_byte_len = len(bytes(seg, "UTF-8")) if seg_byte_len == len(seg): # if pure alphabets and symbols if char_list and seg_byte_len > 1 and char_list[-1] not in " :'\"": char_list.append(" ") char_list.extend(seg) elif polyphone and seg_byte_len == 3 * len(seg): # if pure east asian characters seg_ = lazy_pinyin(seg, style=Style.TONE3, tone_sandhi=True) for i, c in enumerate(seg): if is_chinese(c): char_list.append(" ") char_list.append(seg_[i]) else: # if mixed characters, alphabets and symbols for c in seg: if ord(c) < 256: char_list.extend(c) elif is_chinese(c): char_list.append(" ") char_list.extend(lazy_pinyin(c, style=Style.TONE3, tone_sandhi=True)) else: char_list.append(c) final_text_list.append(char_list)
return final_text_list
# filter func for dirty data with many repetitions
def repetition_found(text, length=2, tolerance=10): pattern_count = defaultdict(int) for i in range(len(text) - length + 1): pattern = text[i : i + length] pattern_count[pattern] += 1 for pattern, count in pattern_count.items(): if count > tolerance: return True return False123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584import gcimport osimport platformimport psutilimport tempfilefrom glob import globimport tracebackimport clickimport gradio as grimport torch
import sysfrom pathlib import Path
# Add the local code directory so that `lemas_tts` can be imported when running this# script directly without installing the package.THIS_FILE = Path(__file__).resolve()SRC_ROOT = THIS_FILE.parents[2] # .../codesys.path.append(str(SRC_ROOT))
def _find_repo_root(start: Path) -> Path: """Locate the repo root by looking for a `pretrained_models` folder upwards.""" for p in [start, *start.parents]: if (p / "pretrained_models").is_dir(): return p cwd = Path.cwd() if (cwd / "pretrained_models").is_dir(): return cwd return start
REPO_ROOT = _find_repo_root(THIS_FILE)PRETRAINED_ROOT = REPO_ROOT / "pretrained_models"CKPTS_ROOT = PRETRAINED_ROOT / "ckpts"DATA_ROOT = PRETRAINED_ROOT / "data"UVR5_CODE_DIR = REPO_ROOT / "code" / "uvr5"UVR5_MODEL_DIR = PRETRAINED_ROOT / "uvr5" / "models" / "MDX_Net_Models" / "model_data"
from lemas_tts.api import F5TTSimport torch, torchaudioimport soundfile as sf
# Global variablestts_api = Nonelast_checkpoint = ""last_device = ""last_ema = None
# Device detectiondevice = ( "cuda" if torch.cuda.is_available() else "xpu" if torch.xpu.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
class UVR5: def __init__(self, model_dir): code_dir = str(UVR5_CODE_DIR) self.model = self.load_model(str(model_dir), code_dir) def load_model(self, model_dir, code_dir): import sys, json, os sys.path.append(code_dir) from multiprocess_cuda_infer import ModelData, Inference model_path = os.path.join(model_dir, 'Kim_Vocal_1.onnx') config_path = os.path.join(model_dir, 'MDX-Net-Kim-Vocal1.json') configs = json.loads(open(config_path, 'r', encoding='utf-8').read()) model_data = ModelData( model_path=model_path, audio_path = model_dir, result_path = model_dir, device = 'cpu', process_method = "MDX-Net", base_dir=code_dir, **configs )
uvr5_model = Inference(model_data, 'cpu') uvr5_model.load_model(model_path, 1) return uvr5_model def denoise(self, audio_info): print("denoise UVR5: ", audio_info) input_audio = load_wav(audio_info, sr=44100, channel=2) output_audio = self.model.demix_base({0:input_audio.squeeze()}, is_match_mix=False) # transform = torchaudio.transforms.Resample(44100, 16000) # output_audio = transform(output_audio) return output_audio.squeeze().T.numpy(), 44100
denoise_model = UVR5(UVR5_MODEL_DIR)
def load_wav(audio_info, sr=16000, channel=1): print("load audio:", audio_info) audio, raw_sr = torchaudio.load(audio_info) audio = audio.T if len(audio.shape) > 1 and audio.shape[1] == 2 else audio audio = audio / torch.max(torch.abs(audio)) audio = audio.squeeze().float() if channel == 1 and len(audio.shape) == 2: # stereo to mono audio = audio.mean(dim=0, keepdim=True) elif channel == 2 and len(audio.shape) == 1: audio = torch.stack((audio, audio)) # mono to stereo if raw_sr != sr: audio = torchaudio.functional.resample(audio.squeeze(), raw_sr, sr) audio = torch.clip(audio, -0.999, 0.999).squeeze() return audio
def denoise(audio_info): save_path = "./denoised_audio.wav" denoised_audio, sr = denoise_model.denoise(audio_info) sf.write(save_path, denoised_audio, sr, format='wav', subtype='PCM_24') print("save denoised audio:", save_path) return save_path
def cancel_denoise(audio_info): return audio_info
def get_checkpoints_project(project_name=None, is_gradio=True): """Get available checkpoint files""" checkpoint_dir = [str(CKPTS_ROOT)] if project_name is None: # Look for checkpoints in common locations files_checkpoints = [] for path in checkpoint_dir: if os.path.isdir(path): files_checkpoints.extend(glob(os.path.join(path, "**/*.pt"), recursive=True)) files_checkpoints.extend(glob(os.path.join(path, "**/*.safetensors"), recursive=True)) break else: # project_name = project_name.replace("_pinyin", "").replace("_char", "") project_name = "_".join(["F5TTS_v1_Base", "vocos", "custom", project_name.replace("_custom", "")]) if project_name != "F5TTS_v1_Base" else project_name if os.path.isdir(checkpoint_dir[0]): files_checkpoints = glob(os.path.join(checkpoint_dir[0], project_name, "*.pt")) files_checkpoints.extend(glob(os.path.join(checkpoint_dir[0], project_name, "*.safetensors"))) else: files_checkpoints = [] print("files_checkpoints:", project_name, files_checkpoints) # Separate pretrained and regular checkpoints pretrained_checkpoints = [f for f in files_checkpoints if "pretrained_" in os.path.basename(f)] regular_checkpoints = [ f for f in files_checkpoints if "pretrained_" not in os.path.basename(f) and "model_last.pt" not in os.path.basename(f) ] last_checkpoint = [f for f in files_checkpoints if "model_last.pt" in os.path.basename(f)]
# Sort regular checkpoints by number try: regular_checkpoints = sorted( regular_checkpoints, key=lambda x: int(os.path.basename(x).split("_")[1].split(".")[0]) ) except (IndexError, ValueError): regular_checkpoints = sorted(regular_checkpoints)
# Combine in order: pretrained, regular, last files_checkpoints = pretrained_checkpoints + regular_checkpoints + last_checkpoint
select_checkpoint = None if not files_checkpoints else files_checkpoints[-1]
if is_gradio: return gr.update(choices=files_checkpoints, value=select_checkpoint)
return files_checkpoints, select_checkpoint
def get_available_projects(): """Get available project names from data directory""" data_path = str(DATA_ROOT)
project_list = [] if os.path.isdir(data_path): for folder in os.listdir(data_path): if "test" in folder: continue project_list.append(folder)
# Fallback to a sensible default if no projects are found if not project_list: project_list = ["multilingual_acc_grl_custom"]
return project_list
def infer( project, file_checkpoint, exp_name, ref_text, ref_audio, denoise_audio, gen_text, nfe_step, use_ema, separate_langs, frontend, speed, cfg_strength, use_acc_grl, ref_ratio, no_ref_audio, sway_sampling_coef, use_prosody_encoder, seed): global last_checkpoint, last_device, tts_api, last_ema
if not os.path.isfile(file_checkpoint): return None, "Checkpoint not found!", ""
if denoise_audio: ref_audio = denoise_audio
device_test = device # Use the global device
if last_checkpoint != file_checkpoint or last_device != device_test or last_ema != use_ema or tts_api is None: if last_checkpoint != file_checkpoint: last_checkpoint = file_checkpoint
if last_device != device_test: last_device = device_test
if last_ema != use_ema: last_ema = use_ema
# Try to find vocab file vocab_file = None possible_vocab_paths = [ str(DATA_ROOT / project / "vocab.txt"), # legacy fallbacks for older layouts f"./data/{project}/vocab.txt", f"../../data/{project}/vocab.txt", "./data/Emilia_ZH_EN_pinyin/vocab.txt", "../../data/Emilia_ZH_EN_pinyin/vocab.txt", ] for path in possible_vocab_paths: if os.path.isfile(path): vocab_file = path break if vocab_file is None: return None, "Vocab file not found!", ""
try: tts_api = F5TTS( model=exp_name, ckpt_file=file_checkpoint, vocab_file=vocab_file, device=device_test, use_ema=use_ema, frontend=frontend, use_prosody_encoder=use_prosody_encoder, prosody_cfg_path=str(CKPTS_ROOT / "prosody_encoder" / "pretssel_cfg.json"), prosody_ckpt_path=str(CKPTS_ROOT / "prosody_encoder" / "prosody_encoder_UnitY2.pt"), ) except Exception as e: traceback.print_exc() return None, f"Error loading model: {str(e)}", ""
print("Model loaded >>", device_test, file_checkpoint, use_ema)
if seed == -1: # -1 used for random seed = None
try: with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f: tts_api.infer( ref_file=ref_audio, ref_text=ref_text.strip(), gen_text=gen_text.strip(), nfe_step=nfe_step, separate_langs=separate_langs, speed=speed, cfg_strength=cfg_strength, sway_sampling_coef=sway_sampling_coef, use_acc_grl=use_acc_grl, ref_ratio=ref_ratio, no_ref_audio=no_ref_audio, use_prosody_encoder=use_prosody_encoder, file_wave=f.name, seed=seed, ) return f.name, f"Device: {tts_api.device}", str(tts_api.seed) except Exception as e: traceback.print_exc() return None, f"Inference error: {str(e)}", ""
def get_gpu_stats(): """Get GPU statistics""" gpu_stats = ""
if torch.cuda.is_available(): gpu_count = torch.cuda.device_count() for i in range(gpu_count): gpu_name = torch.cuda.get_device_name(i) gpu_properties = torch.cuda.get_device_properties(i) total_memory = gpu_properties.total_memory / (1024**3) # in GB allocated_memory = torch.cuda.memory_allocated(i) / (1024**2) # in MB reserved_memory = torch.cuda.memory_reserved(i) / (1024**2) # in MB
gpu_stats += ( f"GPU {i} Name: {gpu_name}\n" f"Total GPU memory (GPU {i}): {total_memory:.2f} GB\n" f"Allocated GPU memory (GPU {i}): {allocated_memory:.2f} MB\n" f"Reserved GPU memory (GPU {i}): {reserved_memory:.2f} MB\n\n" ) elif torch.xpu.is_available(): gpu_count = torch.xpu.device_count() for i in range(gpu_count): gpu_name = torch.xpu.get_device_name(i) gpu_properties = torch.xpu.get_device_properties(i) total_memory = gpu_properties.total_memory / (1024**3) # in GB allocated_memory = torch.xpu.memory_allocated(i) / (1024**2) # in MB reserved_memory = torch.xpu.memory_reserved(i) / (1024**2) # in MB
gpu_stats += ( f"GPU {i} Name: {gpu_name}\n" f"Total GPU memory (GPU {i}): {total_memory:.2f} GB\n" f"Allocated GPU memory (GPU {i}): {allocated_memory:.2f} MB\n" f"Reserved GPU memory (GPU {i}): {reserved_memory:.2f} MB\n\n" ) elif torch.backends.mps.is_available(): gpu_count = 1 gpu_stats += "MPS GPU\n" total_memory = psutil.virtual_memory().total / ( 1024**3 ) # Total system memory (MPS doesn't have its own memory) allocated_memory = 0 reserved_memory = 0
gpu_stats += ( f"Total system memory: {total_memory:.2f} GB\n" f"Allocated GPU memory (MPS): {allocated_memory:.2f} MB\n" f"Reserved GPU memory (MPS): {reserved_memory:.2f} MB\n" )
else: gpu_stats = "No GPU available"
return gpu_stats
def get_cpu_stats(): """Get CPU statistics""" cpu_usage = psutil.cpu_percent(interval=1) memory_info = psutil.virtual_memory() memory_used = memory_info.used / (1024**2) memory_total = memory_info.total / (1024**2) memory_percent = memory_info.percent
pid = os.getpid() process = psutil.Process(pid) nice_value = process.nice()
cpu_stats = ( f"CPU Usage: {cpu_usage:.2f}%\n" f"System Memory: {memory_used:.2f} MB used / {memory_total:.2f} MB total ({memory_percent}% used)\n" f"Process Priority (Nice value): {nice_value}" )
return cpu_stats
def get_combined_stats(): """Get combined system stats""" gpu_stats = get_gpu_stats() cpu_stats = get_cpu_stats() combined_stats = f"### GPU Stats\n{gpu_stats}\n\n### CPU Stats\n{cpu_stats}" return combined_stats
# Create Gradio interfacewith gr.Blocks(title="LEMAS-TTS Inference") as app: gr.Markdown( """ # Zero-Shot TTS
Set seed to -1 for random generation. """ ) with gr.Accordion("Model configuration", open=False): # Model configuration with gr.Row(): exp_name = gr.Radio( label="Model", choices=["F5TTS_v1_Base", "F5TTS_Base", "E2TTS_Base"], value="F5TTS_v1_Base", visible=False ) # Project selection available_projects = get_available_projects()
# Get initial checkpoints list_checkpoints, checkpoint_select = get_checkpoints_project(available_projects[0] if available_projects else None, False)
with gr.Row(): with gr.Column(scale=1): # load_models_btn = gr.Button(value="Load models") cm_project = gr.Dropdown( choices=available_projects, value=available_projects[0] if available_projects else None, label="Project", allow_custom_value=True, scale=4 ) with gr.Column(scale=5): cm_checkpoint = gr.Dropdown( choices=list_checkpoints, value=checkpoint_select, label="Checkpoints", allow_custom_value=True # scale=4, ) bt_checkpoint_refresh = gr.Button("Refresh", scale=1)
with gr.Row(): ch_use_ema = gr.Checkbox(label="Use EMA", value=True, scale=2, info="Turn off at early stage might offer better results") frontend = gr.Radio(label="Frontend", choices=["phone", "char", "bpe"], value="phone", scale=3) separate_langs = gr.Checkbox(label="Separate Languages", value=True, scale=2, info="separate language tokens")
# Inference parameters with gr.Row(): nfe_step = gr.Number(label="NFE Step", scale=1, value=64) speed = gr.Slider(label="Speed", scale=3, value=1.0, minimum=0.5, maximum=1.5, step=0.1) cfg_strength = gr.Slider(label="CFG Strength", scale=2, value=5.0, minimum=0.0, maximum=10.0, step=1) sway_sampling_coef = gr.Slider(label="Sway Sampling Coef", scale=2, value=3, minimum=-1, maximum=5, step=0.1) ref_ratio = gr.Slider(label="Ref Ratio", scale=2, value=1.0, minimum=0.0, maximum=1.0, step=0.1) no_ref_audio = gr.Checkbox(label="No Reference Audio", value=False, scale=1, info="No mel condition") use_acc_grl = gr.Checkbox(label="Use accent grl condition", value=False, scale=1, info="Use accent grl condition") use_prosody_encoder = gr.Checkbox(label="Use prosody encoder", value=False, scale=1, info="Use prosody encoder") seed = gr.Number(label="Random Seed", scale=1, value=5828684826493313192, minimum=-1)
# Input fields ref_text = gr.Textbox(label="Reference Text", placeholder="Enter the text for the reference audio...") ref_audio = gr.Audio(label="Reference Audio", type="filepath", interactive=True, show_download_button=True, editable=True)
with gr.Row(): denoise_btn = gr.Button(value="Denoise") cancel_btn = gr.Button(value="Cancel Denoise") denoise_audio = gr.Audio(label="Denoised Audio", value=None, type="filepath", interactive=True, show_download_button=True, editable=True)
gen_text = gr.Textbox(label="Text to Generate", placeholder="Enter the text you want to generate...")
# Inference button and outputs with gr.Row(): txt_info_gpu = gr.Textbox("", label="Device Info") seed_info = gr.Textbox(label="Used Random Seed") check_button_infer = gr.Button("Generate Audio", variant="primary")
gen_audio = gr.Audio(label="Generated Audio", type="filepath", interactive=True, show_download_button=True, editable=True)
# Examples examples = gr.Examples( examples=[ [ "Ich glaub, mein Schwein pfeift.", str(DATA_ROOT / "test_examples" / "de.wav"), "我觉得我的猪在吹口哨。", ], [ "em, #1 I have a list of YouTubers, and I'm gonna be going to their houses and raiding them by.", str(DATA_ROOT / "test_examples" / "en.wav"), "我有一份 YouTuber 名单,我打算去他们家,对他们进行突袭。", ], [ "Te voy a dar un tip #1 que le copia a John Rockefeller, uno de los empresarios más picudos de la historia.", str(DATA_ROOT / "test_examples" / "es.wav"), "我要给你一个从历史上最精明的商人之一约翰·洛克菲勒那里抄来的秘诀。", ], [ "Per l'amor di Dio #1 fai, #2 se pensi di non poterti fermare, fallo #1 e fallo.", str(DATA_ROOT / "test_examples" / "it.wav"), "看在上帝的份上,去做吧,如果你认为你无法停止,那就去做吧,继续做下去。", ], [ "Nova, #1 dia 25 desse mês vai rolar operação the last Frontier.", str(DATA_ROOT / "test_examples" / "pt.wav"), "新消息,本月二十五日,'最后的边疆行动'将启动。", ], # ["Good morning! #1 ", # "/mnt/code/lemas/F5-TTS/data/trueman/recognition_d0a02641c090813574a8ec398220339f_0.wav", # " #1" # ], # ["Good morning! #1 ", # "/mnt/code/lemas/F5-TTS/data/trueman/recognition_d0a02641c090813574a8ec398220339f_1.wav", # " #1", # ], # ["Good morning! #1 ", # "/mnt/code/lemas/F5-TTS/data/trueman/recognition_d0a02641c090813574a8ec398220339f_2.wav", # " #1", # ], # ["Oh, and in case I don't see ya, #1", # "/mnt/code/lemas/F5-TTS/data/trueman/recognition_d0a02641c090813574a8ec398220339f_3.wav", # " #1", # ], # ["Good afternoon, good evening, and good night. #1", # "/mnt/code/lemas/F5-TTS/data/trueman/recognition_d0a02641c090813574a8ec398220339f_4.wav", # " #1", # ], ], inputs=[ ref_text, ref_audio, gen_text, ], outputs=[gen_audio, txt_info_gpu, seed_info], fn=infer, cache_examples=False )
# System Info section at the bottom gr.Markdown("---") gr.Markdown("## System Information") with gr.Accordion("Update System Stats", open=False): update_button = gr.Button("Update System Stats", scale=1) output_box = gr.Textbox(label="GPU and CPU Information", lines=5, scale=5)
def update_stats(): return get_combined_stats() denoise_btn.click(fn=denoise, inputs=[ref_audio], outputs=[denoise_audio])
cancel_btn.click(fn=cancel_denoise, inputs=[ref_audio], outputs=[denoise_audio])
# Event handlers check_button_infer.click( fn=infer, inputs=[ cm_project, cm_checkpoint, exp_name, ref_text, ref_audio, denoise_audio, gen_text, nfe_step, ch_use_ema, separate_langs, frontend, speed, cfg_strength, use_acc_grl, ref_ratio, no_ref_audio, sway_sampling_coef, use_prosody_encoder, seed, ], outputs=[gen_audio, txt_info_gpu, seed_info], )
bt_checkpoint_refresh.click(fn=get_checkpoints_project, inputs=[cm_project], outputs=[cm_checkpoint]) cm_project.change(fn=get_checkpoints_project, inputs=[cm_project], outputs=[cm_checkpoint])
ref_audio.change( fn=lambda x: None, inputs=[ref_audio], outputs=[denoise_audio] )
update_button.click(fn=update_stats, outputs=output_box) # Auto-load system stats on startup app.load(fn=update_stats, outputs=output_box)
@click.command()@click.option("--port", "-p", default=7860, type=int, help="Port to run the app on")@click.option("--host", "-H", default="0.0.0.0", help="Host to run the app on")@click.option( "--share", "-s", default=False, is_flag=True, help="Share the app via Gradio share link",)@click.option("--api", "-a", default=True, is_flag=True, help="Allow API access")def main(port, host, share, api): global app print("Starting LEMAS-TTS Inference Interface...") print(f"Device: {device}") app.queue(api_open=api).launch( server_name=host, server_port=port, share=share, show_api=api, allowed_paths=[str(DATA_ROOT)], )
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182--extra-index-url https://download.pytorch.org/whl/cu121accelerate>=0.33.0aiofiles==23.2.1aiohappyeyeballs==2.6.1aiohttp==3.13.2aiosignal==1.4.0annotated-doc==0.0.4annotated-types==0.7.0antlr4-python3-runtime==4.9.3anyio==4.12.0attrs==25.4.0audioread==3.1.0babel==2.17.0bitsandbytes>0.37.0; platform_machine != "arm64" and platform_system != "Darwin"boto3==1.42.16botocore==1.42.16brotli==1.2.0cached_pathcachetools==6.2.4certifi==2025.11.12cffi==2.0.0charset-normalizer==3.4.4clickcontourpy==1.3.2csvw==3.7.0cycler==0.12.1datasetsdecorator==5.2.1dill==0.4.0dlinfo==2.0.0docopt==0.6.2einops==0.8.1einx==0.3.0ema-pytorch==0.7.3encodec==0.1.1espeakng==1.0.2espeak_phonemizer==1.3.1fastapi==0.127.0ffmpy==1.0.0filelock==3.20.1fonttools==4.61.1frozendict==2.4.7frozenlist==1.8.0fsspec==2025.10.0gitdb==4.0.12GitPython==3.1.45google-api-core==2.28.1google-auth==2.45.0google-cloud-core==2.5.0google-cloud-storage==3.7.0google-crc32c==1.8.0google-resumable-media==2.8.0googleapis-common-protos==1.72.0gradio==5.38.0gradio-client==1.11.0groovy==0.1.2h11==0.16.0hf-xet==1.2.0httpcore==1.0.9httpx==0.28.1huggingface-hub==0.36.0hydra-core>=1.3.0idna==3.11isodate==0.7.2jiebaJinja2==3.1.6jmespath==1.0.1joblib==1.5.3jsonschema==4.25.1jsonschema-specifications==2025.9.1kiwisolver==1.4.9langid==1.1.6language-tags==1.2.0lazy_loader==0.4librosallvmlite==0.42.0loguru==0.7.3markdown-it-py==4.0.0MarkupSafematplotlibmdurl==0.1.2mpmath==1.3.0msgpack==1.1.2multidict==6.7.0multiprocess==0.70.18networkx==3.1num2words==0.5.13numba==0.59.0numpy==1.26.0nvidia-cublas-cu12==12.1.3.1nvidia-cuda-cupti-cu12==12.1.105nvidia-cuda-nvrtc-cu12==12.1.105nvidia-cuda-runtime-cu12==12.1.105nvidia-cudnn-cu12==8.9.2.26nvidia-cufft-cu12==11.0.2.54nvidia-cufile-cu12==1.11.1.6nvidia-curand-cu12==10.3.2.106nvidia-cusolver-cu12==11.4.5.107nvidia-cusparse-cu12==12.1.0.106nvidia-cusparselt-cu12==0.6.3nvidia-nccl-cu12==2.20.5nvidia-nvjitlink-cu12==12.6.85nvidia-nvtx-cu12==12.1.105omegaconf==2.3.0onnx==1.16.0onnxruntimeonnxruntime-gpuorjson==3.11.5packaging==25.0pandas==2.3.3phonemizer==3.3.0pillow==11.3.0platformdirs==4.5.1pooch==1.8.2propcache==0.4.1proto-plus==1.27.0protobuf==6.33.2psutil==7.2.0pyarrow==22.0.0pyasn1==0.6.1pyasn1_modules==0.4.2pycparser==2.23pydantic<=2.10.6pydantic_core==2.27.2pydubpy-espeak-ng==0.1.8Pygments==2.19.2pyparsing==3.3.1pypinyinpypinyin-dictpython-dateutil==2.9.0.post0python-multipart==0.0.21pytz==2025.2PyYAML==6.0.3rdflib==7.5.0referencing==0.37.0regexrequests==2.32.5rfc3986==1.5.0rich==13.9.4rpds-py==0.30.0rsa==4.9.1s3transfer==0.16.0safehttpx==0.1.7safetensorsscikit-learn==1.7.1scipy==1.15.3segments==2.3.0semantic-version==2.10.0sentry-sdk==2.48.0setuptools==80.9.0shellingham==1.5.4six==1.17.0smmap==5.0.2soundfilesoxr==1.0.0starlette==0.50.0sympy==1.14.0termcolor==3.2.0threadpoolctl==3.6.0tokenizers==0.22.1tomlitomlkit==0.13.3torch==2.3.1torchaudio==2.3.1torchdiffeq==0.2.4tqdm>=4.65.0transformerstransformers-stream-generatortriton==2.3.1typer==0.16.0typing_extensions==4.12.2tzdata==2025.3uritemplate==4.2.0urllib3==2.6.2uromanuvicorn==0.40.0vocosx-transformers>=1.31.14xxhash==3.6.0yarl==1.22.0zhconv1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147import platform
#Platform Details
OPERATING_SYSTEM = platform.system()
SYSTEM_ARCH = platform.platform()
SYSTEM_PROC = platform.processor()
ARM = 'arm'
#Main Font
MAIN_FONT_NAME = "Century Gothic"
#Model Types
VR_ARCH_TYPE = 'VR Arc'
MDX_ARCH_TYPE = 'MDX-Net'
DEMUCS_ARCH_TYPE = 'Demucs'
VR_ARCH_PM = 'VR Architecture'
ENSEMBLE_MODE = 'Ensemble Mode'
ENSEMBLE_STEM_CHECK = 'Ensemble Stem'
SECONDARY_MODEL = 'Secondary Model'
DEMUCS_6_STEM_MODEL = 'htdemucs_6s'
DEMUCS_V3_ARCH_TYPE = 'Demucs v3'
DEMUCS_V4_ARCH_TYPE = 'Demucs v4'
DEMUCS_NEWER_ARCH_TYPES = [DEMUCS_V3_ARCH_TYPE, DEMUCS_V4_ARCH_TYPE]
DEMUCS_V1 = 'v1'
DEMUCS_V2 = 'v2'
DEMUCS_V3 = 'v3'
DEMUCS_V4 = 'v4'
DEMUCS_V1_TAG = 'v1 | '
DEMUCS_V2_TAG = 'v2 | '
DEMUCS_V3_TAG = 'v3 | '
DEMUCS_V4_TAG = 'v4 | '
DEMUCS_NEWER_TAGS = [DEMUCS_V3_TAG, DEMUCS_V4_TAG]
DEMUCS_VERSION_MAPPER = {
DEMUCS_V1:DEMUCS_V1_TAG,
DEMUCS_V2:DEMUCS_V2_TAG,
DEMUCS_V3:DEMUCS_V3_TAG,
DEMUCS_V4:DEMUCS_V4_TAG}
#Download Center
DOWNLOAD_FAILED = 'Download Failed'
DOWNLOAD_STOPPED = 'Download Stopped'
DOWNLOAD_COMPLETE = 'Download Complete'
DOWNLOAD_UPDATE_COMPLETE = 'Update Download Complete'
SETTINGS_MENU_EXIT = 'exit'
NO_CONNECTION = 'No Internet Connection'
VIP_SELECTION = 'VIP:'
DEVELOPER_SELECTION = 'VIP:'
NO_NEW_MODELS = 'All Available Models Downloaded'
ENSEMBLE_PARTITION = ': '
NO_MODEL = 'No Model Selected'
CHOOSE_MODEL = 'Choose Model'
SINGLE_DOWNLOAD = 'Downloading Item 1/1...'
DOWNLOADING_ITEM = 'Downloading Item'
FILE_EXISTS = 'File already exists!'
DOWNLOADING_UPDATE = 'Downloading Update...'
DOWNLOAD_MORE = 'Download More Models'
#Menu Options
AUTO_SELECT = 'Auto'
#LINKS
DOWNLOAD_CHECKS = "https://raw.githubusercontent.com/TRvlvr/application_data/main/filelists/download_checks.json"
MDX_MODEL_DATA_LINK = "https://raw.githubusercontent.com/TRvlvr/application_data/main/mdx_model_data/model_data.json"
VR_MODEL_DATA_LINK = "https://raw.githubusercontent.com/TRvlvr/application_data/main/vr_model_data/model_data.json"
DEMUCS_MODEL_NAME_DATA_LINK = "https://raw.githubusercontent.com/TRvlvr/application_data/main/demucs_model_data/model_name_mapper.json"
MDX_MODEL_NAME_DATA_LINK = "https://raw.githubusercontent.com/TRvlvr/application_data/main/mdx_model_data/model_name_mapper.json"
DONATE_LINK_BMAC = "https://www.buymeacoffee.com/uvr5"
DONATE_LINK_PATREON = "https://www.patreon.com/uvr"
#DOWNLOAD REPOS
NORMAL_REPO = "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/"
UPDATE_REPO = "https://github.com/TRvlvr/model_repo/releases/download/uvr_update_patches/"
UPDATE_MAC_ARM_REPO = "https://github.com/Anjok07/ultimatevocalremovergui/releases/download/v5.5.0/Ultimate_Vocal_Remover_v5_5_MacOS_arm64.dmg"
UPDATE_MAC_X86_64_REPO = "https://github.com/Anjok07/ultimatevocalremovergui/releases/download/v5.5.0/Ultimate_Vocal_Remover_v5_5_MacOS_x86_64.dmg"
UPDATE_LINUX_REPO = "https://github.com/Anjok07/ultimatevocalremovergui#linux-installation"
UPDATE_REPO = "https://github.com/TRvlvr/model_repo/releases/download/uvr_update_patches/"
ISSUE_LINK = 'https://github.com/Anjok07/ultimatevocalremovergui/issues/new'
VIP_REPO = b'\xf3\xc2W\x19\x1foI)\xc2\xa9\xcc\xb67(Z\xf5',\
b'gAAAAABjQAIQ-NpNMMxMedpKHHb7ze_nqB05hw0YhbOy3pFzuzDrfqumn8_qvraxEoUpZC5ZXC0gGvfDxFMqyq9VWbYKlA67SUFI_wZB6QoVyGI581vs7kaGfUqlXHIdDS6tQ_U-BfjbEAK9EU_74-R2zXjz8Xzekw=='
NO_CODE = 'incorrect_code'
#Extensions
ONNX = '.onnx'
CKPT = '.ckpt'
YAML = '.yaml'
PTH = '.pth'
TH_EXT = '.th'
JSON = '.json'
#GUI Buttons
START_PROCESSING = 'Start Processing'
WAIT_PROCESSING = 'Please wait...'
STOP_PROCESSING = 'Halting process, please wait...'
LOADING_MODELS = 'Loading models...'
#---Messages and Logs----
MISSING_MODEL = 'missing'
MODEL_PRESENT = 'present'
UNRECOGNIZED_MODEL = 'Unrecognized Model Detected', ' is an unrecognized model.\n\n' + \
'Would you like to select the correct parameters before continuing?'
STOP_PROCESS_CONFIRM = 'Confirmation', 'You are about to stop all active processes.\n\nAre you sure you wish to continue?'
NO_ENSEMBLE_SELECTED = 'No Models Selected', 'Please select ensemble and try again.'
PICKLE_CORRU = 'File Corrupted', 'Unable to load this ensemble.\n\n' + \
'Would you like to remove this ensemble from your list?'
DELETE_ENS_ENTRY = 'Confirm Removal', 'Are you sure you want to remove this entry?'
ALL_STEMS = 'All Stems'
VOCAL_STEM = 'Vocals'
INST_STEM = 'Instrumental'
OTHER_STEM = 'Other'
BASS_STEM = 'Bass'
DRUM_STEM = 'Drums'
GUITAR_STEM = 'Guitar'
PIANO_STEM = 'Piano'
SYNTH_STEM = 'Synthesizer'
STRINGS_STEM = 'Strings'
WOODWINDS_STEM = 'Woodwinds'
BRASS_STEM = 'Brass'
WIND_INST_STEM = 'Wind Inst'
NO_OTHER_STEM = 'No Other'
NO_BASS_STEM = 'No Bass'
NO_DRUM_STEM = 'No Drums'
NO_GUITAR_STEM = 'No Guitar'
NO_PIANO_STEM = 'No Piano'
NO_SYNTH_STEM = 'No Synthesizer'
NO_STRINGS_STEM = 'No Strings'
NO_WOODWINDS_STEM = 'No Woodwinds'
NO_WIND_INST_STEM = 'No Wind Inst'
NO_BRASS_STEM = 'No Brass'
PRIMARY_STEM = 'Primary Stem'
SECONDARY_STEM = 'Secondary Stem'
#Other Constants
DEMUCS_2_SOURCE = ["instrumental", "vocals"]
DEMUCS_4_SOURCE = ["drums", "bass", "other", "vocals"]
DEMUCS_2_SOURCE_MAPPER = {
INST_STEM: 0,
VOCAL_STEM: 1}
DEMUCS_4_SOURCE_MAPPER = {
BASS_STEM: 0,
DRUM_STEM: 1,
OTHER_STEM: 2,
VOCAL_STEM: 3}
DEMUCS_6_SOURCE_MAPPER = {
BASS_STEM: 0,
DRUM_STEM: 1,
OTHER_STEM: 2,
VOCAL_STEM: 3,
GUITAR_STEM:4,
PIANO_STEM:5}
DEMUCS_4_SOURCE_LIST = [BASS_STEM, DRUM_STEM, OTHER_STEM, VOCAL_STEM]
DEMUCS_6_SOURCE_LIST = [BASS_STEM, DRUM_STEM, OTHER_STEM, VOCAL_STEM, GUITAR_STEM, PIANO_STEM]
DEMUCS_UVR_MODEL = 'UVR_Model'
CHOOSE_STEM_PAIR = 'Choose Stem Pair'
STEM_SET_MENU = (VOCAL_STEM,
INST_STEM,
OTHER_STEM,
BASS_STEM,
DRUM_STEM,
GUITAR_STEM,
PIANO_STEM,
SYNTH_STEM,
STRINGS_STEM,
WOODWINDS_STEM,
BRASS_STEM,
WIND_INST_STEM,
NO_OTHER_STEM,
NO_BASS_STEM,
NO_DRUM_STEM,
NO_GUITAR_STEM,
NO_PIANO_STEM,
NO_SYNTH_STEM,
NO_STRINGS_STEM,
NO_WOODWINDS_STEM,
NO_BRASS_STEM,
NO_WIND_INST_STEM)
STEM_PAIR_MAPPER = {
VOCAL_STEM: INST_STEM,
INST_STEM: VOCAL_STEM,
OTHER_STEM: NO_OTHER_STEM,
BASS_STEM: NO_BASS_STEM,
DRUM_STEM: NO_DRUM_STEM,
GUITAR_STEM: NO_GUITAR_STEM,
PIANO_STEM: NO_PIANO_STEM,
SYNTH_STEM: NO_SYNTH_STEM,
STRINGS_STEM: NO_STRINGS_STEM,
WOODWINDS_STEM: NO_WOODWINDS_STEM,
BRASS_STEM: NO_BRASS_STEM,
WIND_INST_STEM: NO_WIND_INST_STEM,
NO_OTHER_STEM: OTHER_STEM,
NO_BASS_STEM: BASS_STEM,
NO_DRUM_STEM: DRUM_STEM,
NO_GUITAR_STEM: GUITAR_STEM,
NO_PIANO_STEM: PIANO_STEM,
NO_SYNTH_STEM: SYNTH_STEM,
NO_STRINGS_STEM: STRINGS_STEM,
NO_WOODWINDS_STEM: WOODWINDS_STEM,
NO_BRASS_STEM: BRASS_STEM,
NO_WIND_INST_STEM: WIND_INST_STEM,
PRIMARY_STEM: SECONDARY_STEM}
NON_ACCOM_STEMS = (
VOCAL_STEM,
OTHER_STEM,
BASS_STEM,
DRUM_STEM,
GUITAR_STEM,
PIANO_STEM,
SYNTH_STEM,
STRINGS_STEM,
WOODWINDS_STEM,
BRASS_STEM,
WIND_INST_STEM)
MDX_NET_FREQ_CUT = [VOCAL_STEM, INST_STEM]
DEMUCS_4_STEM_OPTIONS = (ALL_STEMS, VOCAL_STEM, OTHER_STEM, BASS_STEM, DRUM_STEM)
DEMUCS_6_STEM_OPTIONS = (ALL_STEMS, VOCAL_STEM, OTHER_STEM, BASS_STEM, DRUM_STEM, GUITAR_STEM, PIANO_STEM)
DEMUCS_2_STEM_OPTIONS = (VOCAL_STEM, INST_STEM)
DEMUCS_4_STEM_CHECK = (OTHER_STEM, BASS_STEM, DRUM_STEM)
#Menu Dropdowns
VOCAL_PAIR = f'{VOCAL_STEM}/{INST_STEM}'
INST_PAIR = f'{INST_STEM}/{VOCAL_STEM}'
OTHER_PAIR = f'{OTHER_STEM}/{NO_OTHER_STEM}'
DRUM_PAIR = f'{DRUM_STEM}/{NO_DRUM_STEM}'
BASS_PAIR = f'{BASS_STEM}/{NO_BASS_STEM}'
FOUR_STEM_ENSEMBLE = '4 Stem Ensemble'
ENSEMBLE_MAIN_STEM = (CHOOSE_STEM_PAIR, VOCAL_PAIR, OTHER_PAIR, DRUM_PAIR, BASS_PAIR, FOUR_STEM_ENSEMBLE)
MIN_SPEC = 'Min Spec'
MAX_SPEC = 'Max Spec'
AUDIO_AVERAGE = 'Average'
MAX_MIN = f'{MAX_SPEC}/{MIN_SPEC}'
MAX_MAX = f'{MAX_SPEC}/{MAX_SPEC}'
MAX_AVE = f'{MAX_SPEC}/{AUDIO_AVERAGE}'
MIN_MAX = f'{MIN_SPEC}/{MAX_SPEC}'
MIN_MIX = f'{MIN_SPEC}/{MIN_SPEC}'
MIN_AVE = f'{MIN_SPEC}/{AUDIO_AVERAGE}'
AVE_MAX = f'{AUDIO_AVERAGE}/{MAX_SPEC}'
AVE_MIN = f'{AUDIO_AVERAGE}/{MIN_SPEC}'
AVE_AVE = f'{AUDIO_AVERAGE}/{AUDIO_AVERAGE}'
ENSEMBLE_TYPE = (MAX_MIN, MAX_MAX, MAX_AVE, MIN_MAX, MIN_MIX, MIN_AVE, AVE_MAX, AVE_MIN, AVE_AVE)
ENSEMBLE_TYPE_4_STEM = (MAX_SPEC, MIN_SPEC, AUDIO_AVERAGE)
BATCH_MODE = 'Batch Mode'
BETA_VERSION = 'BETA'
DEF_OPT = 'Default'
CHUNKS = (AUTO_SELECT, '1', '5', '10', '15', '20',
'25', '30', '35', '40', '45', '50',
'55', '60', '65', '70', '75', '80',
'85', '90', '95', 'Full')
BATCH_SIZE = (DEF_OPT, '2', '3', '4', '5',
'6', '7', '8', '9', '10')
VOL_COMPENSATION = (AUTO_SELECT, '1.035', '1.08')
MARGIN_SIZE = ('44100', '22050', '11025')
AUDIO_TOOLS = 'Audio Tools'
MANUAL_ENSEMBLE = 'Manual Ensemble'
TIME_STRETCH = 'Time Stretch'
CHANGE_PITCH = 'Change Pitch'
ALIGN_INPUTS = 'Align Inputs'
if OPERATING_SYSTEM == 'Windows' or OPERATING_SYSTEM == 'Darwin':
AUDIO_TOOL_OPTIONS = (MANUAL_ENSEMBLE, TIME_STRETCH, CHANGE_PITCH, ALIGN_INPUTS)
else:
AUDIO_TOOL_OPTIONS = (MANUAL_ENSEMBLE, ALIGN_INPUTS)
MANUAL_ENSEMBLE_OPTIONS = (MIN_SPEC, MAX_SPEC, AUDIO_AVERAGE)
PROCESS_METHODS = (VR_ARCH_PM, MDX_ARCH_TYPE, DEMUCS_ARCH_TYPE, ENSEMBLE_MODE, AUDIO_TOOLS)
DEMUCS_SEGMENTS = ('Default', '1', '5', '10', '15', '20',
'25', '30', '35', '40', '45', '50',
'55', '60', '65', '70', '75', '80',
'85', '90', '95', '100')
DEMUCS_SHIFTS = (0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17,
18, 19, 20)
DEMUCS_OVERLAP = (0.25, 0.50, 0.75, 0.99)
VR_AGGRESSION = (1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17,
18, 19, 20)
VR_WINDOW = ('320', '512','1024')
VR_CROP = ('256', '512', '1024')
POST_PROCESSES_THREASHOLD_VALUES = ('0.1', '0.2', '0.3')
MDX_POP_PRO = ('MDX-NET_Noise_Profile_14_kHz', 'MDX-NET_Noise_Profile_17_kHz', 'MDX-NET_Noise_Profile_Full_Band')
MDX_POP_STEMS = ('Vocals', 'Instrumental', 'Other', 'Drums', 'Bass')
MDX_POP_NFFT = ('4096', '5120', '6144', '7680', '8192', '16384')
MDX_POP_DIMF = ('2048', '3072', '4096')
SAVE_ENSEMBLE = 'Save Ensemble'
CLEAR_ENSEMBLE = 'Clear Selection(s)'
MENU_SEPARATOR = 35*'•'
CHOOSE_ENSEMBLE_OPTION = 'Choose Option'
INVALID_ENTRY = 'Invalid Input, Please Try Again'
ENSEMBLE_INPUT_RULE = '1. Only letters, numbers, spaces, and dashes allowed.\n2. No dashes or spaces at the start or end of input.'
ENSEMBLE_OPTIONS = (SAVE_ENSEMBLE, CLEAR_ENSEMBLE)
ENSEMBLE_CHECK = 'ensemble check'
SELECT_SAVED_ENSEMBLE = 'Select Saved Ensemble'
SELECT_SAVED_SETTING = 'Select Saved Setting'
ENSEMBLE_OPTION = "Ensemble Customization Options"
MDX_OPTION = "Advanced MDX-Net Options"
DEMUCS_OPTION = "Advanced Demucs Options"
VR_OPTION = "Advanced VR Options"
HELP_OPTION = "Open Information Guide"
ERROR_OPTION = "Open Error Log"
VERIFY_BEGIN = 'Verifying file '
SAMPLE_BEGIN = 'Creating Sample '
MODEL_MISSING_CHECK = 'Model Missing:'
# Audio Player
PLAYING_SONG = ": Playing"
PAUSE_SONG = ": Paused"
STOP_SONG = ": Stopped"
SELECTED_VER = 'Selected'
DETECTED_VER = 'Detected'
SAMPLE_MODE_CHECKBOX = lambda v:f'Sample Mode ({v}s)'
REMOVED_FILES = lambda r, e:f'Audio Input Verification Report:\n\nRemoved Files:\n\n{r}\n\nError Details:\n\n{e}'
ADVANCED_SETTINGS = (ENSEMBLE_OPTION, MDX_OPTION, DEMUCS_OPTION, VR_OPTION, HELP_OPTION, ERROR_OPTION)
WAV = 'WAV'
FLAC = 'FLAC'
MP3 = 'MP3'
MP3_BIT_RATES = ('96k', '128k', '160k', '224k', '256k', '320k')
WAV_TYPE = ('PCM_U8', 'PCM_16', 'PCM_24', 'PCM_32', '32-bit Float', '64-bit Float')
SELECT_SAVED_SET = 'Choose Option'
SAVE_SETTINGS = 'Save Current Settings'
RESET_TO_DEFAULT = 'Reset to Default'
RESET_FULL_TO_DEFAULT = 'Reset to Default'
RESET_PM_TO_DEFAULT = 'Reset All Application Settings to Default'
SAVE_SET_OPTIONS = (SAVE_SETTINGS, RESET_TO_DEFAULT)
TIME_PITCH = ('1.0', '2.0', '3.0', '4.0')
TIME_TEXT = '_time_stretched'
PITCH_TEXT = '_pitch_shifted'
#RegEx Input Validation
REG_PITCH = r'^[-+]?(1[0]|[0-9]([.][0-9]*)?)$'
REG_TIME = r'^[+]?(1[0]|[0-9]([.][0-9]*)?)$'
REG_COMPENSATION = r'\b^(1[0]|[0-9]([.][0-9]*)?|Auto|None)$\b'
REG_THES_POSTPORCESS = r'\b^([0]([.][0-9]{0,6})?)$\b'
REG_CHUNKS = r'\b^(200|1[0-9][0-9]|[1-9][0-9]?|Auto|Full)$\b'
REG_CHUNKS_DEMUCS = r'\b^(200|1[0-9][0-9]|[1-9][0-9]?|Auto|Full)$\b'
REG_MARGIN = r'\b^[0-9]*$\b'
REG_SEGMENTS = r'\b^(200|1[0-9][0-9]|[1-9][0-9]?|Default)$\b'
REG_SAVE_INPUT = r'\b^([a-zA-Z0-9 -]{0,25})$\b'
REG_AGGRESSION = r'^[-+]?[0-9]\d*?$'
REG_WINDOW = r'\b^[0-9]{0,4}$\b'
REG_SHIFTS = r'\b^[0-9]*$\b'
REG_BATCHES = r'\b^([0-9]*?|Default)$\b'
REG_OVERLAP = r'\b^([0]([.][0-9]{0,6})?|None)$\b'
# Sub Menu
VR_ARCH_SETTING_LOAD = 'Load for VR Arch'
MDX_SETTING_LOAD = 'Load for MDX-Net'
DEMUCS_SETTING_LOAD = 'Load for Demucs'
ALL_ARCH_SETTING_LOAD = 'Load for Full Application'
# Mappers
DEFAULT_DATA = {
'chosen_process_method': MDX_ARCH_TYPE,
'vr_model': CHOOSE_MODEL,
'aggression_setting': 10,
'window_size': 512,
'batch_size': 4,
'crop_size': 256,
'is_tta': False,
'is_output_image': False,
'is_post_process': False,
'is_high_end_process': False,
'post_process_threshold': 0.2,
'vr_voc_inst_secondary_model': NO_MODEL,
'vr_other_secondary_model': NO_MODEL,
'vr_bass_secondary_model': NO_MODEL,
'vr_drums_secondary_model': NO_MODEL,
'vr_is_secondary_model_activate': False,
'vr_voc_inst_secondary_model_scale': 0.9,
'vr_other_secondary_model_scale': 0.7,
'vr_bass_secondary_model_scale': 0.5,
'vr_drums_secondary_model_scale': 0.5,
'demucs_model': CHOOSE_MODEL,
'demucs_stems': ALL_STEMS,
'segment': DEMUCS_SEGMENTS[0],
'overlap': DEMUCS_OVERLAP[0],
'shifts': 2,
'chunks_demucs': CHUNKS[0],
'margin_demucs': 44100,
'is_chunk_demucs': False,
'is_chunk_mdxnet': False,
'is_primary_stem_only_Demucs': False,
'is_secondary_stem_only_Demucs': False,
'is_split_mode': True,
'is_demucs_combine_stems': True,
'demucs_voc_inst_secondary_model': NO_MODEL,
'demucs_other_secondary_model': NO_MODEL,
'demucs_bass_secondary_model': NO_MODEL,
'demucs_drums_secondary_model': NO_MODEL,
'demucs_is_secondary_model_activate': False,
'demucs_voc_inst_secondary_model_scale': 0.9,
'demucs_other_secondary_model_scale': 0.7,
'demucs_bass_secondary_model_scale': 0.5,
'demucs_drums_secondary_model_scale': 0.5,
'demucs_stems': ALL_STEMS,
'demucs_pre_proc_model': NO_MODEL,
'is_demucs_pre_proc_model_activate': False,
'is_demucs_pre_proc_model_inst_mix': False,
'mdx_net_model': CHOOSE_MODEL,
'chunks': CHUNKS[0],
'margin': 44100,
'compensate': AUTO_SELECT,
'is_denoise': False,
'is_invert_spec': False,
'is_mixer_mode': False,
'mdx_batch_size': DEF_OPT,
'mdx_voc_inst_secondary_model': NO_MODEL,
'mdx_other_secondary_model': NO_MODEL,
'mdx_bass_secondary_model': NO_MODEL,
'mdx_drums_secondary_model': NO_MODEL,
'mdx_is_secondary_model_activate': False,
'mdx_voc_inst_secondary_model_scale': 0.9,
'mdx_other_secondary_model_scale': 0.7,
'mdx_bass_secondary_model_scale': 0.5,
'mdx_drums_secondary_model_scale': 0.5,
'is_save_all_outputs_ensemble': True,
'is_append_ensemble_name': False,
'chosen_audio_tool': AUDIO_TOOL_OPTIONS[0],
'choose_algorithm': MANUAL_ENSEMBLE_OPTIONS[0],
'time_stretch_rate': 2.0,
'pitch_rate': 2.0,
'is_gpu_conversion': False,
'is_primary_stem_only': False,
'is_secondary_stem_only': False,
'is_testing_audio': False,
'is_add_model_name': False,
'is_accept_any_input': False,
'is_task_complete': False,
'is_normalization': False,
'is_create_model_folder': False,
'mp3_bit_set': '320k',
'save_format': WAV,
'wav_type_set': 'PCM_16',
'user_code': '',
'export_path': '',
'input_paths': [],
'lastDir': None,
'export_path': '',
'model_hash_table': None,
'help_hints_var': False,
'model_sample_mode': False,
'model_sample_mode_duration': 30
}
SETTING_CHECK = ('vr_model',
'aggression_setting',
'window_size',
'batch_size',
'crop_size',
'is_tta',
'is_output_image',
'is_post_process',
'is_high_end_process',
'post_process_threshold',
'vr_voc_inst_secondary_model',
'vr_other_secondary_model',
'vr_bass_secondary_model',
'vr_drums_secondary_model',
'vr_is_secondary_model_activate',
'vr_voc_inst_secondary_model_scale',
'vr_other_secondary_model_scale',
'vr_bass_secondary_model_scale',
'vr_drums_secondary_model_scale',
'demucs_model',
'segment',
'overlap',
'shifts',
'chunks_demucs',
'margin_demucs',
'is_chunk_demucs',
'is_primary_stem_only_Demucs',
'is_secondary_stem_only_Demucs',
'is_split_mode',
'is_demucs_combine_stems',
'demucs_voc_inst_secondary_model',
'demucs_other_secondary_model',
'demucs_bass_secondary_model',
'demucs_drums_secondary_model',
'demucs_is_secondary_model_activate',
'demucs_voc_inst_secondary_model_scale',
'demucs_other_secondary_model_scale',
'demucs_bass_secondary_model_scale',
'demucs_drums_secondary_model_scale',
'demucs_stems',
'mdx_net_model',
'chunks',
'margin',
'compensate',
'is_denoise',
'is_invert_spec',
'mdx_batch_size',
'mdx_voc_inst_secondary_model',
'mdx_other_secondary_model',
'mdx_bass_secondary_model',
'mdx_drums_secondary_model',
'mdx_is_secondary_model_activate',
'mdx_voc_inst_secondary_model_scale',
'mdx_other_secondary_model_scale',
'mdx_bass_secondary_model_scale',
'mdx_drums_secondary_model_scale',
'is_save_all_outputs_ensemble',
'is_append_ensemble_name',
'chosen_audio_tool',
'choose_algorithm',
'time_stretch_rate',
'pitch_rate',
'is_primary_stem_only',
'is_secondary_stem_only',
'is_testing_audio',
'is_add_model_name',
"is_accept_any_input",
'is_task_complete',
'is_create_model_folder',
'mp3_bit_set',
'save_format',
'wav_type_set',
'user_code',
'is_gpu_conversion',
'is_normalization',
'help_hints_var',
'model_sample_mode',
'model_sample_mode_duration')
# Message Box Text
INVALID_INPUT = 'Invalid Input', 'The input is invalid.\n\nPlease verify the input still exists or is valid and try again.'
INVALID_EXPORT = 'Invalid Export Directory', 'You have selected an invalid export directory.\n\nPlease make sure the selected directory still exists.'
INVALID_ENSEMBLE = 'Not Enough Models', 'You must select 2 or more models to run ensemble.'
INVALID_MODEL = 'No Model Chosen', 'You must select an model to continue.'
MISSING_MODEL = 'Model Missing', 'The selected model is missing or not valid.'
ERROR_OCCURED = 'Error Occured', '\n\nWould you like to open the error log for more details?\n'
# GUI Text Constants
BACK_TO_MAIN_MENU = 'Back to Main Menu'
# Help Hint Text
INTERNAL_MODEL_ATT = 'Internal model attribute. \n\n ***Do not change this setting if you are unsure!***'
STOP_HELP = 'Halts any running processes. \n A pop-up window will ask the user to confirm the action.'
SETTINGS_HELP = 'Opens the main settings guide. This window includes the \"Download Center\"'
COMMAND_TEXT_HELP = 'Provides information on the progress of the current process.'
SAVE_CURRENT_SETTINGS_HELP = 'Allows the user to open any saved settings or save the current application settings.'
CHUNKS_HELP = ('For MDX-Net, all values use the same amount of resources. Using chunks is no longer recommended.\n\n' + \
'• This option is now only for output quality.\n' + \
'• Some tracks may fare better depending on the value.\n' + \
'• Some tracks may fare worse depending on the value.\n' + \
'• Larger chunk sizes use will take less time to process.\n' +\
'• Smaller chunk sizes use will take more time to process.\n')
CHUNKS_DEMUCS_HELP = ('This option allows the user to reduce (or increase) RAM or V-RAM usage.\n\n' + \
'• Smaller chunk sizes use less RAM or V-RAM but can also increase processing times.\n' + \
'• Larger chunk sizes use more RAM or V-RAM but can also reduce processing times.\n' + \
'• Selecting \"Auto\" calculates an appropriate chuck size based on how much RAM or V-RAM your system has.\n' + \
'• Selecting \"Full\" will process the track as one whole chunk. (not recommended)\n' + \
'• The default selection is \"Auto\".')
MARGIN_HELP = 'Selects the frequency margins to slice the chunks from.\n\n• The recommended margin size is 44100.\n• Other values can give unpredictable results.'
AGGRESSION_SETTING_HELP = ('This option allows you to set how strong the primary stem extraction will be.\n\n' + \
'• The range is 0-100.\n' + \
'• Higher values perform deeper extractions.\n' + \
'• The default is 10 for instrumental & vocal models.\n' + \
'• Values over 10 can result in muddy-sounding instrumentals for the non-vocal models')
WINDOW_SIZE_HELP = ('The smaller your window size, the better your conversions will be. \nHowever, a smaller window means longer conversion times and heavier resource usage.\n\n' + \
'Breakdown of the selectable window size values:\n' + \
'• 1024 - Low conversion quality, shortest conversion time, low resource usage.\n' + \
'• 512 - Average conversion quality, average conversion time, normal resource usage.\n' + \
'• 320 - Better conversion quality.')
DEMUCS_STEMS_HELP = ('Here, you can choose which stem to extract using the selected model.\n\n' +\
'Stem Selections:\n\n' +\
'• All Stems - Saves all of the stems the model is able to extract.\n' +\
'• Vocals - Pulls vocal stem only.\n' +\
'• Other - Pulls other stem only.\n' +\
'• Bass - Pulls bass stem only.\n' +\
'• Drums - Pulls drum stem only.\n')
SEGMENT_HELP = ('This option allows the user to reduce (or increase) RAM or V-RAM usage.\n\n' + \
'• Smaller segment sizes use less RAM or V-RAM but can also increase processing times.\n' + \
'• Larger segment sizes use more RAM or V-RAM but can also reduce processing times.\n' + \
'• Selecting \"Default\" uses the recommended segment size.\n' + \
'• It is recommended that you not use segments with \"Chunking\".')
ENSEMBLE_MAIN_STEM_HELP = 'Allows the user to select the type of stems they wish to ensemble.\n\nOptions:\n\n' +\
f'• {VOCAL_PAIR} - The primary stem will be the vocals and the secondary stem will be the the instrumental\n' +\
f'• {OTHER_PAIR} - The primary stem will be other and the secondary stem will be no other (the mixture without the \'other\' stem)\n' +\
f'• {BASS_PAIR} - The primary stem will be bass and the secondary stem will be no bass (the mixture without the \'bass\' stem)\n' +\
f'• {DRUM_PAIR} - The primary stem will be drums and the secondary stem will be no drums (the mixture without the \'drums\' stem)\n' +\
f'• {FOUR_STEM_ENSEMBLE} - This option will gather all the 4 stem Demucs models and ensemble all of the outputs.\n'
ENSEMBLE_TYPE_HELP = 'Allows the user to select the ensemble algorithm to be used to generate the final output.\n\nExample & Other Note:\n\n' +\
f'• {MAX_MIN} - If this option is chosen, the primary stem outputs will be processed through \nthe \'Max Spec\' algorithm, and the secondary stem will be processed through the \'Min Spec\' algorithm.\n' +\
f'• Only a single algorithm will be shown when the \'4 Stem Ensemble\' option is chosen.\n\nAlgorithm Details:\n\n' +\
f'• {MAX_SPEC} - This algorithm combines the final results and generates the highest possible output from them.\nFor example, if this algorithm were processing vocal stems, you would get the fullest possible \n' +\
'result making the ensembled vocal stem sound cleaner. However, it might result in more unwanted artifacts.\n' +\
f'• {MIN_SPEC} - This algorithm combines the results and generates the lowest possible output from them.\nFor example, if this algorithm were processing instrumental stems, you would get the cleanest possible result \n' +\
'result, eliminating more unwanted artifacts. However, the result might also sound \'muddy\' and lack a fuller sound.\n' +\
f'• {AUDIO_AVERAGE} - This algorithm simply combines the results and averages all of them together. \n'
ENSEMBLE_LISTBOX_HELP = 'List of the all the models available for the main stem pair selected.'
IS_GPU_CONVERSION_HELP = ('When checked, the application will attempt to use your GPU (if you have one).\n' +\
'If you do not have a GPU but have this checked, the application will default to your CPU.\n\n' +\
'Note: CPU conversions are much slower than those processed through the GPU.')
SAVE_STEM_ONLY_HELP = 'Allows the user to save only the selected stem.'
IS_NORMALIZATION_HELP = 'Normalizes output to prevent clipping.'
CROP_SIZE_HELP = '**Only compatible with select models only!**\n\n Setting should match training crop-size value. Leave as is if unsure.'
IS_TTA_HELP = ('This option performs Test-Time-Augmentation to improve the separation quality.\n\n' +\
'Note: Having this selected will increase the time it takes to complete a conversion')
IS_POST_PROCESS_HELP = ('This option can potentially identify leftover instrumental artifacts within the vocal outputs. \nThis option may improve the separation of some songs.\n\n' +\
'Note: Selecting this option can adversely affect the conversion process, depending on the track. Because of this, it is only recommended as a last resort.')
IS_HIGH_END_PROCESS_HELP = 'The application will mirror the missing frequency range of the output.'
SHIFTS_HELP = ('Performs multiple predictions with random shifts of the input and averages them.\n\n' +\
'• The higher number of shifts, the longer the prediction will take. \n- Not recommended unless you have a GPU.')
OVERLAP_HELP = 'This option controls the amount of overlap between prediction windows (for Demucs one window is 10 seconds)'
IS_CHUNK_DEMUCS_HELP = '• Enables \"Chunks\".\n• We recommend you not enable this option with \"Split Mode\" enabled or with the Demucs v4 Models.'
IS_CHUNK_MDX_NET_HELP = '• Enables \"Chunks\".\n• Using this option for MDX-Net no longer effects RAM usage.\n• Having this enabled will effect output quality, for better or worse depending on the set value.'
IS_SPLIT_MODE_HELP = ('• Enables \"Segments\". \n• We recommend you not enable this option with \"Enable Chunks\".\n' +\
'• Deselecting this option is only recommended for those with powerful PCs or if using \"Chunk\" mode instead.')
IS_DEMUCS_COMBINE_STEMS_HELP = 'The application will create the secondary stem by combining the remaining stems \ninstead of inverting the primary stem with the mixture.'
COMPENSATE_HELP = 'Compensates the audio of the primary stems to allow for a better secondary stem.'
IS_DENOISE_HELP = '• This option removes a majority of the noise generated by the MDX-Net models.\n• The conversion will take nearly twice as long with this enabled.'
CLEAR_CACHE_HELP = 'Clears any user selected model settings for previously unrecognized models.'
IS_SAVE_ALL_OUTPUTS_ENSEMBLE_HELP = 'Enabling this option will keep all indivudual outputs generated by an ensemble.'
IS_APPEND_ENSEMBLE_NAME_HELP = 'The application will append the ensemble name to the final output \nwhen this option is enabled.'
DONATE_HELP = 'Takes the user to an external web-site to donate to this project!'
IS_INVERT_SPEC_HELP = '• This option may produce a better secondary stem.\n• Inverts primary stem with mixture using spectragrams instead of wavforms.\n• This inversion method is slightly slower.'
IS_MIXER_MODE_HELP = '• This option may improve separations for outputs from 4-stem models.\n• Might produce more noise.\n• This option might slow down separation time.'
IS_TESTING_AUDIO_HELP = 'Appends a unique 10 digit number to output files so the user \ncan compare results with different settings.'
IS_MODEL_TESTING_AUDIO_HELP = 'Appends the model name to output files so the user \ncan compare results with different settings.'
IS_ACCEPT_ANY_INPUT_HELP = 'The application will accept any input when enabled, even if it does not have an audio format extension.\n\nThis is for experimental purposes, and having it enabled is not recommended.'
IS_TASK_COMPLETE_HELP = 'When enabled, chimes will be heard when a process completes or fails.'
IS_CREATE_MODEL_FOLDER_HELP = 'Two new directories will be generated for the outputs in \nthe export directory after each conversion.\n\n' +\
'• First directory - Named after the model.\n' +\
'• Second directory - Named after the track.\n\n' +\
'• Example: \n\n' +\
'─ Export Directory\n' +\
' └── First Directory\n' +\
' └── Second Directory\n' +\
' └── Output File(s)'
DELETE_YOUR_SETTINGS_HELP = 'This menu contains your saved settings. You will be asked to \nconfirm if you wish to delete the selected setting.'
SET_STEM_NAME_HELP = 'Choose the primary stem for the selected model.'
MDX_DIM_T_SET_HELP = INTERNAL_MODEL_ATT
MDX_DIM_F_SET_HELP = INTERNAL_MODEL_ATT
MDX_N_FFT_SCALE_SET_HELP = 'Set the N_FFT size the model was trained with.'
POPUP_COMPENSATE_HELP = f'Choose the appropriate voluem compensattion for the selected model\n\nReminder: {COMPENSATE_HELP}'
VR_MODEL_PARAM_HELP = 'Choose the parameters needed to run the selected model.'
CHOSEN_ENSEMBLE_HELP = 'Select saved enselble or save current ensemble.\n\nDefault Selections:\n\n• Save the current ensemble.\n• Clears all current model selections.'
CHOSEN_PROCESS_METHOD_HELP = 'Here, you choose between different Al networks and algorithms to process your track.\n\n' +\
'There are five options:\n\n' +\
'• VR Architecture - These models use magnitude spectrograms for Source Separation.\n' +\
'• MDX-Net - These models use Hybrid Spectrogram/Waveform for Source Separation.\n' +\
'• Demucs v3 - These models use Hybrid Spectrogram/Waveform for Source Separation.\n' +\
'• Ensemble Mode - Here, you can get the best results from multiple models and networks.\n' +\
'• Audio Tools - These are additional tools for added convenience.'
INPUT_FOLDER_ENTRY_HELP = 'Select Input:\n\nHere is where you select the audio files(s) you wish to process.'
INPUT_FOLDER_ENTRY_HELP_2 = 'Input Option Menu:\n\nClick here to access the input option menu.'
OUTPUT_FOLDER_ENTRY_HELP = 'Select Output:\n\nHere is where you select the directory where your processed files are to be saved.'
INPUT_FOLDER_BUTTON_HELP = 'Open Input Folder Button: \n\nOpens the directory containing the selected input audio file(s).'
OUTPUT_FOLDER_BUTTON_HELP = 'Open Output Folder Button: \n\nOpens the selected output folder.'
CHOOSE_MODEL_HELP = 'Each process method comes with its own set of options and models.\n\nHere is where you choose the model associated with the selected process method.'
FORMAT_SETTING_HELP = 'Save outputs as '
SECONDARY_MODEL_ACTIVATE_HELP = 'When enabled, the application will run an additional inference with the selected model(s) above.'
SECONDARY_MODEL_HELP = 'Choose the secondary model associated with this stem you wish to run with the current process method.'
SECONDARY_MODEL_SCALE_HELP = 'The scale determines how the final audio outputs will be averaged between the primary and secondary models.\n\nFor example:\n\n' +\
'• 10% - 10 percent of the main model result will be factored into the final result.\n' +\
'• 50% - The results from the main and secondary models will be averaged evenly.\n' +\
'• 90% - 90 percent of the main model result will be factored into the final result.'
PRE_PROC_MODEL_ACTIVATE_HELP = 'The application will run an inference with the selected model above, pulling only the instrumental stem when enabled. \nFrom there, all of the non-vocal stems will be pulled from the generated instrumental.\n\nNotes:\n\n' +\
'• This option can significantly reduce vocal bleed within the non-vocal stems.\n' +\
'• It is only available in Demucs.\n' +\
'• It is only compatible with non-vocal and non-instrumental stem outputs.\n' +\
'• This will increase thetotal processing time.\n' +\
'• Only VR and MDX-Net Vocal or Instrumental models are selectable above.'
AUDIO_TOOLS_HELP = 'Here, you choose between different audio tools to process your track.\n\n' +\
'• Manual Ensemble - You must have 2 or more files selected as your inputs. Allows the user to run their tracks through \nthe same algorithms used in Ensemble Mode.\n' +\
'• Align Inputs - You must have exactly 2 files selected as your inputs. The second input will be aligned with the first input.\n' +\
'• Time Stretch - The user can speed up or slow down the selected inputs.\n' +\
'• Change Pitch - The user can change the pitch for the selected inputs.\n'
PRE_PROC_MODEL_INST_MIX_HELP = 'When enabled, the application will generate a third output without the selected stem and vocals.'
MODEL_SAMPLE_MODE_HELP = 'Allows the user to process only part of a track to sample settings or a model without \nrunning a full conversion.\n\nNotes:\n\n' +\
'• The number in the parentheses is the current number of seconds the generated sample will be.\n' +\
'• You can choose the number of seconds to extract from the track in the \"Additional Settings\" menu.'
POST_PROCESS_THREASHOLD_HELP = 'Allows the user to control the intensity of the Post_process option.\n\nNotes:\n\n' +\
'• Higher values potentially remove more artifacts. However, bleed might increase.\n' +\
'• Lower values limit artifact removal.'
BATCH_SIZE_HELP = 'Specify the number of batches to be processed at a time.\n\nNotes:\n\n' +\
'• Higher values mean more RAM usage but slightly faster processing times.\n' +\
'• Lower values mean less RAM usage but slightly longer processing times.\n' +\
'• Batch size value has no effect on output quality.'
# Warning Messages
STORAGE_ERROR = 'Insufficient Storage', 'There is not enough storage on main drive to continue. Your main drive must have at least 3 GB\'s of storage in order for this application function properly. \n\nPlease ensure your main drive has at least 3 GB\'s of storage and try again.\n\n'
STORAGE_WARNING = 'Available Storage Low', 'Your main drive is running low on storage. Your main drive must have at least 3 GB\'s of storage in order for this application function properly.\n\n'
CONFIRM_WARNING = '\nAre you sure you wish to continue?'
PROCESS_FAILED = 'Process failed, please see error log\n'
EXIT_PROCESS_ERROR = 'Active Process', 'Please stop the active process or wait for it to complete before you exit.'
EXIT_HALTED_PROCESS_ERROR = 'Halting Process', 'Please wait for the application to finish halting the process before exiting.'
EXIT_DOWNLOAD_ERROR = 'Active Download', 'Please stop the download or wait for it to complete before you exit.'
SET_TO_DEFAULT_PROCESS_ERROR = 'Active Process', 'You cannot reset all of the application settings during an active process.'
SET_TO_ANY_PROCESS_ERROR = 'Active Process', 'You cannot reset the application settings during an active process.'
RESET_ALL_TO_DEFAULT_WARNING = 'Reset Settings Confirmation', 'All application settings will be set to factory default.\n\nAre you sure you wish to continue?'
AUDIO_VERIFICATION_CHECK = lambda i, e:f'++++++++++++++++++++++++++++++++++++++++++++++++++++\n\nBroken File Removed: \n\n{i}\n\nError Details:\n\n{e}\n++++++++++++++++++++++++++++++++++++++++++++++++++++'
INVALID_ONNX_MODEL_ERROR = 'Invalid Model', 'The file selected is not a valid MDX-Net model. Please see the error log for more information.'
# Separation Text
LOADING_MODEL = 'Loading model...'
INFERENCE_STEP_1 = 'Running inference...'
INFERENCE_STEP_1_SEC = 'Running inference (secondary model)...'
INFERENCE_STEP_1_4_STEM = lambda stem:f'Running inference (secondary model for {stem})...'
INFERENCE_STEP_1_PRE = 'Running inference (pre-process model)...'
INFERENCE_STEP_2_PRE = lambda pm, m:f'Loading pre-process model ({pm}: {m})...'
INFERENCE_STEP_2_SEC = lambda pm, m:f'Loading secondary model ({pm}: {m})...'
INFERENCE_STEP_2_SEC_CACHED_MODOEL = lambda pm, m:f'Secondary model ({pm}: {m}) cache loaded.\n'
INFERENCE_STEP_2_PRE_CACHED_MODOEL = lambda pm, m:f'Pre-process model ({pm}: {m}) cache loaded.\n'
INFERENCE_STEP_2_SEC_CACHED = 'Loading cached secondary model source(s)... Done!\n'
INFERENCE_STEP_2_PRIMARY_CACHED = 'Model cache loaded.\n'
INFERENCE_STEP_2 = 'Inference complete.'
SAVING_STEM = 'Saving ', ' stem...'
SAVING_ALL_STEMS = 'Saving all stems...'
ENSEMBLING_OUTPUTS = 'Ensembling outputs...'
DONE = ' Done!\n'
ENSEMBLES_SAVED = 'Ensembled outputs saved!\n\n'
NEW_LINES = "\n\n"
NEW_LINE = "\n"
NO_LINE = ''
# Widget Placements
MAIN_ROW_Y = -15, -17
MAIN_ROW_X = -4, 21
MAIN_ROW_WIDTH = -53
MAIN_ROW_2_Y = -15, -17
MAIN_ROW_2_X = -28, 1
CHECK_BOX_Y = 0
CHECK_BOX_X = 20
CHECK_BOX_WIDTH = -50
CHECK_BOX_HEIGHT = 2
LEFT_ROW_WIDTH = -10
LABEL_HEIGHT = -5
OPTION_HEIGHT = 7
LOW_MENU_Y = 18, 16
FFMPEG_EXT = (".aac", ".aiff", ".alac" ,".flac", ".FLAC", ".mov", ".mp4", ".MP4",
".m4a", ".M4A", ".mp2", ".mp3", "MP3", ".mpc", ".mpc8",
".mpeg", ".ogg", ".OGG", ".tta", ".wav", ".wave", ".WAV", ".WAVE", ".wma", ".webm", ".eac3", ".mkv")
FFMPEG_MORE_EXT = (".aa", ".aac", ".ac3", ".aiff", ".alac", ".avi", ".f4v",".flac", ".flic", ".flv",
".m4v",".mlv", ".mov", ".mp4", ".m4a", ".mp2", ".mp3", ".mp4", ".mpc", ".mpc8",
".mpeg", ".ogg", ".tta", ".tty", ".vcd", ".wav", ".wma")
ANY_EXT = ""
# Secondary Menu Constants
VOCAL_PAIR_PLACEMENT = 1, 2, 3, 4
OTHER_PAIR_PLACEMENT = 5, 6, 7, 8
BASS_PAIR_PLACEMENT = 9, 10, 11, 12
DRUMS_PAIR_PLACEMENT = 13, 14, 15, 16
# Drag n Drop String Checks
DOUBLE_BRACKET = "} {"
RIGHT_BRACKET = "}"
LEFT_BRACKET = "{"
# Manual Downloads
VR_PLACEMENT_TEXT = 'Place models in \"models/VR_Models\" directory.'
MDX_PLACEMENT_TEXT = 'Place models in \"models/MDX_Net_Models\" directory.'
DEMUCS_PLACEMENT_TEXT = 'Place models in \"models/Demucs_Models\" directory.'
DEMUCS_V3_V4_PLACEMENT_TEXT = 'Place items in \"models/Demucs_Models/v3_v4_repo\" directory.'
FULL_DOWNLOAD_LIST_VR = {
"VR Arch Single Model v5: 1_HP-UVR": "1_HP-UVR.pth",
"VR Arch Single Model v5: 2_HP-UVR": "2_HP-UVR.pth",
"VR Arch Single Model v5: 3_HP-Vocal-UVR": "3_HP-Vocal-UVR.pth",
"VR Arch Single Model v5: 4_HP-Vocal-UVR": "4_HP-Vocal-UVR.pth",
"VR Arch Single Model v5: 5_HP-Karaoke-UVR": "5_HP-Karaoke-UVR.pth",
"VR Arch Single Model v5: 6_HP-Karaoke-UVR": "6_HP-Karaoke-UVR.pth",
"VR Arch Single Model v5: 7_HP2-UVR": "7_HP2-UVR.pth",
"VR Arch Single Model v5: 8_HP2-UVR": "8_HP2-UVR.pth",
"VR Arch Single Model v5: 9_HP2-UVR": "9_HP2-UVR.pth",
"VR Arch Single Model v5: 10_SP-UVR-2B-32000-1": "10_SP-UVR-2B-32000-1.pth",
"VR Arch Single Model v5: 11_SP-UVR-2B-32000-2": "11_SP-UVR-2B-32000-2.pth",
"VR Arch Single Model v5: 12_SP-UVR-3B-44100": "12_SP-UVR-3B-44100.pth",
"VR Arch Single Model v5: 13_SP-UVR-4B-44100-1": "13_SP-UVR-4B-44100-1.pth",
"VR Arch Single Model v5: 14_SP-UVR-4B-44100-2": "14_SP-UVR-4B-44100-2.pth",
"VR Arch Single Model v5: 15_SP-UVR-MID-44100-1": "15_SP-UVR-MID-44100-1.pth",
"VR Arch Single Model v5: 16_SP-UVR-MID-44100-2": "16_SP-UVR-MID-44100-2.pth",
"VR Arch Single Model v4: MGM_HIGHEND_v4": "MGM_HIGHEND_v4.pth",
"VR Arch Single Model v4: MGM_LOWEND_A_v4": "MGM_LOWEND_A_v4.pth",
"VR Arch Single Model v4: MGM_LOWEND_B_v4": "MGM_LOWEND_B_v4.pth",
"VR Arch Single Model v4: MGM_MAIN_v4": "MGM_MAIN_v4.pth"
}
FULL_DOWNLOAD_LIST_MDX = {
"MDX-Net Model: UVR-MDX-NET Main": "UVR_MDXNET_Main.onnx",
"MDX-Net Model: UVR-MDX-NET Inst Main": "UVR-MDX-NET-Inst_Main.onnx",
"MDX-Net Model: UVR-MDX-NET 1": "UVR_MDXNET_1_9703.onnx",
"MDX-Net Model: UVR-MDX-NET 2": "UVR_MDXNET_2_9682.onnx",
"MDX-Net Model: UVR-MDX-NET 3": "UVR_MDXNET_3_9662.onnx",
"MDX-Net Model: UVR-MDX-NET Inst 1": "UVR-MDX-NET-Inst_1.onnx",
"MDX-Net Model: UVR-MDX-NET Inst 2": "UVR-MDX-NET-Inst_2.onnx",
"MDX-Net Model: UVR-MDX-NET Inst 3": "UVR-MDX-NET-Inst_3.onnx",
"MDX-Net Model: UVR-MDX-NET Karaoke": "UVR_MDXNET_KARA.onnx",
"MDX-Net Model: UVR_MDXNET_9482": "UVR_MDXNET_9482.onnx",
"MDX-Net Model: Kim_Vocal_1": "Kim_Vocal_1.onnx",
"MDX-Net Model: kuielab_a_vocals": "kuielab_a_vocals.onnx",
"MDX-Net Model: kuielab_a_other": "kuielab_a_other.onnx",
"MDX-Net Model: kuielab_a_bass": "kuielab_a_bass.onnx",
"MDX-Net Model: kuielab_a_drums": "kuielab_a_drums.onnx",
"MDX-Net Model: kuielab_b_vocals": "kuielab_b_vocals.onnx",
"MDX-Net Model: kuielab_b_other": "kuielab_b_other.onnx",
"MDX-Net Model: kuielab_b_bass": "kuielab_b_bass.onnx",
"MDX-Net Model: kuielab_b_drums": "kuielab_b_drums.onnx"}
FULL_DOWNLOAD_LIST_DEMUCS = {
"Demucs v4: htdemucs_ft":{
"f7e0c4bc-ba3fe64a.th":"https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/f7e0c4bc-ba3fe64a.th",
"d12395a8-e57c48e6.th":"https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/d12395a8-e57c48e6.th",
"92cfc3b6-ef3bcb9c.th":"https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/92cfc3b6-ef3bcb9c.th",
"04573f0d-f3cf25b2.th":"https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/04573f0d-f3cf25b2.th",
"htdemucs_ft.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/htdemucs_ft.yaml"
},
"Demucs v4: htdemucs":{
"955717e8-8726e21a.th": "https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/955717e8-8726e21a.th",
"htdemucs.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/htdemucs.yaml"
},
"Demucs v4: hdemucs_mmi":{
"75fc33f5-1941ce65.th": "https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/75fc33f5-1941ce65.th",
"hdemucs_mmi.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/hdemucs_mmi.yaml"
},
"Demucs v4: htdemucs_6s":{
"5c90dfd2-34c22ccb.th": "https://dl.fbaipublicfiles.com/demucs/hybrid_transformer/5c90dfd2-34c22ccb.th",
"htdemucs_6s.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/htdemucs_6s.yaml"
},
"Demucs v3: mdx":{
"0d19c1c6-0f06f20e.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/0d19c1c6-0f06f20e.th",
"7ecf8ec1-70f50cc9.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/7ecf8ec1-70f50cc9.th",
"c511e2ab-fe698775.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/c511e2ab-fe698775.th",
"7d865c68-3d5dd56b.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/7d865c68-3d5dd56b.th",
"mdx.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/mdx.yaml"
},
"Demucs v3: mdx_q":{
"6b9c2ca1-3fd82607.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/6b9c2ca1-3fd82607.th",
"b72baf4e-8778635e.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/b72baf4e-8778635e.th",
"42e558d4-196e0e1b.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/42e558d4-196e0e1b.th",
"305bc58f-18378783.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/305bc58f-18378783.th",
"mdx_q.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/mdx_q.yaml"
},
"Demucs v3: mdx_extra":{
"e51eebcc-c1b80bdd.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/e51eebcc-c1b80bdd.th",
"a1d90b5c-ae9d2452.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/a1d90b5c-ae9d2452.th",
"5d2d6c55-db83574e.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/5d2d6c55-db83574e.th",
"cfa93e08-61801ae1.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/cfa93e08-61801ae1.th",
"mdx_extra.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/mdx_extra.yaml"
},
"Demucs v3: mdx_extra_q": {
"83fc094f-4a16d450.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/83fc094f-4a16d450.th",
"464b36d7-e5a9386e.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/464b36d7-e5a9386e.th",
"14fc6a69-a89dd0ee.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/14fc6a69-a89dd0ee.th",
"7fd6ef75-a905dd85.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/7fd6ef75-a905dd85.th",
"mdx_extra_q.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/mdx_extra_q.yaml"
},
"Demucs v3: UVR Model":{
"ebf34a2db.th": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/ebf34a2db.th",
"UVR_Demucs_Model_1.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/UVR_Demucs_Model_1.yaml"
},
"Demucs v3: repro_mdx_a":{
"9a6b4851-03af0aa6.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/9a6b4851-03af0aa6.th",
"1ef250f1-592467ce.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/1ef250f1-592467ce.th",
"fa0cb7f9-100d8bf4.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/fa0cb7f9-100d8bf4.th",
"902315c2-b39ce9c9.th": "https://dl.fbaipublicfiles.com/demucs/mdx_final/902315c2-b39ce9c9.th",
"repro_mdx_a.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/repro_mdx_a.yaml"
},
"Demucs v3: repro_mdx_a_time_only":{
"9a6b4851-03af0aa6.th":"https://dl.fbaipublicfiles.com/demucs/mdx_final/9a6b4851-03af0aa6.th",
"1ef250f1-592467ce.th":"https://dl.fbaipublicfiles.com/demucs/mdx_final/1ef250f1-592467ce.th",
"repro_mdx_a_time_only.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/repro_mdx_a_time_only.yaml"
},
"Demucs v3: repro_mdx_a_hybrid_only":{
"fa0cb7f9-100d8bf4.th":"https://dl.fbaipublicfiles.com/demucs/mdx_final/fa0cb7f9-100d8bf4.th",
"902315c2-b39ce9c9.th":"https://dl.fbaipublicfiles.com/demucs/mdx_final/902315c2-b39ce9c9.th",
"repro_mdx_a_hybrid_only.yaml": "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/repro_mdx_a_hybrid_only.yaml"
},
"Demucs v2: demucs": {
"demucs-e07c671f.th": "https://dl.fbaipublicfiles.com/demucs/v3.0/demucs-e07c671f.th"
},
"Demucs v2: demucs_extra": {
"demucs_extra-3646af93.th":"https://dl.fbaipublicfiles.com/demucs/v3.0/demucs_extra-3646af93.th"
},
"Demucs v2: demucs48_hq": {
"demucs48_hq-28a1282c.th":"https://dl.fbaipublicfiles.com/demucs/v3.0/demucs48_hq-28a1282c.th"
},
"Demucs v2: tasnet": {
"tasnet-beb46fac.th":"https://dl.fbaipublicfiles.com/demucs/v3.0/tasnet-beb46fac.th"
},
"Demucs v2: tasnet_extra": {
"tasnet_extra-df3777b2.th":"https://dl.fbaipublicfiles.com/demucs/v3.0/tasnet_extra-df3777b2.th"
},
"Demucs v2: demucs_unittest": {
"demucs_unittest-09ebc15f.th":"https://dl.fbaipublicfiles.com/demucs/v3.0/demucs_unittest-09ebc15f.th"
},
"Demucs v1: demucs": {
"demucs.th":"https://dl.fbaipublicfiles.com/demucs/v2.0/demucs.th"
},
"Demucs v1: demucs_extra": {
"demucs_extra.th":"https://dl.fbaipublicfiles.com/demucs/v2.0/demucs_extra.th"
},
"Demucs v1: light": {
"light.th":"https://dl.fbaipublicfiles.com/demucs/v2.0/light.th"
},
"Demucs v1: light_extra": {
"light_extra.th":"https://dl.fbaipublicfiles.com/demucs/v2.0/light_extra.th"
},
"Demucs v1: tasnet": {
"tasnet.th":"https://dl.fbaipublicfiles.com/demucs/v2.0/tasnet.th"
},
"Demucs v1: tasnet_extra": {
"tasnet_extra.th":"https://dl.fbaipublicfiles.com/demucs/v2.0/tasnet_extra.th"
}
}
# Main Menu Labels
CHOOSE_PROC_METHOD_MAIN_LABEL = 'CHOOSE PROCESS METHOD'
SELECT_SAVED_SETTINGS_MAIN_LABEL = 'SELECT SAVED SETTINGS'
CHOOSE_MDX_MODEL_MAIN_LABEL = 'CHOOSE MDX-NET MODEL'
BATCHES_MDX_MAIN_LABEL = 'BATCH SIZE'
VOL_COMP_MDX_MAIN_LABEL = 'VOLUME COMPENSATION'
SELECT_VR_MODEL_MAIN_LABEL = 'CHOOSE VR MODEL'
AGGRESSION_SETTING_MAIN_LABEL = 'AGGRESSION SETTING'
WINDOW_SIZE_MAIN_LABEL = 'WINDOW SIZE'
CHOOSE_DEMUCS_MODEL_MAIN_LABEL = 'CHOOSE DEMUCS MODEL'
CHOOSE_DEMUCS_STEMS_MAIN_LABEL = 'CHOOSE STEM(S)'
CHOOSE_SEGMENT_MAIN_LABEL = 'SEGMENT'
ENSEMBLE_OPTIONS_MAIN_LABEL = 'ENSEMBLE OPTIONS'
CHOOSE_MAIN_PAIR_MAIN_LABEL = 'MAIN STEM PAIR'
CHOOSE_ENSEMBLE_ALGORITHM_MAIN_LABEL = 'ENSEMBLE ALGORITHM'
AVAILABLE_MODELS_MAIN_LABEL = 'AVAILABLE MODELS'
CHOOSE_AUDIO_TOOLS_MAIN_LABEL = 'CHOOSE AUDIO TOOL'
CHOOSE_MANUAL_ALGORITHM_MAIN_LABEL = 'CHOOSE ALGORITHM'
CHOOSE_RATE_MAIN_LABEL = 'RATE'
CHOOSE_SEMITONES_MAIN_LABEL = 'SEMITONES'
GPU_CONVERSION_MAIN_LABEL = 'GPU Conversion'
if OPERATING_SYSTEM=="Darwin":
LICENSE_OS_SPECIFIC_TEXT = '• This application is intended for those running macOS Catalina and above.\n' +\
'• Application functionality for systems running macOS Mojave or lower is not guaranteed.\n' +\
'• Application functionality for older or budget Mac systems is not guaranteed.\n\n'
FONT_SIZE_F1 = 13
FONT_SIZE_F2 = 11
FONT_SIZE_F3 = 12
FONT_SIZE_0 = 9
FONT_SIZE_1 = 11
FONT_SIZE_2 = 12
FONT_SIZE_3 = 13
FONT_SIZE_4 = 14
FONT_SIZE_5 = 15
FONT_SIZE_6 = 17
HELP_HINT_CHECKBOX_WIDTH = 13
MDX_CHECKBOXS_WIDTH = 14
VR_CHECKBOXS_WIDTH = 14
ENSEMBLE_CHECKBOXS_WIDTH = 18
DEMUCS_CHECKBOXS_WIDTH = 14
DEMUCS_PRE_CHECKBOXS_WIDTH = 20
GEN_SETTINGS_WIDTH = 17
MENU_COMBOBOX_WIDTH = 16
elif OPERATING_SYSTEM=="Linux":
LICENSE_OS_SPECIFIC_TEXT = '• This application is intended for those running Linux Ubuntu 18.04+.\n' +\
'• Application functionality for systems running other Linux platforms is not guaranteed.\n' +\
'• Application functionality for older or budget systems is not guaranteed.\n\n'
FONT_SIZE_F1 = 10
FONT_SIZE_F2 = 8
FONT_SIZE_F3 = 9
FONT_SIZE_0 = 7
FONT_SIZE_1 = 8
FONT_SIZE_2 = 9
FONT_SIZE_3 = 10
FONT_SIZE_4 = 11
FONT_SIZE_5 = 12
FONT_SIZE_6 = 15
HELP_HINT_CHECKBOX_WIDTH = 13
MDX_CHECKBOXS_WIDTH = 14
VR_CHECKBOXS_WIDTH = 16
ENSEMBLE_CHECKBOXS_WIDTH = 25
DEMUCS_CHECKBOXS_WIDTH = 18
DEMUCS_PRE_CHECKBOXS_WIDTH = 27
GEN_SETTINGS_WIDTH = 17
MENU_COMBOBOX_WIDTH = 19
elif OPERATING_SYSTEM=="Windows":
LICENSE_OS_SPECIFIC_TEXT = '• This application is intended for those running Windows 10 or higher.\n' +\
'• Application functionality for systems running Windows 7 or lower is not guaranteed.\n' +\
'• Application functionality for Intel Pentium & Celeron CPUs systems is not guaranteed.\n\n'
FONT_SIZE_F1 = 10
FONT_SIZE_F2 = 8
FONT_SIZE_F3 = 9
FONT_SIZE_0 = 7
FONT_SIZE_1 = 8
FONT_SIZE_2 = 9
FONT_SIZE_3 = 10
FONT_SIZE_4 = 11
FONT_SIZE_5 = 12
FONT_SIZE_6 = 15
HELP_HINT_CHECKBOX_WIDTH = 16
MDX_CHECKBOXS_WIDTH = 16
VR_CHECKBOXS_WIDTH = 16
ENSEMBLE_CHECKBOXS_WIDTH = 25
DEMUCS_CHECKBOXS_WIDTH = 18
DEMUCS_PRE_CHECKBOXS_WIDTH = 27
GEN_SETTINGS_WIDTH = 23
MENU_COMBOBOX_WIDTH = 19
LICENSE_TEXT = lambda a, p:f'Current Application Version: Ultimate Vocal Remover {a}\n' +\
f'Current Patch Version: {p}\n\n' +\
'Copyright (c) 2022 Ultimate Vocal Remover\n\n' +\
'UVR is free and open-source, but MIT licensed. Please credit us if you use our\n' +\
f'models or code for projects unrelated to UVR.\n\n{LICENSE_OS_SPECIFIC_TEXT}' +\
'This bundle contains the UVR interface, Python, PyTorch, and other\n' +\
'dependencies needed to run the application effectively.\n\n' +\
'Website Links: This application, System or Service(s) may contain links to\n' +\
'other websites and downloads, and they are solely provided to you as an\n' +\
'additional convenience. You understand and acknowledge that by clicking\n' +\
'or activating such links you are accessing a site or service outside of\n' +\
'this application, and that we do not screen, review, approve, or otherwise\n' +\
'endorse any content or information contained in these linked websites.\n' +\
'You acknowledge and agree that we, our affiliates and partners are not\n' +\
'responsible for the contents of any of these linked websites, including\n' +\
'the accuracy or availability of information provided by the linked websites,\n' +\
'and we make no representations or warranties regarding your use of\n' +\
'the linked websites.\n\n' +\
'This application is MIT Licensed\n\n' +\
'Permission is hereby granted, free of charge, to any person obtaining a copy\n' +\
'of this software and associated documentation files (the "Software"), to deal\n' +\
'in the Software without restriction, including without limitation the rights\n' +\
'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n' +\
'copies of the Software, and to permit persons to whom the Software is\n' +\
'furnished to do so, subject to the following conditions:\n\n' +\
'The above copyright notice and this permission notice shall be included in all\n' +\
'copies or substantial portions of the Software.\n\n' +\
'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n' +\
'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n' +\
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n' +\
'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n' +\
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n' +\
'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n' +\
'SOFTWARE.'
CHANGE_LOG_HEADER = lambda patch:f"Patch Version:\n\n{patch}"
#DND CONSTS
MAC_DND_CHECK = ('/Users/',
'/Applications/',
'/Library/',
'/System/')
LINUX_DND_CHECK = ('/home/',
'/usr/')
WINDOWS_DND_CHECK = ('A:', 'B:', 'C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', 'L:', 'M:', 'N:', 'O:', 'P:', 'Q:', 'R:', 'S:', 'T:', 'U:', 'V:', 'W:', 'X:', 'Y:', 'Z:')
WOOD_INST_MODEL_HASH = '0ec76fd9e65f81d8b4fbd13af4826ed8'
WOOD_INST_PARAMS = {
"vr_model_param": "4band_v3",
"primary_stem": NO_WIND_INST_STEM
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140from abc import ABCMeta
import torchimport torch.nn as nnfrom pytorch_lightning import LightningModulefrom .modules import TFC_TDF
dim_s = 4
class AbstractMDXNet(LightningModule): __metaclass__ = ABCMeta
def __init__(self, target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, overlap): super().__init__() self.target_name = target_name self.lr = lr self.optimizer = optimizer self.dim_c = dim_c self.dim_f = dim_f self.dim_t = dim_t self.n_fft = n_fft self.n_bins = n_fft // 2 + 1 self.hop_length = hop_length self.window = nn.Parameter(torch.hann_window(window_length=self.n_fft, periodic=True), requires_grad=False) self.freq_pad = nn.Parameter(torch.zeros([1, dim_c, self.n_bins - self.dim_f, self.dim_t]), requires_grad=False)
def configure_optimizers(self): if self.optimizer == 'rmsprop': return torch.optim.RMSprop(self.parameters(), self.lr) if self.optimizer == 'adamw': return torch.optim.AdamW(self.parameters(), self.lr)
class ConvTDFNet(AbstractMDXNet): def __init__(self, target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, num_blocks, l, g, k, bn, bias, overlap):
super(ConvTDFNet, self).__init__( target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, overlap) self.save_hyperparameters()
self.num_blocks = num_blocks self.l = l self.g = g self.k = k self.bn = bn self.bias = bias
if optimizer == 'rmsprop': norm = nn.BatchNorm2d if optimizer == 'adamw': norm = lambda input:nn.GroupNorm(2, input) self.n = num_blocks // 2 scale = (2, 2)
self.first_conv = nn.Sequential( nn.Conv2d(in_channels=self.dim_c, out_channels=g, kernel_size=(1, 1)), norm(g), nn.ReLU(), )
f = self.dim_f c = g self.encoding_blocks = nn.ModuleList() self.ds = nn.ModuleList() for i in range(self.n): self.encoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm)) self.ds.append( nn.Sequential( nn.Conv2d(in_channels=c, out_channels=c + g, kernel_size=scale, stride=scale), norm(c + g), nn.ReLU() ) ) f = f // 2 c += g
self.bottleneck_block = TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm)
self.decoding_blocks = nn.ModuleList() self.us = nn.ModuleList() for i in range(self.n): self.us.append( nn.Sequential( nn.ConvTranspose2d(in_channels=c, out_channels=c - g, kernel_size=scale, stride=scale), norm(c - g), nn.ReLU() ) ) f = f * 2 c -= g
self.decoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm))
self.final_conv = nn.Sequential( nn.Conv2d(in_channels=c, out_channels=self.dim_c, kernel_size=(1, 1)), )
def forward(self, x):
x = self.first_conv(x)
x = x.transpose(-1, -2)
ds_outputs = [] for i in range(self.n): x = self.encoding_blocks[i](x) ds_outputs.append(x) x = self.ds[i](x)
x = self.bottleneck_block(x)
for i in range(self.n): x = self.us[i](x) x *= ds_outputs[-i - 1] x = self.decoding_blocks[i](x)
x = x.transpose(-1, -2)
x = self.final_conv(x)
return x class Mixer(nn.Module): def __init__(self, device, mixer_path): super(Mixer, self).__init__() self.linear = nn.Linear((dim_s+1)*2, dim_s*2, bias=False) self.load_state_dict( torch.load(mixer_path, map_location=device) )
def forward(self, x): x = x.reshape(1,(dim_s+1)*2,-1).transpose(-1,-2) x = self.linear(x) return x.transpose(-1,-2).reshape(dim_s,2,-1)123version https://git-lfs.github.com/spec/v1oid sha256:ea781bd52c6a523b825fa6cdbb6189f52e318edd8b17e6fe404f76f7af8caa9csize 12081234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import torchimport torch.nn as nn
class TFC(nn.Module): def __init__(self, c, l, k, norm): super(TFC, self).__init__()
self.H = nn.ModuleList() for i in range(l): self.H.append( nn.Sequential( nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2), norm(c), nn.ReLU(), ) )
def forward(self, x): for h in self.H: x = h(x) return x
class DenseTFC(nn.Module): def __init__(self, c, l, k, norm): super(DenseTFC, self).__init__()
self.conv = nn.ModuleList() for i in range(l): self.conv.append( nn.Sequential( nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2), norm(c), nn.ReLU(), ) )
def forward(self, x): for layer in self.conv[:-1]: x = torch.cat([layer(x), x], 1) return self.conv[-1](x)
class TFC_TDF(nn.Module): def __init__(self, c, l, f, k, bn, dense=False, bias=True, norm=nn.BatchNorm2d):
super(TFC_TDF, self).__init__()
self.use_tdf = bn is not None
self.tfc = DenseTFC(c, l, k, norm) if dense else TFC(c, l, k, norm)
if self.use_tdf: if bn == 0: self.tdf = nn.Sequential( nn.Linear(f, f, bias=bias), norm(c), nn.ReLU() ) else: self.tdf = nn.Sequential( nn.Linear(f, f // bn, bias=bias), norm(c), nn.ReLU(), nn.Linear(f // bn, f, bias=bias), norm(c), nn.ReLU() )
def forward(self, x): x = self.tfc(x) return x + self.tdf(x) if self.use_tdf else x
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import osimport subprocessimport tempfileimport siximport numpy as npimport soundfile as sfimport sys
if getattr(sys, 'frozen', False): BASE_PATH_RUB = sys._MEIPASSelse: BASE_PATH_RUB = os.path.dirname(os.path.abspath(__file__))
__all__ = ['time_stretch', 'pitch_shift']
__RUBBERBAND_UTIL = os.path.join(BASE_PATH_RUB, 'rubberband')
if six.PY2: DEVNULL = open(os.devnull, 'w')else: DEVNULL = subprocess.DEVNULL
def __rubberband(y, sr, **kwargs):
assert sr > 0
# Get the input and output tempfile fd, infile = tempfile.mkstemp(suffix='.wav') os.close(fd) fd, outfile = tempfile.mkstemp(suffix='.wav') os.close(fd)
# dump the audio sf.write(infile, y, sr)
try: # Execute rubberband arguments = [__RUBBERBAND_UTIL, '-q']
for key, value in six.iteritems(kwargs): arguments.append(str(key)) arguments.append(str(value))
arguments.extend([infile, outfile])
subprocess.check_call(arguments, stdout=DEVNULL, stderr=DEVNULL)
# Load the processed audio. y_out, _ = sf.read(outfile, always_2d=True)
# make sure that output dimensions matches input if y.ndim == 1: y_out = np.squeeze(y_out)
except OSError as exc: six.raise_from(RuntimeError('Failed to execute rubberband. ' 'Please verify that rubberband-cli ' 'is installed.'), exc)
finally: # Remove temp files os.unlink(infile) os.unlink(outfile)
return y_out
def time_stretch(y, sr, rate, rbargs=None): if rate <= 0: raise ValueError('rate must be strictly positive')
if rate == 1.0: return y
if rbargs is None: rbargs = dict()
rbargs.setdefault('--tempo', rate)
return __rubberband(y, sr, **rbargs)
def pitch_shift(y, sr, n_steps, rbargs=None):
if n_steps == 0: return y
if rbargs is None: rbargs = dict()
rbargs.setdefault('--pitch', n_steps)
return __rubberband(y, sr, **rbargs)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703import librosa
import numpy as np
import soundfile as sf
import math
import random
import math
import platform
import traceback
from . import pyrb
#cur
OPERATING_SYSTEM = platform.system()
SYSTEM_ARCH = platform.platform()
SYSTEM_PROC = platform.processor()
ARM = 'arm'
if OPERATING_SYSTEM == 'Windows':
from pyrubberband import pyrb
else:
from . import pyrb
if OPERATING_SYSTEM == 'Darwin':
wav_resolution = "polyphase" if SYSTEM_PROC == ARM or ARM in SYSTEM_ARCH else "sinc_fastest"
else:
wav_resolution = "sinc_fastest"
MAX_SPEC = 'Max Spec'
MIN_SPEC = 'Min Spec'
AVERAGE = 'Average'
def crop_center(h1, h2):
h1_shape = h1.size()
h2_shape = h2.size()
if h1_shape[3] == h2_shape[3]:
return h1
elif h1_shape[3] < h2_shape[3]:
raise ValueError('h1_shape[3] must be greater than h2_shape[3]')
s_time = (h1_shape[3] - h2_shape[3]) // 2
e_time = s_time + h2_shape[3]
h1 = h1[:, :, :, s_time:e_time]
return h1
def preprocess(X_spec):
X_mag = np.abs(X_spec)
X_phase = np.angle(X_spec)
return X_mag, X_phase
def make_padding(width, cropsize, offset):
left = offset
roi_size = cropsize - offset * 2
if roi_size == 0:
roi_size = cropsize
right = roi_size - (width % roi_size) + left
return left, right, roi_size
def wave_to_spectrogram(wave, hop_length, n_fft, mid_side=False, mid_side_b2=False, reverse=False):
if reverse:
wave_left = np.flip(np.asfortranarray(wave[0]))
wave_right = np.flip(np.asfortranarray(wave[1]))
elif mid_side:
wave_left = np.asfortranarray(np.add(wave[0], wave[1]) / 2)
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1]))
elif mid_side_b2:
wave_left = np.asfortranarray(np.add(wave[1], wave[0] * .5))
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1] * .5))
else:
wave_left = np.asfortranarray(wave[0])
wave_right = np.asfortranarray(wave[1])
spec_left = librosa.stft(wave_left, n_fft, hop_length=hop_length)
spec_right = librosa.stft(wave_right, n_fft, hop_length=hop_length)
spec = np.asfortranarray([spec_left, spec_right])
return spec
def wave_to_spectrogram_mt(wave, hop_length, n_fft, mid_side=False, mid_side_b2=False, reverse=False):
import threading
if reverse:
wave_left = np.flip(np.asfortranarray(wave[0]))
wave_right = np.flip(np.asfortranarray(wave[1]))
elif mid_side:
wave_left = np.asfortranarray(np.add(wave[0], wave[1]) / 2)
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1]))
elif mid_side_b2:
wave_left = np.asfortranarray(np.add(wave[1], wave[0] * .5))
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1] * .5))
else:
wave_left = np.asfortranarray(wave[0])
wave_right = np.asfortranarray(wave[1])
def run_thread(**kwargs):
global spec_left
spec_left = librosa.stft(**kwargs)
thread = threading.Thread(target=run_thread, kwargs={'y': wave_left, 'n_fft': n_fft, 'hop_length': hop_length})
thread.start()
# print(wave_right.shape, n_fft, hop_length)
spec_right = librosa.stft(wave_right, n_fft=n_fft, hop_length=hop_length)
thread.join()
spec = np.asfortranarray([spec_left, spec_right])
return spec
def normalize(wave, is_normalize=False):
"""Save output music files"""
maxv = np.max(np.abs(wave))
if maxv > 1.0:
print(f"\nNormalization Set {is_normalize}: Input above threshold for clipping. Max:{maxv}")
if is_normalize:
print(f"The result was normalized.")
wave /= maxv
else:
print(f"The result was not normalized.")
else:
print(f"\nNormalization Set {is_normalize}: Input not above threshold for clipping. Max:{maxv}")
# stereo to mono
if wave.shape[1] < wave.shape[0]:
wave = np.mean(wave, axis=1)
else:
wave = np.mean(wave, axis=0)
return wave
def normalize_two_stem(wave, mix, is_normalize=False):
"""Save output music files"""
maxv = np.abs(wave).max()
max_mix = np.abs(mix).max()
if maxv > 1.0:
print(f"\nNormalization Set {is_normalize}: Primary source above threshold for clipping. Max:{maxv}")
print(f"\nNormalization Set {is_normalize}: Mixture above threshold for clipping. Max:{max_mix}")
if is_normalize:
print(f"The result was normalized.")
wave /= maxv
mix /= maxv
else:
print(f"The result was not normalized.")
else:
print(f"\nNormalization Set {is_normalize}: Input not above threshold for clipping. Max:{maxv}")
print(f"\nNormalization Set {is_normalize}: Primary source - Max:{np.abs(wave).max()}")
print(f"\nNormalization Set {is_normalize}: Mixture - Max:{np.abs(mix).max()}")
return wave, mix
def combine_spectrograms(specs, mp):
l = min([specs[i].shape[2] for i in specs])
spec_c = np.zeros(shape=(2, mp.param['bins'] + 1, l), dtype=np.complex64)
offset = 0
bands_n = len(mp.param['band'])
for d in range(1, bands_n + 1):
h = mp.param['band'][d]['crop_stop'] - mp.param['band'][d]['crop_start']
spec_c[:, offset:offset+h, :l] = specs[d][:, mp.param['band'][d]['crop_start']:mp.param['band'][d]['crop_stop'], :l]
offset += h
if offset > mp.param['bins']:
raise ValueError('Too much bins')
# lowpass fiter
if mp.param['pre_filter_start'] > 0: # and mp.param['band'][bands_n]['res_type'] in ['scipy', 'polyphase']:
if bands_n == 1:
spec_c = fft_lp_filter(spec_c, mp.param['pre_filter_start'], mp.param['pre_filter_stop'])
else:
gp = 1
for b in range(mp.param['pre_filter_start'] + 1, mp.param['pre_filter_stop']):
g = math.pow(10, -(b - mp.param['pre_filter_start']) * (3.5 - gp) / 20.0)
gp = g
spec_c[:, b, :] *= g
return np.asfortranarray(spec_c)
def spectrogram_to_image(spec, mode='magnitude'):
if mode == 'magnitude':
if np.iscomplexobj(spec):
y = np.abs(spec)
else:
y = spec
y = np.log10(y ** 2 + 1e-8)
elif mode == 'phase':
if np.iscomplexobj(spec):
y = np.angle(spec)
else:
y = spec
y -= y.min()
y *= 255 / y.max()
img = np.uint8(y)
if y.ndim == 3:
img = img.transpose(1, 2, 0)
img = np.concatenate([
np.max(img, axis=2, keepdims=True), img
], axis=2)
return img
def reduce_vocal_aggressively(X, y, softmask):
v = X - y
y_mag_tmp = np.abs(y)
v_mag_tmp = np.abs(v)
v_mask = v_mag_tmp > y_mag_tmp
y_mag = np.clip(y_mag_tmp - v_mag_tmp * v_mask * softmask, 0, np.inf)
return y_mag * np.exp(1.j * np.angle(y))
def merge_artifacts(y_mask, thres=0.01, min_range=64, fade_size=32):
mask = y_mask
try:
if min_range < fade_size * 2:
raise ValueError('min_range must be >= fade_size * 2')
idx = np.where(y_mask.min(axis=(0, 1)) > thres)[0]
start_idx = np.insert(idx[np.where(np.diff(idx) != 1)[0] + 1], 0, idx[0])
end_idx = np.append(idx[np.where(np.diff(idx) != 1)[0]], idx[-1])
artifact_idx = np.where(end_idx - start_idx > min_range)[0]
weight = np.zeros_like(y_mask)
if len(artifact_idx) > 0:
start_idx = start_idx[artifact_idx]
end_idx = end_idx[artifact_idx]
old_e = None
for s, e in zip(start_idx, end_idx):
if old_e is not None and s - old_e < fade_size:
s = old_e - fade_size * 2
if s != 0:
weight[:, :, s:s + fade_size] = np.linspace(0, 1, fade_size)
else:
s -= fade_size
if e != y_mask.shape[2]:
weight[:, :, e - fade_size:e] = np.linspace(1, 0, fade_size)
else:
e += fade_size
weight[:, :, s + fade_size:e - fade_size] = 1
old_e = e
v_mask = 1 - y_mask
y_mask += weight * v_mask
mask = y_mask
except Exception as e:
error_name = f'{type(e).__name__}'
traceback_text = ''.join(traceback.format_tb(e.__traceback__))
message = f'{error_name}: "{e}"\n{traceback_text}"'
print('Post Process Failed: ', message)
return mask
def align_wave_head_and_tail(a, b):
l = min([a[0].size, b[0].size])
return a[:l,:l], b[:l,:l]
def spectrogram_to_wave(spec, hop_length, mid_side, mid_side_b2, reverse, clamp=False):
spec_left = np.asfortranarray(spec[0])
spec_right = np.asfortranarray(spec[1])
wave_left = librosa.istft(spec_left, hop_length=hop_length)
wave_right = librosa.istft(spec_right, hop_length=hop_length)
if reverse:
return np.asfortranarray([np.flip(wave_left), np.flip(wave_right)])
elif mid_side:
return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)])
elif mid_side_b2:
return np.asfortranarray([np.add(wave_right / 1.25, .4 * wave_left), np.subtract(wave_left / 1.25, .4 * wave_right)])
else:
return np.asfortranarray([wave_left, wave_right])
def spectrogram_to_wave_mt(spec, hop_length, mid_side, reverse, mid_side_b2):
import threading
spec_left = np.asfortranarray(spec[0])
spec_right = np.asfortranarray(spec[1])
def run_thread(**kwargs):
global wave_left
wave_left = librosa.istft(**kwargs)
thread = threading.Thread(target=run_thread, kwargs={'stft_matrix': spec_left, 'hop_length': hop_length})
thread.start()
wave_right = librosa.istft(spec_right, hop_length=hop_length)
thread.join()
if reverse:
return np.asfortranarray([np.flip(wave_left), np.flip(wave_right)])
elif mid_side:
return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)])
elif mid_side_b2:
return np.asfortranarray([np.add(wave_right / 1.25, .4 * wave_left), np.subtract(wave_left / 1.25, .4 * wave_right)])
else:
return np.asfortranarray([wave_left, wave_right])
def cmb_spectrogram_to_wave(spec_m, mp, extra_bins_h=None, extra_bins=None):
bands_n = len(mp.param['band'])
offset = 0
# print('spec_m: ', spec_m.shape, np.max(spec_m), np.min(spec_m))
for d in range(1, bands_n + 1):
bp = mp.param['band'][d]
spec_s = np.ndarray(shape=(2, bp['n_fft'] // 2 + 1, spec_m.shape[2]), dtype=complex)
h = bp['crop_stop'] - bp['crop_start']
spec_s[:, bp['crop_start']:bp['crop_stop'], :] = spec_m[:, offset:offset+h, :]
# print('\nbp', d, bands_n, bp)
# print('spec_s: ', spec_s.shape, np.max(spec_s), np.min(spec_s))
offset += h
if d == bands_n: # higher
# print('hpf_start: ', extra_bins_h, bp['hpf_start'])
if extra_bins_h: # if --high_end_process bypass
max_bin = bp['n_fft'] // 2
spec_s[:, max_bin-extra_bins_h:max_bin, :] = extra_bins[:, :extra_bins_h, :]
# print('extra_bins_h, max_bin, extra_bins: ', extra_bins_h, max_bin, extra_bins.shape, np.max(extra_bins), np.min(extra_bins))
# print('spec_s d=4: ', spec_s.shape, np.max(spec_s), np.min(spec_s))
if bp['hpf_start'] > 0:
spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
# print('spec_s fft: ', spec_s.shape, np.max(spec_s), np.min(spec_s) )
if bands_n == 1:
wave = spectrogram_to_wave(spec_s, bp['hl'], mp.param['mid_side'], mp.param['mid_side_b2'], mp.param['reverse'])
else:
wave = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp.param['mid_side'], mp.param['mid_side_b2'], mp.param['reverse']))
else:
sr = mp.param['band'][d+1]['sr']
if d == 1: # lower
spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'] - 1) # test
spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
wave = librosa.resample(spectrogram_to_wave(spec_s, bp['hl'], mp.param['mid_side'], mp.param['mid_side_b2'], mp.param['reverse']), bp['sr'], sr, res_type=wav_resolution)
else: # mid
spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
wave2 = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp.param['mid_side'], mp.param['mid_side_b2'], mp.param['reverse']))
wave = librosa.resample(wave2, bp['sr'], sr, res_type=wav_resolution)
# print('spec to wav shape: ', d, wave.shape, np.max(wave), np.min(wave), spec_s.shape, np.max(spec_s), np.min(spec_s))
return wave
def fft_lp_filter(spec, bin_start, bin_stop):
g = 1.0
for b in range(bin_start, bin_stop):
g -= 1 / (bin_stop - bin_start)
spec[:, b, :] = g * spec[:, b, :]
spec[:, bin_stop:, :] *= 0
return spec
def fft_hp_filter(spec, bin_start, bin_stop):
g = 1.0
for b in range(bin_start, bin_stop, -1):
g -= 1 / (bin_start - bin_stop)
spec[:, b, :] = g * spec[:, b, :]
spec[:, 0:bin_stop+1, :] *= 0
return spec
def mirroring(a, spec_m, input_high_end, mp):
if 'mirroring' == a:
mirror = np.flip(np.abs(spec_m[:, mp.param['pre_filter_start']-10-input_high_end.shape[1]:mp.param['pre_filter_start']-10, :]), 1)
mirror = mirror * np.exp(1.j * np.angle(input_high_end))
return np.where(np.abs(input_high_end) <= np.abs(mirror), input_high_end, mirror)
if 'mirroring2' == a:
mirror = np.flip(np.abs(spec_m[:, mp.param['pre_filter_start']-10-input_high_end.shape[1]:mp.param['pre_filter_start']-10, :]), 1)
mi = np.multiply(mirror, input_high_end * 1.7)
return np.where(np.abs(input_high_end) <= np.abs(mi), input_high_end, mi)
def adjust_aggr(mask, is_non_accom_stem, aggressiveness):
aggr = aggressiveness['value']
if aggr != 0:
if is_non_accom_stem:
aggr = 1 - aggr
aggr = [aggr, aggr]
if aggressiveness['aggr_correction'] is not None:
aggr[0] += aggressiveness['aggr_correction']['left']
aggr[1] += aggressiveness['aggr_correction']['right']
for ch in range(2):
mask[ch, :aggressiveness['split_bin']] = np.power(mask[ch, :aggressiveness['split_bin']], 1 + aggr[ch] / 3)
mask[ch, aggressiveness['split_bin']:] = np.power(mask[ch, aggressiveness['split_bin']:], 1 + aggr[ch])
# if is_non_accom_stem:
# mask = (1.0 - mask)
return mask
def stft(wave, nfft, hl):
wave_left = np.asfortranarray(wave[0])
wave_right = np.asfortranarray(wave[1])
spec_left = librosa.stft(wave_left, nfft, hop_length=hl)
spec_right = librosa.stft(wave_right, nfft, hop_length=hl)
spec = np.asfortranarray([spec_left, spec_right])
return spec
def istft(spec, hl):
spec_left = np.asfortranarray(spec[0])
spec_right = np.asfortranarray(spec[1])
wave_left = librosa.istft(spec_left, hop_length=hl)
wave_right = librosa.istft(spec_right, hop_length=hl)
wave = np.asfortranarray([wave_left, wave_right])
return wave
def spec_effects(wave, algorithm='Default', value=None):
spec = [stft(wave[0],2048,1024), stft(wave[1],2048,1024)]
if algorithm == 'Min_Mag':
v_spec_m = np.where(np.abs(spec[1]) <= np.abs(spec[0]), spec[1], spec[0])
wave = istft(v_spec_m,1024)
elif algorithm == 'Max_Mag':
v_spec_m = np.where(np.abs(spec[1]) >= np.abs(spec[0]), spec[1], spec[0])
wave = istft(v_spec_m,1024)
elif algorithm == 'Default':
wave = (wave[1] * value) + (wave[0] * (1-value))
elif algorithm == 'Invert_p':
X_mag = np.abs(spec[0])
y_mag = np.abs(spec[1])
max_mag = np.where(X_mag >= y_mag, X_mag, y_mag)
v_spec = spec[1] - max_mag * np.exp(1.j * np.angle(spec[0]))
wave = istft(v_spec,1024)
return wave
def spectrogram_to_wave_no_mp(spec, n_fft=2048, hop_length=1024):
wave = librosa.istft(spec, n_fft=n_fft, hop_length=hop_length)
if wave.ndim == 1:
wave = np.asfortranarray([wave,wave])
return wave
def wave_to_spectrogram_no_mp(wave):
spec = librosa.stft(wave, n_fft=2048, hop_length=1024)
if spec.ndim == 1:
spec = np.asfortranarray([spec,spec])
return spec
def invert_audio(specs, invert_p=True):
ln = min([specs[0].shape[2], specs[1].shape[2]])
specs[0] = specs[0][:,:,:ln]
specs[1] = specs[1][:,:,:ln]
if invert_p:
X_mag = np.abs(specs[0])
y_mag = np.abs(specs[1])
max_mag = np.where(X_mag >= y_mag, X_mag, y_mag)
v_spec = specs[1] - max_mag * np.exp(1.j * np.angle(specs[0]))
else:
specs[1] = reduce_vocal_aggressively(specs[0], specs[1], 0.2)
v_spec = specs[0] - specs[1]
return v_spec
def invert_stem(mixture, stem):
mixture = wave_to_spectrogram_no_mp(mixture)
stem = wave_to_spectrogram_no_mp(stem)
output = spectrogram_to_wave_no_mp(invert_audio([mixture, stem]))
return -output.T
def ensembling(a, specs):
for i in range(1, len(specs)):
if i == 1:
spec = specs[0]
ln = min([spec.shape[2], specs[i].shape[2]])
spec = spec[:,:,:ln]
specs[i] = specs[i][:,:,:ln]
if MIN_SPEC == a:
spec = np.where(np.abs(specs[i]) <= np.abs(spec), specs[i], spec)
if MAX_SPEC == a:
spec = np.where(np.abs(specs[i]) >= np.abs(spec), specs[i], spec)
if AVERAGE == a:
spec = np.where(np.abs(specs[i]) == np.abs(spec), specs[i], spec)
return spec
def ensemble_inputs(audio_input, algorithm, is_normalization, wav_type_set, save_path):
wavs_ = []
if algorithm == AVERAGE:
output = average_audio(audio_input)
samplerate = 44100
else:
specs = []
for i in range(len(audio_input)):
wave, samplerate = librosa.load(audio_input[i], mono=False, sr=44100)
wavs_.append(wave)
spec = wave_to_spectrogram_no_mp(wave)
specs.append(spec)
wave_shapes = [w.shape[1] for w in wavs_]
target_shape = wavs_[wave_shapes.index(max(wave_shapes))]
output = spectrogram_to_wave_no_mp(ensembling(algorithm, specs))
output = to_shape(output, target_shape.shape)
sf.write(save_path, normalize(output.T, is_normalization), samplerate, subtype=wav_type_set)
def to_shape(x, target_shape):
padding_list = []
for x_dim, target_dim in zip(x.shape, target_shape):
pad_value = (target_dim - x_dim)
pad_tuple = ((0, pad_value))
padding_list.append(pad_tuple)
return np.pad(x, tuple(padding_list), mode='constant')
def to_shape_minimize(x: np.ndarray, target_shape):
padding_list = []
for x_dim, target_dim in zip(x.shape, target_shape):
pad_value = (target_dim - x_dim)
pad_tuple = ((0, pad_value))
padding_list.append(pad_tuple)
return np.pad(x, tuple(padding_list), mode='constant')
def augment_audio(export_path, audio_file, rate, is_normalization, wav_type_set, save_format=None, is_pitch=False):
wav, sr = librosa.load(audio_file, sr=44100, mono=False)
if wav.ndim == 1:
wav = np.asfortranarray([wav,wav])
if is_pitch:
wav_1 = pyrb.pitch_shift(wav[0], sr, rate, rbargs=None)
wav_2 = pyrb.pitch_shift(wav[1], sr, rate, rbargs=None)
else:
wav_1 = pyrb.time_stretch(wav[0], sr, rate, rbargs=None)
wav_2 = pyrb.time_stretch(wav[1], sr, rate, rbargs=None)
if wav_1.shape > wav_2.shape:
wav_2 = to_shape(wav_2, wav_1.shape)
if wav_1.shape < wav_2.shape:
wav_1 = to_shape(wav_1, wav_2.shape)
wav_mix = np.asfortranarray([wav_1, wav_2])
sf.write(export_path, normalize(wav_mix.T, is_normalization), sr, subtype=wav_type_set)
save_format(export_path)
def average_audio(audio):
waves = []
wave_shapes = []
final_waves = []
for i in range(len(audio)):
wave = librosa.load(audio[i], sr=44100, mono=False)
waves.append(wave[0])
wave_shapes.append(wave[0].shape[1])
wave_shapes_index = wave_shapes.index(max(wave_shapes))
target_shape = waves[wave_shapes_index]
waves.pop(wave_shapes_index)
final_waves.append(target_shape)
for n_array in waves:
wav_target = to_shape(n_array, target_shape.shape)
final_waves.append(wav_target)
waves = sum(final_waves)
waves = waves/len(audio)
return waves
def average_dual_sources(wav_1, wav_2, value):
if wav_1.shape > wav_2.shape:
wav_2 = to_shape(wav_2, wav_1.shape)
if wav_1.shape < wav_2.shape:
wav_1 = to_shape(wav_1, wav_2.shape)
wave = (wav_1 * value) + (wav_2 * (1-value))
return wave
def reshape_sources(wav_1: np.ndarray, wav_2: np.ndarray):
if wav_1.shape > wav_2.shape:
wav_2 = to_shape(wav_2, wav_1.shape)
if wav_1.shape < wav_2.shape:
ln = min([wav_1.shape[1], wav_2.shape[1]])
wav_2 = wav_2[:,:ln]
ln = min([wav_1.shape[1], wav_2.shape[1]])
wav_1 = wav_1[:,:ln]
wav_2 = wav_2[:,:ln]
return wav_2
def align_audio(file1, file2, file2_aligned, file_subtracted, wav_type_set, is_normalization, command_Text, progress_bar_main_var, save_format):
def get_diff(a, b):
corr = np.correlate(a, b, "full")
diff = corr.argmax() - (b.shape[0] - 1)
return diff
progress_bar_main_var.set(10)
# read tracks
wav1, sr1 = librosa.load(file1, sr=44100, mono=False)
wav2, sr2 = librosa.load(file2, sr=44100, mono=False)
wav1 = wav1.transpose()
wav2 = wav2.transpose()
command_Text(f"Audio file shapes: {wav1.shape} / {wav2.shape}\n")
wav2_org = wav2.copy()
progress_bar_main_var.set(20)
command_Text("Processing files... \n")
# pick random position and get diff
counts = {} # counting up for each diff value
progress = 20
check_range = 64
base = (64 / check_range)
for i in range(check_range):
index = int(random.uniform(44100 * 2, min(wav1.shape[0], wav2.shape[0]) - 44100 * 2))
shift = int(random.uniform(-22050,+22050))
samp1 = wav1[index :index +44100, 0] # currently use left channel
samp2 = wav2[index+shift:index+shift+44100, 0]
progress += 1 * base
progress_bar_main_var.set(progress)
diff = get_diff(samp1, samp2)
diff -= shift
if abs(diff) < 22050:
if not diff in counts:
counts[diff] = 0
counts[diff] += 1
# use max counted diff value
max_count = 0
est_diff = 0
for diff in counts.keys():
if counts[diff] > max_count:
max_count = counts[diff]
est_diff = diff
command_Text(f"Estimated difference is {est_diff} (count: {max_count})\n")
progress_bar_main_var.set(90)
audio_files = []
def save_aligned_audio(wav2_aligned):
command_Text(f"Aligned File 2 with File 1.\n")
command_Text(f"Saving files... ")
sf.write(file2_aligned, normalize(wav2_aligned, is_normalization), sr2, subtype=wav_type_set)
save_format(file2_aligned)
min_len = min(wav1.shape[0], wav2_aligned.shape[0])
wav_sub = wav1[:min_len] - wav2_aligned[:min_len]
audio_files.append(file2_aligned)
return min_len, wav_sub
# make aligned track 2
if est_diff > 0:
wav2_aligned = np.append(np.zeros((est_diff, 2)), wav2_org, axis=0)
min_len, wav_sub = save_aligned_audio(wav2_aligned)
elif est_diff < 0:
wav2_aligned = wav2_org[-est_diff:]
min_len, wav_sub = save_aligned_audio(wav2_aligned)
else:
command_Text(f"Audio files already aligned.\n")
command_Text(f"Saving inverted track... ")
min_len = min(wav1.shape[0], wav2.shape[0])
wav_sub = wav1[:min_len] - wav2[:min_len]
wav_sub = np.clip(wav_sub, -1, +1)
sf.write(file_subtracted, normalize(wav_sub, is_normalization), sr1, subtype=wav_type_set)
save_format(file_subtracted)
progress_bar_main_var.set(95)1# VR init.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import torch
from torch import nn
import torch.nn.functional as F
from lib_v5 import spec_utils
class Conv2DBNActiv(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
super(Conv2DBNActiv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(
nin, nout,
kernel_size=ksize,
stride=stride,
padding=pad,
dilation=dilation,
bias=False),
nn.BatchNorm2d(nout),
activ()
)
def __call__(self, x):
return self.conv(x)
class SeperableConv2DBNActiv(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
super(SeperableConv2DBNActiv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(
nin, nin,
kernel_size=ksize,
stride=stride,
padding=pad,
dilation=dilation,
groups=nin,
bias=False),
nn.Conv2d(
nin, nout,
kernel_size=1,
bias=False),
nn.BatchNorm2d(nout),
activ()
)
def __call__(self, x):
return self.conv(x)
class Encoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
super(Encoder, self).__init__()
self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
def __call__(self, x):
skip = self.conv1(x)
h = self.conv2(skip)
return h, skip
class Decoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
super(Decoder, self).__init__()
self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
self.dropout = nn.Dropout2d(0.1) if dropout else None
def __call__(self, x, skip=None):
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
if skip is not None:
skip = spec_utils.crop_center(skip, x)
x = torch.cat([x, skip], dim=1)
h = self.conv(x)
if self.dropout is not None:
h = self.dropout(h)
return h
class ASPPModule(nn.Module):
def __init__(self, nn_architecture, nin, nout, dilations=(4, 8, 16), activ=nn.ReLU):
super(ASPPModule, self).__init__()
self.conv1 = nn.Sequential(
nn.AdaptiveAvgPool2d((1, None)),
Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
)
self.nn_architecture = nn_architecture
self.six_layer = [129605]
self.seven_layer = [537238, 537227, 33966]
extra_conv = SeperableConv2DBNActiv(
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
self.conv3 = SeperableConv2DBNActiv(
nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
self.conv4 = SeperableConv2DBNActiv(
nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
self.conv5 = SeperableConv2DBNActiv(
nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
if self.nn_architecture in self.six_layer:
self.conv6 = extra_conv
nin_x = 6
elif self.nn_architecture in self.seven_layer:
self.conv6 = extra_conv
self.conv7 = extra_conv
nin_x = 7
else:
nin_x = 5
self.bottleneck = nn.Sequential(
Conv2DBNActiv(nin * nin_x, nout, 1, 1, 0, activ=activ),
nn.Dropout2d(0.1)
)
def forward(self, x):
_, _, h, w = x.size()
feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
feat2 = self.conv2(x)
feat3 = self.conv3(x)
feat4 = self.conv4(x)
feat5 = self.conv5(x)
if self.nn_architecture in self.six_layer:
feat6 = self.conv6(x)
out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6), dim=1)
elif self.nn_architecture in self.seven_layer:
feat6 = self.conv6(x)
feat7 = self.conv7(x)
out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6, feat7), dim=1)
else:
out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1)
bottle = self.bottleneck(out)
return bottle123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126import torchfrom torch import nnimport torch.nn.functional as F
from lib_v5 import spec_utils
class Conv2DBNActiv(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU): super(Conv2DBNActiv, self).__init__() self.conv = nn.Sequential( nn.Conv2d( nin, nout, kernel_size=ksize, stride=stride, padding=pad, dilation=dilation, bias=False), nn.BatchNorm2d(nout), activ() )
def __call__(self, x): return self.conv(x)
class Encoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU): super(Encoder, self).__init__() self.conv1 = Conv2DBNActiv(nin, nout, ksize, stride, pad, activ=activ) self.conv2 = Conv2DBNActiv(nout, nout, ksize, 1, pad, activ=activ)
def __call__(self, x): h = self.conv1(x) h = self.conv2(h)
return h
class Decoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False): super(Decoder, self).__init__() self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ) # self.conv2 = Conv2DBNActiv(nout, nout, ksize, 1, pad, activ=activ) self.dropout = nn.Dropout2d(0.1) if dropout else None
def __call__(self, x, skip=None): x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
if skip is not None: skip = spec_utils.crop_center(skip, x) x = torch.cat([x, skip], dim=1)
h = self.conv1(x) # h = self.conv2(h)
if self.dropout is not None: h = self.dropout(h)
return h
class ASPPModule(nn.Module):
def __init__(self, nin, nout, dilations=(4, 8, 12), activ=nn.ReLU, dropout=False): super(ASPPModule, self).__init__() self.conv1 = nn.Sequential( nn.AdaptiveAvgPool2d((1, None)), Conv2DBNActiv(nin, nout, 1, 1, 0, activ=activ) ) self.conv2 = Conv2DBNActiv(nin, nout, 1, 1, 0, activ=activ) self.conv3 = Conv2DBNActiv( nin, nout, 3, 1, dilations[0], dilations[0], activ=activ ) self.conv4 = Conv2DBNActiv( nin, nout, 3, 1, dilations[1], dilations[1], activ=activ ) self.conv5 = Conv2DBNActiv( nin, nout, 3, 1, dilations[2], dilations[2], activ=activ ) self.bottleneck = Conv2DBNActiv(nout * 5, nout, 1, 1, 0, activ=activ) self.dropout = nn.Dropout2d(0.1) if dropout else None
def forward(self, x): _, _, h, w = x.size() feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True) feat2 = self.conv2(x) feat3 = self.conv3(x) feat4 = self.conv4(x) feat5 = self.conv5(x) out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1) out = self.bottleneck(out)
if self.dropout is not None: out = self.dropout(out)
return out
class LSTMModule(nn.Module):
def __init__(self, nin_conv, nin_lstm, nout_lstm): super(LSTMModule, self).__init__() self.conv = Conv2DBNActiv(nin_conv, 1, 1, 1, 0) self.lstm = nn.LSTM( input_size=nin_lstm, hidden_size=nout_lstm // 2, bidirectional=True ) self.dense = nn.Sequential( nn.Linear(nout_lstm, nin_lstm), nn.BatchNorm1d(nin_lstm), nn.ReLU() )
def forward(self, x): N, _, nbins, nframes = x.size() h = self.conv(x)[:, 0] # N, nbins, nframes h = h.permute(2, 0, 1) # nframes, N, nbins h, _ = self.lstm(h) h = self.dense(h.reshape(-1, h.size()[-1])) # nframes * N, nbins h = h.reshape(nframes, N, 1, nbins) h = h.permute(1, 2, 3, 0)
return h1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import json
import pathlib
default_param = {}
default_param['bins'] = 768
default_param['unstable_bins'] = 9 # training only
default_param['reduction_bins'] = 762 # training only
default_param['sr'] = 44100
default_param['pre_filter_start'] = 757
default_param['pre_filter_stop'] = 768
default_param['band'] = {}
default_param['band'][1] = {
'sr': 11025,
'hl': 128,
'n_fft': 960,
'crop_start': 0,
'crop_stop': 245,
'lpf_start': 61, # inference only
'res_type': 'polyphase'
}
default_param['band'][2] = {
'sr': 44100,
'hl': 512,
'n_fft': 1536,
'crop_start': 24,
'crop_stop': 547,
'hpf_start': 81, # inference only
'res_type': 'sinc_best'
}
def int_keys(d):
r = {}
for k, v in d:
if k.isdigit():
k = int(k)
r[k] = v
return r
class ModelParameters(object):
def __init__(self, config_path=''):
if '.pth' == pathlib.Path(config_path).suffix:
import zipfile
with zipfile.ZipFile(config_path, 'r') as zip:
self.param = json.loads(zip.read('param.json'), object_pairs_hook=int_keys)
elif '.json' == pathlib.Path(config_path).suffix:
with open(config_path, 'r') as f:
self.param = json.loads(f.read(), object_pairs_hook=int_keys)
else:
self.param = default_param
for k in ['mid_side', 'mid_side_b', 'mid_side_b2', 'stereo_w', 'stereo_n', 'reverse']:
if not k in self.param:
self.param[k] = False12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 16000, "hl": 512, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 16000, "pre_filter_start": 1023, "pre_filter_stop": 1024}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 32000, "hl": 512, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "kaiser_fast" } }, "sr": 32000, "pre_filter_start": 1000, "pre_filter_stop": 1021}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 33075, "hl": 384, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 33075, "pre_filter_start": 1000, "pre_filter_stop": 1021}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 1024, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 1023, "pre_filter_stop": 1024}12345678910111213141516171819{ "bins": 256, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 256, "n_fft": 512, "crop_start": 0, "crop_stop": 256, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 256, "pre_filter_stop": 256}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 512, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 1023, "pre_filter_stop": 1024}.gitattributes+1
README.md+116-9
app.pyAdded+19
apt.txtAdded+3
denoised_audio.wavAdded+3
inference_gradio.pyAdded+603
lemas_tts/__init__.pyAdded+6
lemas_tts/api.pyAdded+252
lemas_tts/configs/multilingual_grl.yamlAdded+78
lemas_tts/configs/multilingual_prosody.yamlAdded+78
lemas_tts/infer/frontend.pyAdded+251
lemas_tts/infer/infer_cli.pyAdded+386
lemas_tts/infer/text_norm/__init__.pyAdded
lemas_tts/infer/text_norm/cn_tn.pyAdded+824
lemas_tts/infer/text_norm/en_tn.pyAdded+178
lemas_tts/infer/text_norm/gp2py.pyAdded+148
lemas_tts/infer/text_norm/id_tn.pyAdded+275
lemas_tts/infer/text_norm/jieba_dict.txtAdded
lemas_tts/infer/text_norm/pinyin-lexicon-r.txtAdded+4120
lemas_tts/infer/text_norm/symbols.pyAdded+419
lemas_tts/infer/text_norm/tokenizer.pyAdded+219
lemas_tts/infer/text_norm/txt2pinyin.pyAdded+225
lemas_tts/infer/utils_infer.pyAdded+651
lemas_tts/model/backbones/README.mdAdded+20
lemas_tts/model/backbones/dit.pyAdded+254
lemas_tts/model/backbones/ecapa_tdnn.pyAdded+931
lemas_tts/model/backbones/mmdit.pyAdded+189
lemas_tts/model/backbones/prosody_encoder.pyAdded+433
lemas_tts/model/backbones/unett.pyAdded+250
lemas_tts/model/cfm.pyAdded+899
lemas_tts/model/modules.pyAdded+802
lemas_tts/model/utils.pyAdded+190
lemas_tts/scripts/inference_gradio.pyAdded+584
requirements.txtAdded+182
uvr5/gui_data/constants.pyAdded+1147
uvr5/lib_v5/mdxnet.pyAdded+140
uvr5/lib_v5/mixer.ckptAdded+3
uvr5/lib_v5/modules.pyAdded+74
uvr5/lib_v5/pyrb.pyAdded+92
uvr5/lib_v5/spec_utils.pyAdded+703
uvr5/lib_v5/vr_network/__init__.pyAdded+1
uvr5/lib_v5/vr_network/layers.pyAdded+143
uvr5/lib_v5/vr_network/layers_new.pyAdded+126
uvr5/lib_v5/vr_network/model_param_init.pyAdded+59
uvr5/lib_v5/vr_network/modelparams/1band_sr16000_hl512.jsonAdded+19
uvr5/lib_v5/vr_network/modelparams/1band_sr32000_hl512.jsonAdded+19
uvr5/lib_v5/vr_network/modelparams/1band_sr33075_hl384.jsonAdded+19
uvr5/lib_v5/vr_network/modelparams/1band_sr44100_hl1024.jsonAdded+19
uvr5/lib_v5/vr_network/modelparams/1band_sr44100_hl256.jsonAdded+19
uvr5/lib_v5/vr_network/modelparams/1band_sr44100_hl512.jsonAdded+19