Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
# 1. Input Validation
|
|
|
|
|
|
|
| 22 |
if not text or not text.strip():
|
| 23 |
return "ERROR: Input text cannot be empty.", None
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
# Get the language code from the name
|
| 27 |
-
lang_code = gtts_languages[language_name]
|
| 28 |
-
|
| 29 |
# Create a temporary file path
|
| 30 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".
|
| 31 |
tmp_path = tmp_file.name
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
return "Speech synthesis complete: {}".format(text), tmp_path
|
| 40 |
-
|
| 41 |
except Exception as e:
|
| 42 |
-
# Handle all gTTS-related errors (e.g., language not supported, network failure)
|
| 43 |
return f"ERROR: Failed to generate audio. Details: {str(e)}", None
|
| 44 |
|
| 45 |
|
| 46 |
-
# ---
|
| 47 |
input_text = gr.Textbox(lines=5, label="Input Text")
|
| 48 |
output_text = gr.Textbox(label="Output Text")
|
| 49 |
output_audio = gr.Audio(type="filepath", label="Generated Audio File")
|
| 50 |
|
| 51 |
-
|
| 52 |
language = gr.Dropdown(
|
| 53 |
-
choices=
|
| 54 |
-
value=
|
| 55 |
-
label="
|
| 56 |
)
|
| 57 |
|
| 58 |
|
| 59 |
# --- Gradio Interface Definition ---
|
| 60 |
interface = gr.Interface(
|
| 61 |
-
fn=
|
| 62 |
-
inputs=[input_text, language],
|
| 63 |
-
outputs=[output_text, output_audio],
|
| 64 |
-
title="
|
| 65 |
-
description="
|
| 66 |
)
|
| 67 |
|
| 68 |
|
| 69 |
-
# --- Standard
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
# Gradio runs synchronous functions reliably without extra configuration.
|
| 72 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# --- Model Loading (Runs only once at startup) ---
|
| 7 |
+
# NOTE: This model is chosen for its balance of quality and CPU compatibility.
|
| 8 |
+
# You can try other models from the Coqui TTS documentation if you need more languages.
|
| 9 |
+
try:
|
| 10 |
+
# Initialize TTS with the chosen model (VITS model for CPU efficiency)
|
| 11 |
+
tts_model = TTS(model_name="tts_models/en/ljspeech/vits", progress_bar=False).to("cpu")
|
| 12 |
+
VOICES = ["ljspeech"] # VITS models often have a single trained speaker
|
| 13 |
+
DEFAULT_VOICE = VOICES[0]
|
| 14 |
+
except Exception as e:
|
| 15 |
+
tts_model = None
|
| 16 |
+
VOICES = ["Model Load Error"]
|
| 17 |
+
DEFAULT_VOICE = VOICES[0]
|
| 18 |
+
print(f"Error loading TTS model: {e}")
|
| 19 |
|
| 20 |
+
|
| 21 |
+
# --- Core TTS Function (Synchronous) ---
|
| 22 |
+
def text_to_speech_coqui(text, speaker_name):
|
| 23 |
# 1. Input Validation
|
| 24 |
+
if not tts_model:
|
| 25 |
+
return "ERROR: TTS Model failed to load at startup. Check Space logs.", None
|
| 26 |
if not text or not text.strip():
|
| 27 |
return "ERROR: Input text cannot be empty.", None
|
| 28 |
|
| 29 |
try:
|
|
|
|
|
|
|
|
|
|
| 30 |
# Create a temporary file path
|
| 31 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 32 |
tmp_path = tmp_file.name
|
| 33 |
|
| 34 |
+
# Generate audio file (Coqui TTS method)
|
| 35 |
+
# We pass the speaker_name even though this VITS model only has one.
|
| 36 |
+
tts_model.tts_to_file(
|
| 37 |
+
text=text,
|
| 38 |
+
speaker=speaker_name,
|
| 39 |
+
file_path=tmp_path
|
| 40 |
+
)
|
| 41 |
|
| 42 |
return "Speech synthesis complete: {}".format(text), tmp_path
|
| 43 |
+
|
| 44 |
except Exception as e:
|
|
|
|
| 45 |
return f"ERROR: Failed to generate audio. Details: {str(e)}", None
|
| 46 |
|
| 47 |
|
| 48 |
+
# --- Gradio UI Definition ---
|
| 49 |
input_text = gr.Textbox(lines=5, label="Input Text")
|
| 50 |
output_text = gr.Textbox(label="Output Text")
|
| 51 |
output_audio = gr.Audio(type="filepath", label="Generated Audio File")
|
| 52 |
|
| 53 |
+
# Dropdown uses the detected voices (or the error message)
|
| 54 |
language = gr.Dropdown(
|
| 55 |
+
choices=VOICES,
|
| 56 |
+
value=DEFAULT_VOICE,
|
| 57 |
+
label="Speaker/Voice"
|
| 58 |
)
|
| 59 |
|
| 60 |
|
| 61 |
# --- Gradio Interface Definition ---
|
| 62 |
interface = gr.Interface(
|
| 63 |
+
fn=text_to_speech_coqui,
|
| 64 |
+
inputs=[input_text, language],
|
| 65 |
+
outputs=[output_text, output_audio],
|
| 66 |
+
title="Coqui TTS (CPU Optimized)",
|
| 67 |
+
description="Customizable, high-quality Text-to-Speech running on CPU."
|
| 68 |
)
|
| 69 |
|
| 70 |
|
| 71 |
+
# --- Standard Launch Command ---
|
| 72 |
if __name__ == "__main__":
|
|
|
|
| 73 |
interface.launch()
|