Pelku commited on
Commit
2797ff6
·
verified ·
1 Parent(s): 53062d7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -22,6 +22,9 @@ VOICE_LIST = [
22
  "michael", "paul", "vera"
23
  ]
24
 
 
 
 
25
  @spaces.GPU
26
  def generate_tts(text: str, voice: str):
27
  clean_text = text.strip()
@@ -31,21 +34,15 @@ def generate_tts(text: str, voice: str):
31
  m = get_model()
32
  clean_voice = voice.replace("pocket-", "").replace("-", "_")
33
 
34
- # Initialize the model state with the specified voice
35
- if hasattr(m, "init_model_state"):
36
  try:
37
- model_state = m.init_model_state(voice=clean_voice)
38
- except TypeError:
39
- model_state = m.init_model_state(clean_voice)
40
- elif hasattr(m, "get_voice_state"):
41
- model_state = m.get_voice_state(clean_voice)
42
- else:
43
- # Check all available methods on m for state initialization
44
- init_fn = getattr(m, "get_initial_state", getattr(m, "new_state", None))
45
- if init_fn:
46
- model_state = init_fn(clean_voice)
47
- else:
48
- raise RuntimeError(f"Available methods on TTSModel: {[fn for fn in dir(m) if not fn.startswith('_')]}")
49
 
50
  # Generate audio
51
  audio = m.generate_audio(model_state, clean_text)
 
22
  "michael", "paul", "vera"
23
  ]
24
 
25
+ # Cache voice states so we don't recompute voice prompt embeddings every request
26
+ voice_state_cache = {}
27
+
28
  @spaces.GPU
29
  def generate_tts(text: str, voice: str):
30
  clean_text = text.strip()
 
34
  m = get_model()
35
  clean_voice = voice.replace("pocket-", "").replace("-", "_")
36
 
37
+ # Get or compute voice state for the voice prompt
38
+ if clean_voice not in voice_state_cache:
39
  try:
40
+ voice_state_cache[clean_voice] = m.get_state_for_audio_prompt(clean_voice)
41
+ except Exception:
42
+ # If string voice identifier is passed directly or with fallback
43
+ voice_state_cache[clean_voice] = m.get_state_for_audio_prompt(f"voices/{clean_voice}.wav") if hasattr(m, "get_state_for_audio_prompt") else {}
44
+
45
+ model_state = voice_state_cache[clean_voice]
 
 
 
 
 
 
46
 
47
  # Generate audio
48
  audio = m.generate_audio(model_state, clean_text)