Merge branch 'refactor/backend-architecture' into development
This commit is contained in:
27
api/app/adapters/tts/openai_tts.py
Normal file
27
api/app/adapters/tts/openai_tts.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""OpenAI TTS adapter — implements TTSProvider port."""
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
|
||||
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
|
||||
|
||||
async def synthesize(self, text: str, voice: str = "alloy") -> bytes:
|
||||
if not self._client:
|
||||
return b""
|
||||
try:
|
||||
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()
|
||||
except Exception:
|
||||
return b""
|
||||
Reference in New Issue
Block a user