Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions tests/integration_tests/test_audio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import base64
import json
import logging
import logging.config
from pathlib import Path
Expand All @@ -16,8 +14,7 @@ def test_audio_speech(logging_conf):
response = client.audio.speech(
model='cogtts',
input='你好呀,欢迎来到智谱开放平台',
voice='tongtong',
stream=False,
voice='female',
response_format='wav',
)
response.stream_to_file(speech_file_path)
Expand All @@ -29,37 +26,6 @@ def test_audio_speech(logging_conf):
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_audio_speech_streaming(logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
response = client.audio.speech(
model='cogtts',
input='你好呀,欢迎来到智谱开放平台',
voice='tongtong',
stream=True,
response_format='wav',
)
with open("output.pcm", "wb") as f:
for item in response:
choice = item.choices[0]
index = choice.index
finish_reason = choice.finish_reason
audio_delta = choice.delta.content
if finish_reason is not None:
break
f.write(base64.b64decode(audio_delta))
print(f"{index}.finish_reason = {finish_reason}, audio_delta = {len(audio_delta)}")

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)
except Exception as e:
print(e)


def test_audio_customization(logging_conf):
logging.config.dictConfig(logging_conf)
Expand Down
13 changes: 4 additions & 9 deletions zhipuai/api_resource/audio/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from zhipuai.types.audio import AudioSpeechParams
from ...types.audio import audio_customization_param

from zhipuai.core import BaseAPI, maybe_transform, StreamResponse
from zhipuai.core import BaseAPI, maybe_transform
from zhipuai.core import NOT_GIVEN, Body, Headers, NotGiven, FileTypes
from zhipuai.core import _legacy_response

Expand All @@ -20,7 +20,6 @@
make_request_options,
)
from zhipuai.core import deepcopy_minimal
from ...types.audio.audio_speech_chunk import AudioSpeechChunk

if TYPE_CHECKING:
from zhipuai._client import ZhipuAI
Expand All @@ -47,17 +46,15 @@ def speech(
sensitive_word_check: Optional[SensitiveWordCheckRequest] | NotGiven = NOT_GIVEN,
request_id: str = None,
user_id: str = None,
stream: bool = False,
extra_headers: Headers | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> _legacy_response.HttpxBinaryResponseContent | StreamResponse[AudioSpeechChunk]:
) -> _legacy_response.HttpxBinaryResponseContent:
body = deepcopy_minimal(
{
"model": model,
"input": input,
"voice": voice,
"stream": stream,
"response_format": response_format,
"sensitive_word_check": sensitive_word_check,
"request_id": request_id,
Expand All @@ -66,13 +63,11 @@ def speech(
)
return self._post(
"/audio/speech",
body=body,
body=maybe_transform(body, AudioSpeechParams),
options=make_request_options(
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
),
cast_type=_legacy_response.HttpxBinaryResponseContent,
stream= stream or False,
stream_cls=StreamResponse[AudioSpeechChunk]
cast_type=_legacy_response.HttpxBinaryResponseContent
)

def customization(
Expand Down
32 changes: 0 additions & 32 deletions zhipuai/types/audio/audio_speech_chunk.py

This file was deleted.

Loading