Spaces:
Sleeping
Sleeping
First upload
Browse files- app.py +47 -4
- requirements.txt +2 -0
app.py
CHANGED
@@ -1,7 +1,50 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from kokoro import KPipeline
|
3 |
+
import soundfile as sf
|
4 |
+
import tempfile
|
5 |
|
6 |
+
# Set up the Kokoro pipeline (choose your language code)
|
7 |
+
pipeline = KPipeline(lang_code='a') # 'a' = American English, see docs for more
|
8 |
|
9 |
+
def kokoro_tts(text: str, voice: str = "af_heart", speed: float = 1.0) -> str:
|
10 |
+
"""
|
11 |
+
Generate speech audio from text using Kokoro TTS.
|
12 |
+
Args:
|
13 |
+
text: The input text to synthesize.
|
14 |
+
voice: The Kokoro voice name (e.g., 'af_heart').
|
15 |
+
speed: Speech speed (default 1.0).
|
16 |
+
Returns:
|
17 |
+
Path to the generated WAV audio file.
|
18 |
+
"""
|
19 |
+
# Generate audio using Kokoro
|
20 |
+
generator = pipeline(text, voice=voice, speed=speed)
|
21 |
+
for i, (_, _, audio) in enumerate(generator):
|
22 |
+
# Save the first audio chunk as a WAV file
|
23 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
24 |
+
sf.write(f.name, audio, 24000)
|
25 |
+
return f.name # Return file path for Gradio to serve
|
26 |
+
|
27 |
+
# List some example voices
|
28 |
+
voice_options = ["af_heart", "af_bella", "af_aria", "af_riley", "af_ryan"]
|
29 |
+
|
30 |
+
description = """
|
31 |
+
Enter text to synthesize speech using Kokoro TTS.<br>
|
32 |
+
**Voices:** af_heart, af_bella, af_aria, af_riley, af_ryan<br>
|
33 |
+
**Languages:** American English (default), see Kokoro docs for more.<br>
|
34 |
+
**Speed:** 1.0 = normal, lower = slower, higher = faster.<br>
|
35 |
+
"""
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn=kokoro_tts,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(label="Text"),
|
41 |
+
gr.Dropdown(voice_options, label="Voice", value="af_heart"),
|
42 |
+
gr.Slider(0.5, 2.0, value=1.0, label="Speed")
|
43 |
+
],
|
44 |
+
outputs=gr.Audio(type="filepath", label="Generated Speech"),
|
45 |
+
title="Kokoro TTS MCP Server",
|
46 |
+
description=description
|
47 |
+
)
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
kokoro
|
2 |
+
soundfile
|