Pipe1213's picture
Update app.py
f272b23 verified
raw
history blame
2.94 kB
import gradio as gr
import os
os.system('cd monotonic_align && python setup.py build_ext --inplace && cd ..')
import json
import math
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader
import commons
import utils
from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
from models import SynthesizerTrn
from text.symbols import symbols
from text import text_to_sequence
from scipy.io.wavfile import write
def get_text(text, hps):
text_norm = text_to_sequence(text, hps.data.text_cleaners)
if hps.data.add_blank:
text_norm = commons.intersperse(text_norm, 0)
text_norm = torch.LongTensor(text_norm)
return text_norm
def load_model(model_path, hps):
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)
_ = net_g.eval()
_ = utils.load_checkpoint(model_path, net_g, None)
return net_g
hps = utils.get_hparams_from_file("configs/vctk_base.json")
# Define a dictionary to store the model paths
model_paths = {
"Model 1": "fr_wa_finetuned_pho/G_125000.pth",
"Model 2": "fr_wa_finetuned/G_198000.pth",
"Model 3": "path_to_model_3_checkpoint.pth",
"Model 4": "path_to_model_4_checkpoint.pth"
}
# Load the initial model
net_g = load_model(model_paths["Model 1"], hps)
def tts(text, speaker_id, model_choice):
global net_g
net_g = load_model(model_paths[model_choice], hps)
if len(text) > 2000:
return "Error: Text is too long", None
sid = torch.LongTensor([speaker_id]) # speaker identity
stn_tst = get_text(text, hps)
with torch.no_grad():
x_tst = stn_tst.unsqueeze(0)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
audio = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][
0, 0].data.float().numpy()
return "Success", (hps.data.sampling_rate, audio)
app = gr.Blocks()
with app:
with gr.Tabs():
for tab_name in ["Phonemes_finetuned", "Graphemes_finetuned", "Phonemes", "Graphemes"]:
with gr.TabItem(tab_name):
tts_input1 = gr.TextArea(label="Text in Walloon in phonemes IPA (2000 words limitation)", value="")
tts_input2 = gr.Dropdown(label="Speaker", choices=["Male", "Female"], type="index", value="Male")
model_choice = gr.Dropdown(label="Model", choices=list(model_paths.keys()), value="Model 1")
tts_submit = gr.Button("Generate", variant="primary")
tts_output1 = gr.Textbox(label="Message")
tts_output2 = gr.Audio(label="Output")
tts_submit.click(tts, [tts_input1, tts_input2, model_choice], [tts_output1, tts_output2])
app.launch()