MicroCore-Labs commited on
Commit
b2ca162
·
verified ·
1 Parent(s): b13b633

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -13,39 +13,52 @@ if not IS_DUPLICATE:
13
  print('DEBUG', kokoro.__version__, CUDA_AVAILABLE, misaki.__version__)
14
 
15
  CHAR_LIMIT = None if IS_DUPLICATE else 5000
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 'ab'}
18
- pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
19
- pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
20
-
21
- @spaces.GPU(duration=30)
22
- def forward_gpu(ps, ref_s, speed):
23
- return models[True](ps, ref_s, speed)
24
 
 
 
25
  _loaded_voices = set()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def _ensure_voice(voice):
27
  if voice not in _loaded_voices:
28
- pipelines[voice[0]].load_voice(voice)
29
  _loaded_voices.add(voice)
30
 
 
 
 
 
31
  def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
32
  text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
33
- pipeline = pipelines[voice[0]]
34
  _ensure_voice(voice)
35
- pack = pipeline.load_voice(voice)
36
  use_gpu = use_gpu and CUDA_AVAILABLE
37
- for _, ps, _ in pipeline(text, voice, speed):
38
  ref_s = pack[len(ps)-1]
39
  try:
40
  if use_gpu:
41
  audio = forward_gpu(ps, ref_s, speed)
42
  else:
43
- audio = models[False](ps, ref_s, speed)
44
  except gr.exceptions.Error as e:
45
  if use_gpu:
46
  gr.Warning(str(e))
47
  gr.Info('Retrying with CPU. To avoid this error, change Hardware to CPU.')
48
- audio = models[False](ps, ref_s, speed)
49
  else:
50
  raise gr.Error(e)
51
  return (24000, audio.numpy()), ps
@@ -55,30 +68,30 @@ def predict(text, voice='af_heart', speed=1):
55
  return generate_first(text, voice, speed, use_gpu=False)[0]
56
 
57
  def tokenize_first(text, voice='af_heart'):
58
- pipeline = pipelines[voice[0]]
59
- for _, ps, _ in pipeline(text, voice):
60
  return ps
61
  return ''
62
 
63
  def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
64
  text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
65
- pipeline = pipelines[voice[0]]
66
  _ensure_voice(voice)
67
- pack = pipeline.load_voice(voice)
68
  use_gpu = use_gpu and CUDA_AVAILABLE
69
  first = True
70
- for _, ps, _ in pipeline(text, voice, speed):
71
  ref_s = pack[len(ps)-1]
72
  try:
73
  if use_gpu:
74
  audio = forward_gpu(ps, ref_s, speed)
75
  else:
76
- audio = models[False](ps, ref_s, speed)
77
  except gr.exceptions.Error as e:
78
  if use_gpu:
79
  gr.Warning(str(e))
80
  gr.Info('Switching to CPU')
81
- audio = models[False](ps, ref_s, speed)
82
  else:
83
  raise gr.Error(e)
84
  yield 24000, audio.numpy()
 
13
  print('DEBUG', kokoro.__version__, CUDA_AVAILABLE, misaki.__version__)
14
 
15
  CHAR_LIMIT = None if IS_DUPLICATE else 5000
 
 
 
 
 
 
 
 
16
 
17
+ _models = None
18
+ _pipelines = None
19
  _loaded_voices = set()
20
+
21
+ def _get_models():
22
+ global _models
23
+ if _models is None:
24
+ _models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
25
+ return _models
26
+
27
+ def _get_pipelines():
28
+ global _pipelines
29
+ if _pipelines is None:
30
+ _pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'ab'}
31
+ _pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
32
+ _pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
33
+ return _pipelines
34
+
35
  def _ensure_voice(voice):
36
  if voice not in _loaded_voices:
37
+ _get_pipelines()[voice[0]].load_voice(voice)
38
  _loaded_voices.add(voice)
39
 
40
+ @spaces.GPU(duration=30)
41
+ def forward_gpu(ps, ref_s, speed):
42
+ return _get_models()[True](ps, ref_s, speed)
43
+
44
  def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
45
  text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
46
+ pipeline = _get_pipelines()
47
  _ensure_voice(voice)
48
+ pack = pipeline[voice[0]].load_voice(voice)
49
  use_gpu = use_gpu and CUDA_AVAILABLE
50
+ for _, ps, _ in pipeline[voice[0]](text, voice, speed):
51
  ref_s = pack[len(ps)-1]
52
  try:
53
  if use_gpu:
54
  audio = forward_gpu(ps, ref_s, speed)
55
  else:
56
+ audio = _get_models()[False](ps, ref_s, speed)
57
  except gr.exceptions.Error as e:
58
  if use_gpu:
59
  gr.Warning(str(e))
60
  gr.Info('Retrying with CPU. To avoid this error, change Hardware to CPU.')
61
+ audio = _get_models()[False](ps, ref_s, speed)
62
  else:
63
  raise gr.Error(e)
64
  return (24000, audio.numpy()), ps
 
68
  return generate_first(text, voice, speed, use_gpu=False)[0]
69
 
70
  def tokenize_first(text, voice='af_heart'):
71
+ pipeline = _get_pipelines()
72
+ for _, ps, _ in pipeline[voice[0]](text, voice):
73
  return ps
74
  return ''
75
 
76
  def generate_all(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE):
77
  text = text if CHAR_LIMIT is None else text.strip()[:CHAR_LIMIT]
78
+ pipeline = _get_pipelines()
79
  _ensure_voice(voice)
80
+ pack = pipeline[voice[0]].load_voice(voice)
81
  use_gpu = use_gpu and CUDA_AVAILABLE
82
  first = True
83
+ for _, ps, _ in pipeline[voice[0]](text, voice, speed):
84
  ref_s = pack[len(ps)-1]
85
  try:
86
  if use_gpu:
87
  audio = forward_gpu(ps, ref_s, speed)
88
  else:
89
+ audio = _get_models()[False](ps, ref_s, speed)
90
  except gr.exceptions.Error as e:
91
  if use_gpu:
92
  gr.Warning(str(e))
93
  gr.Info('Switching to CPU')
94
+ audio = _get_models()[False](ps, ref_s, speed)
95
  else:
96
  raise gr.Error(e)
97
  yield 24000, audio.numpy()