Skip to content

Commit 23ad1cf

Browse files
committed
✨ feat: 添加代理配置选项以支持国内访问 fish-audio
1 parent e68dcd9 commit 23ad1cf

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ _⭐文本生成语音`tts`插件⭐_
2222

2323
> 或者使用官方在线api -> [fish-audio](https://fish.audio/zh-CN/)即可享受快速云端的语音生成。
2424
25+
> [!WARNING]
26+
> 由于目前国内无法正常访问fish-audio,需要配置[代理](#️-配置)或者使用离线fish-speech生成语音。
27+
2528
## 📜 免责声明
2629

27-
> [!note]
30+
> [!CAUTION]
2831
> 本插件仅供**学习****研究**使用,使用者需自行承担使用插件的风险。作者不对插件的使用造成的任何损失或问题负责。请合理使用插件,**遵守相关法律法规。**
2932
使用**本插件即表示您已阅读并同意遵守以上免责声明**。如果您不同意或无法遵守以上声明,请不要使用本插件。
3033

34+
---
3135

3236
## 💿 安装
3337

@@ -78,7 +82,7 @@ git clone https://github.com/Cvandia/nonebot-plugin-fishspeech-tts
7882
| tts_is_online | bool || True | 是否使用云端api |
7983
| tts_chunk_length | literal || "normal" | 请求时音频分片长度,默认为normal,可选:short, normal, long |
8084
| tts_audio_path | str || "./data/参考音频" | 语音素材路径,默认为"./data/参考音频" |
81-
| tts_prefix | str || None | 触发前缀,默认为None |
85+
| tts_prefix | str || None | 触发前缀,默认为None |
8286

8387
**注:参考音频的文件名格式为:[角色名]音频对应的文字标签.[音频后缀名]**
8488

@@ -95,6 +99,7 @@ ___
9599
| :------------------: | :---: | :----: | :-----: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
96100
| online_authorization | str || "xxxxx" | 官网api鉴权秘钥,详见[链接](https://fish.audio/zh-CN/go-api/api-keys/) |
97101
| online_model_first | bool || True | 如果你想调用官方模型,通过自己的参考音频,定制角色音色,将此项设为`False`。当然,如果你没有准备参考音频,也会调用官网已经有的音色,具体详见[链接](https://fish.audio/zh-CN/) |
102+
| online_api_proxy | str || None | 代理地址,如:http://127.0.0.1:7890 |
98103

99104
---
100105

nonebot_plugin_fishspeech_tts/config.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Config(BaseModel):
1717
# 区分配置
1818
online_authorization: Optional[str] = "xxxxx"
1919
online_model_first: bool = True
20+
# 设置代理地址
21+
online_api_proxy: Optional[str] = None
2022

2123
offline_api_url: str = "http://127.0.0.1:8080"
2224

nonebot_plugin_fishspeech_tts/fish_audio_api.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030

3131
is_reference_id_first = config.online_model_first
32+
online_api_proxy = config.online_api_proxy
3233

3334

3435
class FishAudioAPI:
@@ -37,8 +38,9 @@ class FishAudioAPI:
3738
"""
3839

3940
def __init__(self):
40-
self.url = "https://api.fish.audio/v1/tts"
41+
self.url: str = "https://api.fish.audio/v1/tts"
4142
self.path_audio: Path = Path(config.tts_audio_path)
43+
self.proxy = online_api_proxy
4244

4345
# 如果在线授权码为空, 且使用在线api, 则抛出异常
4446
if not config.online_authorization and config.tts_is_online:
@@ -70,7 +72,7 @@ async def _get_reference_id_by_speaker(self, speaker: str) -> str:
7072
"""
7173
request_api = "https://api.fish.audio/model"
7274
sort_options = ["score", "task_count", "created_at"]
73-
async with AsyncClient() as client:
75+
async with AsyncClient(proxies=self.proxy) as client:
7476
for sort_by in sort_options:
7577
params = {"title": speaker, "sort_by": sort_by}
7678
response = await client.get(
@@ -147,7 +149,7 @@ async def generate_tts(self, request: ServeTTSRequest) -> bytes:
147149
if request.references:
148150
self.headers["content-type"] = "application/msgpack"
149151
try:
150-
async with AsyncClient() as client:
152+
async with AsyncClient(proxies=self.proxy) as client:
151153
async with client.stream(
152154
"POST",
153155
self.url,
@@ -166,11 +168,13 @@ async def generate_tts(self, request: ServeTTSRequest) -> bytes:
166168
HTTPStatusError,
167169
) as e:
168170
logger.error(f"获取TTS音频失败: {e}")
171+
if self.proxy:
172+
raise HTTPException("代理地址错误, 请检查代理地址是否正确")
169173
raise HTTPException("网络错误, 请检查网络连接")
170174
else:
171175
self.headers["content-type"] = "application/json"
172176
try:
173-
async with AsyncClient() as client:
177+
async with AsyncClient(proxies=self.proxy) as client:
174178
response = await client.post(
175179
self.url,
176180
headers=self.headers,
@@ -186,14 +190,16 @@ async def generate_tts(self, request: ServeTTSRequest) -> bytes:
186190
HTTPStatusError,
187191
) as e:
188192
logger.error(f"获取TTS音频失败: {e}")
193+
if self.proxy:
194+
raise HTTPException("代理地址错误, 请检查代理地址是否正确")
189195
raise HTTPException("网络错误, 请检查网络连接")
190196

191197
async def get_balance(self) -> float:
192198
"""
193199
获取账户余额
194200
"""
195201
balance_url = "https://api.fish.audio/wallet/self/api-credit"
196-
async with AsyncClient() as client:
202+
async with AsyncClient(proxies=self.proxy) as client:
197203
response = await client.get(balance_url, headers=self.headers)
198204
try:
199205
return response.json()["credit"]

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ build-backend = "pdm.backend"
1010

1111
[project]
1212
name = "nonebot-plugin-fishspeech-tts"
13-
version = "0.3.3"
13+
version = "0.3.4"
1414
description = "小样本TTS,通过调用在线或本地api发送TTS语音"
1515
authors = [
1616
{name = "Cvandia",email = "106718176+Cvandia@users.noreply.github.com"},

0 commit comments

Comments
 (0)