29
29
30
30
31
31
is_reference_id_first = config .online_model_first
32
+ online_api_proxy = config .online_api_proxy
32
33
33
34
34
35
class FishAudioAPI :
@@ -37,8 +38,9 @@ class FishAudioAPI:
37
38
"""
38
39
39
40
def __init__ (self ):
40
- self .url = "https://api.fish.audio/v1/tts"
41
+ self .url : str = "https://api.fish.audio/v1/tts"
41
42
self .path_audio : Path = Path (config .tts_audio_path )
43
+ self .proxy = online_api_proxy
42
44
43
45
# 如果在线授权码为空, 且使用在线api, 则抛出异常
44
46
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:
70
72
"""
71
73
request_api = "https://api.fish.audio/model"
72
74
sort_options = ["score" , "task_count" , "created_at" ]
73
- async with AsyncClient () as client :
75
+ async with AsyncClient (proxies = self . proxy ) as client :
74
76
for sort_by in sort_options :
75
77
params = {"title" : speaker , "sort_by" : sort_by }
76
78
response = await client .get (
@@ -147,7 +149,7 @@ async def generate_tts(self, request: ServeTTSRequest) -> bytes:
147
149
if request .references :
148
150
self .headers ["content-type" ] = "application/msgpack"
149
151
try :
150
- async with AsyncClient () as client :
152
+ async with AsyncClient (proxies = self . proxy ) as client :
151
153
async with client .stream (
152
154
"POST" ,
153
155
self .url ,
@@ -166,11 +168,13 @@ async def generate_tts(self, request: ServeTTSRequest) -> bytes:
166
168
HTTPStatusError ,
167
169
) as e :
168
170
logger .error (f"获取TTS音频失败: { e } " )
171
+ if self .proxy :
172
+ raise HTTPException ("代理地址错误, 请检查代理地址是否正确" )
169
173
raise HTTPException ("网络错误, 请检查网络连接" )
170
174
else :
171
175
self .headers ["content-type" ] = "application/json"
172
176
try :
173
- async with AsyncClient () as client :
177
+ async with AsyncClient (proxies = self . proxy ) as client :
174
178
response = await client .post (
175
179
self .url ,
176
180
headers = self .headers ,
@@ -186,14 +190,16 @@ async def generate_tts(self, request: ServeTTSRequest) -> bytes:
186
190
HTTPStatusError ,
187
191
) as e :
188
192
logger .error (f"获取TTS音频失败: { e } " )
193
+ if self .proxy :
194
+ raise HTTPException ("代理地址错误, 请检查代理地址是否正确" )
189
195
raise HTTPException ("网络错误, 请检查网络连接" )
190
196
191
197
async def get_balance (self ) -> float :
192
198
"""
193
199
获取账户余额
194
200
"""
195
201
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 :
197
203
response = await client .get (balance_url , headers = self .headers )
198
204
try :
199
205
return response .json ()["credit" ]
0 commit comments