Voice-Clone / app.py
Vihang28's picture
Update app.py
f30fca6
raw
history blame
3.71 kB
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 len(text) == 0:
return gr.update(visible=True), gr.update(visible=False)
else:
return gr.update(visible=False), gr.update(visible=True)
def clear_color(text_input, radio):
return gr.update(elem_id="alert"), gr.update(value="mic")
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)
with gr.Blocks(css=css) as demo:
with gr.Row():
with gr.Column():
text_input = gr.Textbox(label="Input the text", value="", max_lines=3)
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("Submit", variant="primary")
with gr.Column():
audio_output = gr.Audio(label="Output", visible=True)
error_box = gr.Textbox(label="WARNING", value="Input box cannot be blank!!", visible=False, elem_id="warning", elem_classes="feedback")
# gr.Examples(examples, fn=inference, inputs=[audio_file, text_input],
# outputs=audio_output, cache_examples=True)
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], [text_input, radio])
btn_clear.add(audio_output)
btn.click(change_color, text_input, text_input)
demo.launch()