@@ -30,36 +30,35 @@ def test_audio_speech(logging_conf):
3030 print (err )
3131
3232def test_audio_speech_streaming (logging_conf ):
33- logging .config .dictConfig (logging_conf ) # type: ignore
34- client = ZhipuAI () # 填写您自己的APIKey
35- try :
36- response = client .audio .speech (
37- model = 'cogtts' ,
38- input = '你好呀,欢迎来到智谱开放平台' ,
39- voice = 'tongtong' ,
40- stream = True ,
41- response_format = 'mp3' ,
42- encode_format = 'base64'
43- )
44- with open ("output.pcm" , "wb" ) as f :
45- for item in response :
46- choice = item .choices [0 ]
47- index = choice .index
48- finish_reason = choice .finish_reason
49- audio_delta = choice .delta .content
50- if finish_reason is not None :
51- break
52- f .write (base64 .b64decode (audio_delta ))
53- print (f"{ index } .finish_reason = { finish_reason } , audio_delta = { len (audio_delta )} " )
54-
55- except zhipuai .core ._errors .APIRequestFailedError as err :
56- print (err )
57- except zhipuai .core ._errors .APIInternalError as err :
58- print (err )
59- except zhipuai .core ._errors .APIStatusError as err :
60- print (err )
61- except Exception as e :
62- print (e )
33+ logging .config .dictConfig (logging_conf ) # type: ignore
34+ client = ZhipuAI () # 填写您自己的APIKey
35+ try :
36+ response = client .audio .speech (
37+ model = 'cogtts' ,
38+ input = '你好呀,欢迎来到智谱开放平台' ,
39+ voice = 'tongtong' ,
40+ stream = True ,
41+ response_format = 'mp3' ,
42+ encode_format = 'hex'
43+ )
44+ with open ("output.mp3" , "wb" ) as f :
45+ for item in response :
46+ choice = item .choices [0 ]
47+ index = choice .index
48+ finish_reason = choice .finish_reason
49+ audio_delta = choice .delta .content
50+ if finish_reason is not None :
51+ break
52+ f .write (bytes .fromhex (audio_delta ))
53+ print (f"audio delta: { audio_delta [:64 ]} ..., 长度:{ len (audio_delta )} " )
54+ except zhipuai .core ._errors .APIRequestFailedError as err :
55+ print (err )
56+ except zhipuai .core ._errors .APIInternalError as err :
57+ print (err )
58+ except zhipuai .core ._errors .APIStatusError as err :
59+ print (err )
60+ except Exception as e :
61+ print (e )
6362
6463
6564def test_audio_customization (logging_conf ):
0 commit comments