|
| 1 | +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing_extensions import Literal |
| 6 | + |
| 7 | +import httpx |
| 8 | + |
| 9 | +from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven |
| 10 | +from ..._utils import ( |
| 11 | + maybe_transform, |
| 12 | + async_maybe_transform, |
| 13 | +) |
| 14 | +from ..._compat import cached_property |
| 15 | +from ..._resource import SyncAPIResource, AsyncAPIResource |
| 16 | +from ..._response import ( |
| 17 | + BinaryAPIResponse, |
| 18 | + AsyncBinaryAPIResponse, |
| 19 | + StreamedBinaryAPIResponse, |
| 20 | + AsyncStreamedBinaryAPIResponse, |
| 21 | + to_custom_raw_response_wrapper, |
| 22 | + to_custom_streamed_response_wrapper, |
| 23 | + async_to_custom_raw_response_wrapper, |
| 24 | + async_to_custom_streamed_response_wrapper, |
| 25 | +) |
| 26 | +from ...types.audio import speech_create_params |
| 27 | +from ..._base_client import make_request_options |
| 28 | + |
| 29 | +__all__ = ["Speech", "AsyncSpeech"] |
| 30 | + |
| 31 | + |
| 32 | +class Speech(SyncAPIResource): |
| 33 | + @cached_property |
| 34 | + def with_raw_response(self) -> SpeechWithRawResponse: |
| 35 | + """ |
| 36 | + This property can be used as a prefix for any HTTP method call to return |
| 37 | + the raw response object instead of the parsed content. |
| 38 | +
|
| 39 | + For more information, see https://www.github.com/groq/groq-python#accessing-raw-response-data-eg-headers |
| 40 | + """ |
| 41 | + return SpeechWithRawResponse(self) |
| 42 | + |
| 43 | + @cached_property |
| 44 | + def with_streaming_response(self) -> SpeechWithStreamingResponse: |
| 45 | + """ |
| 46 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 47 | +
|
| 48 | + For more information, see https://www.github.com/groq/groq-python#with_streaming_response |
| 49 | + """ |
| 50 | + return SpeechWithStreamingResponse(self) |
| 51 | + |
| 52 | + def create( |
| 53 | + self, |
| 54 | + *, |
| 55 | + input: str, |
| 56 | + model: str, |
| 57 | + voice: str, |
| 58 | + response_format: Literal["mp3", "opus", "aac", "flac", "wav", "pcm"] | NotGiven = NOT_GIVEN, |
| 59 | + speed: float | NotGiven = NOT_GIVEN, |
| 60 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 61 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 62 | + extra_headers: Headers | None = None, |
| 63 | + extra_query: Query | None = None, |
| 64 | + extra_body: Body | None = None, |
| 65 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 66 | + ) -> BinaryAPIResponse: |
| 67 | + """ |
| 68 | + Generates audio from the input text. |
| 69 | +
|
| 70 | + Args: |
| 71 | + input: The text to generate audio for. |
| 72 | +
|
| 73 | + model: One of the available TTS models |
| 74 | +
|
| 75 | + voice: The voice to use when generating the audio. |
| 76 | +
|
| 77 | + response_format: The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, |
| 78 | + `wav`, and `pcm`. |
| 79 | +
|
| 80 | + speed: The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is |
| 81 | + the default. |
| 82 | +
|
| 83 | + extra_headers: Send extra headers |
| 84 | +
|
| 85 | + extra_query: Add additional query parameters to the request |
| 86 | +
|
| 87 | + extra_body: Add additional JSON properties to the request |
| 88 | +
|
| 89 | + timeout: Override the client-level default timeout for this request, in seconds |
| 90 | + """ |
| 91 | + extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} |
| 92 | + return self._post( |
| 93 | + "/openai/v1/audio/speech", |
| 94 | + body=maybe_transform( |
| 95 | + { |
| 96 | + "input": input, |
| 97 | + "model": model, |
| 98 | + "voice": voice, |
| 99 | + "response_format": response_format, |
| 100 | + "speed": speed, |
| 101 | + }, |
| 102 | + speech_create_params.SpeechCreateParams, |
| 103 | + ), |
| 104 | + options=make_request_options( |
| 105 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 106 | + ), |
| 107 | + cast_to=BinaryAPIResponse, |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +class AsyncSpeech(AsyncAPIResource): |
| 112 | + @cached_property |
| 113 | + def with_raw_response(self) -> AsyncSpeechWithRawResponse: |
| 114 | + """ |
| 115 | + This property can be used as a prefix for any HTTP method call to return |
| 116 | + the raw response object instead of the parsed content. |
| 117 | +
|
| 118 | + For more information, see https://www.github.com/groq/groq-python#accessing-raw-response-data-eg-headers |
| 119 | + """ |
| 120 | + return AsyncSpeechWithRawResponse(self) |
| 121 | + |
| 122 | + @cached_property |
| 123 | + def with_streaming_response(self) -> AsyncSpeechWithStreamingResponse: |
| 124 | + """ |
| 125 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 126 | +
|
| 127 | + For more information, see https://www.github.com/groq/groq-python#with_streaming_response |
| 128 | + """ |
| 129 | + return AsyncSpeechWithStreamingResponse(self) |
| 130 | + |
| 131 | + async def create( |
| 132 | + self, |
| 133 | + *, |
| 134 | + input: str, |
| 135 | + model: str, |
| 136 | + voice: str, |
| 137 | + response_format: Literal["mp3", "opus", "aac", "flac", "wav", "pcm"] | NotGiven = NOT_GIVEN, |
| 138 | + speed: float | NotGiven = NOT_GIVEN, |
| 139 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 140 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 141 | + extra_headers: Headers | None = None, |
| 142 | + extra_query: Query | None = None, |
| 143 | + extra_body: Body | None = None, |
| 144 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 145 | + ) -> AsyncBinaryAPIResponse: |
| 146 | + """ |
| 147 | + Generates audio from the input text. |
| 148 | +
|
| 149 | + Args: |
| 150 | + input: The text to generate audio for. |
| 151 | +
|
| 152 | + model: One of the available TTS models |
| 153 | +
|
| 154 | + voice: The voice to use when generating the audio. |
| 155 | +
|
| 156 | + response_format: The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, |
| 157 | + `wav`, and `pcm`. |
| 158 | +
|
| 159 | + speed: The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is |
| 160 | + the default. |
| 161 | +
|
| 162 | + extra_headers: Send extra headers |
| 163 | +
|
| 164 | + extra_query: Add additional query parameters to the request |
| 165 | +
|
| 166 | + extra_body: Add additional JSON properties to the request |
| 167 | +
|
| 168 | + timeout: Override the client-level default timeout for this request, in seconds |
| 169 | + """ |
| 170 | + extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} |
| 171 | + return await self._post( |
| 172 | + "/openai/v1/audio/speech", |
| 173 | + body=await async_maybe_transform( |
| 174 | + { |
| 175 | + "input": input, |
| 176 | + "model": model, |
| 177 | + "voice": voice, |
| 178 | + "response_format": response_format, |
| 179 | + "speed": speed, |
| 180 | + }, |
| 181 | + speech_create_params.SpeechCreateParams, |
| 182 | + ), |
| 183 | + options=make_request_options( |
| 184 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 185 | + ), |
| 186 | + cast_to=AsyncBinaryAPIResponse, |
| 187 | + ) |
| 188 | + |
| 189 | + |
| 190 | +class SpeechWithRawResponse: |
| 191 | + def __init__(self, speech: Speech) -> None: |
| 192 | + self._speech = speech |
| 193 | + |
| 194 | + self.create = to_custom_raw_response_wrapper( |
| 195 | + speech.create, |
| 196 | + BinaryAPIResponse, |
| 197 | + ) |
| 198 | + |
| 199 | + |
| 200 | +class AsyncSpeechWithRawResponse: |
| 201 | + def __init__(self, speech: AsyncSpeech) -> None: |
| 202 | + self._speech = speech |
| 203 | + |
| 204 | + self.create = async_to_custom_raw_response_wrapper( |
| 205 | + speech.create, |
| 206 | + AsyncBinaryAPIResponse, |
| 207 | + ) |
| 208 | + |
| 209 | + |
| 210 | +class SpeechWithStreamingResponse: |
| 211 | + def __init__(self, speech: Speech) -> None: |
| 212 | + self._speech = speech |
| 213 | + |
| 214 | + self.create = to_custom_streamed_response_wrapper( |
| 215 | + speech.create, |
| 216 | + StreamedBinaryAPIResponse, |
| 217 | + ) |
| 218 | + |
| 219 | + |
| 220 | +class AsyncSpeechWithStreamingResponse: |
| 221 | + def __init__(self, speech: AsyncSpeech) -> None: |
| 222 | + self._speech = speech |
| 223 | + |
| 224 | + self.create = async_to_custom_streamed_response_wrapper( |
| 225 | + speech.create, |
| 226 | + AsyncStreamedBinaryAPIResponse, |
| 227 | + ) |
0 commit comments