mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-23 07:33:53 +08:00
* fix: 修复manual模式无法识别 (#404) * feat(docs): 新增Issues模板 * fix: 补全core依赖 * fix: 修复manual模式无法识别 * 去除重复依赖.txt 已经有torch和torchaudio --------- Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com> * 修复iot功能中表达式问题 (#400) * Custom paths asr tts (#388) * #164 自定义asr、tts缓存目录,项目启动自动创建目录 * #164 自定义asr、tts缓存目录,项目启动自动创建目录 * fix:修复语音无法找到新配置项output_file的bug * fix:电脑不支持iot音量控制bug --------- Co-authored-by: Junsen <66542771+Huang-junsen@users.noreply.github.com> Co-authored-by: tang <tangyiyong@gmail.com> Co-authored-by: shudongW <178200623@qq.com> Co-authored-by: hrz <1710360675@qq.com>
41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
import os
|
|
import uuid
|
|
import requests
|
|
from datetime import datetime
|
|
from core.utils.util import check_model_key
|
|
from core.providers.tts.base import TTSProviderBase
|
|
|
|
class TTSProvider(TTSProviderBase):
|
|
def __init__(self, config, delete_audio_file):
|
|
super().__init__(config, delete_audio_file)
|
|
self.api_key = config.get("api_key")
|
|
self.api_url = config.get("api_url", "https://api.openai.com/v1/audio/speech")
|
|
self.model = config.get("model", "tts-1")
|
|
self.voice = config.get("voice", "alloy")
|
|
self.response_format = "wav"
|
|
self.speed = config.get("speed", 1.0)
|
|
self.output_file = config.get("output_dir", "tmp/")
|
|
check_model_key("TTS", self.api_key)
|
|
|
|
def generate_filename(self, extension=".wav"):
|
|
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
|
|
|
async def text_to_speak(self, text, output_file):
|
|
headers = {
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
data = {
|
|
"model": self.model,
|
|
"input": text,
|
|
"voice": self.voice,
|
|
"response_format": "wav",
|
|
"speed": self.speed
|
|
}
|
|
response = requests.post(self.api_url, json=data, headers=headers)
|
|
if response.status_code == 200:
|
|
with open(output_file, "wb") as audio_file:
|
|
audio_file.write(response.content)
|
|
else:
|
|
raise Exception(f"OpenAI TTS请求失败: {response.status_code} - {response.text}")
|