Vihang28 commited on
Commit
d31f3e8
·
1 Parent(s): fba7a02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -1,6 +1,14 @@
1
  import gradio as gr
2
  from TTS.api import TTS
3
 
 
 
 
 
 
 
 
 
4
 
5
  # Init TTS
6
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
@@ -8,20 +16,27 @@ zh_tts = TTS(model_name="tts_models/zh-CN/baker/tacotron2-DDC-GST", progress_bar
8
  de_tts = TTS(model_name="tts_models/de/thorsten/vits", gpu=False)
9
  es_tts = TTS(model_name="tts_models/es/mai/tacotron2-DDC", progress_bar=False, gpu=False)
10
 
11
-
12
  def text_to_speech(text: str, speaker_wav, speaker_wav_file):
13
  if len(text) > 0:
14
- if speaker_wav_file and not speaker_wav:
15
- speaker_wav = speaker_wav_file
16
- file_path = "output.wav"
17
- if speaker_wav is not None:
18
- tts.tts_to_file(text, speaker_wav=speaker_wav, language="en", file_path=file_path)
19
- else:
20
- tts.tts_to_file(text, speaker=tts.speakers[0], language="en", file_path=file_path)
21
- return file_path
 
 
22
  else:
23
- raise gr.Error("Input box cannot be blank")
 
24
 
 
 
 
 
 
25
 
26
  title = "Voice-Cloning-Demo"
27
 
@@ -30,9 +45,13 @@ def toggle(choice):
30
  return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
31
  else:
32
  return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
 
 
 
 
 
33
 
34
-
35
- with gr.Blocks() as demo:
36
  with gr.Row():
37
  with gr.Column():
38
  text_input = gr.Textbox(label="Input the text", value="", max_lines=3)
@@ -47,12 +66,15 @@ with gr.Blocks() as demo:
47
  with gr.Column():
48
  btn = gr.Button("Submit", variant="primary")
49
  with gr.Column():
50
- audio_output = gr.Audio(label="Output")
 
51
 
52
  # gr.Examples(examples, fn=inference, inputs=[audio_file, text_input],
53
  # outputs=audio_output, cache_examples=True)
54
- btn.click(text_to_speech, inputs=[text_input, audio_input_mic,audio_input_file], outputs=audio_output)
 
55
  radio.change(toggle, radio, [audio_input_mic, audio_input_file])
56
  btn_clear.add(audio_output)
 
57
 
58
  demo.launch(share=True)
 
1
  import gradio as gr
2
  from TTS.api import TTS
3
 
4
+ css = """
5
+ #warning {background-color: #FFCCCB !important}
6
+ .feedback label textarea {height: auto !important;
7
+ font-size: 25px !important;
8
+ font-weight: 600 !important;
9
+ text-align: center !important}
10
+ #alert {background-color: #fff !important}
11
+ """
12
 
13
  # Init TTS
14
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
 
16
  de_tts = TTS(model_name="tts_models/de/thorsten/vits", gpu=False)
17
  es_tts = TTS(model_name="tts_models/es/mai/tacotron2-DDC", progress_bar=False, gpu=False)
18
 
 
19
  def text_to_speech(text: str, speaker_wav, speaker_wav_file):
20
  if len(text) > 0:
21
+ return change_aud(text, speaker_wav, speaker_wav_file)
22
+ else:
23
+ return (None)
24
+
25
+ def change_aud(text: str, speaker_wav, speaker_wav_file):
26
+ if speaker_wav_file and not speaker_wav:
27
+ speaker_wav = speaker_wav_file
28
+ file_path = "output.wav"
29
+ if speaker_wav is not None:
30
+ tts.tts_to_file(text, speaker_wav=speaker_wav, language="en", file_path=file_path)
31
  else:
32
+ tts.tts_to_file(text, speaker=tts.speakers[0], language="en", file_path=file_path)
33
+ return file_path
34
 
35
+ def show_error(text):
36
+ if len(text) == 0:
37
+ return gr.update(visible=True, elem_id="warning", elem_classes="feedback"), gr.update(visible=False)
38
+ else:
39
+ return gr.update(visible=False), gr.update(visible=True)
40
 
41
  title = "Voice-Cloning-Demo"
42
 
 
45
  return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
46
  else:
47
  return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
48
+ def change_color(text_input):
49
+ if len(text_input) == 0:
50
+ return gr.update(elem_id="warning")
51
+ else:
52
+ return gr.update(elem_id="alert")
53
 
54
+ with gr.Blocks(css=css) as demo:
 
55
  with gr.Row():
56
  with gr.Column():
57
  text_input = gr.Textbox(label="Input the text", value="", max_lines=3)
 
66
  with gr.Column():
67
  btn = gr.Button("Submit", variant="primary")
68
  with gr.Column():
69
+ audio_output = gr.Audio(label="Output", visible=True)
70
+ error_box = gr.Textbox(label="ERROR", value="Input box cannot be blank!!", visible=False)
71
 
72
  # gr.Examples(examples, fn=inference, inputs=[audio_file, text_input],
73
  # outputs=audio_output, cache_examples=True)
74
+ btn.click(text_to_speech, inputs=[text_input, audio_input_mic, audio_input_file], outputs=audio_output)
75
+ btn.click(show_error, text_input, [error_box, audio_output])
76
  radio.change(toggle, radio, [audio_input_mic, audio_input_file])
77
  btn_clear.add(audio_output)
78
+ btn.click(change_color, text_input, text_input)
79
 
80
  demo.launch(share=True)