Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import tempfile
|
|
| 5 |
import anyio
|
| 6 |
import asyncio
|
| 7 |
|
|
|
|
|
|
|
| 8 |
# --- Voice Data ---
|
| 9 |
# Uses the dictionary loaded from your tts_voice.py file
|
| 10 |
language_dict = tts_order_voice
|
|
@@ -12,26 +14,29 @@ DEFAULT_LANGUAGE = list(language_dict.keys())[0]
|
|
| 12 |
|
| 13 |
# --- Core TTS Function (Asynchronous) ---
|
| 14 |
async def text_to_speech_edge(text, language_code):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
try:
|
| 20 |
-
voice = language_dict[language_code]
|
| 21 |
communicate = edge_tts.Communicate(text, voice)
|
| 22 |
-
|
| 23 |
-
# Create a temporary file path
|
| 24 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 25 |
tmp_path = tmp_file.name
|
| 26 |
-
|
| 27 |
-
# Generate audio file
|
| 28 |
await communicate.save(tmp_path)
|
| 29 |
-
|
| 30 |
-
return
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# --- Gradio UI Definition ---
|
|
|
|
| 5 |
import anyio
|
| 6 |
import asyncio
|
| 7 |
|
| 8 |
+
from edge_tts.exceptions import NoAudioReceived # Import the specific exception
|
| 9 |
+
|
| 10 |
# --- Voice Data ---
|
| 11 |
# Uses the dictionary loaded from your tts_voice.py file
|
| 12 |
language_dict = tts_order_voice
|
|
|
|
| 14 |
|
| 15 |
# --- Core TTS Function (Asynchronous) ---
|
| 16 |
async def text_to_speech_edge(text, language_code):
|
| 17 |
+
voice = language_dict[language_code]
|
| 18 |
+
|
| 19 |
+
# Try block to catch the failure
|
|
|
|
| 20 |
try:
|
|
|
|
| 21 |
communicate = edge_tts.Communicate(text, voice)
|
|
|
|
|
|
|
| 22 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 23 |
tmp_path = tmp_file.name
|
| 24 |
+
|
|
|
|
| 25 |
await communicate.save(tmp_path)
|
| 26 |
+
|
| 27 |
+
return "语音合成完成:{}".format(text), tmp_path
|
| 28 |
+
|
| 29 |
+
except NoAudioReceived as e:
|
| 30 |
+
error_msg = f"ERROR: Failed to generate audio for voice '{voice}'. The Edge TTS service did not return any audio data. Please try a different voice, use shorter text, or upgrade the 'edge-tts' library. Details: {str(e)}"
|
| 31 |
+
print(error_msg)
|
| 32 |
+
|
| 33 |
+
# Return the error message and None for audio
|
| 34 |
+
return error_msg, None
|
| 35 |
+
|
| 36 |
except Exception as e:
|
| 37 |
+
error_msg = f"An unexpected error occurred: {str(e)}"
|
| 38 |
+
print(error_msg)
|
| 39 |
+
return error_msg, None
|
| 40 |
|
| 41 |
|
| 42 |
# --- Gradio UI Definition ---
|