Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
os.system('cd monotonic_align && python setup.py build_ext --inplace && cd ..')
|
4 |
+
|
5 |
+
import json
|
6 |
+
import math
|
7 |
+
import torch
|
8 |
+
from torch import nn
|
9 |
+
from torch.nn import functional as F
|
10 |
+
from torch.utils.data import DataLoader
|
11 |
+
|
12 |
+
import commons
|
13 |
+
import utils
|
14 |
+
from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
|
15 |
+
from models import SynthesizerTrn
|
16 |
+
#from text.symbols import symbols
|
17 |
+
from wa_graphemes.symbols import symbols
|
18 |
+
from text import text_to_sequence
|
19 |
+
from scipy.io.wavfile import write
|
20 |
+
|
21 |
+
def get_text(text, hps):
|
22 |
+
text_norm = text_to_sequence(text, hps.data.text_cleaners)
|
23 |
+
if hps.data.add_blank:
|
24 |
+
text_norm = commons.intersperse(text_norm, 0)
|
25 |
+
text_norm = torch.LongTensor(text_norm)
|
26 |
+
return text_norm
|
27 |
+
|
28 |
+
def load_model(model_path, hps):
|
29 |
+
net_g = SynthesizerTrn(
|
30 |
+
len(symbols),
|
31 |
+
hps.data.filter_length // 2 + 1,
|
32 |
+
hps.train.segment_size // hps.data.hop_length,
|
33 |
+
n_speakers=hps.data.n_speakers,
|
34 |
+
**hps.model)
|
35 |
+
_ = net_g.eval()
|
36 |
+
_ = utils.load_checkpoint(model_path, net_g, None)
|
37 |
+
return net_g
|
38 |
+
|
39 |
+
#hps = utils.get_hparams_from_file("configs/vctk_base.json")
|
40 |
+
hps = utils.get_hparams_from_file("wa_graphemes/config.json")
|
41 |
+
|
42 |
+
# Define a dictionary to store the model paths for each tab
|
43 |
+
model_paths = {
|
44 |
+
"Graphemes": "wa_graphemes/G_258000.pth"
|
45 |
+
}
|
46 |
+
|
47 |
+
# Load the initial model
|
48 |
+
net_g = load_model(model_paths["Graphemes"], hps)
|
49 |
+
|
50 |
+
def tts(text, speaker_id, tab_name):
|
51 |
+
global net_g
|
52 |
+
net_g = load_model(model_paths[tab_name], hps)
|
53 |
+
sid = torch.LongTensor([speaker_id]) # speaker identity
|
54 |
+
stn_tst = get_text(text, hps)
|
55 |
+
|
56 |
+
with torch.no_grad():
|
57 |
+
x_tst = stn_tst.unsqueeze(0)
|
58 |
+
x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
|
59 |
+
audio = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][
|
60 |
+
0, 0].data.float().numpy()
|
61 |
+
return "Success", (hps.data.sampling_rate, audio)
|
62 |
+
|
63 |
+
def create_tab(tab_name):
|
64 |
+
with gr.TabItem(tab_name):
|
65 |
+
gr.Markdown(f"### {tab_name} TTS Model")
|
66 |
+
tts_input1 = gr.TextArea(label="Text in Walloon on IPA phonemes", value="")
|
67 |
+
tts_input2 = gr.Dropdown(label="Speaker", choices=["Male", "Female"], type="index", value="Male")
|
68 |
+
tts_submit = gr.Button("Generate", variant="primary")
|
69 |
+
tts_output1 = gr.Textbox(label="Message")
|
70 |
+
tts_output2 = gr.Audio(label="Output")
|
71 |
+
tts_submit.click(lambda text, speaker_id: tts(text, speaker_id, tab_name), [tts_input1, tts_input2], [tts_output1, tts_output2])
|
72 |
+
|
73 |
+
app = gr.Blocks()
|
74 |
+
with app:
|
75 |
+
gr.Markdown(
|
76 |
+
"""
|
77 |
+
# First Text to Speech (TTS) for Walloon
|
78 |
+
Based on VITS (https://github.com/jaywalnut310/vits).
|
79 |
+
Write the text in graphemes. For faster inference speed it is recommended to use short sentences.
|
80 |
+
"""
|
81 |
+
)
|
82 |
+
with gr.Tabs():
|
83 |
+
create_tab("Phonemes_finetuned")
|
84 |
+
|
85 |
+
gr.Markdown(
|
86 |
+
"""
|
87 |
+
### Examples
|
88 |
+
| Input Text | Speaker | Input Method |
|
89 |
+
|------------|---------|---------------|
|
90 |
+
| li biːç ɛ l sɔlja ɛstẽ ki s maʁɡajẽ pɔ sawɛ kiː ski , dɛ døː , ɛstøː l py fwaʁ . m ɛ̃ s koː la , la k i vɛjɛ õ tsminɔː k aʁivef pjim pjam , d ɛ̃ õ bja nuː tsoː paltɔ . | Female | Phonemes |
|
91 |
+
| Li bijhe et l’ solea estént ki s’ margayént po sawè kî çki, des deus, esteut l’ pus foirt. Mins ç’ côp la, la k’ i veyèt on tchminåd k' arivéve pyim piam, dins on bea noû tchôd paltot. | Male | Graphemes |
|
92 |
+
"""
|
93 |
+
)
|
94 |
+
|
95 |
+
app.launch()
|