File size: 3,937 Bytes
4bcd77c
 
 
d31f3e8
 
 
c696d38
 
 
 
 
d31f3e8
 
045d935
4bcd77c
 
 
 
 
 
8d85881
 
d31f3e8
 
 
 
 
 
 
 
 
 
8d85881
d31f3e8
 
4bcd77c
d31f3e8
633f6b9
 
d31f3e8
 
4bcd77c
e2220ee
 
db99199
4bcd77c
 
 
 
 
 
 
d31f3e8
 
c696d38
d31f3e8
c696d38
4bcd77c
633f6b9
 
 
e239f70
633f6b9
38bfce8
4bcd77c
 
f14a85d
4bcd77c
 
045d935
4bcd77c
 
 
 
6ddfcd2
4bcd77c
aae4b89
4bcd77c
26ccac7
fc73a18
633f6b9
a858aca
6457b6b
633f6b9
d31f3e8
 
4bcd77c
633f6b9
d31f3e8
4bcd77c
fdff59e
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
88
89
90
91
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(aud_file):
    return aud_file

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, audio_output, 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()