Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -8,11 +8,8 @@ import gradio as gr
|
|
| 8 |
import asyncio
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
-
import nest_asyncio
|
| 12 |
import edge_tts
|
| 13 |
|
| 14 |
-
nest_asyncio.apply()
|
| 15 |
-
|
| 16 |
# High-quality neural voices per language
|
| 17 |
VOICES = {
|
| 18 |
"en": "en-US-GuyNeural",
|
|
@@ -43,6 +40,18 @@ async def _generate_async(text: str, voice: str, output_path: str) -> None:
|
|
| 43 |
await communicate.save(output_path)
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def generate_speech(text: str, language: str, voice_type: str) -> str:
|
| 47 |
"""Generate speech in the specified language."""
|
| 48 |
if not text.strip():
|
|
@@ -54,7 +63,7 @@ def generate_speech(text: str, language: str, voice_type: str) -> str:
|
|
| 54 |
voice = voices[language]
|
| 55 |
|
| 56 |
output_path = tempfile.mktemp(suffix=".mp3")
|
| 57 |
-
|
| 58 |
|
| 59 |
return output_path
|
| 60 |
|
|
@@ -72,7 +81,7 @@ def batch_generate(texts: str, language: str, voice_type: str):
|
|
| 72 |
for i, segment in enumerate(segments):
|
| 73 |
print(f"[{i+1}/{len(segments)}] {segment[:60]}...")
|
| 74 |
output_path = tempfile.mktemp(suffix=f"_seg{i+1:03d}.mp3")
|
| 75 |
-
|
| 76 |
results.append(output_path)
|
| 77 |
|
| 78 |
return results
|
|
|
|
| 8 |
import asyncio
|
| 9 |
import tempfile
|
| 10 |
import os
|
|
|
|
| 11 |
import edge_tts
|
| 12 |
|
|
|
|
|
|
|
| 13 |
# High-quality neural voices per language
|
| 14 |
VOICES = {
|
| 15 |
"en": "en-US-GuyNeural",
|
|
|
|
| 40 |
await communicate.save(output_path)
|
| 41 |
|
| 42 |
|
| 43 |
+
def _run_async(coro):
|
| 44 |
+
"""Run async coroutine, handling both fresh and existing event loops."""
|
| 45 |
+
try:
|
| 46 |
+
loop = asyncio.get_running_loop()
|
| 47 |
+
import concurrent.futures
|
| 48 |
+
with concurrent.futures.ThreadPoolExecutor() as pool:
|
| 49 |
+
future = pool.submit(asyncio.run, coro)
|
| 50 |
+
return future.result()
|
| 51 |
+
except RuntimeError:
|
| 52 |
+
return asyncio.run(coro)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
def generate_speech(text: str, language: str, voice_type: str) -> str:
|
| 56 |
"""Generate speech in the specified language."""
|
| 57 |
if not text.strip():
|
|
|
|
| 63 |
voice = voices[language]
|
| 64 |
|
| 65 |
output_path = tempfile.mktemp(suffix=".mp3")
|
| 66 |
+
_run_async(_generate_async(text, voice, output_path))
|
| 67 |
|
| 68 |
return output_path
|
| 69 |
|
|
|
|
| 81 |
for i, segment in enumerate(segments):
|
| 82 |
print(f"[{i+1}/{len(segments)}] {segment[:60]}...")
|
| 83 |
output_path = tempfile.mktemp(suffix=f"_seg{i+1:03d}.mp3")
|
| 84 |
+
_run_async(_generate_async(segment, voice, output_path))
|
| 85 |
results.append(output_path)
|
| 86 |
|
| 87 |
return results
|