hexgrad commited on
Commit
e8c4059
·
verified ·
1 Parent(s): 0c4c8a7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -6,11 +6,9 @@ import random
6
  import torch
7
 
8
  IS_DUPLICATE = not os.getenv('SPACE_ID', '').startswith('hexgrad/')
9
- N_MAX_CHARS = None if IS_DUPLICATE else 5000
10
- S_MAX_CHARS = '∞' if IS_DUPLICATE else str(N_MAX_CHARS)
11
 
12
  CUDA_AVAILABLE = torch.cuda.is_available()
13
-
14
  models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
15
  pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'ab'}
16
  pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
@@ -20,8 +18,8 @@ pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
20
  def forward_gpu(ps, ref_s, speed):
21
  return models[True](ps, ref_s, speed)
22
 
23
- def return_audio_ps(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
24
- text = text if N_MAX_CHARS is None else text.strip()[:N_MAX_CHARS]
25
  pipeline = pipelines[voice[0]]
26
  pack = pipeline.load_voice(voice)
27
  use_gpu = use_gpu and CUDA_AVAILABLE
@@ -46,14 +44,14 @@ def return_audio_ps(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
46
  def predict(text, voice='af_heart', speed=1):
47
  return return_audio_ps(text, voice, speed, use_gpu=False)[0]
48
 
49
- def return_ps(text, voice='af_heart'):
50
  pipeline = pipelines[voice[0]]
51
  for _, ps, _ in pipeline(text, voice):
52
  return ps
53
  return ''
54
 
55
- def yield_audio(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
56
- text = text if N_MAX_CHARS is None else text.strip()[:N_MAX_CHARS]
57
  pipeline = pipelines[voice[0]]
58
  pack = pipeline.load_voice(voice)
59
  use_gpu = use_gpu and CUDA_AVAILABLE
@@ -133,8 +131,8 @@ with gr.Blocks() as generate_tab:
133
  predict_btn = gr.Button('Predict', variant='secondary', visible=False)
134
 
135
  STREAM_NOTE = ['⚠️ There is an unknown Gradio bug that might yield no audio the first time you click `Stream`.']
136
- if N_MAX_CHARS is not None:
137
- STREAM_NOTE.append(f'✂️ Each stream is capped at {N_MAX_CHARS} characters.')
138
  STREAM_NOTE.append('🚀 Want more characters? You can [use Kokoro directly](https://huggingface.co/hexgrad/Kokoro-82M#usage) or duplicate this space:')
139
  STREAM_NOTE = '\n\n'.join(STREAM_NOTE)
140
 
@@ -147,12 +145,14 @@ with gr.Blocks() as stream_tab:
147
  gr.Markdown(STREAM_NOTE)
148
  gr.DuplicateButton()
149
 
 
 
150
  with gr.Blocks() as app:
151
  with gr.Row():
152
  gr.Markdown('[***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://hf.co/hexgrad/Kokoro-82M)', container=True)
153
  with gr.Row():
154
  with gr.Column():
155
- text = gr.Textbox(label='Input Text', info=f'Up to ~500 characters per Generate, or {S_MAX_CHARS} characters per Stream')
156
  with gr.Row():
157
  voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice', info='Quality and availability vary by language')
158
  use_gpu = gr.Dropdown(
@@ -164,16 +164,14 @@ with gr.Blocks() as app:
164
  )
165
  speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
166
  random_btn = gr.Button('Random Text', variant='secondary')
167
- random_btn.click(get_random_text, inputs=[voice], outputs=[text])
168
  with gr.Column():
169
  gr.TabbedInterface([generate_tab, stream_tab], ['Generate', 'Stream'])
170
- generate_btn.click(return_audio_ps, inputs=[text, voice, speed, use_gpu], outputs=[out_audio, out_ps])
171
- tokenize_btn.click(return_ps, inputs=[text, voice], outputs=[out_ps])
172
- stream_event = stream_btn.click(yield_audio, inputs=[text, voice, speed, use_gpu], outputs=[out_stream])
 
173
  stop_btn.click(fn=None, cancels=stream_event)
174
- predict_btn.click(predict, inputs=[text, voice, speed], outputs=[out_audio])
175
 
176
- if IS_DUPLICATE:
177
- app.queue(api_open=True).launch(show_api=True, ssr_mode=True)
178
- else:
179
- app.queue(api_open=False).launch(show_api=False, ssr_mode=True)
 
6
  import torch
7
 
8
  IS_DUPLICATE = not os.getenv('SPACE_ID', '').startswith('hexgrad/')
9
+ 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 'ab'}
14
  pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
 
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[voice[0]]
24
  pack = pipeline.load_voice(voice)
25
  use_gpu = use_gpu and CUDA_AVAILABLE
 
44
  def predict(text, voice='af_heart', speed=1):
45
  return return_audio_ps(text, voice, speed, use_gpu=False)[0]
46
 
47
+ def tokenize_first(text, voice='af_heart'):
48
  pipeline = pipelines[voice[0]]
49
  for _, ps, _ in pipeline(text, voice):
50
  return ps
51
  return ''
52
 
53
+ def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
54
+ text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
55
  pipeline = pipelines[voice[0]]
56
  pack = pipeline.load_voice(voice)
57
  use_gpu = use_gpu and CUDA_AVAILABLE
 
131
  predict_btn = gr.Button('Predict', variant='secondary', visible=False)
132
 
133
  STREAM_NOTE = ['⚠️ There is an unknown Gradio bug that might yield no audio the first time you click `Stream`.']
134
+ if CHAR_LIMIT is not None:
135
+ STREAM_NOTE.append(f'✂️ Each stream is capped at {CHAR_LIMIT} characters.')
136
  STREAM_NOTE.append('🚀 Want more characters? You can [use Kokoro directly](https://huggingface.co/hexgrad/Kokoro-82M#usage) or duplicate this space:')
137
  STREAM_NOTE = '\n\n'.join(STREAM_NOTE)
138
 
 
145
  gr.Markdown(STREAM_NOTE)
146
  gr.DuplicateButton()
147
 
148
+ API_OPEN = os.getenv('SPACE_ID') != 'hexgrad/Kokoro-TTS'
149
+ API_NAME = None if API_OPEN else False
150
  with gr.Blocks() as app:
151
  with gr.Row():
152
  gr.Markdown('[***Kokoro*** **is an open-weight TTS model with 82 million parameters.**](https://hf.co/hexgrad/Kokoro-82M)', container=True)
153
  with gr.Row():
154
  with gr.Column():
155
+ text = gr.Textbox(label='Input Text', info=f"Up to ~500 characters per Generate, or {'∞' if IS_DUPLICATE else CHAR_LIMIT} characters per Stream")
156
  with gr.Row():
157
  voice = gr.Dropdown(list(CHOICES.items()), value='af_heart', label='Voice', info='Quality and availability vary by language')
158
  use_gpu = gr.Dropdown(
 
164
  )
165
  speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
166
  random_btn = gr.Button('Random Text', variant='secondary')
 
167
  with gr.Column():
168
  gr.TabbedInterface([generate_tab, stream_tab], ['Generate', 'Stream'])
169
+ random_btn.click(fn=get_random_text, inputs=[voice], outputs=[text], api_name=API_NAME)
170
+ generate_btn.click(fn=generate_first, inputs=[text, voice, speed, use_gpu], outputs=[out_audio, out_ps], api_name=API_NAME)
171
+ tokenize_btn.click(fn=tokenize_first, inputs=[text, voice], outputs=[out_ps], api_name=API_NAME)
172
+ stream_event = stream_btn.click(fn=generate_all, inputs=[text, voice, speed, use_gpu], outputs=[out_stream], api_name=API_NAME)
173
  stop_btn.click(fn=None, cancels=stream_event)
174
+ predict_btn.click(fn=predict, inputs=[text, voice, speed], outputs=[out_audio], api_name=API_NAME)
175
 
176
+ if __name__ == '__main__':
177
+ app.queue(api_open=API_OPEN).launch(show_api=API_OPEN, ssr_mode=True)