import sys, os
import torch
import argparse
import commons
import utils
from models import SynthesizerTrn
from text.symbols import symbols
from text import cleaned_text_to_sequence, get_bert
from text.cleaner import clean_text
import gradio as gr
import soundfile as sf
from datetime import datetime
import pytz
tz = pytz.timezone('Asia/Shanghai')
net_g = None
models = {
"MistyNikki": "./MODELS/nikki.pth",
"AlluWin": "./MODELS/AW.pth",
"VVV":"./MODELS/v3.pth",
"StayTerra": "./MODELS/TERRA.pth",
"Rrabbitt": "./MODELS/rabbit4900.pth",
"Xelo": "./MODELS/HER_1100.pth",
"Hypnosia": "./MODELS/hypno.pth",
"Appelsin":"./MODELS/orange.pth",
"Kitsune": "./MODELS/fox2100.pth",
"Silverleg": "./MODELS/J8900.pth",
"PremJ": "./MODELS/premj.pth",
"Robo!Girl": "./MODELS/BG1300.pth",
"HeavyHammer": "./MODELS/hammer.pth",
"TaxiAI": "./MODELS/DLM.pth",
}
def get_text(text, language_str, hps):
norm_text, phone, tone, word2ph = clean_text(text, language_str)
phone, tone, language = cleaned_text_to_sequence(phone, tone, language_str)
if hps.data.add_blank:
phone = commons.intersperse(phone, 0)
tone = commons.intersperse(tone, 0)
language = commons.intersperse(language, 0)
for i in range(len(word2ph)):
word2ph[i] = word2ph[i] * 2
word2ph[0] += 1
bert = get_bert(norm_text, word2ph, language_str)
del word2ph
assert bert.shape[-1] == len(phone)
phone = torch.LongTensor(phone)
tone = torch.LongTensor(tone)
language = torch.LongTensor(language)
return bert, phone, tone, language
def infer(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid, model_dir):
global net_g
bert, phones, tones, lang_ids = get_text(text, "ZH", hps)
with torch.no_grad():
x_tst=phones.to(device).unsqueeze(0)
tones=tones.to(device).unsqueeze(0)
lang_ids=lang_ids.to(device).unsqueeze(0)
bert = bert.to(device).unsqueeze(0)
x_tst_lengths = torch.LongTensor([phones.size(0)]).to(device)
del phones
speakers = torch.LongTensor([hps.data.spk2id[sid]]).to(device)
audio = net_g.infer(x_tst, x_tst_lengths, speakers, tones, lang_ids, bert, sdp_ratio=sdp_ratio
, noise_scale=noise_scale, noise_scale_w=noise_scale_w, length_scale=length_scale)[0][0,0].data.cpu().float().numpy()
del x_tst, tones, lang_ids, bert, x_tst_lengths, speakers
sf.write("tmp.wav", audio, 44100)
return audio
def convert_wav_to_mp3(wav_file):
global tz
now = datetime.now(tz).strftime('%m%d%H%M%S')
os.makedirs('out', exist_ok=True)
output_path_mp3 = os.path.join('out', f"{now}.mp3")
renamed_input_path = os.path.join('in', f"in.wav")
os.makedirs('in', exist_ok=True)
os.rename(wav_file.name, renamed_input_path)
command = ["ffmpeg", "-i", renamed_input_path, "-acodec", "libmp3lame", "-y", output_path_mp3]
os.system(" ".join(command))
print(str(output_path_mp3))
return output_path_mp3
def tts_generator(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, model):
global net_g,speakers,tz
now = datetime.now(tz).strftime('%m-%d %H:%M:%S')
model_path = models[model]
net_g, _, _, _ = utils.load_checkpoint(model_path, net_g, None, skip_optimizer=True)
print(now+text)
try:
with torch.no_grad():
audio = infer(text, sdp_ratio=sdp_ratio, noise_scale=noise_scale, noise_scale_w=noise_scale_w, length_scale=length_scale, sid=speaker,model_dir=model)
with open('tmp.wav', 'rb') as wav_file:
mp3 = convert_wav_to_mp3(wav_file)
return "生成语音成功", (hps.data.sampling_rate, audio), mp3
except Exception as e:
return "生成语音失败:" + str(e), None, None
if __name__ == "__main__":
hps = utils.get_hparams_from_file("./configs/config.json")
device = "cuda:0" if torch.cuda.is_available() else "cpu"
net_g = SynthesizerTrn(
len(symbols),
hps.data.filter_length // 2 + 1,
hps.train.segment_size // hps.data.hop_length,
n_speakers=hps.data.n_speakers,
**hps.model).to(device)
_ = net_g.eval()
speaker_ids = hps.data.spk2id
speaker = list(speaker_ids.keys())[0]
theme='remilia/Ghostly'
with gr.Blocks(theme=theme) as app:
with gr.Column():
gr.HTML('''
本空间仅支持中文生成
Currently, this space only supports Chinese generation.
現在、このスペースは中国語の生成のみをサポートしています。
Use English to generate, please go to this SPACE
日本語で生成するために、こちらへ進んでください。