Files
life-echo/api/app/adapters/tts/openai_tts.py
Kevin ccdc4e4277 feat(i18n): persist language preference and thread through chat, memoir, TTS
- Add users.language_preference (Alembic 0018, default zh); capture at signup/SMS
  only; expose on auth and profile APIs
- Lite English prompts for chat and memoir; localized stage labels and agent
  names (Life Echo / 岁月知己)
- Tencent TTS: language-aware synthesis, ModelType=1 for 501004, English chunking
- WebSocket pipeline: emit all AGENT_RESPONSE segments when TTS cancels; INFO logs
  for tts_this_turn and TTS decisions; on-demand TTS logging
- Expo: device language on auth, i18n tiers/agent name, [SPLIT] streaming UX fixes
- Tests for migration, prompts, pipeline, router tts_this_turn, reply segments

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 16:16:49 +08:00

45 lines
1.2 KiB
Python

"""OpenAI TTS adapter — implements TTSProvider port."""
import asyncio
from io import BytesIO
from openai import OpenAI
from app.core.logging import get_logger
logger = get_logger(__name__)
class OpenAITTSProvider:
def __init__(self, api_key: str, model: str = "tts-1"):
self._client = OpenAI(api_key=api_key) if api_key else None
self._model = model
def _synthesize_sync(self, text: str, voice: str) -> bytes:
if not self._client:
return b""
response = self._client.audio.speech.create(
model=self._model,
voice=voice,
input=text,
)
buf = BytesIO()
for chunk in response.iter_bytes():
buf.write(chunk)
return buf.getvalue()
async def synthesize(
self,
text: str,
voice: str = "alloy",
*,
language: str = "zh", # noqa: ARG002 — OpenAI TTS auto-detects language
) -> bytes:
if not self._client:
return b""
try:
return await asyncio.to_thread(self._synthesize_sync, text, voice)
except Exception as e:
logger.warning("OpenAI TTS synthesize failed: {}", e)
return b""