2929
3030
3131is_reference_id_first = config .online_model_first
32+ online_api_proxy = config .online_api_proxy
3233
3334
3435class 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" ]
0 commit comments