File size: 2,359 Bytes
88a0625
78e02b1
c22a65c
88a0625
78e02b1
 
 
 
 
 
 
c22a65c
88a0625
78e02b1
 
88a0625
78e02b1
 
 
88a0625
78e02b1
 
 
 
 
 
 
 
 
 
becd07c
78e02b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
becd07c
78e02b1
 
 
 
 
 
 
 
 
becd07c
03aa2b1
becd07c
78e02b1
 
 
 
 
 
 
 
 
c22a65c
ab35b57
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
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()