Spaces:
Configuration error
Configuration error
| import spaces | |
| from kokoro import KModel, KPipeline | |
| import gradio as gr | |
| import os | |
| import random | |
| import torch | |
| 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 'ab'} | |
| pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kΛOkΙΙΉO' | |
| pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kΛQkΙΙΉQ' | |
| 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): | |
| 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'): | |
| pipeline = pipelines[voice[0]] | |
| for _, ps, _ in pipeline(text, voice): | |
| return ps | |
| return '' | |
| def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE): | |
| pipeline = pipelines[voice[0]] | |
| pack = pipeline.load_voice(voice) | |
| use_gpu = use_gpu and CUDA_AVAILABLE | |
| first = True | |
| 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() | |
| if first: | |
| first = False | |
| yield 24000, torch.zeros(1).numpy() | |
| with open('en.txt', 'r') as r: | |
| random_quotes = [line.strip() for line in r] | |
| def get_random_quote(): | |
| return random.choice(random_quotes) | |
| def get_gatsby(): | |
| with open('gatsby5k.md', 'r') as r: | |
| return r.read().strip() | |
| def get_frankenstein(): | |
| with open('frankenstein5k.md', 'r') as r: | |
| return r.read().strip() | |
| 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', | |
| } | |
| for v in CHOICES.values(): | |
| pipelines[v[0]].load_voice(v) | |
| TOKEN_NOTE = ''' | |
| π‘ Customize pronunciation with Markdown link syntax and /slashes/ like `[Kokoro](/kΛOkΙΙΉO/)` | |
| π¬ To adjust intonation, try punctuation `;:,.!?ββ¦"()ββ` or stress `Λ` and `Λ` | |
| β¬οΈ Lower stress `[1 level](-1)` or `[2 levels](-2)` | |
| β¬οΈ Raise stress 1 level `[or](+2)` 2 levels (only works on less stressed, usually short words) | |
| ''' | |
| 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') | |
| gr.Markdown(TOKEN_NOTE) | |
| predict_btn = gr.Button('Predict', variant='secondary', visible=False) | |
| STREAM_NOTE = ['β οΈ There is an unknown Gradio bug that might yield no audio the first time you click `Stream`.'] | |
| STREAM_NOTE = '\n\n'.join(STREAM_NOTE) | |
| with gr.Blocks() as stream_tab: | |
| out_stream = gr.Audio(label='Output Audio Stream', interactive=False, streaming=True, autoplay=True) | |
| with gr.Row(): | |
| stream_btn = gr.Button('Stream', variant='primary') | |
| stop_btn = gr.Button('Stop', variant='stop') | |
| with gr.Accordion('Note', open=True): | |
| gr.Markdown(STREAM_NOTE) | |
| gr.DuplicateButton() | |
| API_OPEN = True | |
| with gr.Blocks() as app: | |
| with gr.Row(): | |
| with gr.Column(): | |
| text = gr.Textbox(label='Input Text', info=f"Arbitrarily many characters supported") | |
| 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 Quote π¬', variant='secondary') | |
| with gr.Row(): | |
| gatsby_btn = gr.Button('π₯ Gatsby π', variant='secondary') | |
| frankenstein_btn = gr.Button('π Frankenstein π', variant='secondary') | |
| with gr.Column(): | |
| gr.TabbedInterface([generate_tab, stream_tab], ['Generate', 'Stream']) | |
| random_btn.click(fn=get_random_quote, inputs=[], outputs=[text]) | |
| gatsby_btn.click(fn=get_gatsby, inputs=[], outputs=[text]) | |
| frankenstein_btn.click(fn=get_frankenstein, inputs=[], outputs=[text]) | |
| generate_btn.click(fn=generate_first, inputs=[text, voice, speed, use_gpu], outputs=[out_audio, out_ps]) | |
| tokenize_btn.click(fn=tokenize_first, inputs=[text, voice], outputs=[out_ps]) | |
| stream_event = stream_btn.click(fn=generate_all, inputs=[text, voice, speed, use_gpu], outputs=[out_stream]) | |
| stop_btn.click(fn=None, cancels=stream_event) | |
| predict_btn.click(fn=predict, inputs=[text, voice, speed], outputs=[out_audio]) | |
| if __name__ == '__main__': | |
| app.queue(api_open=API_OPEN).launch(server_name="0.0.0.0", server_port=40001, show_api=API_OPEN) | |