|
import os |
|
import gradio as gr |
|
import base64 |
|
from Zonos_main.tts import ZonosTTS |
|
|
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" |
|
|
|
def load_model(): |
|
try: |
|
tts = ZonosTTS() |
|
print("β
TTS model loaded successfully") |
|
return tts |
|
except Exception as e: |
|
print(f"β Model loading failed: {str(e)}") |
|
raise RuntimeError("Could not initialize TTS model") from e |
|
|
|
tts = load_model() |
|
|
|
def generate_audio(text): |
|
try: |
|
if not text.strip(): |
|
raise ValueError("Input text cannot be empty") |
|
|
|
|
|
audio_bytes = tts.generate(text) |
|
|
|
if not isinstance(audio_bytes, bytes): |
|
raise TypeError("Model must return bytes") |
|
|
|
if len(audio_bytes) < 100: |
|
raise ValueError("Generated audio too small") |
|
|
|
return { |
|
"name": "output.wav", |
|
"data": f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode()}", |
|
"size": len(audio_bytes) |
|
} |
|
|
|
except Exception as e: |
|
print(f"β Generation error: {str(e)}") |
|
raise gr.Error(f"Audio generation failed: {str(e)}") from e |
|
|
|
|
|
demo = gr.Interface( |
|
fn=generate_audio, |
|
inputs=gr.Textbox(label="Input Text", placeholder="Type something..."), |
|
outputs=gr.Audio(label="Output", type="filepath"), |
|
title="Zonos TTS", |
|
allow_flagging="never" |
|
) |
|
|
|
|
|
try: |
|
demo.launch(show_error=True) |
|
except Exception as e: |
|
print(f"β Gradio launch failed: {str(e)}") |
|
raise |