Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import spaces
|
|
2 |
from kokoro import KModel, KPipeline
|
3 |
import gradio as gr
|
4 |
import os
|
|
|
5 |
import torch
|
6 |
|
7 |
IS_DUPLICATE = not os.getenv('SPACE_ID', '').startswith('hexgrad/')
|
@@ -43,6 +44,33 @@ def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
|
|
43 |
def predict(text, voice='af_heart', speed=1):
|
44 |
return generate_first(text, voice, speed, use_gpu=False)[0]
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
random_texts = {}
|
47 |
for lang in ['en']:
|
48 |
with open(f'{lang}.txt', 'r') as r:
|
@@ -64,7 +92,7 @@ CHOICES = {
|
|
64 |
'🇺🇸 🚺 Alloy': 'af_alloy',
|
65 |
'🇺🇸 🚺 Jessica': 'af_jessica',
|
66 |
'🇺🇸 🚺 River': 'af_river',
|
67 |
-
|
68 |
'🇺🇸 🚹 Michael': 'am_michael',
|
69 |
'🇺🇸 🚹 Fenrir': 'am_fenrir',
|
70 |
'🇺🇸 🚹 Puck': 'am_puck',
|
@@ -74,46 +102,46 @@ CHOICES = {
|
|
74 |
'🇺🇸 🚹 Onyx': 'am_onyx',
|
75 |
'🇺🇸 🚹 Santa': 'am_santa',
|
76 |
'🇺🇸 🚹 Adam': 'am_adam',
|
77 |
-
|
78 |
'🇬🇧 🚺 Emma': 'bf_emma',
|
79 |
'🇬🇧 🚺 Isabella': 'bf_isabella',
|
80 |
'🇬🇧 🚺 Alice': 'bf_alice',
|
81 |
'🇬🇧 🚺 Lily': 'bf_lily',
|
82 |
-
|
83 |
'🇬🇧 🚹 George': 'bm_george',
|
84 |
'🇬🇧 🚹 Fable': 'bm_fable',
|
85 |
'🇬🇧 🚹 Lewis': 'bm_lewis',
|
86 |
'🇬🇧 🚹 Daniel': 'bm_daniel',
|
87 |
-
|
88 |
'🇪🇸 🚺 Dora': 'ef_dora',
|
89 |
-
|
90 |
'🇪🇸 🚹 Alex': 'em_alex',
|
91 |
'🇪🇸 🚹 Santa': 'em_santa',
|
92 |
-
|
93 |
'🇫🇷 🚺 Siwis': 'ff_siwis',
|
94 |
-
|
95 |
'🇮🇳 🚹 Alpha': 'hf_alpha',
|
96 |
'🇮🇳 🚹 Beta': 'hf_beta',
|
97 |
-
|
98 |
'🇮🇳 🚹 Omega': 'hm_omega',
|
99 |
'🇮🇳 🚹 Psi': 'hm_psi',
|
100 |
-
|
101 |
'🇮🇹 🚺 Sara': 'if_sara',
|
102 |
-
|
103 |
'🇮🇹 🚺 Nicola': 'im_nicola',
|
104 |
-
|
105 |
'🇯🇵 🚹 Alpha': 'jf_alpha',
|
106 |
'🇯🇵 🚹 Gongitsune': 'jf_gongitsune',
|
107 |
'🇯🇵 🚹 Nezumi': 'jf_nezumi',
|
108 |
'🇯🇵 🚹 Tebukuro': 'jf_tebukuro',
|
109 |
-
|
110 |
'🇯🇵 🚹 Kumo': 'jm_kumo',
|
111 |
-
|
112 |
'🇧🇷 🚺 Dora': 'pf_dora',
|
113 |
-
|
114 |
'🇧🇷 🚹 Alex': 'pm_alex',
|
115 |
'🇧🇷 🚹 Santa': 'pm_santa',
|
116 |
-
|
117 |
'🇨🇳 🚺 Xiaobei': 'zf_xiaobei',
|
118 |
'🇨🇳 🚺 Xiaoni': 'zf_xiaoni',
|
119 |
'🇨🇳 🚺 Xiaoxiao': 'zf_xiaoxiao',
|
@@ -127,28 +155,69 @@ CHOICES = {
|
|
127 |
for v in CHOICES.values():
|
128 |
pipelines[v[0]].load_voice(v)
|
129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
BANNER_TEXT = '''
|
131 |
[***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://huggingface.co/hexgrad/Kokoro-82M)
|
132 |
As of January 31st, 2025, Kokoro was the most-liked [**TTS model**](https://huggingface.co/models?pipeline_tag=text-to-speech&sort=likes) and the most-liked [**TTS space**](https://huggingface.co/spaces?sort=likes&search=tts) on Hugging Face.
|
133 |
This demo only showcases English, but you can directly use the model to access other languages.
|
134 |
'''
|
135 |
-
|
|
|
136 |
with gr.Blocks() as app:
|
137 |
with gr.Row():
|
138 |
gr.Markdown(BANNER_TEXT, container=True)
|
139 |
with gr.Row():
|
140 |
with gr.Column():
|
141 |
-
text = gr.Textbox(label='Input Text', info=f"Up to ~500 characters per Generate")
|
142 |
with gr.Row():
|
143 |
voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice', info='Quality and availability vary by language')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
|
145 |
random_btn = gr.Button('Random Text', variant='secondary')
|
146 |
with gr.Column():
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
152 |
|
153 |
if __name__ == '__main__':
|
154 |
-
app.queue().launch()
|
|
|
2 |
from kokoro import KModel, KPipeline
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
+
import random
|
6 |
import torch
|
7 |
|
8 |
IS_DUPLICATE = not os.getenv('SPACE_ID', '').startswith('hexgrad/')
|
|
|
44 |
def predict(text, voice='af_heart', speed=1):
|
45 |
return generate_first(text, voice, speed, use_gpu=False)[0]
|
46 |
|
47 |
+
def tokenize_first(text, voice='af_heart'):
|
48 |
+
pipeline = pipelines[voice[0]]
|
49 |
+
for _, ps, _ in pipeline(text, voice):
|
50 |
+
return ps
|
51 |
+
return ''
|
52 |
+
|
53 |
+
def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
|
54 |
+
text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
|
55 |
+
pipeline = pipelines[voice[0]]
|
56 |
+
pack = pipeline.load_voice(voice)
|
57 |
+
use_gpu = use_gpu and CUDA_AVAILABLE
|
58 |
+
for _, ps, _ in pipeline(text, voice, speed):
|
59 |
+
ref_s = pack[len(ps)-1]
|
60 |
+
try:
|
61 |
+
if use_gpu:
|
62 |
+
audio = forward_gpu(ps, ref_s, speed)
|
63 |
+
else:
|
64 |
+
audio = models[False](ps, ref_s, speed)
|
65 |
+
except gr.exceptions.Error as e:
|
66 |
+
if use_gpu:
|
67 |
+
gr.Warning(str(e))
|
68 |
+
gr.Info('Switching to CPU')
|
69 |
+
audio = models[False](ps, ref_s, speed)
|
70 |
+
else:
|
71 |
+
raise gr.Error(e)
|
72 |
+
yield 24000, audio.numpy()
|
73 |
+
|
74 |
random_texts = {}
|
75 |
for lang in ['en']:
|
76 |
with open(f'{lang}.txt', 'r') as r:
|
|
|
92 |
'🇺🇸 🚺 Alloy': 'af_alloy',
|
93 |
'🇺🇸 🚺 Jessica': 'af_jessica',
|
94 |
'🇺🇸 🚺 River': 'af_river',
|
95 |
+
|
96 |
'🇺🇸 🚹 Michael': 'am_michael',
|
97 |
'🇺🇸 🚹 Fenrir': 'am_fenrir',
|
98 |
'🇺🇸 🚹 Puck': 'am_puck',
|
|
|
102 |
'🇺🇸 🚹 Onyx': 'am_onyx',
|
103 |
'🇺🇸 🚹 Santa': 'am_santa',
|
104 |
'🇺🇸 🚹 Adam': 'am_adam',
|
105 |
+
|
106 |
'🇬🇧 🚺 Emma': 'bf_emma',
|
107 |
'🇬🇧 🚺 Isabella': 'bf_isabella',
|
108 |
'🇬🇧 🚺 Alice': 'bf_alice',
|
109 |
'🇬🇧 🚺 Lily': 'bf_lily',
|
110 |
+
|
111 |
'🇬🇧 🚹 George': 'bm_george',
|
112 |
'🇬🇧 🚹 Fable': 'bm_fable',
|
113 |
'🇬🇧 🚹 Lewis': 'bm_lewis',
|
114 |
'🇬🇧 🚹 Daniel': 'bm_daniel',
|
115 |
+
|
116 |
'🇪🇸 🚺 Dora': 'ef_dora',
|
117 |
+
|
118 |
'🇪🇸 🚹 Alex': 'em_alex',
|
119 |
'🇪🇸 🚹 Santa': 'em_santa',
|
120 |
+
|
121 |
'🇫🇷 🚺 Siwis': 'ff_siwis',
|
122 |
+
|
123 |
'🇮🇳 🚹 Alpha': 'hf_alpha',
|
124 |
'🇮🇳 🚹 Beta': 'hf_beta',
|
125 |
+
|
126 |
'🇮🇳 🚹 Omega': 'hm_omega',
|
127 |
'🇮🇳 🚹 Psi': 'hm_psi',
|
128 |
+
|
129 |
'🇮🇹 🚺 Sara': 'if_sara',
|
130 |
+
|
131 |
'🇮🇹 🚺 Nicola': 'im_nicola',
|
132 |
+
|
133 |
'🇯🇵 🚹 Alpha': 'jf_alpha',
|
134 |
'🇯🇵 🚹 Gongitsune': 'jf_gongitsune',
|
135 |
'🇯🇵 🚹 Nezumi': 'jf_nezumi',
|
136 |
'🇯🇵 🚹 Tebukuro': 'jf_tebukuro',
|
137 |
+
|
138 |
'🇯🇵 🚹 Kumo': 'jm_kumo',
|
139 |
+
|
140 |
'🇧🇷 🚺 Dora': 'pf_dora',
|
141 |
+
|
142 |
'🇧🇷 🚹 Alex': 'pm_alex',
|
143 |
'🇧🇷 🚹 Santa': 'pm_santa',
|
144 |
+
|
145 |
'🇨🇳 🚺 Xiaobei': 'zf_xiaobei',
|
146 |
'🇨🇳 🚺 Xiaoni': 'zf_xiaoni',
|
147 |
'🇨🇳 🚺 Xiaoxiao': 'zf_xiaoxiao',
|
|
|
155 |
for v in CHOICES.values():
|
156 |
pipelines[v[0]].load_voice(v)
|
157 |
|
158 |
+
TOKEN_NOTE = '''
|
159 |
+
💡 Customize pronunciation with Markdown link syntax and /slashes/ like `[Kokoro](/kˈOkəɹO/)`
|
160 |
+
💬 To adjust intonation, try punctuation `;:,.!?—…"()“”` or stress `ˈ` and `ˌ`
|
161 |
+
⬇️ Lower stress `[1 level](-1)` or `[2 levels](-2)`
|
162 |
+
⬆️ Raise stress 1 level `[or](+2)` 2 levels (only works on less stressed, usually short words)
|
163 |
+
'''
|
164 |
+
|
165 |
+
with gr.Blocks() as generate_tab:
|
166 |
+
out_audio = gr.Audio(label='Output Audio', interactive=False, streaming=False, autoplay=True)
|
167 |
+
generate_btn = gr.Button('Generate', variant='primary')
|
168 |
+
with gr.Accordion('Output Tokens', open=True):
|
169 |
+
out_ps = gr.Textbox(interactive=False, show_label=False, info='Tokens used to generate the audio, up to 510 context length.')
|
170 |
+
tokenize_btn = gr.Button('Tokenize', variant='secondary')
|
171 |
+
gr.Markdown(TOKEN_NOTE)
|
172 |
+
predict_btn = gr.Button('Predict', variant='secondary', visible=False)
|
173 |
+
|
174 |
+
STREAM_NOTE = ['⚠️ There is an unknown Gradio bug that might yield no audio the first time you click `Stream`.']
|
175 |
+
if CHAR_LIMIT is not None:
|
176 |
+
STREAM_NOTE.append(f'✂️ Each stream is capped at {CHAR_LIMIT} characters.')
|
177 |
+
STREAM_NOTE.append('🚀 Want more characters? You can [use Kokoro directly](https://huggingface.co/hexgrad/Kokoro-82M#usage) or duplicate this space:')
|
178 |
+
STREAM_NOTE = '\n\n'.join(STREAM_NOTE)
|
179 |
+
|
180 |
+
with gr.Blocks() as stream_tab:
|
181 |
+
out_stream = gr.Audio(label='Output Audio Stream', interactive=False, streaming=True, autoplay=True)
|
182 |
+
with gr.Row():
|
183 |
+
stream_btn = gr.Button('Stream', variant='primary')
|
184 |
+
stop_btn = gr.Button('Stop', variant='stop')
|
185 |
+
with gr.Accordion('Note', open=True):
|
186 |
+
gr.Markdown(STREAM_NOTE)
|
187 |
+
gr.DuplicateButton()
|
188 |
+
|
189 |
BANNER_TEXT = '''
|
190 |
[***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://huggingface.co/hexgrad/Kokoro-82M)
|
191 |
As of January 31st, 2025, Kokoro was the most-liked [**TTS model**](https://huggingface.co/models?pipeline_tag=text-to-speech&sort=likes) and the most-liked [**TTS space**](https://huggingface.co/spaces?sort=likes&search=tts) on Hugging Face.
|
192 |
This demo only showcases English, but you can directly use the model to access other languages.
|
193 |
'''
|
194 |
+
API_OPEN = os.getenv('SPACE_ID') != 'hexgrad/Kokoro-TTS'
|
195 |
+
API_NAME = None if API_OPEN else False
|
196 |
with gr.Blocks() as app:
|
197 |
with gr.Row():
|
198 |
gr.Markdown(BANNER_TEXT, container=True)
|
199 |
with gr.Row():
|
200 |
with gr.Column():
|
201 |
+
text = gr.Textbox(label='Input Text', info=f"Up to ~500 characters per Generate, or {'∞' if CHAR_LIMIT is None else CHAR_LIMIT} characters per Stream")
|
202 |
with gr.Row():
|
203 |
voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice', info='Quality and availability vary by language')
|
204 |
+
use_gpu = gr.Dropdown(
|
205 |
+
[('ZeroGPU 🚀', True), ('CPU 🐌', False)],
|
206 |
+
value=CUDA_AVAILABLE,
|
207 |
+
label='Hardware',
|
208 |
+
info='GPU is usually faster, but has a usage quota',
|
209 |
+
interactive=CUDA_AVAILABLE
|
210 |
+
)
|
211 |
speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
|
212 |
random_btn = gr.Button('Random Text', variant='secondary')
|
213 |
with gr.Column():
|
214 |
+
gr.TabbedInterface([generate_tab, stream_tab], ['Generate', 'Stream'])
|
215 |
+
random_btn.click(fn=get_random_text, inputs=[voice], outputs=[text], api_name=API_NAME)
|
216 |
+
generate_btn.click(fn=generate_first, inputs=[text, voice, speed, use_gpu], outputs=[out_audio, out_ps], api_name=API_NAME)
|
217 |
+
tokenize_btn.click(fn=tokenize_first, inputs=[text, voice], outputs=[out_ps], api_name=API_NAME)
|
218 |
+
stream_event = stream_btn.click(fn=generate_all, inputs=[text, voice, speed, use_gpu], outputs=[out_stream], api_name=API_NAME)
|
219 |
+
stop_btn.click(fn=None, cancels=stream_event)
|
220 |
+
predict_btn.click(fn=predict, inputs=[text, voice, speed], outputs=[out_audio], api_name=API_NAME)
|
221 |
|
222 |
if __name__ == '__main__':
|
223 |
+
app.queue(api_open=API_OPEN).launch(show_api=API_OPEN, ssr_mode=True)
|