Spaces:
Running
Running
Michael Hu
fix: keep API parameter names, reject unknown voices, and stop cloned Chatterbox voices reaching other users
4d9637e Download src/ui.py from DroolingPanda/tts_gallery: direct link, hf CLI and curl.
- Browser
- Download file 8.34 kB
-
https://huggingface.co/spaces/DroolingPanda/tts_gallery/resolve/main/src/ui.py
- Command line
-
hf download hf://spaces/DroolingPanda/tts_gallery/src/ui.py
-
curl -L -o ui.py https://huggingface.co/spaces/DroolingPanda/tts_gallery/resolve/main/src/ui.py
8.34 kB
| 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 | |