Vihang28 commited on
Commit
4bcd77c
·
1 Parent(s): 1445671

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from TTS.api import TTS
3
+
4
+ # Init TTS
5
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False)
6
+ zh_tts = TTS(model_name="tts_models/zh-CN/baker/tacotron2-DDC-GST", progress_bar=False, gpu=False)
7
+ de_tts = TTS(model_name="tts_models/de/thorsten/vits", gpu=False)
8
+ es_tts = TTS(model_name="tts_models/es/mai/tacotron2-DDC", progress_bar=False, gpu=False)
9
+
10
+ def text_to_speech(text: str, speaker_wav, speaker_wav_file, language: str):
11
+ if speaker_wav_file and not speaker_wav:
12
+ speaker_wav = speaker_wav_file
13
+ file_path = "output.wav"
14
+ if language == "zh-CN":
15
+ # if speaker_wav is not None:
16
+ # zh_tts.tts_to_file(text, speaker_wav=speaker_wav, file_path=file_path)
17
+ # else:
18
+ zh_tts.tts_to_file(text, file_path=file_path)
19
+ elif language == "de":
20
+ # if speaker_wav is not None:
21
+ # de_tts.tts_to_file(text, speaker_wav=speaker_wav, file_path=file_path)
22
+ # else:
23
+ de_tts.tts_to_file(text, file_path=file_path)
24
+ elif language == "es":
25
+ # if speaker_wav is not None:
26
+ # es_tts.tts_to_file(text, speaker_wav=speaker_wav, file_path=file_path)
27
+ # else:
28
+ es_tts.tts_to_file(text, file_path=file_path)
29
+ else:
30
+ if speaker_wav is not None:
31
+ tts.tts_to_file(text, speaker_wav=speaker_wav, language=language, file_path=file_path)
32
+ else:
33
+ tts.tts_to_file(text, speaker=tts.speakers[0], language=language, file_path=file_path)
34
+ return file_path
35
+
36
+
37
+
38
+ title = "Voice-Cloning-Demo"
39
+
40
+ def toggle(choice):
41
+ if choice == "mic":
42
+ return gr.update(visible=True, value=None), gr.update(visible=False, value=None)
43
+ else:
44
+ return gr.update(visible=False, value=None), gr.update(visible=True, value=None)
45
+
46
+ def handle_language_change(choice):
47
+ if choice == "zh-CN" or choice == "de" or choice == "es":
48
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
49
+ else:
50
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
51
+
52
+ warming_text = """Please note that Chinese, German, and Spanish are currently not supported for voice cloning."""
53
+
54
+ with gr.Blocks() as demo:
55
+ with gr.Row():
56
+ with gr.Column():
57
+ text_input = gr.Textbox(label="Input the text", value="", max_lines=3)
58
+ lan_input = gr.Radio(label="Language", choices=["en", "fr-fr", "pt-br", "zh-CN", "de", "es"], value="en")
59
+ gr.Markdown(warming_text)
60
+ radio = gr.Radio(["mic", "file"], value="mic",
61
+ label="How would you like to upload your audio?")
62
+ audio_input_mic = gr.Audio(label="Voice to clone", source="microphone", type="filepath", visible=True)
63
+ audio_input_file = gr.Audio(label="Voice to clone", type="filepath", visible=False)
64
+
65
+ with gr.Row():
66
+ with gr.Column():
67
+ btn_clear = gr.Button("Clear")
68
+ with gr.Column():
69
+ btn = gr.Button("Submit", variant="primary")
70
+ with gr.Column():
71
+ audio_output = gr.Audio(label="Output")
72
+
73
+ # gr.Examples(examples, fn=inference, inputs=[audio_file, text_input],
74
+ # outputs=audio_output, cache_examples=True)
75
+ if text_input != "":
76
+ btn.click(text_to_speech, inputs=[text_input, audio_input_mic,
77
+ audio_input_file, lan_input], outputs=audio_output)
78
+ radio.change(toggle, radio, [audio_input_mic, audio_input_file])
79
+ lan_input.change(handle_language_change, lan_input, [radio, audio_input_mic, audio_input_file])
80
+
81
+ demo.launch(enable_queue=True)