@@ -30,35 +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 = 'wav ' ,
42- )
43- with open ( "output.pcm" , "wb" ) as f :
44- for item in response :
45- choice = item . choices [ 0 ]
46- index = choice . index
47- finish_reason = choice .finish_reason
48- audio_delta = choice .delta . content
49- if finish_reason is not None :
50- break
51- f . write ( base64 . b64decode ( audio_delta ))
52- print ( f" { index } .finish_reason = { finish_reason } , audio_delta = { len ( audio_delta )} " )
53-
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 )
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 )
6262
6363
6464def test_audio_customization (logging_conf ):
0 commit comments