Timemaster commited on
Commit
c5e6bf0
·
verified ·
1 Parent(s): 68f106b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
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
- # 1. Input Validation
16
- if not text or not text.strip():
17
- return "ERROR: Input text cannot be empty.", None
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 f"语音合成完成:{text}", tmp_path
31
-
 
 
 
 
 
 
 
32
  except Exception as e:
33
- # Handle all generation errors
34
- return f"ERROR: Failed to generate audio. Details: {str(e)}", None
 
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 ---