Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,36 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
from kokoro import KModel, KPipeline
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
|
|
|
| 5 |
import random
|
|
|
|
| 6 |
import torch
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 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 'abefhijpz'
|
|
|
|
| 14 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
| 15 |
pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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[voice[0]]
|
| 24 |
pack = pipeline.load_voice(voice)
|
| 25 |
use_gpu = use_gpu and CUDA_AVAILABLE
|
|
@@ -40,18 +51,23 @@ def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
|
|
| 40 |
return (24000, audio.numpy()), ps
|
| 41 |
return None, ''
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
def
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
pipeline = pipelines[voice[0]]
|
| 55 |
pack = pipeline.load_voice(voice)
|
| 56 |
use_gpu = use_gpu and CUDA_AVAILABLE
|
| 57 |
for _, ps, _ in pipeline(text, voice, speed):
|
|
@@ -68,131 +84,114 @@ def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
|
|
| 68 |
audio = models[False](ps, ref_s, speed)
|
| 69 |
else:
|
| 70 |
raise gr.Error(e)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
random_texts = {}
|
| 74 |
-
for lang in ['en']:
|
| 75 |
-
with open(f'{lang}.txt', 'r') as r:
|
| 76 |
-
random_texts[lang] = [line.strip() for line in r]
|
| 77 |
-
|
| 78 |
-
def get_random_text(voice):
|
| 79 |
-
lang = dict(a='en', b='en')[voice[0]]
|
| 80 |
-
return random.choice(random_texts[lang])
|
| 81 |
|
|
|
|
| 82 |
CHOICES = {
|
| 83 |
-
'🇺🇸 🚺 Heart ❤️'
|
| 84 |
-
'🇺🇸 🚺 Bella 🔥'
|
| 85 |
-
'🇺🇸 🚺 Nicole 🎧'
|
| 86 |
-
'🇺🇸 🚺 Aoede'
|
| 87 |
-
'🇺🇸 🚺 Kore'
|
| 88 |
-
'🇺🇸 🚺 Sarah'
|
| 89 |
-
'🇺🇸 🚺 Nova'
|
| 90 |
-
'🇺🇸 🚺 Sky'
|
| 91 |
-
'🇺🇸 🚺 Alloy'
|
| 92 |
-
'🇺🇸 🚺 Jessica'
|
| 93 |
-
'🇺🇸 🚺 River'
|
| 94 |
-
|
| 95 |
-
'🇺🇸 🚹 Michael'
|
| 96 |
-
'🇺🇸 🚹 Fenrir'
|
| 97 |
-
'🇺🇸 🚹 Puck'
|
| 98 |
-
'🇺🇸 🚹 Echo'
|
| 99 |
-
'🇺🇸 🚹 Eric'
|
| 100 |
-
'🇺🇸 🚹 Liam'
|
| 101 |
-
'🇺🇸 🚹 Onyx'
|
| 102 |
-
'🇺🇸 🚹 Santa'
|
| 103 |
-
'🇺🇸 🚹 Adam'
|
| 104 |
-
|
| 105 |
-
'🇬🇧 🚺 Emma'
|
| 106 |
-
'🇬🇧 🚺 Isabella'
|
| 107 |
-
'🇬🇧 🚺 Alice'
|
| 108 |
-
'🇬🇧 🚺 Lily'
|
| 109 |
-
|
| 110 |
-
'🇬🇧 🚹 George'
|
| 111 |
-
'🇬🇧 🚹 Fable'
|
| 112 |
-
'🇬🇧 🚹 Lewis'
|
| 113 |
-
'🇬🇧 🚹 Daniel'
|
| 114 |
-
|
| 115 |
-
'🇪🇸 🚺 Dora'
|
| 116 |
-
|
| 117 |
-
'🇪🇸 🚹 Alex'
|
| 118 |
-
'🇪🇸 🚹 Santa'
|
| 119 |
-
|
| 120 |
-
'🇫🇷 🚺 Siwis'
|
| 121 |
-
|
| 122 |
-
'🇮🇳 🚹 Alpha'
|
| 123 |
-
'🇮🇳 🚹 Beta'
|
| 124 |
-
|
| 125 |
-
'🇮🇳 🚹 Omega'
|
| 126 |
-
'🇮🇳 🚹 Psi'
|
| 127 |
-
|
| 128 |
-
'🇮🇹 🚺 Sara'
|
| 129 |
-
|
| 130 |
-
'🇮🇹 🚺 Nicola'
|
| 131 |
-
|
| 132 |
-
'🇯🇵 🚹 Alpha'
|
| 133 |
-
'🇯🇵 🚹 Gongitsune'
|
| 134 |
-
'🇯🇵 🚹 Nezumi'
|
| 135 |
-
'🇯🇵 🚹 Tebukuro'
|
| 136 |
-
|
| 137 |
-
'🇯🇵 🚹 Kumo'
|
| 138 |
-
|
| 139 |
-
'🇧🇷 🚺 Dora'
|
| 140 |
-
|
| 141 |
-
'🇧🇷 🚹 Alex'
|
| 142 |
-
'🇧🇷 🚹 Santa'
|
| 143 |
-
|
| 144 |
-
'🇨🇳 🚺 Xiaobei'
|
| 145 |
-
'🇨🇳 🚺 Xiaoni'
|
| 146 |
-
'🇨🇳 🚺 Xiaoxiao'
|
| 147 |
-
'🇨🇳 🚺 Xiaoyi'
|
| 148 |
-
|
| 149 |
-
'🇨🇳 🚹 Yunjian'
|
| 150 |
-
'🇨🇳 🚹 Yunxi'
|
| 151 |
-
'🇨🇳 🚹 Yunxia'
|
| 152 |
-
'🇨🇳 🚹 Yunyang'
|
| 153 |
}
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
with gr.Blocks() as generate_tab:
|
| 158 |
-
out_audio = gr.Audio(label='Output Audio', interactive=False, streaming=False, autoplay=True)
|
| 159 |
-
generate_btn = gr.Button('Generate', variant='primary')
|
| 160 |
-
with gr.Accordion('Output Tokens', open=True):
|
| 161 |
-
out_ps = gr.Textbox(interactive=False, show_label=False, info='Tokens used to generate the audio, up to 510 context length.')
|
| 162 |
-
tokenize_btn = gr.Button('Tokenize', variant='secondary')
|
| 163 |
-
predict_btn = gr.Button('Predict', variant='secondary', visible=False)
|
| 164 |
-
|
| 165 |
-
BANNER_TEXT = '''
|
| 166 |
-
[***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://huggingface.co/hexgrad/Kokoro-82M)
|
| 167 |
-
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.
|
| 168 |
-
This demo only showcases English, but you can directly use the model to access other languages.
|
| 169 |
-
'''
|
| 170 |
-
|
| 171 |
-
API_OPEN = os.getenv('SPACE_ID') != 'hexgrad/Kokoro-TTS'
|
| 172 |
-
API_NAME = None if API_OPEN else False
|
| 173 |
with gr.Blocks() as app:
|
| 174 |
-
|
| 175 |
-
gr.Markdown(BANNER_TEXT, container=True)
|
| 176 |
with gr.Row():
|
| 177 |
with gr.Column():
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
interactive=CUDA_AVAILABLE
|
| 187 |
-
)
|
| 188 |
-
speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
|
| 189 |
-
random_btn = gr.Button('Random Text', variant='secondary')
|
| 190 |
with gr.Column():
|
| 191 |
-
gr.
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
from kokoro import KPipeline
|
| 4 |
import random
|
| 5 |
+
import os
|
| 6 |
import torch
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
+
# Set up the OpenAI API key (optional)
|
| 10 |
+
openai.api_key = None # Will be set by the user through the UI
|
| 11 |
|
| 12 |
+
# Check if GPU is available
|
| 13 |
CUDA_AVAILABLE = torch.cuda.is_available()
|
| 14 |
+
|
| 15 |
+
# Initialize the models and pipelines (for TTS)
|
| 16 |
models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
|
| 17 |
+
pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'abefhijpz']
|
| 18 |
+
# Load lexicon for specific languages
|
| 19 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
| 20 |
pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
|
| 21 |
|
| 22 |
+
# Initialize random texts for generating sample text
|
| 23 |
+
random_texts = {}
|
| 24 |
+
for lang in ['en']:
|
| 25 |
+
with open(f'{lang}.txt', 'r') as r:
|
| 26 |
+
random_texts[lang] = [line.strip() for line in r]
|
| 27 |
+
|
| 28 |
+
def get_random_text(voice):
|
| 29 |
+
lang = dict(a='en', b='en')[voice[0]]
|
| 30 |
+
return random.choice(random_texts[lang])
|
| 31 |
|
| 32 |
+
# Generate function to create speech from text
|
| 33 |
def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
|
|
|
|
| 34 |
pipeline = pipelines[voice[0]]
|
| 35 |
pack = pipeline.load_voice(voice)
|
| 36 |
use_gpu = use_gpu and CUDA_AVAILABLE
|
|
|
|
| 51 |
return (24000, audio.numpy()), ps
|
| 52 |
return None, ''
|
| 53 |
|
| 54 |
+
# Translator function using OpenAI API
|
| 55 |
+
def translate_to_english(api_key, text, lang_code):
|
| 56 |
+
openai.api_key = api_key
|
| 57 |
+
try:
|
| 58 |
+
prompt = f"Translate the following text from {lang_code} to English: \n\n{text}"
|
| 59 |
+
response = openai.ChatCompletion.create(
|
| 60 |
+
model="gpt-4",
|
| 61 |
+
messages=[{"role": "system", "content": "You are a helpful assistant that translates text."},
|
| 62 |
+
{"role": "user", "content": prompt}]
|
| 63 |
+
)
|
| 64 |
+
translated_text = response['choices'][0]['message']['content'].strip()
|
| 65 |
+
return translated_text
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error: {str(e)}"
|
| 68 |
|
| 69 |
+
def generate_audio_from_text(text, lang_code, voice, speed, use_gpu=True):
|
| 70 |
+
pipeline = pipelines[lang_code]
|
|
|
|
| 71 |
pack = pipeline.load_voice(voice)
|
| 72 |
use_gpu = use_gpu and CUDA_AVAILABLE
|
| 73 |
for _, ps, _ in pipeline(text, voice, speed):
|
|
|
|
| 84 |
audio = models[False](ps, ref_s, speed)
|
| 85 |
else:
|
| 86 |
raise gr.Error(e)
|
| 87 |
+
return (24000, audio.numpy())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Define your available voices here in the CHOICES dictionary
|
| 90 |
CHOICES = {
|
| 91 |
+
'af_heart': '🇺🇸 🚺 Heart ❤️',
|
| 92 |
+
'af_bella': '🇺🇸 🚺 Bella 🔥',
|
| 93 |
+
'af_nicole': '🇺🇸 🚺 Nicole 🎧',
|
| 94 |
+
'af_aoede': '🇺🇸 🚺 Aoede',
|
| 95 |
+
'af_kore': '🇺🇸 🚺 Kore',
|
| 96 |
+
'af_sarah': '🇺🇸 🚺 Sarah',
|
| 97 |
+
'af_nova': '🇺🇸 🚺 Nova',
|
| 98 |
+
'af_sky': '🇺🇸 🚺 Sky',
|
| 99 |
+
'af_alloy': '🇺🇸 🚺 Alloy',
|
| 100 |
+
'af_jessica': '🇺🇸 🚺 Jessica',
|
| 101 |
+
'af_river': '🇺🇸 🚺 River',
|
| 102 |
+
|
| 103 |
+
'am_michael': '🇺🇸 🚹 Michael',
|
| 104 |
+
'am_fenrir': '🇺🇸 🚹 Fenrir',
|
| 105 |
+
'am_puck': '🇺🇸 🚹 Puck',
|
| 106 |
+
'am_echo': '🇺🇸 🚹 Echo',
|
| 107 |
+
'am_eric': '🇺🇸 🚹 Eric',
|
| 108 |
+
'am_liam': '🇺🇸 🚹 Liam',
|
| 109 |
+
'am_onyx': '🇺🇸 🚹 Onyx',
|
| 110 |
+
'am_santa': '🇺🇸 🚹 Santa',
|
| 111 |
+
'am_adam': '🇺🇸 🚹 Adam',
|
| 112 |
+
|
| 113 |
+
'bf_emma': '🇬🇧 🚺 Emma',
|
| 114 |
+
'bf_isabella': '🇬🇧 🚺 Isabella',
|
| 115 |
+
'bf_alice': '🇬🇧 🚺 Alice',
|
| 116 |
+
'bf_lily': '🇬🇧 🚺 Lily',
|
| 117 |
+
|
| 118 |
+
'bm_george': '🇬🇧 🚹 George',
|
| 119 |
+
'bm_fable': '🇬🇧 🚹 Fable',
|
| 120 |
+
'bm_lewis': '🇬🇧 🚹 Lewis',
|
| 121 |
+
'bm_daniel': '🇬🇧 🚹 Daniel',
|
| 122 |
+
|
| 123 |
+
'ef_dora': '🇪🇸 🚺 Dora',
|
| 124 |
+
|
| 125 |
+
'em_alex': '🇪🇸 🚹 Alex',
|
| 126 |
+
'em_santa': '🇪🇸 🚹 Santa',
|
| 127 |
+
|
| 128 |
+
'ff_siwis': '🇫🇷 🚺 Siwis',
|
| 129 |
+
|
| 130 |
+
'hf_alpha': '🇮🇳 🚹 Alpha',
|
| 131 |
+
'hf_beta': '🇮🇳 🚹 Beta',
|
| 132 |
+
|
| 133 |
+
'hm_omega': '🇮🇳 🚹 Omega',
|
| 134 |
+
'hm_psi': '🇮🇳 🚹 Psi',
|
| 135 |
+
|
| 136 |
+
'if_sara': '🇮🇹 🚺 Sara',
|
| 137 |
+
|
| 138 |
+
'im_nicola': '🇮🇹 🚺 Nicola',
|
| 139 |
+
|
| 140 |
+
'jf_alpha': '🇯🇵 🚹 Alpha',
|
| 141 |
+
'jf_gongitsune': '🇯🇵 🚹 Gongitsune',
|
| 142 |
+
'jf_nezumi': '🇯🇵 🚹 Nezumi',
|
| 143 |
+
'jf_tebukuro': '🇯🇵 🚹 Tebukuro',
|
| 144 |
+
|
| 145 |
+
'jm_kumo': '🇯🇵 🚹 Kumo',
|
| 146 |
+
|
| 147 |
+
'pf_dora': '🇧🇷 🚺 Dora',
|
| 148 |
+
|
| 149 |
+
'pm_alex': '🇧🇷 🚹 Alex',
|
| 150 |
+
'pm_santa': '🇧🇷 🚹 Santa',
|
| 151 |
+
|
| 152 |
+
'zf_xiaobei': '🇨🇳 🚺 Xiaobei',
|
| 153 |
+
'zf_xiaoni': '🇨🇳 🚺 Xiaoni',
|
| 154 |
+
'zf_xiaoxiao': '🇨🇳 🚺 Xiaoxiao',
|
| 155 |
+
'zf_xiaoyi': '🇨🇳 🚺 Xiaoyi',
|
| 156 |
+
|
| 157 |
+
'zm_yunjian': '🇨🇳 🚹 Yunjian',
|
| 158 |
+
'zm_yunxi': '🇨🇳 🚹 Yunxi',
|
| 159 |
+
'zm_yunxia': '🇨🇳 🚹 Yunxia',
|
| 160 |
+
'zm_yunyang': '🇨🇳 🚹 Yunyang'
|
| 161 |
}
|
| 162 |
+
|
| 163 |
+
# Gradio interface setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
with gr.Blocks() as app:
|
| 165 |
+
gr.Markdown("### Kokoro Text-to-Speech with Translation")
|
|
|
|
| 166 |
with gr.Row():
|
| 167 |
with gr.Column():
|
| 168 |
+
# Input for text and language settings
|
| 169 |
+
input_text = gr.Textbox(label="Enter Text", placeholder="Type your text here...")
|
| 170 |
+
voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice')
|
| 171 |
+
use_gpu = gr.Checkbox(label="Use GPU", value=CUDA_AVAILABLE)
|
| 172 |
+
speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label="Speed")
|
| 173 |
+
openai_api_key = gr.Textbox(label="Enter OpenAI API Key (for translation)", type="password")
|
| 174 |
+
random_btn = gr.Button("Random Text")
|
| 175 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
with gr.Column():
|
| 177 |
+
out_audio = gr.Audio(label="Generated Audio", interactive=False, autoplay=True)
|
| 178 |
+
out_text = gr.Textbox(label="Generated Audio Tokens", interactive=False)
|
| 179 |
+
generate_btn = gr.Button("Generate Audio")
|
| 180 |
+
translate_btn = gr.Button("Translate and Generate Audio")
|
| 181 |
+
|
| 182 |
+
random_btn.click(fn=get_random_text, inputs=[voice], outputs=[input_text])
|
| 183 |
+
|
| 184 |
+
def handle_translation(text, api_key, lang_code, voice, speed, use_gpu):
|
| 185 |
+
translated_text = translate_to_english(api_key, text, lang_code)
|
| 186 |
+
translated_audio = generate_audio_from_text(translated_text, 'a', voice, speed, use_gpu)
|
| 187 |
+
return translated_audio, translated_text
|
| 188 |
+
|
| 189 |
+
translate_btn.click(fn=handle_translation, inputs=[input_text, openai_api_key, voice, speed, use_gpu], outputs=[out_audio, out_text])
|
| 190 |
+
|
| 191 |
+
def generate_and_play(text, voice, speed, use_gpu):
|
| 192 |
+
audio, tokens = generate_first(text, voice, speed, use_gpu)
|
| 193 |
+
return audio, tokens
|
| 194 |
+
|
| 195 |
+
generate_btn.click(fn=generate_and_play, inputs=[input_text, voice, speed, use_gpu], outputs=[out_audio, out_text])
|
| 196 |
+
|
| 197 |
+
app.launch()
|