Dzkaka commited on
Commit
23e6529
1 Parent(s): 1d91c75

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -4
app.py CHANGED
@@ -1,7 +1,105 @@
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
1
+ import os
2
+ import random
3
+ import argparse
4
+
5
+ import torch
6
  import gradio as gr
7
+ import numpy as np
8
+
9
+ import ChatTTS
10
+
11
+ print("loading ChatTTS model...")
12
+ chat = ChatTTS.Chat()
13
+ chat.load_models()
14
+
15
+ def generate_seed():
16
+ new_seed = random.randint(1, 100000000)
17
+ return {
18
+ "__type__": "update",
19
+ "value": new_seed
20
+ }
21
+
22
+
23
+ def generate_audio(text, temperature, top_P, top_K, audio_seed_input, text_seed_input, refine_text_flag):
24
+
25
+ torch.manual_seed(audio_seed_input)
26
+ rand_spk = torch.randn(768)
27
+ params_infer_code = {
28
+ 'spk_emb': rand_spk,
29
+ 'temperature': temperature,
30
+ 'top_P': top_P,
31
+ 'top_K': top_K,
32
+ }
33
+ params_refine_text = {'prompt': '[oral_2][laugh_0][break_6]'}
34
+
35
+ torch.manual_seed(text_seed_input)
36
+
37
+ if refine_text_flag:
38
+ text = chat.infer(text,
39
+ skip_refine_text=False,
40
+ refine_text_only=True,
41
+ params_refine_text=params_refine_text,
42
+ params_infer_code=params_infer_code
43
+ )
44
+
45
+ wav = chat.infer(text,
46
+ skip_refine_text=True,
47
+ params_refine_text=params_refine_text,
48
+ params_infer_code=params_infer_code
49
+ )
50
+
51
+ audio_data = np.array(wav[0]).flatten()
52
+ sample_rate = 24000
53
+ text_data = text[0] if isinstance(text, list) else text
54
+
55
+ return [(sample_rate, audio_data), text_data]
56
+
57
+
58
+ def main():
59
+
60
+ with gr.Blocks() as demo:
61
+ gr.Markdown("# ChatTTS Webui")
62
+ gr.Markdown("ChatTTS Model: [2noise/ChatTTS](https://github.com/2noise/ChatTTS)")
63
+
64
+ default_text = "四川美食确实以辣闻名,但也有不辣的选择。比如甜水面、赖汤圆、蛋烘糕、叶儿粑等,这些小吃口味温和,甜而不腻,也很受欢迎。"
65
+ text_input = gr.Textbox(label="Input Text", lines=4, placeholder="Please Input Text...", value=default_text)
66
+
67
+ with gr.Row():
68
+ refine_text_checkbox = gr.Checkbox(label="Refine text", value=True)
69
+ temperature_slider = gr.Slider(minimum=0.00001, maximum=1.0, step=0.00001, value=0.3, label="Audio temperature")
70
+ top_p_slider = gr.Slider(minimum=0.1, maximum=0.9, step=0.05, value=0.7, label="top_P")
71
+ top_k_slider = gr.Slider(minimum=1, maximum=20, step=1, value=20, label="top_K")
72
+
73
+ with gr.Row():
74
+ audio_seed_input = gr.Number(value=42, label="Audio Seed")
75
+ generate_audio_seed = gr.Button("\U0001F3B2")
76
+ text_seed_input = gr.Number(value=42, label="Text Seed")
77
+ generate_text_seed = gr.Button("\U0001F3B2")
78
+
79
+ generate_button = gr.Button("Generate")
80
+
81
+ text_output = gr.Textbox(label="Output Text", interactive=False)
82
+ audio_output = gr.Audio(label="Output Audio")
83
+
84
+ generate_audio_seed.click(generate_seed,
85
+ inputs=[],
86
+ outputs=audio_seed_input)
87
+
88
+ generate_text_seed.click(generate_seed,
89
+ inputs=[],
90
+ outputs=text_seed_input)
91
+
92
+ generate_button.click(generate_audio,
93
+ inputs=[text_input, temperature_slider, top_p_slider, top_k_slider, audio_seed_input, text_seed_input, refine_text_checkbox],
94
+ outputs=[audio_output, text_output])
95
+
96
+ parser = argparse.ArgumentParser(description='ChatTTS demo Launch')
97
+ parser.add_argument('--server_name', type=str, default='0.0.0.0', help='Server name')
98
+ parser.add_argument('--server_port', type=int, default=8080, help='Server port')
99
+ args = parser.parse_args()
100
+
101
+ demo.launch(server_name=args.server_name, server_port=args.server_port, inbrowser=True)
102
 
 
 
103
 
104
+ if __name__ == '__main__':
105
+ main()