1- import os
2- import urllib .parse
31import uuid
2+ from pathlib import Path
43
54import 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