Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from TTS.api import TTS | |
| css = """ | |
| #warning {background-color: #FFCCCB !important} | |
| .feedback label textarea {height: auto !important; | |
| font-size: 22px !important; | |
| font-weight: 800 !important; | |
| text-align: center !important; | |
| color: #801313 !important; | |
| padding: 0px !important} | |
| #alert {background-color: #fff !important} | |
| """ | |
| # Init TTS | |
| tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=False) | |
| zh_tts = TTS(model_name="tts_models/zh-CN/baker/tacotron2-DDC-GST", progress_bar=False, gpu=False) | |
| de_tts = TTS(model_name="tts_models/de/thorsten/vits", gpu=False) | |
| es_tts = TTS(model_name="tts_models/es/mai/tacotron2-DDC", progress_bar=False, gpu=False) | |
| def text_to_speech(text: str, speaker_wav, speaker_wav_file): | |
| if len(text) > 0: | |
| return change_aud(text, speaker_wav, speaker_wav_file) | |
| else: | |
| return (None) | |
| def change_aud(text: str, speaker_wav, speaker_wav_file): | |
| if speaker_wav_file and not speaker_wav: | |
| speaker_wav = speaker_wav_file | |
| file_path = "output.wav" | |
| if speaker_wav is not None: | |
| tts.tts_to_file(text, speaker_wav=speaker_wav, language="en", file_path=file_path) | |
| else: | |
| tts.tts_to_file(text, speaker=tts.speakers[0], language="en", file_path=file_path) | |
| return file_path | |
| def show_error(text): | |
| if text == "": | |
| return gr.update(visible=True, elem_id="warning", elem_classes="feedback"), gr.update(visible=False) | |
| else: | |
| return gr.update(visible=False), gr.update(visible=True) | |
| # def download_file(): | |
| # return file_path | |
| title = "Voice-Cloning-Demo" | |
| def toggle(choice): | |
| if choice == "mic": | |
| return gr.update(visible=True, value=None), gr.update(visible=False, value=None) | |
| else: | |
| return gr.update(visible=False, value=None), gr.update(visible=True, value=None) | |
| def change_color(text_input): | |
| if len(text_input) == 0: | |
| return gr.update(elem_id="warning", autofocus=True) | |
| else: | |
| return gr.update(elem_id="alert", autofocus=False) | |
| def clear_color(text_input, radio,error_box): | |
| return gr.update(elem_id="alert"), gr.update(value="mic"), gr.update(visible=False) | |
| with gr.Blocks(css="footer {visibility: hidden}") as demo: | |
| with gr.Row(): | |
| with gr.Column(): | |
| text_input = gr.Textbox(label="Input the text", value="", max_lines=4, lines=4) | |
| radio = gr.Radio(["mic", "file"], value="mic", | |
| label="How would you like to upload your audio?") | |
| audio_input_mic = gr.Audio(label="Voice to clone", sources="microphone", type="filepath", visible=True) | |
| audio_input_file = gr.Audio(label="Voice to clone", type="filepath", visible=False) | |
| with gr.Row(): | |
| with gr.Column(): | |
| btn_clear = gr.ClearButton([text_input, radio, audio_input_file]) | |
| with gr.Column(): | |
| btn = gr.Button("Generate", variant="primary") | |
| with gr.Column(): | |
| audio_output = gr.Audio(label="Output", visible=True, autoplay=True, show_share_button=False) | |
| # download_button = gr.DownloadButton(label="Download Audio", value=None, visible=True) | |
| error_box = gr.Textbox(label="WARNING", value="Input box cannot be blank!!", visible=False, container=True) | |
| # download_button.click(download_file, outputs=download_button) | |
| btn_clear.add(audio_output) | |
| btn.click(text_to_speech, inputs=[text_input, audio_input_mic, audio_input_file], outputs=audio_output) | |
| btn.click(show_error, text_input, [error_box, audio_output]) | |
| radio.change(toggle, radio, [audio_input_mic, audio_input_file]) | |
| btn_clear.click(clear_color, [text_input, radio, error_box], [text_input, radio, error_box]) | |
| btn.click(change_color, text_input, text_input) | |
| demo.launch() |