mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
新增openai tts
On branch openai_tts Changes to be committed: modified: config.yaml new file: core/providers/tts/openai_tts.py
This commit is contained in:
parent
94a90c4124
commit
d9caa44881
2 changed files with 47 additions and 0 deletions
|
|
@ -305,6 +305,15 @@ 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 # 国内需要使用代理
|
||||||
|
model: tts-1 # 可选tts-1或tts-1-hd,tts-1速度更快tts-1-hd质量更好
|
||||||
|
voice: alloy # 演讲者,可选alloy, echo, fable, onyx, nova, shimmer
|
||||||
|
speed: 1.2 # 语速范围0.25-4.0
|
||||||
|
output_file: tmp/
|
||||||
|
|
||||||
# 模块测试配置
|
# 模块测试配置
|
||||||
module_test:
|
module_test:
|
||||||
|
|
|
||||||
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