Files
life-echo/api/services/tts_service.py

59 lines
1.5 KiB
Python
Raw Normal View History

2026-01-07 11:56:46 +08:00
"""
TTS 服务文字转语音
"""
import base64
import os
from io import BytesIO
from openai import OpenAI
class TTSService:
"""TTS 服务(文字转语音)"""
def __init__(self):
api_key = os.getenv("OPENAI_API_KEY", "")
if api_key:
self.client = OpenAI(api_key=api_key)
else:
self.client = None
async def synthesize(self, text: str) -> str:
"""
将文字转换为语音
Args:
text: 要转换的文字
Returns:
Base64 编码的音频数据
"""
if not self.client:
# 如果没有配置 API Key返回空字符串
return ""
try:
# 调用 OpenAI TTS API
response = self.client.audio.speech.create(
model="tts-1",
voice="alloy", # 可选: alloy, echo, fable, onyx, nova, shimmer
input=text
)
# 读取音频数据
audio_bytes = BytesIO()
for chunk in response.iter_bytes():
audio_bytes.write(chunk)
# 转换为 Base64
audio_data = audio_bytes.getvalue()
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
return audio_base64
except Exception as e:
# 出错时返回空字符串
print(f"TTS 生成失败: {str(e)}")
return ""
# 全局实例
tts_service = TTSService()