## build wavegru-cpp # import os # os.system("./bazelisk-linux-amd64 clean --expunge") # os.system("./bazelisk-linux-amd64 build wavegru_mod -c opt --copt=-march=native") # install espeak import os import re import unicodedata import regex if not os.path.isfile("./wavegru_mod.so"): os.system("bash ./build_ext.sh") import gradio as gr from inference import load_tacotron_model, load_wavegru_net, mel_to_wav, text_to_mel from wavegru_cpp import extract_weight_mask, load_wavegru_cpp alphabet, tacotron_net, tacotron_config = load_tacotron_model( "./alphabet.txt", "./tacotron.toml", "./mono_tts_cbhg_small_0700000.ckpt" ) wavegru_config, wavegru_net = load_wavegru_net( "./wavegru.yaml", "./wavegru_vocoder_tpu_gta_preemphasis_pruning_0400000.ckpt" ) wave_cpp_weight_mask = extract_weight_mask(wavegru_net) wavecpp = load_wavegru_cpp(wave_cpp_weight_mask, wavegru_config["upsample_factors"][-1]) space_re = regex.compile(r"\s+") number_re = regex.compile("([0-9]+)") digits = ["không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín"] num_re = regex.compile(r"([0-9.,]*[0-9])") alphabet_ = "aàáảãạăằắẳẵặâầấẩẫậeèéẻẽẹêềếểễệiìíỉĩịoòóỏõọôồốổỗộơờớởỡợuùúủũụưừứửữựyỳýỷỹỵbcdđghklmnpqrstvx" keep_text_and_num_re = regex.compile(rf"[^\s{alphabet_}.,0-9]") keep_text_re = regex.compile(rf"[^\s{alphabet_}]") def read_number(num: str) -> str: if len(num) == 1: return digits[int(num)] elif len(num) == 2 and num.isdigit(): n = int(num) end = digits[n % 10] if n == 10: return "mười" if n % 10 == 5: end = "lăm" if n % 10 == 0: return digits[n // 10] + " mươi" elif n < 20: return "mười " + end else: if n % 10 == 1: end = "mốt" return digits[n // 10] + " mươi " + end elif len(num) == 3 and num.isdigit(): n = int(num) if n % 100 == 0: return digits[n // 100] + " trăm" elif num[1] == "0": return digits[n // 100] + " trăm lẻ " + digits[n % 100] else: return digits[n // 100] + " trăm " + read_number(num[1:]) elif len(num) >= 4 and len(num) <= 6 and num.isdigit(): n = int(num) n1 = n // 1000 return read_number(str(n1)) + " ngàn " + read_number(num[-3:]) elif "," in num: n1, n2 = num.split(",") return read_number(n1) + " phẩy " + read_number(n2) elif "." in num: parts = num.split(".") if len(parts) == 2: if parts[1] == "000": return read_number(parts[0]) + " ngàn" elif parts[1].startswith("00"): end = digits[int(parts[1][2:])] return read_number(parts[0]) + " ngàn lẻ " + end else: return read_number(parts[0]) + " ngàn " + read_number(parts[1]) elif len(parts) == 3: return ( read_number(parts[0]) + " triệu " + read_number(parts[1]) + " ngàn " + read_number(parts[2]) ) return num def normalize_text(text): # lowercase text = text.lower() # unicode normalize text = unicodedata.normalize("NFKC", text) text = text.replace(".", ". ") text = text.replace(",", ", ") text = text.replace(";", "; ") text = text.replace(":", ": ") text = text.replace("!", "! ") text = text.replace("?", "? ") text = text.replace("(", "( ") text = num_re.sub(r" \1 ", text) words = text.split() words = [read_number(w) if num_re.fullmatch(w) else w for w in words] text = " ".join(words) # remove redundant spaces text = re.sub(r"\s+", " ", text) # remove leading and trailing spaces text = text.strip() return text def speak(text): text = normalize_text(text) mel = text_to_mel(tacotron_net, text, alphabet, tacotron_config) y = mel_to_wav(wavegru_net, wavecpp, mel, wavegru_config) return 24_000, y title = "WaveGRU-TTS" description = "WaveGRU text-to-speech demo." gr.Interface( fn=speak, inputs="text", examples=[ "Trăm năm trong cõi người ta, chữ tài chữ mệnh khéo là ghét nhau.", "Đoạn trường tân thanh, thường được biết đến với cái tên đơn giản là Truyện Kiều, là một truyện thơ của đại thi hào Nguyễn Du.", "Lục Vân Tiên quê ở huyện Đông Thành, khôi ngô tuấn tú, tài kiêm văn võ. Nghe tin triều đình mở khoa thi, Vân Tiên từ giã thầy xuống núi đua tài.", "Lê Quý Đôn, tên thuở nhỏ là Lê Danh Phương, là vị quan thời Lê trung hưng, cũng là nhà thơ và được mệnh danh là nhà bác học lớn của Việt Nam trong thời phong kiến.", "Tất cả mọi người đều sinh ra có quyền bình đẳng. Tạo hóa cho họ những quyền không ai có thể xâm phạm được, trong những quyền ấy, có quyền được sống, quyền tự do và quyền mưu cầu hạnh phúc.", ], outputs="audio", title=title, description=description, theme="default", allow_screenshot=False, allow_flagging="never", ).launch(enable_queue=True)