atomic saving, LRU eviction, max files, max size, async operations
This commit is contained in:
+77
-74
@@ -846,87 +846,90 @@ class VoiceSession:
|
||||
tts_language = resolve_tts_language(language_code)
|
||||
prepared_text = prepare_tts_text(text, tts_language)
|
||||
output_format = settings.elevenlabs_tts_output_format
|
||||
|
||||
# Check if audio in cache
|
||||
cached_audio = tts_cache.get(text=prepared_text,
|
||||
voice_id=voice_id,
|
||||
output_format=output_format,
|
||||
model_id=settings.elevenlabs_tts_model,
|
||||
stability=settings.elevenlabs_tts_stability,
|
||||
similarity_boost=settings.elevenlabs_tts_similarity_boost)
|
||||
|
||||
# cached audio found
|
||||
if cached_audio:
|
||||
logger.info("Using cached TTS audio for text: %s", prepared_text[:30])
|
||||
await self.send_json(
|
||||
{
|
||||
"type": "tts_audio",
|
||||
"audio": base64.b64encode(cached_audio).decode("ascii"),
|
||||
"format": audio_format_label(output_format),
|
||||
"sample_rate": audio_sample_rate(output_format),
|
||||
"mime_type": audio_mime_type(output_format),
|
||||
}
|
||||
)
|
||||
return
|
||||
|
||||
logger.info(
|
||||
"Calling ElevenLabs HTTP TTS: model=%s voice_id=%s language=%s",
|
||||
settings.elevenlabs_tts_model,
|
||||
voice_id,
|
||||
tts_language or "auto",
|
||||
)
|
||||
params = {"output_format": output_format}
|
||||
payload: dict[str, Any] = {
|
||||
|
||||
cache_params = {
|
||||
"text": prepared_text,
|
||||
"voice_id": voice_id,
|
||||
"output_format": output_format,
|
||||
"model_id": settings.elevenlabs_tts_model,
|
||||
"voice_settings": {
|
||||
"stability": settings.elevenlabs_tts_stability,
|
||||
"similarity_boost": settings.elevenlabs_tts_similarity_boost,
|
||||
"use_speaker_boost": False,
|
||||
"speed": settings.elevenlabs_tts_speed,
|
||||
},
|
||||
"stability": settings.elevenlabs_tts_stability,
|
||||
"similarity_boost": settings.elevenlabs_tts_similarity_boost,
|
||||
}
|
||||
if tts_language:
|
||||
payload["language_code"] = tts_language
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
response = await client.post(
|
||||
f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
|
||||
params=params,
|
||||
headers={
|
||||
"xi-api-key": settings.elevenlabs_api_key or "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json=payload,
|
||||
# Generate key & fetch lock dedicated ONLY to specific text/voice config
|
||||
key = tts_cache._generate_key(**cache_params)
|
||||
lock = await tts_cache.get_lock(key)
|
||||
|
||||
async with lock:
|
||||
# Check cache
|
||||
cached_audio = await tts_cache.get(**cache_params)
|
||||
|
||||
if cached_audio:
|
||||
logger.info("Using cached TTS audio for text: %s", prepared_text[:30])
|
||||
await self.send_json(
|
||||
{
|
||||
"type": "tts_audio",
|
||||
"audio": base64.b64encode(cached_audio).decode("ascii"),
|
||||
"format": audio_format_label(output_format),
|
||||
"sample_rate": audio_sample_rate(output_format),
|
||||
"mime_type": audio_mime_type(output_format),
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
# put audio in cache
|
||||
tts_cache.put(text=prepared_text,
|
||||
voice_id=voice_id,
|
||||
output_format=output_format,
|
||||
audio_data=response.content,
|
||||
model_id=settings.elevenlabs_tts_model,
|
||||
stability=settings.elevenlabs_tts_stability,
|
||||
similarity_boost=settings.elevenlabs_tts_similarity_boost)
|
||||
return
|
||||
|
||||
await self.send_json(
|
||||
{
|
||||
"type": "tts_audio",
|
||||
"audio": base64.b64encode(response.content).decode("ascii"),
|
||||
"format": audio_format_label(settings.elevenlabs_tts_output_format),
|
||||
"sample_rate": audio_sample_rate(settings.elevenlabs_tts_output_format),
|
||||
"mime_type": audio_mime_type(settings.elevenlabs_tts_output_format),
|
||||
}
|
||||
# Miss: Only ONE request per unique key executes the API call
|
||||
logger.info(
|
||||
"Calling ElevenLabs HTTP TTS: model=%s voice_id=%s language=%s",
|
||||
settings.elevenlabs_tts_model,
|
||||
voice_id,
|
||||
tts_language or "auto",
|
||||
)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
message = exc.response.text[:500] if exc.response is not None else str(exc)
|
||||
await self.send_tts_failed(
|
||||
f"ElevenLabs HTTP TTS error: {exc.response.status_code} {message}"
|
||||
)
|
||||
except Exception as exc:
|
||||
await self.send_tts_failed(f"ElevenLabs HTTP TTS error: {exc}")
|
||||
params = {"output_format": output_format}
|
||||
payload: dict[str, Any] = {
|
||||
"text": prepared_text,
|
||||
"model_id": settings.elevenlabs_tts_model,
|
||||
"voice_settings": {
|
||||
"stability": settings.elevenlabs_tts_stability,
|
||||
"similarity_boost": settings.elevenlabs_tts_similarity_boost,
|
||||
"use_speaker_boost": False,
|
||||
"speed": settings.elevenlabs_tts_speed,
|
||||
},
|
||||
}
|
||||
if tts_language:
|
||||
payload["language_code"] = tts_language
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
response = await client.post(
|
||||
f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
|
||||
params=params,
|
||||
headers={
|
||||
"xi-api-key": settings.elevenlabs_api_key or "",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json=payload,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
# Put in cache
|
||||
await tts_cache.put(audio_data=response.content, **cache_params)
|
||||
|
||||
await self.send_json(
|
||||
{
|
||||
"type": "tts_audio",
|
||||
"audio": base64.b64encode(response.content).decode("ascii"),
|
||||
"format": audio_format_label(settings.elevenlabs_tts_output_format),
|
||||
"sample_rate": audio_sample_rate(settings.elevenlabs_tts_output_format),
|
||||
"mime_type": audio_mime_type(settings.elevenlabs_tts_output_format),
|
||||
}
|
||||
)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
message = exc.response.text[:500] if exc.response is not None else str(exc)
|
||||
await self.send_tts_failed(
|
||||
f"ElevenLabs HTTP TTS error: {exc.response.status_code} {message}"
|
||||
)
|
||||
except Exception as exc:
|
||||
await self.send_tts_failed(f"ElevenLabs HTTP TTS error: {exc}")
|
||||
|
||||
async def stream_tts(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user