kushan1988 commited on
Commit
bf87380
·
1 Parent(s): 25eea19
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile
2
+ from typing import Optional
3
+ from TTS.config import load_config
4
+ import gradio as gr
5
+ import numpy as np
6
+ from TTS.utils.manage import ModelManager
7
+ from TTS.utils.synthesizer import Synthesizer
8
+
9
+
10
+ MODELS = {}
11
+ SPEAKERS = {}
12
+ MAX_TXT_LEN = 100
13
+
14
+
15
+ manager = ModelManager()
16
+ MODEL_NAMES = manager.list_tts_models()
17
+
18
+ # filter out multi-speaker models and slow wavegrad vocoders
19
+ filters = ["vctk", "your_tts", "ek1"]
20
+ MODEL_NAMES = [model_name for model_name in MODEL_NAMES if not any(f in model_name for f in filters)]
21
+
22
+ EN = [el for el in MODEL_NAMES if "/en/" in el]
23
+ OTHER = [el for el in MODEL_NAMES if "/en/" not in el]
24
+ EN[0], EN[5] = EN[5], EN[0]
25
+ MODEL_NAMES = EN + OTHER
26
+
27
+ # reorder models
28
+ print(MODEL_NAMES)
29
+
30
+
31
+ def tts(text: str, model_name: str):
32
+ if len(text) > MAX_TXT_LEN:
33
+ text = text[:MAX_TXT_LEN]
34
+ print(f"Input text was cutoff since it went over the {MAX_TXT_LEN} character limit.")
35
+ print(text, model_name)
36
+ # download model
37
+ model_path, config_path, model_item = manager.download_model(model_name)
38
+ vocoder_name: Optional[str] = model_item["default_vocoder"]
39
+ # download vocoder
40
+ vocoder_path = None
41
+ vocoder_config_path = None
42
+ if vocoder_name is not None:
43
+ vocoder_path, vocoder_config_path, _ = manager.download_model(vocoder_name)
44
+ # init synthesizer
45
+ synthesizer = Synthesizer(
46
+ model_path, config_path, None, None, vocoder_path, vocoder_config_path,
47
+ )
48
+ # synthesize
49
+ if synthesizer is None:
50
+ raise NameError("model not found")
51
+ wavs = synthesizer.tts(text, None)
52
+ # return output
53
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
54
+ synthesizer.save_wav(wavs, fp)
55
+ return fp.name
56
+
57
+ with gr.Blocks(analytics_enabled=False) as demo:
58
+ gr.Markdown("<h2 align='center'>🐸💬 CoquiTTS Model Testing Playground 🐸💬</h2>")
59
+ with gr.Row():
60
+ with gr.Column():
61
+ input_text = gr.Textbox(
62
+ label="Input Text",
63
+ value="This sentence has been generated by a speech synthesis system.",
64
+ )
65
+ model_select = gr.Dropdown(
66
+ label="Pick Model: tts_models/<language>/<dataset>/<model_name>",
67
+ choices=MODEL_NAMES,
68
+ value="tts_models/en/jenny/jenny"
69
+ )
70
+ tts_button = gr.Button("Send", elem_id="send-btn", visible=True)
71
+
72
+ with gr.Column():
73
+ output_audio = gr.Audio(label="Output", type="filepath")
74
+
75
+ tts_button.click(
76
+ tts,
77
+ inputs=[
78
+ input_text,
79
+ model_select,
80
+ ],
81
+ outputs=[output_audio],
82
+ )
83
+
84
+ # iface = gr.Interface(fn=tts,
85
+ # inputs=[
86
+ # gr.Text(label="text", value="Hello, how are you?"),
87
+ # gr.Dropdown(choices=MODEL_NAMES, label="Models", info="Chose a model.", default="tts_models/en/jenny/jenny")
88
+ # ],
89
+ # outputs=[gr.Audio(label="Output", type="filepath")],
90
+ # examples=[
91
+ # ["Hello Jhon. Welcome to our group.", "model1.wav"],
92
+ # ["Hello Jhon. Welcome to our group.", "model2.mp3"]]
93
+ # )
94
+ # iface.launch(share=True, debug=True)
95
+ demo.queue(concurrency_count=16).launch(debug=True)