Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import spaces
|
2 |
from kokoro import KModel, KPipeline
|
3 |
import gradio as gr
|
4 |
import os
|
@@ -10,17 +9,18 @@ CHAR_LIMIT = None if IS_DUPLICATE else 5000
|
|
10 |
|
11 |
CUDA_AVAILABLE = torch.cuda.is_available()
|
12 |
models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
|
13 |
-
pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in '
|
14 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
15 |
pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
|
16 |
|
17 |
-
|
|
|
18 |
def forward_gpu(ps, ref_s, speed):
|
19 |
return models[True](ps, ref_s, speed)
|
20 |
|
21 |
def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
|
22 |
text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
|
23 |
-
pipeline = pipelines
|
24 |
pack = pipeline.load_voice(voice)
|
25 |
use_gpu = use_gpu and CUDA_AVAILABLE
|
26 |
for _, ps, _ in pipeline(text, voice, speed):
|
@@ -125,9 +125,11 @@ CHOICES = {
|
|
125 |
'🇯🇵 🚹 Alpha':'jm_alpha',
|
126 |
'🇬🇧 🚹 Emma': 'bf_emma'
|
127 |
}
|
128 |
-
|
129 |
for v in CHOICES.values():
|
130 |
-
|
|
|
|
|
|
|
131 |
|
132 |
TOKEN_NOTE = '''
|
133 |
💡 Customize pronunciation with Markdown link syntax and /slashes/ like `[Kokoro](/kˈOkəɹO/)`
|
@@ -163,18 +165,10 @@ with gr.Blocks() as stream_tab:
|
|
163 |
gr.Markdown(STREAM_NOTE)
|
164 |
gr.DuplicateButton()
|
165 |
|
166 |
-
BANNER_TEXT = '''
|
167 |
-
[***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://huggingface.co/hexgrad/Kokoro-82M)
|
168 |
-
|
169 |
-
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.
|
170 |
-
|
171 |
-
This demo only showcases English, but you can directly use the model to access other languages.
|
172 |
-
'''
|
173 |
API_OPEN = os.getenv('SPACE_ID') != 'hexgrad/Kokoro-TTS'
|
174 |
API_NAME = None if API_OPEN else False
|
175 |
with gr.Blocks() as app:
|
176 |
-
|
177 |
-
gr.Markdown(BANNER_TEXT, container=True)
|
178 |
with gr.Row():
|
179 |
with gr.Column():
|
180 |
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")
|
|
|
|
|
1 |
from kokoro import KModel, KPipeline
|
2 |
import gradio as gr
|
3 |
import os
|
|
|
9 |
|
10 |
CUDA_AVAILABLE = torch.cuda.is_available()
|
11 |
models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
|
12 |
+
pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in ['a', 'b', 'hi']}
|
13 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
14 |
pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
|
15 |
|
16 |
+
|
17 |
+
|
18 |
def forward_gpu(ps, ref_s, speed):
|
19 |
return models[True](ps, ref_s, speed)
|
20 |
|
21 |
def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
|
22 |
text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
|
23 |
+
pipeline = pipelines.get(voice[:2], pipelines['a'])
|
24 |
pack = pipeline.load_voice(voice)
|
25 |
use_gpu = use_gpu and CUDA_AVAILABLE
|
26 |
for _, ps, _ in pipeline(text, voice, speed):
|
|
|
125 |
'🇯🇵 🚹 Alpha':'jm_alpha',
|
126 |
'🇬🇧 🚹 Emma': 'bf_emma'
|
127 |
}
|
|
|
128 |
for v in CHOICES.values():
|
129 |
+
lang_code = v.split('_')[0]
|
130 |
+
if lang_code in pipelines:
|
131 |
+
pipelines[lang_code].load_voice(v)
|
132 |
+
|
133 |
|
134 |
TOKEN_NOTE = '''
|
135 |
💡 Customize pronunciation with Markdown link syntax and /slashes/ like `[Kokoro](/kˈOkəɹO/)`
|
|
|
165 |
gr.Markdown(STREAM_NOTE)
|
166 |
gr.DuplicateButton()
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
API_OPEN = os.getenv('SPACE_ID') != 'hexgrad/Kokoro-TTS'
|
169 |
API_NAME = None if API_OPEN else False
|
170 |
with gr.Blocks() as app:
|
171 |
+
|
|
|
172 |
with gr.Row():
|
173 |
with gr.Column():
|
174 |
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")
|