Spaces:
Sleeping
Sleeping
Run gTTS in thread to avoid asyncio loop errors
Browse files- app.py +2 -1
- utility/audio_generator.py +6 -8
app.py
CHANGED
|
@@ -28,7 +28,8 @@ async def generate_video(topic, progress=gr.Progress()):
|
|
| 28 |
|
| 29 |
|
| 30 |
progress(0.3, desc="Generating audio...")
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
progress(0.5, desc="Generating captions...")
|
| 34 |
timed_captions = generate_timed_captions(SAMPLE_FILE_NAME)
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
progress(0.3, desc="Generating audio...")
|
| 31 |
+
# Run blocking gTTS generation in a thread to keep event loop healthy
|
| 32 |
+
await asyncio.to_thread(generate_audio, response, SAMPLE_FILE_NAME)
|
| 33 |
|
| 34 |
progress(0.5, desc="Generating captions...")
|
| 35 |
timed_captions = generate_timed_captions(SAMPLE_FILE_NAME)
|
utility/audio_generator.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
-
import asyncio
|
| 4 |
|
| 5 |
logger = logging.getLogger(__name__)
|
| 6 |
|
| 7 |
-
|
| 8 |
"""Generate audio from text using Google gTTS (reliable fallback)
|
| 9 |
|
| 10 |
Args:
|
|
@@ -22,19 +21,18 @@ async def generate_audio(text, outputFilename):
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
from gtts import gTTS
|
| 25 |
-
|
| 26 |
# Generate speech with gTTS
|
| 27 |
-
# Slow=False for normal speed, slow=True for slower (more natural) speech
|
| 28 |
tts = gTTS(text=text, lang='en', slow=True, tld='com')
|
| 29 |
-
|
| 30 |
# Save to file
|
| 31 |
tts.save(outputFilename)
|
| 32 |
-
|
| 33 |
if not os.path.exists(outputFilename):
|
| 34 |
raise Exception(f"Failed to create audio file at {outputFilename}")
|
| 35 |
-
|
| 36 |
logger.info(f"Successfully generated audio at {outputFilename}")
|
| 37 |
-
|
| 38 |
except ImportError:
|
| 39 |
logger.warning("gTTS not found")
|
| 40 |
raise Exception("gTTS not found. Install with: pip install gtts")
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
|
|
|
| 3 |
|
| 4 |
logger = logging.getLogger(__name__)
|
| 5 |
|
| 6 |
+
def generate_audio(text, outputFilename):
|
| 7 |
"""Generate audio from text using Google gTTS (reliable fallback)
|
| 8 |
|
| 9 |
Args:
|
|
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
from gtts import gTTS
|
| 24 |
+
|
| 25 |
# Generate speech with gTTS
|
|
|
|
| 26 |
tts = gTTS(text=text, lang='en', slow=True, tld='com')
|
| 27 |
+
|
| 28 |
# Save to file
|
| 29 |
tts.save(outputFilename)
|
| 30 |
+
|
| 31 |
if not os.path.exists(outputFilename):
|
| 32 |
raise Exception(f"Failed to create audio file at {outputFilename}")
|
| 33 |
+
|
| 34 |
logger.info(f"Successfully generated audio at {outputFilename}")
|
| 35 |
+
|
| 36 |
except ImportError:
|
| 37 |
logger.warning("gTTS not found")
|
| 38 |
raise Exception("gTTS not found. Install with: pip install gtts")
|