Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import numpy as np
|
|
|
|
|
|
|
4 |
from kokoro import KModel, KPipeline
|
5 |
|
6 |
# Check if CUDA is available
|
@@ -16,7 +19,15 @@ pipelines = {'a': KPipeline(lang_code='a', model=False)}
|
|
16 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
17 |
|
18 |
def text_to_audio(text, speed=1.0):
|
19 |
-
"""Convert text to audio using Kokoro model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
if not text:
|
21 |
return None
|
22 |
|
@@ -40,6 +51,33 @@ def text_to_audio(text, speed=1.0):
|
|
40 |
|
41 |
return None
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
# Create Gradio interface
|
44 |
with gr.Blocks(title="Kokoro Text-to-Audio") as app:
|
45 |
gr.Markdown("# 🎵 Kokoro Text-to-Audio Converter")
|
@@ -74,7 +112,32 @@ with gr.Blocks(title="Kokoro Text-to-Audio") as app:
|
|
74 |
gr.Markdown("- For best results, keep your text reasonably short (up to ~500 characters)")
|
75 |
gr.Markdown("- Adjust the speed slider to modify the pace of speech")
|
76 |
gr.Markdown("- The model may take a moment to load on first use")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
# Launch the app
|
79 |
if __name__ == "__main__":
|
80 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
+
import os
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
from kokoro import KModel, KPipeline
|
8 |
|
9 |
# Check if CUDA is available
|
|
|
19 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
20 |
|
21 |
def text_to_audio(text, speed=1.0):
|
22 |
+
"""Convert text to audio using Kokoro model.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
text: The text to convert to speech
|
26 |
+
speed: Speech speed multiplier (0.5-2.0, where 1.0 is normal speed)
|
27 |
+
|
28 |
+
Returns:
|
29 |
+
Audio data as a tuple of (sample_rate, audio_array)
|
30 |
+
"""
|
31 |
if not text:
|
32 |
return None
|
33 |
|
|
|
51 |
|
52 |
return None
|
53 |
|
54 |
+
def text_to_audio_b64(text, speed=1.0):
|
55 |
+
"""Convert text to audio and return as base64 encoded WAV file.
|
56 |
+
|
57 |
+
Args:
|
58 |
+
text: The text to convert to speech
|
59 |
+
speed: Speech speed multiplier (0.5-2.0, where 1.0 is normal speed)
|
60 |
+
|
61 |
+
Returns:
|
62 |
+
Base64 encoded WAV file as a string
|
63 |
+
"""
|
64 |
+
import soundfile as sf
|
65 |
+
|
66 |
+
result = text_to_audio(text, speed)
|
67 |
+
if result is None:
|
68 |
+
return None
|
69 |
+
|
70 |
+
sample_rate, audio_data = result
|
71 |
+
|
72 |
+
# Save to BytesIO object
|
73 |
+
wav_io = io.BytesIO()
|
74 |
+
sf.write(wav_io, audio_data, sample_rate, format='WAV')
|
75 |
+
wav_io.seek(0)
|
76 |
+
|
77 |
+
# Convert to base64
|
78 |
+
wav_b64 = base64.b64encode(wav_io.read()).decode('utf-8')
|
79 |
+
return wav_b64
|
80 |
+
|
81 |
# Create Gradio interface
|
82 |
with gr.Blocks(title="Kokoro Text-to-Audio") as app:
|
83 |
gr.Markdown("# 🎵 Kokoro Text-to-Audio Converter")
|
|
|
112 |
gr.Markdown("- For best results, keep your text reasonably short (up to ~500 characters)")
|
113 |
gr.Markdown("- Adjust the speed slider to modify the pace of speech")
|
114 |
gr.Markdown("- The model may take a moment to load on first use")
|
115 |
+
|
116 |
+
# Add section about MCP support
|
117 |
+
with gr.Accordion("MCP Support (for LLMs)", open=False):
|
118 |
+
gr.Markdown("""
|
119 |
+
### MCP Support
|
120 |
+
|
121 |
+
This app supports the Model Context Protocol (MCP), allowing Large Language Models like Claude Desktop to use it as a tool.
|
122 |
+
|
123 |
+
To use this app with an MCP client, add the following configuration:
|
124 |
+
|
125 |
+
```json
|
126 |
+
{
|
127 |
+
"mcpServers": {
|
128 |
+
"kokoroTTS": {
|
129 |
+
"url": "https://your-app-url.hf.space/gradio_api/mcp/sse"
|
130 |
+
}
|
131 |
+
}
|
132 |
+
}
|
133 |
+
```
|
134 |
+
|
135 |
+
Replace `your-app-url.hf.space` with your actual Hugging Face Space URL.
|
136 |
+
""")
|
137 |
|
138 |
+
# Launch the app with MCP support
|
139 |
if __name__ == "__main__":
|
140 |
+
# Check for environment variable to enable MCP
|
141 |
+
enable_mcp = os.environ.get('GRADIO_MCP_SERVER', 'False').lower() in ('true', '1', 't')
|
142 |
+
|
143 |
+
app.launch(mcp_server=True)
|