SAD43W commited on
Commit
10f692e
·
verified ·
1 Parent(s): f868edd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -3,16 +3,22 @@ import torch
3
  import gradio as gr
4
  from TTS.api import TTS
5
 
6
- # Accept license
7
  os.environ["COQUI_TOS_AGREED"] = "1"
8
 
9
  # Load model
10
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
11
  tts.to("cuda" if torch.cuda.is_available() else "cpu")
12
 
 
 
 
 
 
 
 
13
  def generate(text, lang, speaker_wav=None):
14
  out_path = "output.wav"
15
-
16
  if speaker_wav:
17
  tts.tts_to_file(
18
  text=text,
@@ -21,17 +27,15 @@ def generate(text, lang, speaker_wav=None):
21
  file_path=out_path
22
  )
23
  else:
24
- # use default speaker
25
  tts.tts_to_file(
26
  text=text,
27
- speaker="default", # 🧠 THIS LINE FIXES THE ERROR
28
  language=lang,
29
  file_path=out_path
30
  )
31
-
32
  return out_path
33
 
34
-
35
  demo = gr.Interface(
36
  fn=generate,
37
  inputs=[
 
3
  import gradio as gr
4
  from TTS.api import TTS
5
 
6
+ # Auto accept Coqui license (no input prompt)
7
  os.environ["COQUI_TOS_AGREED"] = "1"
8
 
9
  # Load model
10
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
11
  tts.to("cuda" if torch.cuda.is_available() else "cpu")
12
 
13
+ # Grab available speakers once for fallback
14
+ available_speakers = list(tts.synthesizer.speaker_manager.speakers.keys())
15
+ if not available_speakers:
16
+ raise RuntimeError("No speakers found in model! Abort.")
17
+
18
+ default_speaker = available_speakers[0] # pick first speaker ID for fallback
19
+
20
  def generate(text, lang, speaker_wav=None):
21
  out_path = "output.wav"
 
22
  if speaker_wav:
23
  tts.tts_to_file(
24
  text=text,
 
27
  file_path=out_path
28
  )
29
  else:
30
+ # Use fallback speaker ID — no more 'default' crashes
31
  tts.tts_to_file(
32
  text=text,
33
+ speaker=default_speaker,
34
  language=lang,
35
  file_path=out_path
36
  )
 
37
  return out_path
38
 
 
39
  demo = gr.Interface(
40
  fn=generate,
41
  inputs=[