File size: 8,335 Bytes
4d9637e
 
9a0aa93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9637e
 
 
9a0aa93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9637e
 
 
 
 
 
 
 
 
 
 
9a0aa93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9637e
 
 
9a0aa93
4d9637e
9a0aa93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import inspect

import gradio as gr


def generate(model, text, **kwargs):
    """Run a TTS model and show any failure in the UI"""
    if not text or not text.strip():
        raise gr.Error("Enter some text to speak")
    try:
        return model.generate_speech(text, **kwargs)
    except Exception as e:
        raise gr.Error(f"Error: {e}")


def transcribe(model, audio_path, language):
    """Run an STT model and return its text, or the error"""
    try:
        return model.transcribe(
            audio_path, language=None if language == "auto" else language
        )
    except Exception as e:
        return f"Error: {e}"


def voice_dropdown(voices, value=None, **kwargs):
    return gr.Dropdown(
        choices=voices,
        value=value or (voices[0] if voices else None),
        label="Voice",
        **kwargs,
    )


def language_and_voice(model):
    """A language dropdown that refills the voice dropdown when it changes"""
    languages = model.get_supported_languages()
    language = gr.Dropdown(
        choices=languages, value=languages[0] if languages else None, label="Language"
    )
    # API calls are checked against the initial choices, so allow voices of the other languages too
    voice = voice_dropdown(
        model.get_available_voices(languages[0]) if languages else [],
        allow_custom_value=True,
    )

    def update_voices(name):
        voices = model.get_available_voices(name)
        return gr.update(choices=voices, value=voices[0] if voices else None)

    language.change(
        update_voices, inputs=[language], outputs=[voice], api_visibility="private"
    )
    return {"language": language, "voice": voice}


def tts_tab(
    title, api_name, model, controls, placeholder="Enter text here...", prepare=None
):
    """One TTS tab: a text box, the model's own controls, a button and an audio player.

    controls() builds the extra inputs inside the tab and returns {generate_speech keyword: component}.
    prepare(text, kwargs), when given, may change those keywords before synthesis.
    """
    with gr.Tab(title), gr.Row():
        with gr.Column():
            text_box = gr.Textbox(
                label="Text to speak", placeholder=placeholder, lines=5
            )
            inputs = controls()
            submit = gr.Button("Generate Speech")
        with gr.Column():
            output = gr.Audio(label="Generated Speech")

    def synthesize(text, *values):
        kwargs = dict(zip(inputs, values))
        return generate(model, text, **(prepare(text, kwargs) if prepare else kwargs))

    # Gradio names API parameters from the signature, so give *values the control names and optional defaults
    synthesize.__signature__ = inspect.Signature(
        [inspect.Parameter("text", inspect.Parameter.POSITIONAL_OR_KEYWORD)]
        + [
            inspect.Parameter(
                name, inspect.Parameter.POSITIONAL_OR_KEYWORD, default=None
            )
            for name in inputs
        ]
    )

    submit.click(
        synthesize,
        inputs=[text_box, *inputs.values()],
        outputs=[output],
        api_name=api_name,
    )


def stt_tab(title, api_name, model):
    """One STT tab: an audio input, a language picker from model.languages, and the transcription"""
    with gr.Tab(title), gr.Row():
        with gr.Column():
            audio = gr.Audio(label="Audio to transcribe", type="filepath")
            language = gr.Dropdown(
                choices=[("Auto-detect", "auto"), *model.languages.items()],
                value="auto",
                label="Language (optional)",
            )
            submit = gr.Button("Transcribe")
        with gr.Column():
            output = gr.Textbox(label="Transcription", lines=5)

    def transcribe_audio(audio, lang):
        return transcribe(model, audio, lang)

    submit.click(
        transcribe_audio,
        inputs=[audio, language],
        outputs=[output],
        api_name=api_name,
    )


def create_interface(tts_models, stt_models):
    """Build the gallery from the model instances the app loaded"""
    chatterbox = tts_models["ResembleAI/chatterbox"]
    kitten = tts_models["KittenML/KittenTTS"]
    kitten2 = tts_models["KittenML/kitten-tts-2"]
    whisper = stt_models["SYSTRAN/faster-whisper"]

    def add_reference_transcript(text, kwargs):
        # Reuse the loaded Faster Whisper; KittenTTS 2 would otherwise download its own Whisper
        if (
            text
            and text.strip()
            and kwargs["audio_prompt"]
            and not (kwargs["reference_text"] or "").strip()
        ):
            try:
                kwargs["reference_text"] = whisper.transcribe(kwargs["audio_prompt"])
            except Exception as e:
                print(
                    f"Faster Whisper could not transcribe the reference, KittenTTS 2 will: {e}"
                )
        return kwargs

    with gr.Blocks(title="TTS & STT Gallery") as demo:
        gr.Markdown("# TTS & STT Model Gallery")
        gr.Markdown("Explore different Text-to-Speech and Speech-to-Text models")
        with gr.Tabs():
            with gr.Tab("Text-to-Speech"):
                gr.Markdown("## Text-to-Speech Models")
                with gr.Tabs():
                    tts_tab(
                        "Chatterbox",
                        "tts_chatterbox",
                        chatterbox,
                        lambda: {
                            "language": gr.Dropdown(
                                choices=chatterbox.get_supported_languages(),
                                value="English",
                                label="Language",
                            ),
                            "audio_prompt": gr.Audio(
                                label="Voice reference (optional)", type="filepath"
                            ),
                        },
                    )
                    tts_tab(
                        "KittenTTS",
                        "tts_kittentts",
                        kitten,
                        lambda: {
                            "voice": voice_dropdown(kitten.get_available_voices()),
                        },
                    )
                    tts_tab(
                        "KittenTTS 2",
                        "tts_kittentts2",
                        kitten2,
                        lambda: {
                            "voice": voice_dropdown(
                                kitten2.get_available_voices(),
                                kitten2.get_default_voice(),
                            ),
                            "preset": gr.Radio(
                                choices=["stable", "expressive"],
                                value="stable",
                                label="Preset",
                            ),
                            "audio_prompt": gr.Audio(
                                label="Voice reference to clone, 5-30 s (optional, replaces the voice above)",
                                type="filepath",
                            ),
                            "reference_text": gr.Textbox(
                                label="Reference transcript (optional, transcribed automatically when empty)"
                            ),
                        },
                        placeholder="Enter text here... Supports [joyful] emotion tags, <laugh> events and (((emphasis)))",
                        prepare=add_reference_transcript,
                    )
                    for title, name in [
                        ("Piper", "piper-tts"),
                        ("Kokoro", "hexgrad/kokoro"),
                    ]:
                        model = tts_models[name]
                        tts_tab(
                            title,
                            f"tts_{title.lower()}",
                            model,
                            lambda model=model: language_and_voice(model),
                        )
            with gr.Tab("Speech-to-Text"):
                gr.Markdown("## Speech-to-Text Models")
                with gr.Tabs():
                    stt_tab("Faster Whisper", "stt_whisper", whisper)
                    stt_tab(
                        "Whistle", "stt_whistle", stt_models["Cactus-Compute/whistle"]
                    )
    return demo