Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import edge_tts
|
| 3 |
-
import
|
| 4 |
-
import tempfile
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
async def generate_tts(text, voice, proxy):
|
| 14 |
-
if not text:
|
| 15 |
-
return None, "แแปแฑแธแแฐแธแแผแฏแ แ
แฌแแฌแธแแแทแบแแซแ"
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|