Skip to content

Commit 995a318

Browse files
fix(gsvi_tts): Use the correct calling method (#7083)
* fix(gsvi_tts): Use the correct calling method (#5638) Add some configuration items for GSVI * fix(gsvi_tts): add default value for api_key in provider configuration * fix(gsvi_tts): Adjust wherever the Authorization header is built to only include it when `self.api_key` is truthy Delete some comments * chore: ruff format --------- Co-authored-by: Soulter <905617992@qq.com>
1 parent bcbf7dd commit 995a318

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

astrbot/core/config/default.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,10 +1623,14 @@ class ChatProviderTemplate(TypedDict):
16231623
"type": "gsvi_tts_api",
16241624
"provider": "gpt_sovits_inference",
16251625
"provider_type": "text_to_speech",
1626-
"api_base": "http://127.0.0.1:5000",
1627-
"character": "",
1628-
"emotion": "default",
16291626
"enable": False,
1627+
"api_key": "",
1628+
"api_base": "http://127.0.0.1:8000",
1629+
"version": "v4",
1630+
"character": "",
1631+
"prompt_text_lang": "中文",
1632+
"emotion": "默认",
1633+
"text_lang": "中文",
16301634
"timeout": 20,
16311635
},
16321636
"FishAudio TTS(API)": {
Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import os
2-
import urllib.parse
31
import uuid
2+
from pathlib import Path
43

54
import aiohttp
65

@@ -23,37 +22,55 @@ def __init__(
2322
provider_settings: dict,
2423
) -> None:
2524
super().__init__(provider_config, provider_settings)
26-
self.api_base = provider_config.get("api_base", "http://127.0.0.1:5000")
25+
self.api_key = provider_config.get("api_key", "")
26+
self.api_base = provider_config.get("api_base", "http://127.0.0.1:8000")
2727
self.api_base = self.api_base.removesuffix("/")
28+
self.version = provider_config.get("version", "v4")
2829
self.character = provider_config.get("character")
29-
self.emotion = provider_config.get("emotion")
30+
self.prompt_text_lang = provider_config.get("prompt_text_lang", "中文")
31+
self.emotion = provider_config.get("emotion", "默认")
32+
self.text_lang = provider_config.get("text_lang", "中文")
3033

3134
async def get_audio(self, text: str) -> str:
3235
temp_dir = get_astrbot_temp_path()
33-
path = os.path.join(temp_dir, f"gsvi_tts_{uuid.uuid4()}.wav")
34-
params = {"text": text}
36+
path = Path(temp_dir) / f"gsvi_tts_{uuid.uuid4()}.wav"
37+
url = f"{self.api_base}/infer_single"
3538

36-
if self.character:
37-
params["character"] = self.character
38-
if self.emotion:
39-
params["emotion"] = self.emotion
39+
headers = {"Content-Type": "application/json"}
40+
if self.api_key:
41+
headers["Authorization"] = f"Bearer {self.api_key}"
4042

41-
query_parts = []
42-
for key, value in params.items():
43-
encoded_value = urllib.parse.quote(str(value))
44-
query_parts.append(f"{key}={encoded_value}")
45-
46-
url = f"{self.api_base}/tts?{'&'.join(query_parts)}"
43+
data = {
44+
"dl_url": self.api_base,
45+
"version": self.version,
46+
"model_name": self.character,
47+
"prompt_text_lang": self.prompt_text_lang,
48+
"emotion": self.emotion,
49+
"text": text,
50+
"text_lang": self.text_lang,
51+
}
4752

4853
async with aiohttp.ClientSession() as session:
49-
async with session.get(url) as response:
54+
async with session.post(url, json=data, headers=headers) as response:
5055
if response.status == 200:
51-
with open(path, "wb") as f:
52-
f.write(await response.read())
56+
resp_json = await response.json()
57+
msg = resp_json.get("msg")
58+
audio_url = resp_json.get("audio_url")
59+
if not msg or msg != "合成成功":
60+
raise Exception(f"GSVI TTS API 合成失败: {msg}")
61+
async with session.get(audio_url) as audio_response:
62+
if audio_response.status == 200:
63+
with open(path, "wb") as f:
64+
f.write(await audio_response.read())
65+
else:
66+
error_text = await audio_response.text()
67+
raise Exception(
68+
f"GSVI TTS API 下载音频失败,状态码: {audio_response.status},错误: {error_text}",
69+
)
5370
else:
5471
error_text = await response.text()
5572
raise Exception(
5673
f"GSVI TTS API 请求失败,状态码: {response.status},错误: {error_text}",
5774
)
5875

59-
return path
76+
return str(path)

0 commit comments

Comments
 (0)