alysa commited on
Commit
b89ac4f
1 Parent(s): 22b7a23

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from models import SynthesizerTrn
2
+ from vits_pinyin import VITS_PinYin
3
+ from text import cleaned_text_to_sequence
4
+ from text.symbols import symbols
5
+ import gradio as gr
6
+ import utils
7
+ import torch
8
+ import argparse
9
+ import os
10
+ import re
11
+ import logging
12
+
13
+ logging.getLogger('numba').setLevel(logging.WARNING)
14
+ limitation = os.getenv("SYSTEM") == "spaces"
15
+
16
+
17
+ def create_calback(net_g: SynthesizerTrn, tts_front: VITS_PinYin):
18
+ def tts_calback(text, dur_scale):
19
+ if limitation:
20
+ text_len = len(re.sub("\[([A-Z]{2})\]", "", text))
21
+ max_len = 150
22
+ if text_len > max_len:
23
+ return "Error: Text is too long", None
24
+
25
+ phonemes, char_embeds = tts_front.chinese_to_phonemes(text)
26
+ input_ids = cleaned_text_to_sequence(phonemes)
27
+ with torch.no_grad():
28
+ x_tst = torch.LongTensor(input_ids).unsqueeze(0).to(device)
29
+ x_tst_lengths = torch.LongTensor([len(input_ids)]).to(device)
30
+ x_tst_prosody = torch.FloatTensor(
31
+ char_embeds).unsqueeze(0).to(device)
32
+ audio = net_g.infer(x_tst, x_tst_lengths, x_tst_prosody, noise_scale=0.5,
33
+ length_scale=dur_scale)[0][0, 0].data.cpu().float().numpy()
34
+ del x_tst, x_tst_lengths, x_tst_prosody
35
+ return "Success", (16000, audio)
36
+
37
+ return tts_calback
38
+
39
+
40
+ example = [['天空呈现的透心的蓝,像极了当年。总在这样的时候,透过窗棂,心,在天空里无尽的游弋!柔柔的,浓浓的,痴痴的风,牵引起心底灵动的思潮;情愫悠悠,思情绵绵,风里默坐,红尘中的浅醉,诗词中的优柔,任那自在飞花轻似梦的情怀,裁一束霓衣,织就清浅淡薄的安寂。', 1],
41
+ ['风的影子翻阅过淡蓝色的信笺,柔和的文字浅浅地漫过我安静的眸,一如几朵悠闲的云儿,忽而氤氲成汽,忽而修饰成花,铅华洗尽后的透彻和靓丽,爽爽朗朗,轻轻盈盈', 1],
42
+ ['时光仿佛有穿越到了从前,在你诗情画意的眼波中,在你舒适浪漫的暇思里,我如风中的思绪徜徉广阔天际,仿佛一片沾染了快乐的羽毛,在云环影绕颤动里浸润着风的呼吸,风的诗韵,那清新的耳语,那婉约的甜蜜,那恬淡的温馨,将一腔情澜染得愈发的缠绵。', 1],]
43
+
44
+
45
+ if __name__ == "__main__":
46
+ parser = argparse.ArgumentParser()
47
+ parser.add_argument("--share", action="store_true",
48
+ default=False, help="share gradio app")
49
+ args = parser.parse_args()
50
+
51
+ device = torch.device("cpu")
52
+
53
+ # pinyin
54
+ tts_front = VITS_PinYin("./bert", device)
55
+
56
+ # config
57
+ hps = utils.get_hparams_from_file("./configs/bert_vits.json")
58
+
59
+ # model
60
+ net_g = SynthesizerTrn(
61
+ len(symbols),
62
+ hps.data.filter_length // 2 + 1,
63
+ hps.train.segment_size // hps.data.hop_length,
64
+ **hps.model)
65
+
66
+ model_path = "vits_bert_model.pth"
67
+ utils.load_model(model_path, net_g)
68
+ net_g.eval()
69
+ net_g.to(device)
70
+
71
+ tts_calback = create_calback(net_g, tts_front)
72
+
73
+ app = gr.Blocks()
74
+ with app:
75
+ gr.Markdown("# Best TTS based on BERT and VITS with some Natural Speech Features Of Microsoft\n\n"
76
+ "code : github.com/PlayVoice/vits_chinese\n\n"
77
+ "1, Hidden prosody embedding from BERT,get natural pauses in grammar\n\n"
78
+ "2, Infer loss from NaturalSpeech,get less sound error\n\n"
79
+ "3, Framework of VITS,get high audio quality\n\n"
80
+ "<video id='video' controls='' preload='yes'>\n\n"
81
+ "<source id='mp4' src='https://user-images.githubusercontent.com/16432329/220678182-4775dec8-9229-4578-870f-2eebc3a5d660.mp4' type='video/mp4'>\n\n"
82
+ "</videos>\n\n"
83
+ )
84
+
85
+ with gr.Tabs():
86
+ with gr.TabItem("TTS"):
87
+ with gr.Row():
88
+ with gr.Column():
89
+ textbox = gr.TextArea(label="Text",
90
+ placeholder="Type your sentence here (Maximum 150 words)",
91
+ value="中文语音合成", elem_id=f"tts-input")
92
+ duration_slider = gr.Slider(minimum=0.1, maximum=5, value=1, step=0.1,
93
+ label='速度 Speed')
94
+ with gr.Column():
95
+ text_output = gr.Textbox(label="Message")
96
+ audio_output = gr.Audio(
97
+ label="Output Audio", elem_id="tts-audio")
98
+ btn = gr.Button("Generate!")
99
+ btn.click(tts_calback,
100
+ inputs=[textbox, duration_slider],
101
+ outputs=[text_output, audio_output])
102
+ gr.Examples(
103
+ examples=example,
104
+ inputs=[textbox, duration_slider],
105
+ outputs=[text_output, audio_output],
106
+ fn=tts_calback
107
+ )
108
+ app.queue(concurrency_count=3).launch(show_api=False, share=args.share)