2026-03-18 17:18:23 +08:00
|
|
|
"""OpenAI TTS adapter — implements TTSProvider port."""
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
import asyncio
|
2026-03-18 17:18:23 +08:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
from openai import OpenAI
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
from app.core.logging import get_logger
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
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()
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
async def synthesize(self, text: str, voice: str = "alloy") -> bytes:
|
|
|
|
|
if not self._client:
|
|
|
|
|
return b""
|
|
|
|
|
try:
|
2026-04-08 15:37:09 +08:00
|
|
|
return await asyncio.to_thread(self._synthesize_sync, text, voice)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning("OpenAI TTS synthesize failed: {}", e)
|
2026-03-18 17:18:23 +08:00
|
|
|
return b""
|