bdstar commited on
Commit
c9304ac
·
verified ·
1 Parent(s): d2c0c59

update app.py file

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -68,29 +68,31 @@ def chat_with_llm(history_messages, user_text):
68
 
69
 
70
 
71
- # ---- TTS ----
 
 
72
  def tts_silero(text: str) -> str:
73
  """
74
- Return path to a WAV file synthesized by Silero (CPU-friendly).
75
- Works across recent torch.hub return signatures.
76
  """
77
  import torch, tempfile
78
  import soundfile as sf
79
 
80
- # Newer torch.hub supports "trust_repo"; set to True or 'check'
81
- obj = torch.hub.load(
82
- repo_or_dir="snakers4/silero-models",
83
- model="silero_tts",
84
- language="en",
85
- speaker="v3_en",
86
- trust_repo=True # or 'check' to be prompted the first time
87
- )
88
-
89
- # Handle both cases: either a single model, or a (model, something) tuple
90
- model = obj[0] if isinstance(obj, (list, tuple)) else obj
91
 
 
92
  sample_rate = 48000
93
- speaker = "en_0" # valid default voice in v3_en pack
94
  audio = model.apply_tts(text=text, speaker=speaker, sample_rate=sample_rate)
95
 
96
  out_wav = tempfile.mktemp(suffix=".wav")
@@ -98,6 +100,7 @@ def tts_silero(text: str) -> str:
98
  return out_wav
99
 
100
 
 
101
  def tts_coqui_xtts(text: str) -> str:
102
  """
103
  Returns path to a WAV file synthesized by Coqui XTTS v2 (higher quality; GPU-friendly).
@@ -166,4 +169,4 @@ with gr.Blocks(title="Voice Coach") as demo:
166
  btn.click(pipeline, inputs=[audio_in, chatbox], outputs=[chatbox, audio_out, status])
167
 
168
  if __name__ == "__main__":
169
- demo.launch(share=True)
 
68
 
69
 
70
 
71
+ # near top-level (global singletons)
72
+ _SILERO_TTS = None
73
+
74
  def tts_silero(text: str) -> str:
75
  """
76
+ Return path to WAV synthesized by Silero TTS.
77
+ Uses a cached model instance to avoid re-downloading each request.
78
  """
79
  import torch, tempfile
80
  import soundfile as sf
81
 
82
+ global _SILERO_TTS
83
+ if _SILERO_TTS is None:
84
+ obj = torch.hub.load(
85
+ repo_or_dir="snakers4/silero-models",
86
+ model="silero_tts",
87
+ language="en",
88
+ speaker="v3_en",
89
+ trust_repo=True, # avoids interactive trust prompt
90
+ )
91
+ _SILERO_TTS = obj[0] if isinstance(obj, (list, tuple)) else obj
 
92
 
93
+ model = _SILERO_TTS
94
  sample_rate = 48000
95
+ speaker = "en_0"
96
  audio = model.apply_tts(text=text, speaker=speaker, sample_rate=sample_rate)
97
 
98
  out_wav = tempfile.mktemp(suffix=".wav")
 
100
  return out_wav
101
 
102
 
103
+
104
  def tts_coqui_xtts(text: str) -> str:
105
  """
106
  Returns path to a WAV file synthesized by Coqui XTTS v2 (higher quality; GPU-friendly).
 
169
  btn.click(pipeline, inputs=[audio_in, chatbox], outputs=[chatbox, audio_out, status])
170
 
171
  if __name__ == "__main__":
172
+ demo.launch()