@@ -56,10 +56,14 @@ def __init__(
5656 "api_base" ,
5757 "https://api.fish-audio.cn/v1" ,
5858 )
59+ try :
60+ self .timeout : int = int (provider_config .get ("timeout" , 20 ))
61+ except ValueError :
62+ self .timeout = 20
5963 self .headers = {
6064 "Authorization" : f"Bearer { self .chosen_api_key } " ,
6165 }
62- self .set_model (provider_config [ "model" ] )
66+ self .set_model (provider_config . get ( "model" , None ) )
6367
6468 async def _get_reference_id_by_character (self , character : str ) -> str | None :
6569 """获取角色的reference_id
@@ -135,17 +139,21 @@ async def get_audio(self, text: str) -> str:
135139 path = os .path .join (temp_dir , f"fishaudio_tts_api_{ uuid .uuid4 ()} .wav" )
136140 self .headers ["content-type" ] = "application/msgpack"
137141 request = await self ._generate_request (text )
138- async with AsyncClient (base_url = self .api_base ).stream (
142+ async with AsyncClient (base_url = self .api_base , timeout = self . timeout ).stream (
139143 "POST" ,
140144 "/tts" ,
141145 headers = self .headers ,
142146 content = ormsgpack .packb (request , option = ormsgpack .OPT_SERIALIZE_PYDANTIC ),
143147 ) as response :
144- if response .headers ["content-type" ] == "audio/wav" :
148+ if response .status_code == 200 and response .headers .get (
149+ "content-type" , ""
150+ ).startswith ("audio/" ):
145151 with open (path , "wb" ) as f :
146152 async for chunk in response .aiter_bytes ():
147153 f .write (chunk )
148154 return path
149- body = await response .aread ()
150- text = body .decode ("utf-8" , errors = "replace" )
151- raise Exception (f"Fish Audio API请求失败: { text } " )
155+ error_bytes = await response .aread ()
156+ error_text = error_bytes .decode ("utf-8" , errors = "replace" )[:1024 ]
157+ raise Exception (
158+ f"Fish Audio API请求失败: 状态码 { response .status_code } , 响应内容: { error_text } "
159+ )
0 commit comments