import gradio as gr import numpy as np from kittentts import KittenTTS print("Loading KittenTTS model...") try: tts_model = KittenTTS("KittenML/kitten-tts-nano-0.1") print("Model loaded successfully.") except Exception as e: print(f"Error loading model: {e}") exit() AVAILABLE_VOICES = tts_model.available_voices DEFAULT_VOICE = "expr-voice-5-m" if "expr-voice-5-m" in AVAILABLE_VOICES else AVAILABLE_VOICES[0] def synthesize_speech(text, voice, speed): """ Generates audio using the selected text, voice, and speed. """ if not text.strip(): return (24000, np.zeros(0, dtype=np.int16)) audio_data = tts_model.generate(text, voice=voice, speed=speed) return (24000, audio_data) with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown( """ # 🐱 KittenTTS UI A user-friendly interface for the KittenTTS text-to-speech model. Select a voice, adjust the speed, and type your text to generate audio. """ ) with gr.Row(): with gr.Column(scale=3): text_input = gr.Textbox( lines=5, label="Input Text", placeholder="Type something here..." ) with gr.Column(scale=1): voice_dropdown = gr.Dropdown( choices=AVAILABLE_VOICES, value=DEFAULT_VOICE, label="Voice Selection" ) speed_slider = gr.Slider( minimum=0.5, maximum=2.0, step=0.05, value=1.0, label="Speech Speed" ) generate_button = gr.Button("Generate Audio", variant="primary") audio_output = gr.Audio(label="Generated Speech", autoplay=True) gr.Examples( examples=[ ["This is an example of a female voice.", "expr-voice-4-f", 1.0], ["This is an example of a male voice, speaking a bit faster.", "expr-voice-3-m", 1.25], ["The speed can also be slowed down for clarity.", "expr-voice-5-f", 0.8], ], inputs=[text_input, voice_dropdown, speed_slider] ) generate_button.click( fn=synthesize_speech, inputs=[text_input, voice_dropdown, speed_slider], outputs=audio_output ) demo.queue().launch()