|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, List, Mapping, cast, Optional, Dict |
| 4 | + |
| 5 | +from zhipuai.core._utils import extract_files |
| 6 | + |
| 7 | +from zhipuai.types.sensitive_word_check import SensitiveWordCheckRequest |
| 8 | +from zhipuai.types.audio import AudioSpeechParams |
| 9 | +from ...types.audio import audio_customization_param |
| 10 | + |
| 11 | +from zhipuai.core import BaseAPI, maybe_transform |
| 12 | +from zhipuai.core import NOT_GIVEN, Body, Headers, NotGiven, FileTypes |
| 13 | +from zhipuai.core import _legacy_response |
| 14 | + |
| 15 | +import httpx |
| 16 | + |
| 17 | +from zhipuai.core import ( |
| 18 | + make_request_options, |
| 19 | +) |
| 20 | +from zhipuai.core import deepcopy_minimal |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from zhipuai._client import ZhipuAI |
| 24 | + |
| 25 | +__all__ = ["Audio"] |
| 26 | + |
| 27 | + |
| 28 | +class Audio(BaseAPI): |
| 29 | + |
| 30 | + def __init__(self, client: "ZhipuAI") -> None: |
| 31 | + super().__init__(client) |
| 32 | + |
| 33 | + def speech( |
| 34 | + self, |
| 35 | + *, |
| 36 | + model: str, |
| 37 | + input: str = None, |
| 38 | + voice: str = None, |
| 39 | + response_format: str = None, |
| 40 | + sensitive_word_check: Optional[SensitiveWordCheckRequest] | NotGiven = NOT_GIVEN, |
| 41 | + request_id: str = None, |
| 42 | + user_id: str = None, |
| 43 | + extra_headers: Headers | None = None, |
| 44 | + extra_body: Body | None = None, |
| 45 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 46 | + ) -> _legacy_response.HttpxBinaryResponseContent: |
| 47 | + body = deepcopy_minimal( |
| 48 | + { |
| 49 | + "model": model, |
| 50 | + "input": input, |
| 51 | + "voice": voice, |
| 52 | + "response_format": response_format, |
| 53 | + "sensitive_word_check": sensitive_word_check, |
| 54 | + "request_id": request_id, |
| 55 | + "user_id": user_id |
| 56 | + } |
| 57 | + ) |
| 58 | + return self._post( |
| 59 | + "/audio/speech", |
| 60 | + body=maybe_transform(body, AudioSpeechParams), |
| 61 | + options=make_request_options( |
| 62 | + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout |
| 63 | + ), |
| 64 | + cast_type=_legacy_response.HttpxBinaryResponseContent |
| 65 | + ) |
| 66 | + |
| 67 | + def customization( |
| 68 | + self, |
| 69 | + *, |
| 70 | + model: str, |
| 71 | + input: str = None, |
| 72 | + voice_text: str = None, |
| 73 | + voice_data: FileTypes = None, |
| 74 | + response_format: str = None, |
| 75 | + sensitive_word_check: Optional[SensitiveWordCheckRequest] | NotGiven = NOT_GIVEN, |
| 76 | + request_id: str = None, |
| 77 | + user_id: str = None, |
| 78 | + extra_headers: Headers | None = None, |
| 79 | + extra_body: Body | None = None, |
| 80 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 81 | + ) -> _legacy_response.HttpxBinaryResponseContent: |
| 82 | + body = deepcopy_minimal( |
| 83 | + { |
| 84 | + "model": model, |
| 85 | + "input": input, |
| 86 | + "voice_text": voice_text, |
| 87 | + "voice_data": voice_data, |
| 88 | + "response_format": response_format, |
| 89 | + "sensitive_word_check": sensitive_word_check, |
| 90 | + "request_id": request_id, |
| 91 | + "user_id": user_id |
| 92 | + } |
| 93 | + ) |
| 94 | + files = extract_files(cast(Mapping[str, object], body), paths=[["voice_data"]]) |
| 95 | + |
| 96 | + if files: |
| 97 | + extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} |
| 98 | + return self._post( |
| 99 | + "/audio/customization", |
| 100 | + body=maybe_transform(body, audio_customization_param.AudioCustomizationParam), |
| 101 | + files=files, |
| 102 | + options=make_request_options( |
| 103 | + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout |
| 104 | + ), |
| 105 | + cast_type=_legacy_response.HttpxBinaryResponseContent |
| 106 | + ) |
0 commit comments