Spaces:
Sleeping
Sleeping
Commit ·
73a7287
1
Parent(s): 71558a8
Fix 503 crash: Add TOS agreement and auto-download sample voice
Browse files
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
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Generate
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 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(
|