mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
新增openai tts (#137)
On branch openai_tts Changes to be committed: modified: config.yaml new file: core/providers/tts/openai_tts.py Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com>
This commit is contained in:
parent
effd79b465
commit
59c2deaadc
2 changed files with 52 additions and 1 deletions
15
config.yaml
15
config.yaml
|
|
@ -307,9 +307,22 @@ TTS:
|
||||||
# volume: 50
|
# volume: 50
|
||||||
# speech_rate: 0
|
# speech_rate: 0
|
||||||
# pitch_rate: 0
|
# pitch_rate: 0
|
||||||
|
OpenAITTS:
|
||||||
|
# openai官方文本转语音服务,可支持全球大多数语种
|
||||||
|
type: openai_tts
|
||||||
|
api_key: 你的openai api key
|
||||||
|
# 国内需要使用代理
|
||||||
|
api_url: https://api.openai.com/v1/audio/speech
|
||||||
|
# 可选tts-1或tts-1-hd,tts-1速度更快tts-1-hd质量更好
|
||||||
|
model: tts-1
|
||||||
|
# 演讲者,可选alloy, echo, fable, onyx, nova, shimmer
|
||||||
|
voice: alloy
|
||||||
|
# 语速范围0.25-4.0
|
||||||
|
speed: 1
|
||||||
|
output_file: tmp/
|
||||||
|
TTS302AI:
|
||||||
# 添加 302.ai TTS 配置
|
# 添加 302.ai TTS 配置
|
||||||
# token申请地址:https://dash.302.ai/
|
# token申请地址:https://dash.302.ai/
|
||||||
TTS302AI:
|
|
||||||
# 302AI语音合成服务,需要先在302平台创建账户充值,并获取密钥信息
|
# 302AI语音合成服务,需要先在302平台创建账户充值,并获取密钥信息
|
||||||
# 获取api_keyn路径:https://dash.302.ai/apis/list
|
# 获取api_keyn路径:https://dash.302.ai/apis/list
|
||||||
# 价格,$35/百万字符。火山原版¥450元/万字符
|
# 价格,$35/百万字符。火山原版¥450元/万字符
|
||||||
|
|
|
||||||
38
core/providers/tts/openai_tts.py
Normal file
38
core/providers/tts/openai_tts.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
|
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_file", "tmp/")
|
||||||
|
|
||||||
|
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}")
|
||||||
Loading…
Reference in a new issue