BenjaminPittsley commited on
Commit
73a7287
·
1 Parent(s): 71558a8

Fix 503 crash: Add TOS agreement and auto-download sample voice

Browse files
Files changed (1) hide show
  1. app.py +32 -12
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import gradio as gr
2
  from TTS.api import TTS
3
  import os
 
 
 
 
4
 
5
  # Initialize TTS (XTTS-v2)
6
  # This will download the model on the first run
@@ -18,6 +22,17 @@ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=use_gpu)
18
 
19
  # Output folder
20
  os.makedirs("output", exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def generate_speech(text, voice_id):
23
  """
@@ -31,20 +46,25 @@ def generate_speech(text, voice_id):
31
  speaker_wav = f"voices/{voice_id}.wav"
32
 
33
  if not os.path.exists(speaker_wav):
34
- # Fallback to a default voice if specific one missing, or error out
35
- # For now, let's list available voices to help debug
36
- available = os.listdir("voices") if os.path.exists("voices") else []
37
- return None, f"Error: Voice ID '{voice_id}' not found. Available: {available}"
 
 
 
38
 
39
  # Generate
40
- tts.tts_to_file(
41
- text=text,
42
- file_path=output_path,
43
- speaker_wav=speaker_wav,
44
- language="en"
45
- )
46
-
47
- return output_path, "Success"
 
 
48
 
49
  # Define Gradio Interface
50
  iface = gr.Interface(
 
1
  import gradio as gr
2
  from TTS.api import TTS
3
  import os
4
+ import urllib.request
5
+
6
+ # Agree to Coqui TOS
7
+ os.environ["COQUI_TOS_AGREED"] = "1"
8
 
9
  # Initialize TTS (XTTS-v2)
10
  # This will download the model on the first run
 
22
 
23
  # Output folder
24
  os.makedirs("output", exist_ok=True)
25
+ os.makedirs("voices", exist_ok=True)
26
+
27
+ # Ensure at least one voice exists
28
+ if not os.listdir("voices"):
29
+ print("No voices found. Downloading sample...")
30
+ try:
31
+ # Download a sample female voice
32
+ urllib.request.urlretrieve("https://huggingface.co/spaces/coqui/xtts/resolve/main/examples/female.wav", "voices/female_calm.wav")
33
+ print("Downloaded female_calm.wav")
34
+ except Exception as e:
35
+ print(f"Failed to download sample voice: {e}")
36
 
37
  def generate_speech(text, voice_id):
38
  """
 
46
  speaker_wav = f"voices/{voice_id}.wav"
47
 
48
  if not os.path.exists(speaker_wav):
49
+ # Fallback to the first available voice if specific one missing
50
+ available = os.listdir("voices")
51
+ if available:
52
+ print(f"Voice '{voice_id}' not found. Falling back to '{available[0]}'")
53
+ speaker_wav = f"voices/{available[0]}"
54
+ else:
55
+ return None, f"Error: Voice ID '{voice_id}' not found and no voices available."
56
 
57
  # Generate
58
+ try:
59
+ tts.tts_to_file(
60
+ text=text,
61
+ file_path=output_path,
62
+ speaker_wav=speaker_wav,
63
+ language="en"
64
+ )
65
+ return output_path, "Success"
66
+ except Exception as e:
67
+ return None, f"Error generating speech: {str(e)}"
68
 
69
  # Define Gradio Interface
70
  iface = gr.Interface(