Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,37 @@ from tts_voice import tts_order_voice
|
|
| 2 |
import edge_tts
|
| 3 |
import gradio as gr
|
| 4 |
import tempfile
|
| 5 |
-
# Keep anyio imported for use by edge_tts's internal dependencies
|
| 6 |
import anyio
|
| 7 |
|
| 8 |
language_dict = tts_order_voice
|
| 9 |
|
| 10 |
-
#
|
| 11 |
async def text_to_speech_edge(text, language_code):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
input_text = gr.Textbox(lines=5, label="Input Text")
|
| 26 |
output_text = gr.Textbox(label="Output Text")
|
| 27 |
output_audio = gr.Audio(type="filepath", label="Generated Audio File")
|
|
@@ -33,7 +44,7 @@ language = gr.Dropdown(
|
|
| 33 |
)
|
| 34 |
|
| 35 |
|
| 36 |
-
# --- Gradio Interface Definition
|
| 37 |
interface = gr.Interface(
|
| 38 |
fn=text_to_speech_edge,
|
| 39 |
inputs=[input_text, language],
|
|
@@ -43,8 +54,6 @@ interface = gr.Interface(
|
|
| 43 |
)
|
| 44 |
|
| 45 |
|
| 46 |
-
# --- Simplified Launch Command
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
# Gradio will automatically detect the async function and manage the event loop.
|
| 49 |
-
# This is the most reliable way to launch on Hugging Face Spaces.
|
| 50 |
interface.launch()
|
|
|
|
| 2 |
import edge_tts
|
| 3 |
import gradio as gr
|
| 4 |
import tempfile
|
|
|
|
| 5 |
import anyio
|
| 6 |
|
| 7 |
language_dict = tts_order_voice
|
| 8 |
|
| 9 |
+
# --- MODIFIED FUNCTION WITH INPUT VALIDATION ---
|
| 10 |
async def text_to_speech_edge(text, language_code):
|
| 11 |
+
# 1. Input Validation
|
| 12 |
+
if not text or not text.strip():
|
| 13 |
+
# Return a warning message and None for the audio output
|
| 14 |
+
return "ERROR: Input text cannot be empty.", None
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
voice = language_dict[language_code]
|
| 18 |
+
communicate = edge_tts.Communicate(text, voice)
|
| 19 |
+
|
| 20 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 21 |
+
tmp_path = tmp_file.name
|
| 22 |
+
|
| 23 |
+
await communicate.save(tmp_path)
|
| 24 |
+
|
| 25 |
+
return "Speech synthesis complete: {}".format(text), tmp_path
|
| 26 |
+
|
| 27 |
+
except edge_tts.exceptions.NoAudioReceived:
|
| 28 |
+
# Catch the specific error and return a friendly message
|
| 29 |
+
return "ERROR: Failed to generate audio. The TTS service may be unavailable or the voice code is invalid.", None
|
| 30 |
+
except Exception as e:
|
| 31 |
+
# Catch any other unexpected errors
|
| 32 |
+
return f"An unexpected error occurred: {str(e)}", None
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# --- Modern Gradio Component Syntax ---
|
| 36 |
input_text = gr.Textbox(lines=5, label="Input Text")
|
| 37 |
output_text = gr.Textbox(label="Output Text")
|
| 38 |
output_audio = gr.Audio(type="filepath", label="Generated Audio File")
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
|
| 47 |
+
# --- Gradio Interface Definition ---
|
| 48 |
interface = gr.Interface(
|
| 49 |
fn=text_to_speech_edge,
|
| 50 |
inputs=[input_text, language],
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
|
| 57 |
+
# --- Simplified Launch Command ---
|
| 58 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 59 |
interface.launch()
|