File size: 3,717 Bytes
4bcd77c
 
 
d31f3e8
 
 
c696d38
 
 
 
 
d31f3e8
 
045d935
4bcd77c
 
 
 
 
 
8d85881
 
d31f3e8
 
 
 
 
 
 
 
 
 
8d85881
d31f3e8
 
4bcd77c
d31f3e8
 
 
 
 
4bcd77c
4c79d49
 
 
4bcd77c
 
 
 
 
 
 
c696d38
d31f3e8
 
c696d38
d31f3e8
c696d38
4bcd77c
d31f3e8
4bcd77c
 
 
 
 
045d935
4bcd77c
 
 
 
6ddfcd2
4bcd77c
8d85881
4bcd77c
d31f3e8
4c79d49
4bcd77c
 
 
d31f3e8
 
4bcd77c
4c79d49
6ddfcd2
d31f3e8
4bcd77c
045d935
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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, elem_id="warning", elem_classes="feedback"), 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)

    # 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(share=True)