teamseven commited on
Commit
6fa18df
Β·
verified Β·
1 Parent(s): 17210f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -113
app.py CHANGED
@@ -1,146 +1,101 @@
1
  import os
2
- import asyncio
3
  import tempfile
4
  import logging
5
 
6
  import gradio as gr
7
- import edge_tts
8
 
9
- logging.basicConfig(
10
- level=logging.INFO,
11
- format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
12
- )
13
 
14
  logger = logging.getLogger("edge-tts-bridge")
15
 
16
  APP_TITLE = "Edge TTS Bridge"
17
 
 
 
18
  VOICES = {
19
  "Myanmar Female": "my-MM-NilarNeural",
20
  "Myanmar Male": "my-MM-ThihaNeural",
21
  }
22
 
23
 
24
- async def synthesize(text: str, voice: str) -> str:
25
- """Generate speech using Microsoft Edge TTS."""
 
 
 
 
 
 
 
 
 
26
 
27
- with tempfile.NamedTemporaryFile(
28
- suffix=".mp3",
29
- delete=False,
30
- ) as temp:
31
- output_path = temp.name
32
 
33
  try:
34
  logger.info(
35
- "TTS request started | voice=%s | characters=%d",
36
  voice,
37
  len(text),
38
  )
39
 
40
- communicate = edge_tts.Communicate(
41
- text=text,
42
- voice=voice,
 
 
 
 
43
  )
44
 
45
- await communicate.save(output_path)
46
-
47
- if not os.path.exists(output_path):
48
- raise RuntimeError("Audio file was not created.")
49
-
50
- file_size = os.path.getsize(output_path)
51
-
52
- if file_size < 100:
53
  raise RuntimeError(
54
- f"Audio file is unexpectedly small: {file_size} bytes"
 
55
  )
56
 
57
- logger.info(
58
- "TTS request succeeded | voice=%s | file_size=%d",
59
- voice,
60
- file_size,
61
- )
62
 
63
- return output_path
 
64
 
65
- except Exception as exc:
66
- logger.exception(
67
- "TTS synthesis failed | voice=%s | "
68
- "error_type=%s | error=%r",
69
- voice,
70
- type(exc).__name__,
71
- exc,
72
- )
73
-
74
- try:
75
- os.remove(output_path)
76
- except OSError:
77
- pass
78
-
79
- raise
80
-
81
-
82
- def generate_audio(text, voice_name):
83
- """Gradio interface handler."""
84
 
85
- text = (text or "").strip()
86
-
87
- if not text:
88
- raise gr.Error("Please enter text.")
89
-
90
- if len(text) > 3000:
91
- raise gr.Error(
92
- "Maximum 3000 characters per request."
93
  )
94
 
95
- if voice_name not in VOICES:
96
- raise gr.Error("Unsupported voice.")
97
-
98
- voice = VOICES[voice_name]
99
-
100
- try:
101
- return asyncio.run(
102
- synthesize(text, voice)
103
- )
104
 
105
  except Exception as exc:
106
- logger.exception(
107
- "Edge TTS handler failed | type=%s",
108
- type(exc).__name__,
109
- )
110
-
111
- message = str(exc).strip() or repr(exc)
112
 
113
  raise gr.Error(
114
- f"Edge TTS failed ({type(exc).__name__}): "
115
- f"{message[:500]}"
116
  )
117
 
118
 
119
  with gr.Blocks(title=APP_TITLE) as demo:
120
- gr.Markdown(
121
- """
122
- # Edge TTS Bridge
123
-
124
- Test Microsoft Edge TTS from Hugging Face Spaces.
125
 
126
- This service requires an active connection to
127
- Microsoft's speech service.
128
- """
 
129
  )
130
 
131
- with gr.Row():
132
- text_input = gr.Textbox(
133
- label="Text",
134
- placeholder="α€™α€„α€Ία€Ήα€‚α€œα€¬α€•α€«αŠ α€‘α€žα€Άα€…α€™α€Ία€Έα€žα€•α€Ία€”α€±α€•α€«α€α€šα€Ία‹",
135
- lines=4,
136
- max_lines=10,
137
- )
138
-
139
- voice_input = gr.Dropdown(
140
- choices=list(VOICES.keys()),
141
- value="Myanmar Female",
142
- label="Voice",
143
- )
144
 
145
  generate_button = gr.Button(
146
  "Generate Audio",
@@ -154,25 +109,14 @@ with gr.Blocks(title=APP_TITLE) as demo:
154
 
155
  generate_button.click(
156
  fn=generate_audio,
157
- inputs=[
158
- text_input,
159
- voice_input,
160
- ],
161
  outputs=audio_output,
162
  api_name="generate",
163
  )
164
 
165
- gr.Markdown(
166
- """
167
- ## Test instructions
168
-
169
- 1. Enter a short Myanmar sentence.
170
- 2. Select a voice.
171
- 3. Click Generate Audio.
172
- 4. Check whether the MP3 is playable.
173
- """
174
- )
175
-
176
 
177
  if __name__ == "__main__":
178
- demo.queue(max_size=5).launch()
 
 
 
 
1
  import os
 
2
  import tempfile
3
  import logging
4
 
5
  import gradio as gr
6
+ import requests
7
 
8
+ logging.basicConfig(level=logging.INFO)
 
 
 
9
 
10
  logger = logging.getLogger("edge-tts-bridge")
11
 
12
  APP_TITLE = "Edge TTS Bridge"
13
 
14
+ GO_TTS_URL = "http://127.0.0.1:8080/v1/synthesize"
15
+
16
  VOICES = {
17
  "Myanmar Female": "my-MM-NilarNeural",
18
  "Myanmar Male": "my-MM-ThihaNeural",
19
  }
20
 
21
 
22
+ def generate_audio(text, voice_name):
23
+ text = (text or "").strip()
24
+
25
+ if not text:
26
+ raise gr.Error("Please enter text.")
27
+
28
+ if len(text) > 3000:
29
+ raise gr.Error("Maximum 3000 characters per request.")
30
+
31
+ if voice_name not in VOICES:
32
+ raise gr.Error("Unsupported voice.")
33
 
34
+ voice = VOICES[voice_name]
 
 
 
 
35
 
36
  try:
37
  logger.info(
38
+ "Generating audio: voice=%s, characters=%d",
39
  voice,
40
  len(text),
41
  )
42
 
43
+ response = requests.post(
44
+ GO_TTS_URL,
45
+ json={
46
+ "text": text,
47
+ "voice": voice,
48
+ },
49
+ timeout=120,
50
  )
51
 
52
+ if response.status_code != 200:
 
 
 
 
 
 
 
53
  raise RuntimeError(
54
+ f"Go TTS HTTP {response.status_code}: "
55
+ f"{response.text[:400]}"
56
  )
57
 
58
+ audio_data = response.content
 
 
 
 
59
 
60
+ if len(audio_data) < 100:
61
+ raise RuntimeError("Audio response is too small.")
62
 
63
+ with tempfile.NamedTemporaryFile(
64
+ suffix=".mp3",
65
+ delete=False,
66
+ ) as temp:
67
+ temp.write(audio_data)
68
+ output_path = temp.name
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ logger.info(
71
+ "Audio generated successfully: %d bytes",
72
+ len(audio_data),
 
 
 
 
 
73
  )
74
 
75
+ return output_path
 
 
 
 
 
 
 
 
76
 
77
  except Exception as exc:
78
+ logger.exception("TTS failed")
 
 
 
 
 
79
 
80
  raise gr.Error(
81
+ f"TTS failed ({type(exc).__name__}): {str(exc)[:400]}"
 
82
  )
83
 
84
 
85
  with gr.Blocks(title=APP_TITLE) as demo:
86
+ gr.Markdown("# Edge TTS Bridge β€” Go Test")
 
 
 
 
87
 
88
+ text_input = gr.Textbox(
89
+ label="Text",
90
+ placeholder="α€™α€„α€Ία€Ήα€‚α€œα€¬α€•α€«αŠ α€‘α€žα€Άα€…α€™α€Ία€Έα€žα€•α€Ία€”α€±α€•α€«α€α€šα€Ία‹",
91
+ lines=4,
92
  )
93
 
94
+ voice_input = gr.Dropdown(
95
+ choices=list(VOICES.keys()),
96
+ value="Myanmar Female",
97
+ label="Voice",
98
+ )
 
 
 
 
 
 
 
 
99
 
100
  generate_button = gr.Button(
101
  "Generate Audio",
 
109
 
110
  generate_button.click(
111
  fn=generate_audio,
112
+ inputs=[text_input, voice_input],
 
 
 
113
  outputs=audio_output,
114
  api_name="generate",
115
  )
116
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  if __name__ == "__main__":
119
+ demo.queue(max_size=5).launch(
120
+ server_name="0.0.0.0",
121
+ server_port=7860,
122
+ )