Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,58 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import base64
|
| 3 |
from Zonos_main.tts import ZonosTTS
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def generate_audio(text):
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
except Exception as e:
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
gr.Interface(
|
| 21 |
fn=generate_audio,
|
| 22 |
-
inputs=gr.Textbox(label="Input"),
|
| 23 |
-
outputs=gr.Audio(type="filepath"),
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import base64
|
| 4 |
from Zonos_main.tts import ZonosTTS
|
| 5 |
|
| 6 |
+
# Force CPU mode to avoid CUDA issues
|
| 7 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 8 |
+
|
| 9 |
+
def load_model():
|
| 10 |
+
try:
|
| 11 |
+
tts = ZonosTTS()
|
| 12 |
+
print("✅ TTS model loaded successfully")
|
| 13 |
+
return tts
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"❌ Model loading failed: {str(e)}")
|
| 16 |
+
raise RuntimeError("Could not initialize TTS model") from e
|
| 17 |
+
|
| 18 |
+
tts = load_model()
|
| 19 |
|
| 20 |
def generate_audio(text):
|
| 21 |
try:
|
| 22 |
+
if not text.strip():
|
| 23 |
+
raise ValueError("Input text cannot be empty")
|
| 24 |
+
|
| 25 |
+
# Generate audio (must return raw bytes)
|
| 26 |
+
audio_bytes = tts.generate(text)
|
| 27 |
|
| 28 |
+
if not isinstance(audio_bytes, bytes):
|
| 29 |
+
raise TypeError("Model must return bytes")
|
| 30 |
+
|
| 31 |
+
if len(audio_bytes) < 100: # Minimum valid audio size
|
| 32 |
+
raise ValueError("Generated audio too small")
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"name": "output.wav",
|
| 36 |
+
"data": f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode()}",
|
| 37 |
+
"size": len(audio_bytes)
|
| 38 |
+
}
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
+
print(f"❌ Generation error: {str(e)}")
|
| 42 |
+
raise gr.Error(f"Audio generation failed: {str(e)}") from e
|
| 43 |
|
| 44 |
+
# Robust Gradio interface
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
fn=generate_audio,
|
| 47 |
+
inputs=gr.Textbox(label="Input Text", placeholder="Type something..."),
|
| 48 |
+
outputs=gr.Audio(label="Output", type="filepath"),
|
| 49 |
+
title="Zonos TTS",
|
| 50 |
+
allow_flagging="never"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Launch with error handling
|
| 54 |
+
try:
|
| 55 |
+
demo.launch(show_error=True)
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"❌ Gradio launch failed: {str(e)}")
|
| 58 |
+
raise
|