Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -357,6 +357,7 @@ def clean_for_speech(text: str) -> str:
|
|
| 357 |
text = re.sub(r"\s+", " ", text).strip()
|
| 358 |
return text
|
| 359 |
|
|
|
|
| 360 |
async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 361 |
"""Async TTS generation with edge-tts library."""
|
| 362 |
text = clean_for_speech(text)
|
|
@@ -364,15 +365,43 @@ async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=
|
|
| 364 |
return None
|
| 365 |
rate_str = f"{rate:+d}%"
|
| 366 |
pitch_str = f"{pitch:+d}Hz"
|
| 367 |
-
communicate =
|
| 368 |
out_fn = generate_filename(text, text, file_type=file_format)
|
| 369 |
await communicate.save(out_fn)
|
| 370 |
return out_fn
|
| 371 |
|
| 372 |
def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 373 |
"""Wrapper for the async TTS generate call."""
|
| 374 |
-
|
| 375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
return
|
| 377 |
|
| 378 |
def play_and_download_audio(file_path, file_type="mp3"):
|
|
|
|
| 357 |
text = re.sub(r"\s+", " ", text).strip()
|
| 358 |
return text
|
| 359 |
|
| 360 |
+
|
| 361 |
async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 362 |
"""Async TTS generation with edge-tts library."""
|
| 363 |
text = clean_for_speech(text)
|
|
|
|
| 365 |
return None
|
| 366 |
rate_str = f"{rate:+d}%"
|
| 367 |
pitch_str = f"{pitch:+d}Hz"
|
| 368 |
+
communicate = Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
| 369 |
out_fn = generate_filename(text, text, file_type=file_format)
|
| 370 |
await communicate.save(out_fn)
|
| 371 |
return out_fn
|
| 372 |
|
| 373 |
def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 374 |
"""Wrapper for the async TTS generate call."""
|
| 375 |
+
try:
|
| 376 |
+
loop = asyncio.get_event_loop()
|
| 377 |
+
except RuntimeError:
|
| 378 |
+
# No event loop running, create one
|
| 379 |
+
loop = asyncio.new_event_loop()
|
| 380 |
+
asyncio.set_event_loop(loop)
|
| 381 |
+
|
| 382 |
+
try:
|
| 383 |
+
result = loop.run_until_complete(edge_tts_generate_audio(text, voice, rate, pitch, file_format))
|
| 384 |
+
return result
|
| 385 |
+
finally:
|
| 386 |
+
if not asyncio.get_event_loop().is_running():
|
| 387 |
+
loop.close()
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
async def edge_tts_generate_audio0(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 391 |
+
"""Async TTS generation with edge-tts library."""
|
| 392 |
+
text = clean_for_speech(text)
|
| 393 |
+
if not text.strip():
|
| 394 |
+
return None
|
| 395 |
+
rate_str = f"{rate:+d}%"
|
| 396 |
+
pitch_str = f"{pitch:+d}Hz"
|
| 397 |
+
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
| 398 |
+
out_fn = generate_filename(text, text, file_type=file_format)
|
| 399 |
+
await communicate.save(out_fn)
|
| 400 |
+
return out_fn
|
| 401 |
+
|
| 402 |
+
def speak_with_edge_tts0(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
|
| 403 |
+
"""Wrapper for the async TTS generate call."""
|
| 404 |
+
asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch, file_format))
|
| 405 |
return
|
| 406 |
|
| 407 |
def play_and_download_audio(file_path, file_type="mp3"):
|