hexgrad commited on
Commit
ebcaf6e
·
verified ·
1 Parent(s): a2e0f11

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -17
app.py CHANGED
@@ -9,7 +9,6 @@ import pypdf
9
  import random
10
  import re
11
  import spaces
12
- import threading
13
  import torch
14
  import yaml
15
 
@@ -323,15 +322,11 @@ with gr.Blocks() as basic_tts:
323
  text.submit(generate, inputs=[text, voice, in_ps, speed, trim, use_gpu], outputs=[audio, out_ps])
324
  generate_btn.click(generate, inputs=[text, voice, in_ps, speed, trim, use_gpu], outputs=[audio, out_ps])
325
 
326
- stop_event = threading.Event()
327
-
328
  @torch.no_grad()
329
  def lf_forward(token_lists, voices, speed, device='cpu'):
330
  voicepack = torch.mean(torch.stack([VOICES[device][v] for v in voices]), dim=0)
331
  outs = []
332
  for tokens in token_lists:
333
- if stop_event.is_set():
334
- break
335
  ref_s = voicepack[len(tokens)]
336
  s = ref_s[:, 128:]
337
  tokens = torch.LongTensor([[0, *tokens, 0]]).to(device)
@@ -413,8 +408,6 @@ def segment_and_tokenize(text, voice, skip_square_brackets=True, newline_split=2
413
  return [(i, *row) for i, row in enumerate(segments)]
414
 
415
  def lf_generate(segments, voice, speed=1, trim=0, pad_between=0, use_gpu=True):
416
- global stop_event
417
- stop_event.clear()
418
  token_lists = list(map(tokenize, segments['Tokens']))
419
  voices = resolve_voices(voice)
420
  speed = clamp_speed(speed)
@@ -447,10 +440,6 @@ def lf_generate(segments, voice, speed=1, trim=0, pad_between=0, use_gpu=True):
447
  yield (SAMPLE_RATE, out)
448
  i += bs
449
 
450
- def lf_stop():
451
- global stop_event
452
- stop_event.set()
453
-
454
  def did_change_segments(segments):
455
  x = len(segments) if segments['Length'].any() else 0
456
  return [
@@ -471,9 +460,7 @@ def extract_text(file):
471
  with gr.Blocks() as lf_tts:
472
  with gr.Row():
473
  with gr.Column():
474
- file_input = gr.File(file_types=['.pdf', '.txt'], label='Input File: pdf or txt')
475
  text = gr.Textbox(label='Input Text', info='Generate speech in batches of 100 text segments and automatically join them together')
476
- file_input.upload(fn=extract_text, inputs=[file_input], outputs=[text])
477
  with gr.Row():
478
  voice = gr.Dropdown(list(CHOICES.items()), value='af', allow_custom_value=True, label='Voice', info='Starred voices are more stable')
479
  use_gpu = gr.Dropdown(
@@ -487,8 +474,9 @@ with gr.Blocks() as lf_tts:
487
  skip_square_brackets = gr.Checkbox(True, label='Skip [Square Brackets]', info='Recommended for academic papers, Wikipedia articles, or texts with citations')
488
  newline_split = gr.Number(2, label='Newline Split', info='Split the input text on this many newlines. Affects how the text is segmented.', precision=0, minimum=0)
489
  with gr.Row():
490
- upload_btn = gr.Button('Upload', variant='secondary')
491
  segment_btn = gr.Button('Tokenize', variant='primary')
 
492
  with gr.Column():
493
  audio_stream = gr.Audio(label='Output Audio Stream', interactive=False, streaming=True, autoplay=True)
494
  with gr.Accordion('Audio Settings', open=True):
@@ -502,12 +490,12 @@ with gr.Blocks() as lf_tts:
502
  segments = gr.Dataframe(headers=['#', 'Text', 'Tokens', 'Length'], row_count=(1, 'dynamic'), col_count=(4, 'fixed'), label='Segments', interactive=False, wrap=True)
503
  segments.change(fn=did_change_segments, inputs=[segments], outputs=[segment_btn, generate_btn])
504
  segment_btn.click(segment_and_tokenize, inputs=[text, voice, skip_square_brackets, newline_split], outputs=[segments])
505
- generate_btn.click(lf_generate, inputs=[segments, voice, speed, trim, pad_between, use_gpu], outputs=[audio_stream])
506
- stop_btn.click(lf_stop)
507
 
508
  with gr.Blocks() as about:
509
  gr.Markdown('''
510
- Kokoro is a frontier TTS model for its size. It has [80 million](https://hf.co/spaces/hexgrad/Kokoro-TTS/blob/main/app.py#L32) parameters, uses a lean [StyleTTS 2](https://github.com/yl4579/StyleTTS2) architecture, and was trained on high-quality data. The weights are currently private, but a free public demo is hosted here, at `https://hf.co/spaces/hexgrad/Kokoro-TTS`. The Community tab is open for feature requests, bug reports, etc. For other inquiries, contact `@rzvzn` on Discord.
511
 
512
  ### FAQ
513
  **Will this be open sourced?**<br/>
 
9
  import random
10
  import re
11
  import spaces
 
12
  import torch
13
  import yaml
14
 
 
322
  text.submit(generate, inputs=[text, voice, in_ps, speed, trim, use_gpu], outputs=[audio, out_ps])
323
  generate_btn.click(generate, inputs=[text, voice, in_ps, speed, trim, use_gpu], outputs=[audio, out_ps])
324
 
 
 
325
  @torch.no_grad()
326
  def lf_forward(token_lists, voices, speed, device='cpu'):
327
  voicepack = torch.mean(torch.stack([VOICES[device][v] for v in voices]), dim=0)
328
  outs = []
329
  for tokens in token_lists:
 
 
330
  ref_s = voicepack[len(tokens)]
331
  s = ref_s[:, 128:]
332
  tokens = torch.LongTensor([[0, *tokens, 0]]).to(device)
 
408
  return [(i, *row) for i, row in enumerate(segments)]
409
 
410
  def lf_generate(segments, voice, speed=1, trim=0, pad_between=0, use_gpu=True):
 
 
411
  token_lists = list(map(tokenize, segments['Tokens']))
412
  voices = resolve_voices(voice)
413
  speed = clamp_speed(speed)
 
440
  yield (SAMPLE_RATE, out)
441
  i += bs
442
 
 
 
 
 
443
  def did_change_segments(segments):
444
  x = len(segments) if segments['Length'].any() else 0
445
  return [
 
460
  with gr.Blocks() as lf_tts:
461
  with gr.Row():
462
  with gr.Column():
 
463
  text = gr.Textbox(label='Input Text', info='Generate speech in batches of 100 text segments and automatically join them together')
 
464
  with gr.Row():
465
  voice = gr.Dropdown(list(CHOICES.items()), value='af', allow_custom_value=True, label='Voice', info='Starred voices are more stable')
466
  use_gpu = gr.Dropdown(
 
474
  skip_square_brackets = gr.Checkbox(True, label='Skip [Square Brackets]', info='Recommended for academic papers, Wikipedia articles, or texts with citations')
475
  newline_split = gr.Number(2, label='Newline Split', info='Split the input text on this many newlines. Affects how the text is segmented.', precision=0, minimum=0)
476
  with gr.Row():
477
+ upload_btn = gr.UploadButton('Upload txt or pdf', file_types=['text'])
478
  segment_btn = gr.Button('Tokenize', variant='primary')
479
+ upload_btn.upload(fn=extract_text, inputs=[upload_btn], outputs=[text])
480
  with gr.Column():
481
  audio_stream = gr.Audio(label='Output Audio Stream', interactive=False, streaming=True, autoplay=True)
482
  with gr.Accordion('Audio Settings', open=True):
 
490
  segments = gr.Dataframe(headers=['#', 'Text', 'Tokens', 'Length'], row_count=(1, 'dynamic'), col_count=(4, 'fixed'), label='Segments', interactive=False, wrap=True)
491
  segments.change(fn=did_change_segments, inputs=[segments], outputs=[segment_btn, generate_btn])
492
  segment_btn.click(segment_and_tokenize, inputs=[text, voice, skip_square_brackets, newline_split], outputs=[segments])
493
+ generate_event = generate_btn.click(lf_generate, inputs=[segments, voice, speed, trim, pad_between, use_gpu], outputs=[audio_stream])
494
+ stop_btn.click(cancels=[generate_event])
495
 
496
  with gr.Blocks() as about:
497
  gr.Markdown('''
498
+ Kokoro is a frontier TTS model for its size. It has [80 million](https://hf.co/spaces/hexgrad/Kokoro-TTS/blob/main/app.py#L31) parameters, uses a lean [StyleTTS 2](https://github.com/yl4579/StyleTTS2) architecture, and was trained on high-quality data. The weights are currently private, but a free public demo is hosted here, at `https://hf.co/spaces/hexgrad/Kokoro-TTS`. The Community tab is open for feature requests, bug reports, etc. For other inquiries, contact `@rzvzn` on Discord.
499
 
500
  ### FAQ
501
  **Will this be open sourced?**<br/>