feat: 支持自定义tts接口服务

This commit is contained in:
Jad 2025-03-13 02:35:15 +08:00
parent 2dbf51f5b8
commit 1f8645340b
2 changed files with 48 additions and 0 deletions

View file

@ -412,6 +412,20 @@ TTS:
# 语速范围0.25-4.0
speed: 1
output_file: tmp/
CustomTTS:
# 自定义的TTS接口服务,请求参数可自定义
# 要求接口使用GET方式请求,返回wav格式音频
type: custom
url: "http://127.0.0.1:9880/tts"
params: # 自定义请求参数
text: "{prompt_text}" # {prompt_text}会被替换为实际的提示词内容
speaker: jok老师
speed: 1
foo: bar
testabc: 123456
headers: # 自定义请求头
Authorization: Bearer xxxx
output_file: tmp/
# 模块测试配置
module_test:
test_sentences: # 自定义测试语句

View file

@ -0,0 +1,34 @@
import os
import uuid
import requests
from config.logger import setup_logging
from datetime import datetime
from core.providers.tts.base import TTSProviderBase
TAG = __name__
logger = setup_logging()
class TTSProvider(TTSProviderBase):
def __init__(self, config, delete_audio_file):
super().__init__(config, delete_audio_file)
self.url = config.get("url")
self.headers = config.get("headers", {})
self.params = config.get("params")
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):
request_params = {}
for k, v in self.params.items():
if isinstance(v, str) and "{prompt_text}" in v:
v = v.replace("{prompt_text}", text)
request_params[k] = v
resp = requests.get(self.url, params=request_params, headers=self.headers)
if resp.status_code == 200:
with open(output_file, "wb") as file:
file.write(resp.content)
else:
logger.bind(tag=TAG).error(f"Custom TTS请求失败: {resp.status_code} - {resp.text}")