translations.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. import logging
  4. from typing import TYPE_CHECKING, Union, Mapping, cast
  5. from typing_extensions import Literal, overload, assert_never
  6. import httpx
  7. from ... import _legacy_response
  8. from ..._types import Body, Omit, Query, Headers, NotGiven, FileTypes, omit, not_given
  9. from ..._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform
  10. from ..._compat import cached_property
  11. from ..._resource import SyncAPIResource, AsyncAPIResource
  12. from ..._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  13. from ...types.audio import translation_create_params
  14. from ..._base_client import make_request_options
  15. from ...types.audio_model import AudioModel
  16. from ...types.audio.translation import Translation
  17. from ...types.audio_response_format import AudioResponseFormat
  18. from ...types.audio.translation_verbose import TranslationVerbose
  19. __all__ = ["Translations", "AsyncTranslations"]
  20. log: logging.Logger = logging.getLogger("openai.audio.transcriptions")
  21. class Translations(SyncAPIResource):
  22. @cached_property
  23. def with_raw_response(self) -> TranslationsWithRawResponse:
  24. """
  25. This property can be used as a prefix for any HTTP method call to return
  26. the raw response object instead of the parsed content.
  27. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  28. """
  29. return TranslationsWithRawResponse(self)
  30. @cached_property
  31. def with_streaming_response(self) -> TranslationsWithStreamingResponse:
  32. """
  33. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  34. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  35. """
  36. return TranslationsWithStreamingResponse(self)
  37. @overload
  38. def create(
  39. self,
  40. *,
  41. file: FileTypes,
  42. model: Union[str, AudioModel],
  43. response_format: Union[Literal["json"], Omit] = omit,
  44. prompt: str | Omit = omit,
  45. temperature: float | Omit = omit,
  46. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  47. # The extra values given here take precedence over values defined on the client or passed to this method.
  48. extra_headers: Headers | None = None,
  49. extra_query: Query | None = None,
  50. extra_body: Body | None = None,
  51. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  52. ) -> Translation: ...
  53. @overload
  54. def create(
  55. self,
  56. *,
  57. file: FileTypes,
  58. model: Union[str, AudioModel],
  59. response_format: Literal["verbose_json"],
  60. prompt: str | Omit = omit,
  61. temperature: float | Omit = omit,
  62. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  63. # The extra values given here take precedence over values defined on the client or passed to this method.
  64. extra_headers: Headers | None = None,
  65. extra_query: Query | None = None,
  66. extra_body: Body | None = None,
  67. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  68. ) -> TranslationVerbose: ...
  69. @overload
  70. def create(
  71. self,
  72. *,
  73. file: FileTypes,
  74. model: Union[str, AudioModel],
  75. response_format: Literal["text", "srt", "vtt"],
  76. prompt: str | Omit = omit,
  77. temperature: float | Omit = omit,
  78. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  79. # The extra values given here take precedence over values defined on the client or passed to this method.
  80. extra_headers: Headers | None = None,
  81. extra_query: Query | None = None,
  82. extra_body: Body | None = None,
  83. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  84. ) -> str: ...
  85. def create(
  86. self,
  87. *,
  88. file: FileTypes,
  89. model: Union[str, AudioModel],
  90. prompt: str | Omit = omit,
  91. response_format: Union[Literal["json", "text", "srt", "verbose_json", "vtt"], Omit] = omit,
  92. temperature: float | Omit = omit,
  93. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  94. # The extra values given here take precedence over values defined on the client or passed to this method.
  95. extra_headers: Headers | None = None,
  96. extra_query: Query | None = None,
  97. extra_body: Body | None = None,
  98. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  99. ) -> Translation | TranslationVerbose | str:
  100. """
  101. Translates audio into English.
  102. Args:
  103. file: The audio file object (not file name) translate, in one of these formats: flac,
  104. mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
  105. model: ID of the model to use. Only `whisper-1` (which is powered by our open source
  106. Whisper V2 model) is currently available.
  107. prompt: An optional text to guide the model's style or continue a previous audio
  108. segment. The
  109. [prompt](https://platform.openai.com/docs/guides/speech-to-text#prompting)
  110. should be in English.
  111. response_format: The format of the output, in one of these options: `json`, `text`, `srt`,
  112. `verbose_json`, or `vtt`.
  113. temperature: The sampling temperature, between 0 and 1. Higher values like 0.8 will make the
  114. output more random, while lower values like 0.2 will make it more focused and
  115. deterministic. If set to 0, the model will use
  116. [log probability](https://en.wikipedia.org/wiki/Log_probability) to
  117. automatically increase the temperature until certain thresholds are hit.
  118. extra_headers: Send extra headers
  119. extra_query: Add additional query parameters to the request
  120. extra_body: Add additional JSON properties to the request
  121. timeout: Override the client-level default timeout for this request, in seconds
  122. """
  123. body = deepcopy_minimal(
  124. {
  125. "file": file,
  126. "model": model,
  127. "prompt": prompt,
  128. "response_format": response_format,
  129. "temperature": temperature,
  130. }
  131. )
  132. files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
  133. # It should be noted that the actual Content-Type header that will be
  134. # sent to the server will contain a `boundary` parameter, e.g.
  135. # multipart/form-data; boundary=---abc--
  136. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  137. return self._post( # type: ignore[return-value]
  138. "/audio/translations",
  139. body=maybe_transform(body, translation_create_params.TranslationCreateParams),
  140. files=files,
  141. options=make_request_options(
  142. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  143. ),
  144. cast_to=_get_response_format_type(response_format),
  145. )
  146. class AsyncTranslations(AsyncAPIResource):
  147. @cached_property
  148. def with_raw_response(self) -> AsyncTranslationsWithRawResponse:
  149. """
  150. This property can be used as a prefix for any HTTP method call to return
  151. the raw response object instead of the parsed content.
  152. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  153. """
  154. return AsyncTranslationsWithRawResponse(self)
  155. @cached_property
  156. def with_streaming_response(self) -> AsyncTranslationsWithStreamingResponse:
  157. """
  158. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  159. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  160. """
  161. return AsyncTranslationsWithStreamingResponse(self)
  162. @overload
  163. async def create(
  164. self,
  165. *,
  166. file: FileTypes,
  167. model: Union[str, AudioModel],
  168. response_format: Union[Literal["json"], Omit] = omit,
  169. prompt: str | Omit = omit,
  170. temperature: float | Omit = omit,
  171. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  172. # The extra values given here take precedence over values defined on the client or passed to this method.
  173. extra_headers: Headers | None = None,
  174. extra_query: Query | None = None,
  175. extra_body: Body | None = None,
  176. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  177. ) -> Translation: ...
  178. @overload
  179. async def create(
  180. self,
  181. *,
  182. file: FileTypes,
  183. model: Union[str, AudioModel],
  184. response_format: Literal["verbose_json"],
  185. prompt: str | Omit = omit,
  186. temperature: float | Omit = omit,
  187. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  188. # The extra values given here take precedence over values defined on the client or passed to this method.
  189. extra_headers: Headers | None = None,
  190. extra_query: Query | None = None,
  191. extra_body: Body | None = None,
  192. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  193. ) -> TranslationVerbose: ...
  194. @overload
  195. async def create(
  196. self,
  197. *,
  198. file: FileTypes,
  199. model: Union[str, AudioModel],
  200. response_format: Literal["text", "srt", "vtt"],
  201. prompt: str | Omit = omit,
  202. temperature: float | Omit = omit,
  203. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  204. # The extra values given here take precedence over values defined on the client or passed to this method.
  205. extra_headers: Headers | None = None,
  206. extra_query: Query | None = None,
  207. extra_body: Body | None = None,
  208. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  209. ) -> str: ...
  210. async def create(
  211. self,
  212. *,
  213. file: FileTypes,
  214. model: Union[str, AudioModel],
  215. prompt: str | Omit = omit,
  216. response_format: Union[AudioResponseFormat, Omit] = omit,
  217. temperature: float | Omit = omit,
  218. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  219. # The extra values given here take precedence over values defined on the client or passed to this method.
  220. extra_headers: Headers | None = None,
  221. extra_query: Query | None = None,
  222. extra_body: Body | None = None,
  223. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  224. ) -> Translation | TranslationVerbose | str:
  225. """
  226. Translates audio into English.
  227. Args:
  228. file: The audio file object (not file name) translate, in one of these formats: flac,
  229. mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
  230. model: ID of the model to use. Only `whisper-1` (which is powered by our open source
  231. Whisper V2 model) is currently available.
  232. prompt: An optional text to guide the model's style or continue a previous audio
  233. segment. The
  234. [prompt](https://platform.openai.com/docs/guides/speech-to-text#prompting)
  235. should be in English.
  236. response_format: The format of the output, in one of these options: `json`, `text`, `srt`,
  237. `verbose_json`, or `vtt`.
  238. temperature: The sampling temperature, between 0 and 1. Higher values like 0.8 will make the
  239. output more random, while lower values like 0.2 will make it more focused and
  240. deterministic. If set to 0, the model will use
  241. [log probability](https://en.wikipedia.org/wiki/Log_probability) to
  242. automatically increase the temperature until certain thresholds are hit.
  243. extra_headers: Send extra headers
  244. extra_query: Add additional query parameters to the request
  245. extra_body: Add additional JSON properties to the request
  246. timeout: Override the client-level default timeout for this request, in seconds
  247. """
  248. body = deepcopy_minimal(
  249. {
  250. "file": file,
  251. "model": model,
  252. "prompt": prompt,
  253. "response_format": response_format,
  254. "temperature": temperature,
  255. }
  256. )
  257. files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
  258. # It should be noted that the actual Content-Type header that will be
  259. # sent to the server will contain a `boundary` parameter, e.g.
  260. # multipart/form-data; boundary=---abc--
  261. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  262. return await self._post(
  263. "/audio/translations",
  264. body=await async_maybe_transform(body, translation_create_params.TranslationCreateParams),
  265. files=files,
  266. options=make_request_options(
  267. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  268. ),
  269. cast_to=_get_response_format_type(response_format),
  270. )
  271. class TranslationsWithRawResponse:
  272. def __init__(self, translations: Translations) -> None:
  273. self._translations = translations
  274. self.create = _legacy_response.to_raw_response_wrapper(
  275. translations.create,
  276. )
  277. class AsyncTranslationsWithRawResponse:
  278. def __init__(self, translations: AsyncTranslations) -> None:
  279. self._translations = translations
  280. self.create = _legacy_response.async_to_raw_response_wrapper(
  281. translations.create,
  282. )
  283. class TranslationsWithStreamingResponse:
  284. def __init__(self, translations: Translations) -> None:
  285. self._translations = translations
  286. self.create = to_streamed_response_wrapper(
  287. translations.create,
  288. )
  289. class AsyncTranslationsWithStreamingResponse:
  290. def __init__(self, translations: AsyncTranslations) -> None:
  291. self._translations = translations
  292. self.create = async_to_streamed_response_wrapper(
  293. translations.create,
  294. )
  295. def _get_response_format_type(
  296. response_format: AudioResponseFormat | Omit,
  297. ) -> type[Translation | TranslationVerbose | str]:
  298. if isinstance(response_format, Omit) or response_format is None: # pyright: ignore[reportUnnecessaryComparison]
  299. return Translation
  300. if response_format == "json":
  301. return Translation
  302. elif response_format == "verbose_json":
  303. return TranslationVerbose
  304. elif response_format == "srt" or response_format == "text" or response_format == "vtt":
  305. return str
  306. elif TYPE_CHECKING and response_format != "diarized_json": # type: ignore[unreachable]
  307. assert_never(response_format)
  308. else:
  309. log.warning("Unexpected audio response format: %s", response_format)
  310. return Translation