Spaces:
Paused
Paused
Update tts.py
Browse files
tts.py
CHANGED
|
@@ -94,10 +94,25 @@ def generate_audio(text: str) -> str:
|
|
| 94 |
fname = f"tts_{int(time.time()*1000)}_{uuid.uuid4().hex[:6]}.mp3"
|
| 95 |
output_path = os.path.join(TTS_DIR, fname)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
return None
|
| 102 |
|
| 103 |
if not os.path.exists(output_path):
|
|
@@ -107,4 +122,4 @@ def generate_audio(text: str) -> str:
|
|
| 107 |
size_kb = os.path.getsize(output_path) // 1024
|
| 108 |
elapsed = time.time() - t0
|
| 109 |
logger.info(f"[TTS] Saved: {output_path} ({size_kb}KB) | total: {elapsed:.2f}s")
|
| 110 |
-
return output_path
|
|
|
|
| 94 |
fname = f"tts_{int(time.time()*1000)}_{uuid.uuid4().hex[:6]}.mp3"
|
| 95 |
output_path = os.path.join(TTS_DIR, fname)
|
| 96 |
|
| 97 |
+
# Run edge-tts in a dedicated thread so asyncio.run() never collides with
|
| 98 |
+
# a running event loop in the caller. Works whether generate_audio is
|
| 99 |
+
# invoked from a sync thread, an async handler, or a ThreadPoolExecutor.
|
| 100 |
+
import threading
|
| 101 |
+
result = {"ok": False, "err": None}
|
| 102 |
+
|
| 103 |
+
def _worker():
|
| 104 |
+
try:
|
| 105 |
+
asyncio.run(_generate_audio_async(cleaned, output_path, _current_voice))
|
| 106 |
+
result["ok"] = True
|
| 107 |
+
except Exception as e:
|
| 108 |
+
result["err"] = e
|
| 109 |
+
|
| 110 |
+
th = threading.Thread(target=_worker, daemon=True)
|
| 111 |
+
th.start()
|
| 112 |
+
th.join()
|
| 113 |
+
|
| 114 |
+
if not result["ok"]:
|
| 115 |
+
logger.error(f"[TTS] edge-tts failed: {result['err']}")
|
| 116 |
return None
|
| 117 |
|
| 118 |
if not os.path.exists(output_path):
|
|
|
|
| 122 |
size_kb = os.path.getsize(output_path) // 1024
|
| 123 |
elapsed = time.time() - t0
|
| 124 |
logger.info(f"[TTS] Saved: {output_path} ({size_kb}KB) | total: {elapsed:.2f}s")
|
| 125 |
+
return output_path
|