teamseven commited on
Commit
97cdefd
ยท
verified ยท
1 Parent(s): 1687620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -46
app.py CHANGED
@@ -1,55 +1,139 @@
 
 
 
 
 
1
  import gradio as gr
2
  import edge_tts
3
- import asyncio
4
- import tempfile
5
 
6
- VOICES = [
7
- "my-MM-NilarNeural",
8
- "my-MM-ThihaNeural",
9
- "en-US-AriaNeural",
10
- "en-US-GuyNeural"
11
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- async def generate_tts(text, voice, proxy):
14
- if not text:
15
- return None, "แ€€แ€ปแ€ฑแ€ธแ€‡แ€ฐแ€ธแ€•แ€ผแ€ฏแ แ€…แ€ฌแ€žแ€ฌแ€ธแ€‘แ€Šแ€ทแ€บแ€•แ€ซแ‹"
16
  try:
17
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
18
- temp_file.close()
19
-
20
- proxy_url = proxy if proxy and proxy.strip() != "" else None
21
-
22
- # Proxy แ€™แ€พแ€ฌ Username/Password แ€•แ€ซแ€œแ€ฌแ€›แ€„แ€บ aiohttp/edge-tts แ€€ แ€’แ€ฎแ€กแ€แ€ญแ€ฏแ€„แ€บแ€ธ แ€œแ€€แ€บแ€แ€ถแ€”แ€ญแ€ฏแ€„แ€บแ€•แ€ซแ€แ€šแ€บ
23
- communicate = edge_tts.Communicate(text, voice, proxy=proxy_url)
24
- await communicate.save(temp_file.name)
25
- return temp_file.name, "แ€กแ€ฑแ€ฌแ€„แ€บแ€™แ€ผแ€„แ€บแ€•แ€ซแ€žแ€Šแ€บแ‹"
26
- except Exception as e:
27
- return None, f"Error: {str(e)}"
28
-
29
- def tts_interface(text, voice, proxy):
30
- return asyncio.run(generate_tts(text, voice, proxy))
31
-
32
- with gr.Blocks() as app:
33
- gr.Markdown("## Edge TTS with Proxy Support")
34
- with gr.Row():
35
- with gr.Column():
36
- text_input = gr.Textbox(label="แ€…แ€ฌแ€žแ€ฌแ€ธแ€‘แ€Šแ€ทแ€บแ€•แ€ซ", lines=5)
37
- voice_dropdown = gr.Dropdown(choices=VOICES, value="my-MM-NilarNeural", label="แ€กแ€žแ€ถแ€›แ€ฝแ€ฑแ€ธแ€แ€ปแ€šแ€บแ€•แ€ซ")
38
- proxy_input = gr.Textbox(
39
- label="Proxy URL (Optional)",
40
- value="http://mhuekxym:29u66euxw5ww@p.webshare.io:80",
41
- placeholder="http://user:pass@ip:port"
42
  )
43
- submit_btn = gr.Button("แ€กแ€žแ€ถแ€–แ€”แ€บแ€แ€ฎแ€ธแ€™แ€Šแ€บ", variant="primary")
44
- with gr.Column():
45
- audio_output = gr.Audio(label="แ€‘แ€ฝแ€€แ€บแ€•แ€ฑแ€ซแ€บแ€œแ€ฌแ€žแ€ฑแ€ฌแ€กแ€žแ€ถ", type="filepath")
46
- status_output = gr.Textbox(label="แ€กแ€แ€ผแ€ฑแ€กแ€”แ€ฑ")
47
-
48
- submit_btn.click(
49
- fn=tts_interface,
50
- inputs=[text_input, voice_dropdown, proxy_input],
51
- outputs=[audio_output, status_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  )
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  if __name__ == "__main__":
55
- app.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
1
+ import os
2
+ import tempfile
3
+ import logging
4
+ import asyncio
5
+
6
  import gradio as gr
7
  import edge_tts
8
+ import edge_tts.communicate as communicate_module
 
9
 
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger("MyanmarEdgeTTS")
12
+
13
+ APP_TITLE = "Myanmar Edge TTS"
14
+
15
+ # New Microsoft Edge Read Aloud endpoint
16
+ NEW_WSS_URL = (
17
+ "wss://api.msedgeservices.com/"
18
+ "tts/cognitiveservices/websocket/v1"
19
+ "?TrustedClientToken=6A5AA1D4EAFF4E9FB37E23D68491D6F4"
20
+ )
21
+
22
+ # Set endpoint in the module used by Communicate
23
+ communicate_module.WSS_URL = NEW_WSS_URL
24
+
25
+ VOICES = {
26
+ "Myanmar Female โ€” Nilar": "my-MM-NilarNeural",
27
+ "Myanmar Male โ€” Thiha": "my-MM-ThihaNeural",
28
+ }
29
+
30
+
31
+ async def create_audio(text, voice):
32
+ communicate = edge_tts.Communicate(
33
+ text=text,
34
+ voice=voice,
35
+ )
36
+
37
+ with tempfile.NamedTemporaryFile(
38
+ suffix=".mp3",
39
+ delete=False,
40
+ ) as temp:
41
+ output_path = temp.name
42
 
 
 
 
43
  try:
44
+ await communicate.save(output_path)
45
+
46
+ if (
47
+ not os.path.exists(output_path)
48
+ or os.path.getsize(output_path) < 100
49
+ ):
50
+ raise RuntimeError(
51
+ "Audio file is empty. Microsoft TTS returned no audio."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  )
53
+
54
+ logger.info(
55
+ "Audio generated: voice=%s size=%d",
56
+ voice,
57
+ os.path.getsize(output_path),
58
+ )
59
+
60
+ return output_path
61
+
62
+ except Exception:
63
+ if os.path.exists(output_path):
64
+ os.remove(output_path)
65
+
66
+ logger.exception("TTS generation failed")
67
+ raise
68
+
69
+
70
+ def generate_audio(text, voice_name):
71
+ text = (text or "").strip()
72
+
73
+ if not text:
74
+ raise gr.Error("แ€™แ€ผแ€”แ€บแ€™แ€ฌแ€…แ€ฌแ€แ€…แ€บแ€แ€ฏแ€แ€ฏ แ€กแ€›แ€„แ€บแ€‘แ€Šแ€ทแ€บแ€•แ€ซแ‹")
75
+
76
+ if len(text) > 2000:
77
+ raise gr.Error("แ€…แ€ฌแ€œแ€ฏแ€ถแ€ธแ€›แ€ฑ แ‚แ€แ€แ€ แ€‘แ€€แ€บ แ€™แ€•แ€ญแ€ฏแ€›แ€•แ€ซแ‹")
78
+
79
+ if voice_name not in VOICES:
80
+ raise gr.Error("Voice แ€™แ€™แ€พแ€”แ€บแ€•แ€ซแ‹")
81
+
82
+ voice = VOICES[voice_name]
83
+
84
+ try:
85
+ return asyncio.run(create_audio(text, voice))
86
+
87
+ except Exception as exc:
88
+ logger.exception("Generation failed")
89
+
90
+ raise gr.Error(
91
+ f"TTS Error: {type(exc).__name__}: {str(exc)[:400]}"
92
+ )
93
+
94
+
95
+ with gr.Blocks(title=APP_TITLE) as demo:
96
+ gr.Markdown(
97
+ """
98
+ # ๐Ÿ‡ฒ๐Ÿ‡ฒ Myanmar Edge TTS
99
+
100
+ Microsoft Edge Read Aloud endpoint แ€กแ€žแ€…แ€บแ€–แ€ผแ€„แ€ทแ€บ
101
+ แ€™แ€ผแ€”แ€บแ€™แ€ฌแ€กแ€žแ€ถแ€‘แ€ฏแ€แ€บแ€…แ€™แ€บแ€ธแ€žแ€•แ€บแ€›แ€”แ€บแ‹
102
+ """
103
  )
104
 
105
+ text_input = gr.Textbox(
106
+ label="แ€™แ€ผแ€”แ€บแ€™แ€ฌแ€…แ€ฌ",
107
+ placeholder="แ€™แ€„แ€บแ€นแ€‚แ€œแ€ฌแ€•แ€ซแ‹ แ€กแ€žแ€ถแ€…แ€™แ€บแ€ธแ€žแ€•แ€บแ€”แ€ฑแ€•แ€ซแ€แ€šแ€บแ‹",
108
+ lines=4,
109
+ )
110
+
111
+ voice_input = gr.Dropdown(
112
+ choices=list(VOICES.keys()),
113
+ value="Myanmar Female โ€” Nilar",
114
+ label="แ€กแ€žแ€ถแ€›แ€ฝแ€ฑแ€ธแ€•แ€ซ",
115
+ )
116
+
117
+ generate_button = gr.Button(
118
+ "แ€กแ€žแ€ถแ€‘แ€ฏแ€แ€บแ€™แ€šแ€บ",
119
+ variant="primary",
120
+ )
121
+
122
+ audio_output = gr.Audio(
123
+ label="แ€‘แ€ฏแ€แ€บแ€œแ€ฏแ€•แ€บแ€•แ€ผแ€ฎแ€ธแ€žแ€ฑแ€ฌแ€กแ€žแ€ถ",
124
+ type="filepath",
125
+ )
126
+
127
+ generate_button.click(
128
+ fn=generate_audio,
129
+ inputs=[text_input, voice_input],
130
+ outputs=audio_output,
131
+ api_name="generate",
132
+ )
133
+
134
+
135
  if __name__ == "__main__":
136
+ demo.queue(max_size=5).launch(
137
+ server_name="0.0.0.0",
138
+ server_port=7860,
139
+ )