import spaces from kokoro import KModel, KPipeline import gradio as gr import os import random import torch IS_DUPLICATE = not os.getenv('SPACE_ID', '').startswith('hexgrad/') CHAR_LIMIT = None if IS_DUPLICATE else 5000 CUDA_AVAILABLE = torch.cuda.is_available() models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])} pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'abefhijpz'} pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO' pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ' @spaces.GPU(duration=10) def forward_gpu(ps, ref_s, speed): return models[True](ps, ref_s, speed) def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE): text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT] pipeline = pipelines[voice[0]] pack = pipeline.load_voice(voice) use_gpu = use_gpu and CUDA_AVAILABLE for _, ps, _ in pipeline(text, voice, speed): ref_s = pack[len(ps)-1] try: if use_gpu: audio = forward_gpu(ps, ref_s, speed) else: audio = models[False](ps, ref_s, speed) except gr.exceptions.Error as e: if use_gpu: gr.Warning(str(e)) gr.Info('Retrying with CPU. To avoid this error, change Hardware to CPU.') audio = models[False](ps, ref_s, speed) else: raise gr.Error(e) return (24000, audio.numpy()), ps return None, '' # Arena API def predict(text, voice='af_heart', speed=1): return generate_first(text, voice, speed, use_gpu=False)[0] def tokenize_first(text, voice='af_heart'): # Split the input text into words and return as a list of words (fix applied here) words = text.split() # This splits the text into words based on spaces return words # Return a list of words def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE): text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT] pipeline = pipelines[voice[0]] pack = pipeline.load_voice(voice) use_gpu = use_gpu and CUDA_AVAILABLE for _, ps, _ in pipeline(text, voice, speed): ref_s = pack[len(ps)-1] try: if use_gpu: audio = forward_gpu(ps, ref_s, speed) else: audio = models[False](ps, ref_s, speed) except gr.exceptions.Error as e: if use_gpu: gr.Warning(str(e)) gr.Info('Switching to CPU') audio = models[False](ps, ref_s, speed) else: raise gr.Error(e) yield 24000, audio.numpy() random_texts = {} for lang in ['en']: with open(f'{lang}.txt', 'r') as r: random_texts[lang] = [line.strip() for line in r] def get_random_text(voice): lang = dict(a='en', b='en')[voice[0]] return random.choice(random_texts[lang]) CHOICES = { '🇺🇸 🚺 Heart ❤️': 'af_heart', '🇺🇸 🚺 Bella 🔥': 'af_bella', '🇺🇸 🚺 Nicole 🎧': 'af_nicole', '🇺🇸 🚺 Aoede': 'af_aoede', '🇺🇸 🚺 Kore': 'af_kore', '🇺🇸 🚺 Sarah': 'af_sarah', '🇺🇸 🚺 Nova': 'af_nova', '🇺🇸 🚺 Sky': 'af_sky', '🇺🇸 🚺 Alloy': 'af_alloy', '🇺🇸 🚺 Jessica': 'af_jessica', '🇺🇸 🚺 River': 'af_river', '🇺🇸 🚹 Michael': 'am_michael', '🇺🇸 🚹 Fenrir': 'am_fenrir', '🇺🇸 🚹 Puck': 'am_puck', '🇺🇸 🚹 Echo': 'am_echo', '🇺🇸 🚹 Eric': 'am_eric', '🇺🇸 🚹 Liam': 'am_liam', '🇺🇸 🚹 Onyx': 'am_onyx', '🇺🇸 🚹 Santa': 'am_santa', '🇺🇸 🚹 Adam': 'am_adam', '🇬🇧 🚺 Emma': 'bf_emma', '🇬🇧 🚺 Isabella': 'bf_isabella', '🇬🇧 🚺 Alice': 'bf_alice', '🇬🇧 🚺 Lily': 'bf_lily', '🇬🇧 🚹 George': 'bm_george', '🇬🇧 🚹 Fable': 'bm_fable', '🇬🇧 🚹 Lewis': 'bm_lewis', '🇬🇧 🚹 Daniel': 'bm_daniel', '🇪🇸 🚺 Dora': 'ef_dora', '🇪🇸 🚹 Alex': 'em_alex', '🇪🇸 🚹 Santa': 'em_santa', '🇫🇷 🚺 Siwis': 'ff_siwis', '🇮🇳 🚹 Alpha': 'hf_alpha', '🇮🇳 🚹 Beta': 'hf_beta', '🇮🇳 🚹 Omega': 'hm_omega', '🇮🇳 🚹 Psi': 'hm_psi', '🇮🇹 🚺 Sara': 'if_sara', '🇮🇹 🚺 Nicola': 'im_nicola', '🇯🇵 🚹 Alpha': 'jf_alpha', '🇯🇵 🚹 Gongitsune': 'jf_gongitsune', '🇯🇵 🚹 Nezumi': 'jf_nezumi', '🇯🇵 🚹 Tebukuro': 'jf_tebukuro', '🇯🇵 🚹 Kumo': 'jm_kumo', '🇧🇷 🚺 Dora': 'pf_dora', '🇧🇷 🚹 Alex': 'pm_alex', '🇧🇷 🚹 Santa': 'pm_santa', '🇨🇳 🚺 Xiaobei': 'zf_xiaobei', '🇨🇳 🚺 Xiaoni': 'zf_xiaoni', '🇨🇳 🚺 Xiaoxiao': 'zf_xiaoxiao', '🇨🇳 🚺 Xiaoyi': 'zf_xiaoyi', '🇨🇳 🚹 Yunjian': 'zm_yunjian', '🇨🇳 🚹 Yunxi': 'zm_yunxi', '🇨🇳 🚹 Yunxia': 'zm_yunxia', '🇨🇳 🚹 Yunyang': 'zm_yunyang', } for v in CHOICES.values(): pipelines[v[0]].load_voice(v) with gr.Blocks() as generate_tab: out_audio = gr.Audio(label='Output Audio', interactive=False, streaming=False, autoplay=True) generate_btn = gr.Button('Generate', variant='primary') with gr.Accordion('Output Tokens', open=True): out_ps = gr.Textbox(interactive=False, show_label=False, info='Tokens used to generate the audio, up to 510 context length.') tokenize_btn = gr.Button('Tokenize', variant='secondary') predict_btn = gr.Button('Predict', variant='secondary', visible=False) BANNER_TEXT = ''' [***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://huggingface.co/hexgrad/Kokoro-82M) This is our work on Kokoro TTS [**V1 Model GPU**](https://shukdevdatta123-kokoro-tts-translate-gpu.hf.space) and the next version Kokoro TTS [**V2 Model CPU**](https://shukdevdatta123-kokoro-tts.hf.space). If you would like to use our V2 Model with GPU, then go to this [link](https://colab.research.google.com/drive/1DIpBzJSBBeTcpkyxkHcpngLumMapEWQz?usp=sharing). ''' API_OPEN = os.getenv('SPACE_ID') != 'hexgrad/Kokoro-TTS' API_NAME = None if API_OPEN else False with gr.Blocks() as app: with gr.Row(): gr.Markdown(BANNER_TEXT, container=True) with gr.Row(): with gr.Column(): 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") with gr.Row(): voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice', info='Quality and availability vary by language') use_gpu = gr.Dropdown( [('ZeroGPU 🚀', True), ('CPU 🐌', False)], value=CUDA_AVAILABLE, label='Hardware', info='GPU is usually faster, but has a usage quota', interactive=CUDA_AVAILABLE ) speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed') random_btn = gr.Button('Random Text', variant='secondary') with gr.Column(): gr.TabbedInterface([generate_tab], ['Generate']) random_btn.click(fn=get_random_text, inputs=[voice], outputs=[text], api_name=API_NAME) generate_btn.click(fn=generate_first, inputs=[text, voice, speed, use_gpu], outputs=[out_audio, out_ps], api_name=API_NAME) tokenize_btn.click(fn=tokenize_first, inputs=[text, voice], outputs=[out_ps], api_name=API_NAME) predict_btn.click(fn=predict, inputs=[text, voice, speed], outputs=[out_audio], api_name=API_NAME) if __name__ == '__main__': app.queue(api_open=API_OPEN).launch(show_api=API_OPEN, ssr_mode=True)