teamseven commited on
Commit
5459dfa
·
verified ·
1 Parent(s): f839e45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -38
app.py CHANGED
@@ -2,17 +2,18 @@ import os
2
  import asyncio
3
  import tempfile
4
  import logging
5
- import secrets
6
- from pathlib import Path
7
 
8
  import gradio as gr
9
  import edge_tts
10
 
11
- logging.basicConfig(level=logging.INFO)
 
 
 
 
12
  logger = logging.getLogger("edge-tts-bridge")
13
 
14
  APP_TITLE = "Edge TTS Bridge"
15
- API_KEY = os.environ.get("EDGE_TTS_API_KEY", "").strip()
16
 
17
  VOICES = {
18
  "Myanmar Female": "my-MM-NilarNeural",
@@ -25,11 +26,17 @@ async def synthesize(text: str, voice: str) -> str:
25
 
26
  with tempfile.NamedTemporaryFile(
27
  suffix=".mp3",
28
- delete=False
29
  ) as temp:
30
  output_path = temp.name
31
 
32
  try:
 
 
 
 
 
 
33
  communicate = edge_tts.Communicate(
34
  text=text,
35
  voice=voice,
@@ -40,22 +47,39 @@ async def synthesize(text: str, voice: str) -> str:
40
  if not os.path.exists(output_path):
41
  raise RuntimeError("Audio file was not created.")
42
 
43
- if os.path.getsize(output_path) < 100:
 
 
44
  raise RuntimeError(
45
- "Audio file is empty or unexpectedly small."
46
  )
47
 
 
 
 
 
 
 
48
  return output_path
49
 
50
- except Exception:
 
 
 
 
 
 
 
 
51
  try:
52
  os.remove(output_path)
53
  except OSError:
54
  pass
 
55
  raise
56
 
57
 
58
- def generate_audio(text, voice_name, api_key):
59
  """Gradio interface handler."""
60
 
61
  text = (text or "").strip()
@@ -71,15 +95,6 @@ def generate_audio(text, voice_name, api_key):
71
  if voice_name not in VOICES:
72
  raise gr.Error("Unsupported voice.")
73
 
74
- if API_KEY:
75
- supplied_key = (api_key or "").strip()
76
-
77
- if not secrets.compare_digest(
78
- supplied_key,
79
- API_KEY
80
- ):
81
- raise gr.Error("Invalid API key.")
82
-
83
  voice = VOICES[voice_name]
84
 
85
  try:
@@ -88,25 +103,19 @@ def generate_audio(text, voice_name, api_key):
88
  )
89
 
90
  except Exception as exc:
91
- logger.exception("Edge TTS synthesis failed")
 
 
 
92
 
93
- message = str(exc)
94
 
95
  raise gr.Error(
96
- f"Edge TTS failed: {message[:500]}"
 
97
  )
98
 
99
 
100
- def api_generate(text, voice_name, api_key):
101
- """Programmatic API handler."""
102
-
103
- return generate_audio(
104
- text,
105
- voice_name,
106
- api_key
107
- )
108
-
109
-
110
  with gr.Blocks(title=APP_TITLE) as demo:
111
  gr.Markdown(
112
  """
@@ -133,12 +142,6 @@ with gr.Blocks(title=APP_TITLE) as demo:
133
  label="Voice",
134
  )
135
 
136
- key_input = gr.Textbox(
137
- label="API Key (if configured)",
138
- type="password",
139
- placeholder="Enter your API key",
140
- )
141
-
142
  generate_button = gr.Button(
143
  "Generate Audio",
144
  variant="primary",
@@ -154,7 +157,6 @@ with gr.Blocks(title=APP_TITLE) as demo:
154
  inputs=[
155
  text_input,
156
  voice_input,
157
- key_input,
158
  ],
159
  outputs=audio_output,
160
  api_name="generate",
 
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",
 
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,
 
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()
 
95
  if voice_name not in VOICES:
96
  raise gr.Error("Unsupported voice.")
97
 
 
 
 
 
 
 
 
 
 
98
  voice = VOICES[voice_name]
99
 
100
  try:
 
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
  """
 
142
  label="Voice",
143
  )
144
 
 
 
 
 
 
 
145
  generate_button = gr.Button(
146
  "Generate Audio",
147
  variant="primary",
 
157
  inputs=[
158
  text_input,
159
  voice_input,
 
160
  ],
161
  outputs=audio_output,
162
  api_name="generate",