Timemaster commited on
Commit
b0f6a83
·
verified ·
1 Parent(s): 4ed7f20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -20
app.py CHANGED
@@ -1,49 +1,70 @@
1
- from tts_voice import tts_order_voice
2
- import edge_tts
3
  import gradio as gr
4
  import tempfile
5
- import anyio
 
 
 
6
 
7
  language_dict = tts_order_voice
8
 
 
 
 
9
  async def text_to_speech_edge(text, language_code):
10
- voice = language_dict[language_code]
11
- communicate = edge_tts.Communicate(text, voice)
12
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
13
- tmp_path = tmp_file.name
14
 
15
- await communicate.save(tmp_path)
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Translated output message
18
- return "Speech synthesis complete: {}".format(text), tmp_path
19
 
 
 
 
 
 
 
 
 
20
 
21
- # --- UI TRANSLATION AND MODERN GRADIO SYNTAX ---
22
- # Input Component: Textbox label translated
23
  input_text = gr.Textbox(lines=5, label="Input Text")
24
- # Output Component: Textbox label translated
25
  output_text = gr.Textbox(label="Output Text")
26
- # Output Component: Audio label translated
27
  output_audio = gr.Audio(type="filepath", label="Generated Audio File")
28
-
29
  default_language = list(language_dict.keys())[0]
30
-
31
- # Input Component: Dropdown label translated
32
  language = gr.Dropdown(
33
  choices=list(language_dict.keys()),
34
  value=default_language,
35
  label="Language / Voice"
36
  )
37
 
38
- # Interface Translation: Title and Description translated
 
39
  interface = gr.Interface(
40
  fn=text_to_speech_edge,
41
  inputs=[input_text, language],
42
  outputs=[output_text, output_audio],
43
- title="Edge TTS Text-to-Speech", # Translated title
44
- description="Convert text into high-quality audio using various Edge TTS voices." # Added description in English
45
  )
46
 
47
 
 
48
  if __name__ == "__main__":
49
  interface.launch()
 
1
+ # WARNING: This file assumes you have a tts_voice.py file with English-translated keys.
2
+
3
  import gradio as gr
4
  import tempfile
5
+ import edge_tts
6
+ import anyio
7
+ # Import the translated voice mapping (assuming you kept it)
8
+ from tts_voice import tts_order_voice
9
 
10
  language_dict = tts_order_voice
11
 
12
+ # HARDCODED ENDPOINT: This is the fix to bypass connection instability
13
+ VOICE_SERVICE_ENDPOINT = "wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/v1"
14
+
15
  async def text_to_speech_edge(text, language_code):
16
+ # Input Validation (kept from previous successful step)
17
+ if not text or not text.strip():
18
+ return "ERROR: Input text cannot be empty. Please enter text.", None
 
19
 
20
+ try:
21
+ voice = language_dict[language_code]
22
+
23
+ # Initialize Communicate with the specific endpoint
24
+ communicate = edge_tts.Communicate(
25
+ text,
26
+ voice,
27
+ # FORCED FIX: Use a specific, known WebSocket endpoint
28
+ websocket_url=VOICE_SERVICE_ENDPOINT
29
+ )
30
+
31
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
32
+ tmp_path = tmp_file.name
33
 
34
+ await communicate.save(tmp_path)
 
35
 
36
+ return "Speech synthesis complete: {}".format(text), tmp_path
37
+
38
+ except edge_tts.exceptions.NoAudioReceived:
39
+ # Catch the specific error with a clear message
40
+ return "ERROR: Failed to generate audio. The TTS service is rejecting the request. Check your text length or try a different voice.", None
41
+ except Exception as e:
42
+ # Catch any other unexpected errors
43
+ return f"An unexpected error occurred: {str(e)}", None
44
 
45
+
46
+ # --- Modern Gradio Component Syntax ---
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
  default_language = list(language_dict.keys())[0]
 
 
51
  language = gr.Dropdown(
52
  choices=list(language_dict.keys()),
53
  value=default_language,
54
  label="Language / Voice"
55
  )
56
 
57
+
58
+ # --- Gradio Interface Definition ---
59
  interface = gr.Interface(
60
  fn=text_to_speech_edge,
61
  inputs=[input_text, language],
62
  outputs=[output_text, output_audio],
63
+ title="Edge TTS Text-to-Speech",
64
+ description="Convert text into high-quality audio using various Edge TTS voices."
65
  )
66
 
67
 
68
+ # --- Standard Launch Command ---
69
  if __name__ == "__main__":
70
  interface.launch()