33
44import logging
55import logging .config
6+ from pathlib import Path
7+
68
79def test_audio_speech (logging_conf ):
810 logging .config .dictConfig (logging_conf ) # type: ignore
911 client = ZhipuAI () # 填写您自己的APIKey
1012 try :
13+ speech_file_path = Path (__file__ ).parent / "speech.wav"
1114 response = client .audio .speech (
1215 model = "cogtts" ,
13- input = "智谱ai, 你好呀" ,
16+ input = "你好呀,欢迎来到智谱开放平台 " ,
1417 voice = "female" ,
1518 response_format = "wav"
1619 )
17- print (response )
18- with open ("output.wav" , "wb" ) as f :
19- f .write (response .content )
20- print ("文件已保存为 output.wav" )
20+ response .stream_to_file (speech_file_path )
2121
2222 except zhipuai .core ._errors .APIRequestFailedError as err :
2323 print (err )
@@ -31,17 +31,15 @@ def test_audio_customization(logging_conf):
3131 client = ZhipuAI () # 填写您自己的APIKey
3232 with open ('/Users/jhy/Desktop/tts/test_case_8s.wav' , 'rb' ) as file :
3333 try :
34+ speech_file_path = Path (__file__ ).parent / "customization.wav"
3435 response = client .audio .customization (
3536 model = "cogtts" ,
36- input = "智谱ai, 你好呀" ,
37+ input = "你好呀,欢迎来到智谱开放平台 " ,
3738 voice_text = "这是一条测试用例" ,
3839 voice_data = file ,
3940 response_format = "wav"
4041 )
41- print (response )
42- with open ("output.wav" , "wb" ) as f :
43- f .write (response .content )
44- print ("文件已保存为 output.wav" )
42+ response .stream_to_file (speech_file_path )
4543
4644 except zhipuai .core ._errors .APIRequestFailedError as err :
4745 print (err )
0 commit comments