Fix: wrap sync generator in executor, not async for
This commit is contained in:
21
main.py
21
main.py
@@ -166,7 +166,7 @@ def get_custom_voices() -> List[str]:
|
||||
|
||||
async def generate_speech(text: str, voice: str) -> bytes:
|
||||
"""
|
||||
Generate speech using Orpheus model (async version).
|
||||
Generate speech using Orpheus model (async wrapper).
|
||||
|
||||
Args:
|
||||
text: Text to convert (may include emotion tags)
|
||||
@@ -191,7 +191,8 @@ async def generate_speech(text: str, voice: str) -> bytes:
|
||||
|
||||
print(f"{text}")
|
||||
|
||||
# Generate speech using Orpheus - async iteration!
|
||||
# Run synchronous generation in thread pool to not block event loop
|
||||
def _generate_sync():
|
||||
import numpy as np
|
||||
audio_chunks = []
|
||||
|
||||
@@ -200,8 +201,8 @@ async def generate_speech(text: str, voice: str) -> bytes:
|
||||
voice=voice,
|
||||
)
|
||||
|
||||
# Async iteration over the generator
|
||||
async for audio_chunk in syn_tokens:
|
||||
# Sync iteration - generator yields audio chunks
|
||||
for audio_chunk in syn_tokens:
|
||||
audio_chunks.append(audio_chunk)
|
||||
|
||||
# Combine chunks into single audio
|
||||
@@ -217,6 +218,10 @@ async def generate_speech(text: str, voice: str) -> bytes:
|
||||
|
||||
return buffer.getvalue()
|
||||
|
||||
# Run in executor to avoid blocking
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(None, _generate_sync)
|
||||
|
||||
|
||||
def save_audio_to_file(job_id: str, audio_bytes: bytes) -> str:
|
||||
"""Save audio bytes to WAV file."""
|
||||
@@ -528,15 +533,15 @@ async def stream_tts(request: TTSStreamRequest):
|
||||
if voice not in BUILTIN_VOICES:
|
||||
voice = DEFAULT_VOICE
|
||||
|
||||
async def audio_generator():
|
||||
"""Generate audio chunks (async)"""
|
||||
def sync_audio_generator():
|
||||
"""Generate audio chunks (sync generator)"""
|
||||
try:
|
||||
syn_tokens = model.generate_speech(
|
||||
prompt=request.text,
|
||||
voice=voice,
|
||||
)
|
||||
|
||||
async for audio_chunk in syn_tokens:
|
||||
for audio_chunk in syn_tokens:
|
||||
yield audio_chunk
|
||||
|
||||
except Exception as e:
|
||||
@@ -544,7 +549,7 @@ async def stream_tts(request: TTSStreamRequest):
|
||||
raise
|
||||
|
||||
return StreamingResponse(
|
||||
audio_generator(),
|
||||
sync_audio_generator(),
|
||||
media_type="audio/wav"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user