Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,40 +2,29 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from kittentts import KittenTTS
|
4 |
|
5 |
-
# 1. Initialize the KittenTTS model.
|
6 |
-
# This will download the model from Hugging Face on the first run.
|
7 |
print("Loading KittenTTS model...")
|
8 |
try:
|
9 |
tts_model = KittenTTS("KittenML/kitten-tts-nano-0.1")
|
10 |
print("Model loaded successfully.")
|
11 |
except Exception as e:
|
12 |
print(f"Error loading model: {e}")
|
13 |
-
# You might want to handle this more gracefully
|
14 |
exit()
|
15 |
|
16 |
|
17 |
-
# 2. Get the list of available voices directly from the model instance.
|
18 |
AVAILABLE_VOICES = tts_model.available_voices
|
19 |
DEFAULT_VOICE = "expr-voice-5-m" if "expr-voice-5-m" in AVAILABLE_VOICES else AVAILABLE_VOICES[0]
|
20 |
|
21 |
-
# 3. Define the core function that Gradio will call.
|
22 |
-
# This function now accepts 'voice' and 'speed' as arguments.
|
23 |
def synthesize_speech(text, voice, speed):
|
24 |
"""
|
25 |
Generates audio using the selected text, voice, and speed.
|
26 |
"""
|
27 |
-
# Handle empty input gracefully
|
28 |
if not text.strip():
|
29 |
-
# Return a silent, empty audio clip
|
30 |
return (24000, np.zeros(0, dtype=np.int16))
|
31 |
|
32 |
-
# Call the model's generate method with all the parameters
|
33 |
audio_data = tts_model.generate(text, voice=voice, speed=speed)
|
34 |
|
35 |
-
# Return the audio in the format Gradio expects: (sample_rate, numpy_array)
|
36 |
return (24000, audio_data)
|
37 |
|
38 |
-
# 4. Create the Gradio UI with the new controls.
|
39 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
40 |
gr.Markdown(
|
41 |
"""
|
@@ -79,12 +68,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
79 |
inputs=[text_input, voice_dropdown, speed_slider]
|
80 |
)
|
81 |
|
82 |
-
# Connect the UI components to the function
|
83 |
generate_button.click(
|
84 |
fn=synthesize_speech,
|
85 |
inputs=[text_input, voice_dropdown, speed_slider],
|
86 |
outputs=audio_output
|
87 |
)
|
88 |
|
89 |
-
# Launch the Gradio app
|
90 |
demo.launch()
|
|
|
2 |
import numpy as np
|
3 |
from kittentts import KittenTTS
|
4 |
|
|
|
|
|
5 |
print("Loading KittenTTS model...")
|
6 |
try:
|
7 |
tts_model = KittenTTS("KittenML/kitten-tts-nano-0.1")
|
8 |
print("Model loaded successfully.")
|
9 |
except Exception as e:
|
10 |
print(f"Error loading model: {e}")
|
|
|
11 |
exit()
|
12 |
|
13 |
|
|
|
14 |
AVAILABLE_VOICES = tts_model.available_voices
|
15 |
DEFAULT_VOICE = "expr-voice-5-m" if "expr-voice-5-m" in AVAILABLE_VOICES else AVAILABLE_VOICES[0]
|
16 |
|
|
|
|
|
17 |
def synthesize_speech(text, voice, speed):
|
18 |
"""
|
19 |
Generates audio using the selected text, voice, and speed.
|
20 |
"""
|
|
|
21 |
if not text.strip():
|
|
|
22 |
return (24000, np.zeros(0, dtype=np.int16))
|
23 |
|
|
|
24 |
audio_data = tts_model.generate(text, voice=voice, speed=speed)
|
25 |
|
|
|
26 |
return (24000, audio_data)
|
27 |
|
|
|
28 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
29 |
gr.Markdown(
|
30 |
"""
|
|
|
68 |
inputs=[text_input, voice_dropdown, speed_slider]
|
69 |
)
|
70 |
|
|
|
71 |
generate_button.click(
|
72 |
fn=synthesize_speech,
|
73 |
inputs=[text_input, voice_dropdown, speed_slider],
|
74 |
outputs=audio_output
|
75 |
)
|
76 |
|
|
|
77 |
demo.launch()
|