feat: add 302.ai TTS integration (#166)

This commit is contained in:
Junsen 2025-03-02 15:29:17 +08:00 committed by GitHub
parent bc531652e9
commit 781a24516f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 1 deletions

View file

@ -305,7 +305,13 @@ TTS:
# volume: 50
# speech_rate: 0
# pitch_rate: 0
# 添加 302.ai TTS 配置
# token申请地址:https://dash.302.ai/
TTS302AI:
type: tts_302ai # 对应 tts_302ai.py 文件名
voice: "zh_female_wanwanxiaohe_moon_bigtts" # 默认语音类型
output_file: tmp/
access_token: "你的302API密钥" # 您的 302.ai 访问令牌
# 模块测试配置
module_test:
test_sentences: # 自定义测试语句

View file

@ -0,0 +1,49 @@
import os
import uuid
import base64
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.access_token = config.get("access_token")
self.voice = config.get("voice", "zh_female_wanwanxiaohe_moon_bigtts")
# self.api_url = "https://api.302.ai/doubao/tts_hd" 国外
self.api_url = "https://api.302ai.cn/doubao/tts_hd" #国内中转
self.header = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
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_json = {
"audio": {
"voice_type": self.voice,
"encoding": "wav",
"speed_ratio": 1.0
},
"request": {
"reqid": str(uuid.uuid4()),
"text": text,
"operation": "query"
}
}
print("执行", text,request_json)
try:
resp = requests.post(self.api_url, json=request_json, headers=self.header)
if resp.status_code == 200 and "data" in resp.json():
data = resp.json()["data"]
with open(output_file, "wb") as file_to_save:
file_to_save.write(base64.b64decode(data))
else:
raise Exception(f"{__name__} status_code: {resp.status_code} response: {resp.content}")
except Exception as e:
raise Exception(f"{__name__} error: {e}")