devupai 1.0.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,663 @@
1
+ """Audio: text to speech (``POST /audio/speech``) and speech to text (``POST /audio/transcriptions``)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Mapping, Sequence
6
+ from typing import TYPE_CHECKING, Literal, Unpack, overload
7
+
8
+ from devupai._response import APIResponse
9
+ from devupai.resources._base import (
10
+ AsyncAPIResource,
11
+ SyncAPIResource,
12
+ billed_post_retry,
13
+ endpoint,
14
+ json_body,
15
+ options,
16
+ with_data,
17
+ )
18
+ from devupai.resources.shared import MultipartPart, fixed_form
19
+ from devupai.types import SpeechResponse, TranscriptionResponse, TranscriptionVerboseResponse
20
+ from devupai.types._base import parse_model
21
+
22
+ if TYPE_CHECKING:
23
+ from devupai._client import AsyncDevupAI, DevupAI
24
+ from devupai.types import FileInput, SpeechCreateParams
25
+
26
+ __all__ = [
27
+ "Audio",
28
+ "AsyncAudio",
29
+ "Speech",
30
+ "AsyncSpeech",
31
+ "SpeechWithResponse",
32
+ "AsyncSpeechWithResponse",
33
+ "Transcriptions",
34
+ "AsyncTranscriptions",
35
+ "TranscriptionsWithResponse",
36
+ "AsyncTranscriptionsWithResponse",
37
+ ]
38
+
39
+ _SPEECH_PATH = "/audio/speech"
40
+ _TRANSCRIPTIONS_PATH = "/audio/transcriptions"
41
+
42
+ TextTranscriptionFormat = Literal["text", "srt", "vtt"]
43
+ """The transcription formats returned as plain text (a ``str``)."""
44
+ TranscriptionFormat = Literal["json", "verbose_json", "text", "srt", "vtt"]
45
+ """Every transcription ``response_format``."""
46
+ TimestampGranularity = Literal["word", "segment"]
47
+ """A ``timestamp_granularities`` value."""
48
+
49
+ _TEXT_FORMATS = frozenset({"text", "srt", "vtt"})
50
+
51
+
52
+ def _transcription_parts(
53
+ *,
54
+ file: FileInput,
55
+ model: str | None,
56
+ language: str | None,
57
+ response_format: str | None,
58
+ prompt: str | None,
59
+ temperature: float | None,
60
+ timestamp_granularities: Sequence[str] | None,
61
+ ) -> list[MultipartPart]:
62
+ """The form parts of a transcription: ``file``, then the set fields in a fixed order, then one
63
+ ``timestamp_granularities[]`` part per value. A field set to ``None`` is left out."""
64
+ return fixed_form(
65
+ (
66
+ ("file", file),
67
+ ("model", model),
68
+ ("language", language),
69
+ ("response_format", response_format),
70
+ ("prompt", prompt),
71
+ ("temperature", temperature),
72
+ ("timestamp_granularities[]", timestamp_granularities),
73
+ ),
74
+ file_fields=("file",),
75
+ )
76
+
77
+
78
+ def _transcription_kind(response_format: str | None) -> Literal["json", "text"]:
79
+ return "text" if response_format in _TEXT_FORMATS else "json"
80
+
81
+
82
+ def _transcription_result(
83
+ response: APIResponse[object], response_format: str | None
84
+ ) -> APIResponse[TranscriptionResponse] | APIResponse[TranscriptionVerboseResponse] | APIResponse[str]:
85
+ if response_format in _TEXT_FORMATS:
86
+ return with_data(response, str(response.data))
87
+ if response_format == "verbose_json":
88
+ return with_data(response, parse_model(TranscriptionVerboseResponse, response.data))
89
+ return with_data(response, parse_model(TranscriptionResponse, response.data))
90
+
91
+
92
+ class SpeechWithResponse(SyncAPIResource):
93
+ """The methods of :class:`Speech`, returning the whole :class:`~devupai.APIResponse`."""
94
+
95
+ @endpoint("POST", _SPEECH_PATH, billed=True)
96
+ def create(
97
+ self,
98
+ *,
99
+ extra_headers: Mapping[str, str] | None = None,
100
+ timeout: float | None = None,
101
+ max_retries: int | None = None,
102
+ extra_body: Mapping[str, object] | None = None,
103
+ **params: Unpack[SpeechCreateParams],
104
+ ) -> APIResponse[SpeechResponse]:
105
+ """Generates spoken audio from the input text; see :meth:`Speech.create`."""
106
+ response = self._send(
107
+ "POST",
108
+ _SPEECH_PATH,
109
+ body=json_body(params, extra_body),
110
+ retry=billed_post_retry(),
111
+ options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers),
112
+ )
113
+ return with_data(response, parse_model(SpeechResponse, response.data))
114
+
115
+
116
+ class Speech(SyncAPIResource):
117
+ """Text to speech: ``POST /audio/speech``."""
118
+
119
+ @property
120
+ def with_response(self) -> SpeechWithResponse:
121
+ """The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
122
+ return SpeechWithResponse(self._client)
123
+
124
+ @endpoint("POST", _SPEECH_PATH, billed=True)
125
+ def create(
126
+ self,
127
+ *,
128
+ extra_headers: Mapping[str, str] | None = None,
129
+ timeout: float | None = None,
130
+ max_retries: int | None = None,
131
+ extra_body: Mapping[str, object] | None = None,
132
+ **params: Unpack[SpeechCreateParams],
133
+ ) -> SpeechResponse:
134
+ """Generates spoken audio from the input text.
135
+
136
+ The body fields are passed as keyword arguments. ``extra_body`` adds fields to the JSON body (merged last),
137
+ ``extra_headers`` adds request headers, and ``timeout`` and ``max_retries`` apply to this call only.
138
+
139
+ Example::
140
+
141
+ speech = client.audio.speech.create(model="<model-id>", input="Hello")
142
+ """
143
+ return self.with_response.create(
144
+ extra_headers=extra_headers, timeout=timeout, max_retries=max_retries, extra_body=extra_body, **params
145
+ ).data
146
+
147
+
148
+ class TranscriptionsWithResponse(SyncAPIResource):
149
+ """The methods of :class:`Transcriptions`, returning the whole :class:`~devupai.APIResponse`."""
150
+
151
+ @overload
152
+ def create(
153
+ self,
154
+ *,
155
+ file: FileInput,
156
+ model: str | None = None,
157
+ language: str | None = None,
158
+ response_format: TextTranscriptionFormat,
159
+ prompt: str | None = None,
160
+ temperature: float | None = None,
161
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
162
+ extra_headers: Mapping[str, str] | None = None,
163
+ timeout: float | None = None,
164
+ max_retries: int | None = None,
165
+ ) -> APIResponse[str]: ...
166
+ @overload
167
+ def create(
168
+ self,
169
+ *,
170
+ file: FileInput,
171
+ model: str | None = None,
172
+ language: str | None = None,
173
+ response_format: Literal["verbose_json"],
174
+ prompt: str | None = None,
175
+ temperature: float | None = None,
176
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
177
+ extra_headers: Mapping[str, str] | None = None,
178
+ timeout: float | None = None,
179
+ max_retries: int | None = None,
180
+ ) -> APIResponse[TranscriptionVerboseResponse]: ...
181
+ @overload
182
+ def create(
183
+ self,
184
+ *,
185
+ file: FileInput,
186
+ model: str | None = None,
187
+ language: str | None = None,
188
+ response_format: Literal["json"] | None = None,
189
+ prompt: str | None = None,
190
+ temperature: float | None = None,
191
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
192
+ extra_headers: Mapping[str, str] | None = None,
193
+ timeout: float | None = None,
194
+ max_retries: int | None = None,
195
+ ) -> APIResponse[TranscriptionResponse]: ...
196
+ @overload
197
+ def create(
198
+ self,
199
+ *,
200
+ file: FileInput,
201
+ model: str | None = None,
202
+ language: str | None = None,
203
+ response_format: TranscriptionFormat | None = None,
204
+ prompt: str | None = None,
205
+ temperature: float | None = None,
206
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
207
+ extra_headers: Mapping[str, str] | None = None,
208
+ timeout: float | None = None,
209
+ max_retries: int | None = None,
210
+ ) -> APIResponse[TranscriptionResponse] | APIResponse[TranscriptionVerboseResponse] | APIResponse[str]: ...
211
+ @endpoint("POST", _TRANSCRIPTIONS_PATH, billed=True)
212
+ def create(
213
+ self,
214
+ *,
215
+ file: FileInput,
216
+ model: str | None = None,
217
+ language: str | None = None,
218
+ response_format: TranscriptionFormat | None = None,
219
+ prompt: str | None = None,
220
+ temperature: float | None = None,
221
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
222
+ extra_headers: Mapping[str, str] | None = None,
223
+ timeout: float | None = None,
224
+ max_retries: int | None = None,
225
+ ) -> APIResponse[TranscriptionResponse] | APIResponse[TranscriptionVerboseResponse] | APIResponse[str]:
226
+ """Transcribes an audio file; see :meth:`Transcriptions.create`."""
227
+ parts = _transcription_parts(
228
+ file=file,
229
+ model=model,
230
+ language=language,
231
+ response_format=response_format,
232
+ prompt=prompt,
233
+ temperature=temperature,
234
+ timestamp_granularities=timestamp_granularities,
235
+ )
236
+ response = self._send(
237
+ "POST",
238
+ _TRANSCRIPTIONS_PATH,
239
+ multipart=parts,
240
+ retry=billed_post_retry(),
241
+ kind=_transcription_kind(response_format),
242
+ options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers),
243
+ )
244
+ return _transcription_result(response, response_format)
245
+
246
+
247
+ class Transcriptions(SyncAPIResource):
248
+ """Speech to text: ``POST /audio/transcriptions`` (multipart)."""
249
+
250
+ @property
251
+ def with_response(self) -> TranscriptionsWithResponse:
252
+ """The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
253
+ return TranscriptionsWithResponse(self._client)
254
+
255
+ @overload
256
+ def create(
257
+ self,
258
+ *,
259
+ file: FileInput,
260
+ model: str | None = None,
261
+ language: str | None = None,
262
+ response_format: TextTranscriptionFormat,
263
+ prompt: str | None = None,
264
+ temperature: float | None = None,
265
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
266
+ extra_headers: Mapping[str, str] | None = None,
267
+ timeout: float | None = None,
268
+ max_retries: int | None = None,
269
+ ) -> str: ...
270
+ @overload
271
+ def create(
272
+ self,
273
+ *,
274
+ file: FileInput,
275
+ model: str | None = None,
276
+ language: str | None = None,
277
+ response_format: Literal["verbose_json"],
278
+ prompt: str | None = None,
279
+ temperature: float | None = None,
280
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
281
+ extra_headers: Mapping[str, str] | None = None,
282
+ timeout: float | None = None,
283
+ max_retries: int | None = None,
284
+ ) -> TranscriptionVerboseResponse: ...
285
+ @overload
286
+ def create(
287
+ self,
288
+ *,
289
+ file: FileInput,
290
+ model: str | None = None,
291
+ language: str | None = None,
292
+ response_format: Literal["json"] | None = None,
293
+ prompt: str | None = None,
294
+ temperature: float | None = None,
295
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
296
+ extra_headers: Mapping[str, str] | None = None,
297
+ timeout: float | None = None,
298
+ max_retries: int | None = None,
299
+ ) -> TranscriptionResponse: ...
300
+ @overload
301
+ def create(
302
+ self,
303
+ *,
304
+ file: FileInput,
305
+ model: str | None = None,
306
+ language: str | None = None,
307
+ response_format: TranscriptionFormat | None = None,
308
+ prompt: str | None = None,
309
+ temperature: float | None = None,
310
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
311
+ extra_headers: Mapping[str, str] | None = None,
312
+ timeout: float | None = None,
313
+ max_retries: int | None = None,
314
+ ) -> TranscriptionResponse | TranscriptionVerboseResponse | str: ...
315
+ @endpoint("POST", _TRANSCRIPTIONS_PATH, billed=True)
316
+ def create(
317
+ self,
318
+ *,
319
+ file: FileInput,
320
+ model: str | None = None,
321
+ language: str | None = None,
322
+ response_format: TranscriptionFormat | None = None,
323
+ prompt: str | None = None,
324
+ temperature: float | None = None,
325
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
326
+ extra_headers: Mapping[str, str] | None = None,
327
+ timeout: float | None = None,
328
+ max_retries: int | None = None,
329
+ ) -> TranscriptionResponse | TranscriptionVerboseResponse | str:
330
+ """Transcribes an audio file.
331
+
332
+ The file and the fields are sent as ``multipart/form-data``: ``file`` (bytes, a path, an open binary file
333
+ or a ``(filename, content[, content_type])`` tuple), then ``model``, ``language``, ``response_format``,
334
+ ``prompt``, ``temperature`` and one ``timestamp_granularities[]`` part per value. A field left as ``None``
335
+ is not sent. A path is opened and closed by the SDK; an open file is read and left open.
336
+
337
+ The result follows ``response_format``: ``text``, ``srt`` and ``vtt`` return a ``str``; ``verbose_json``
338
+ returns a :class:`~devupai.types.TranscriptionVerboseResponse`; ``json`` (the default) returns a
339
+ :class:`~devupai.types.TranscriptionResponse`.
340
+
341
+ ``extra_headers`` adds request headers, and ``timeout`` and ``max_retries`` apply to this call only.
342
+
343
+ Example::
344
+
345
+ from pathlib import Path
346
+
347
+ result = client.audio.transcriptions.create(file=Path("clip.wav"), model="<model-id>")
348
+ print(result.text)
349
+ """
350
+ return self.with_response.create(
351
+ file=file,
352
+ model=model,
353
+ language=language,
354
+ response_format=response_format,
355
+ prompt=prompt,
356
+ temperature=temperature,
357
+ timestamp_granularities=timestamp_granularities,
358
+ extra_headers=extra_headers,
359
+ timeout=timeout,
360
+ max_retries=max_retries,
361
+ ).data
362
+
363
+
364
+ class Audio(SyncAPIResource):
365
+ """Audio resources."""
366
+
367
+ speech: Speech
368
+ """Text to speech."""
369
+ transcriptions: Transcriptions
370
+ """Speech to text."""
371
+
372
+ def __init__(self, client: DevupAI) -> None:
373
+ super().__init__(client)
374
+ self.speech = Speech(client)
375
+ self.transcriptions = Transcriptions(client)
376
+
377
+
378
+ class AsyncSpeechWithResponse(AsyncAPIResource):
379
+ """The methods of :class:`AsyncSpeech`, returning the whole :class:`~devupai.APIResponse`."""
380
+
381
+ @endpoint("POST", _SPEECH_PATH, billed=True)
382
+ async def create(
383
+ self,
384
+ *,
385
+ extra_headers: Mapping[str, str] | None = None,
386
+ timeout: float | None = None,
387
+ max_retries: int | None = None,
388
+ extra_body: Mapping[str, object] | None = None,
389
+ **params: Unpack[SpeechCreateParams],
390
+ ) -> APIResponse[SpeechResponse]:
391
+ """Generates spoken audio from the input text; see :meth:`AsyncSpeech.create`."""
392
+ response = await self._send(
393
+ "POST",
394
+ _SPEECH_PATH,
395
+ body=json_body(params, extra_body),
396
+ retry=billed_post_retry(),
397
+ options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers),
398
+ )
399
+ return with_data(response, parse_model(SpeechResponse, response.data))
400
+
401
+
402
+ class AsyncSpeech(AsyncAPIResource):
403
+ """Text to speech: ``POST /audio/speech``."""
404
+
405
+ @property
406
+ def with_response(self) -> AsyncSpeechWithResponse:
407
+ """The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
408
+ return AsyncSpeechWithResponse(self._client)
409
+
410
+ @endpoint("POST", _SPEECH_PATH, billed=True)
411
+ async def create(
412
+ self,
413
+ *,
414
+ extra_headers: Mapping[str, str] | None = None,
415
+ timeout: float | None = None,
416
+ max_retries: int | None = None,
417
+ extra_body: Mapping[str, object] | None = None,
418
+ **params: Unpack[SpeechCreateParams],
419
+ ) -> SpeechResponse:
420
+ """Generates spoken audio from the input text.
421
+
422
+ The body fields are passed as keyword arguments. ``extra_body`` adds fields to the JSON body (merged last),
423
+ ``extra_headers`` adds request headers, and ``timeout`` and ``max_retries`` apply to this call only.
424
+
425
+ Example::
426
+
427
+ speech = await client.audio.speech.create(model="<model-id>", input="Hello")
428
+ """
429
+ response = await self.with_response.create(
430
+ extra_headers=extra_headers, timeout=timeout, max_retries=max_retries, extra_body=extra_body, **params
431
+ )
432
+ return response.data
433
+
434
+
435
+ class AsyncTranscriptionsWithResponse(AsyncAPIResource):
436
+ """The methods of :class:`AsyncTranscriptions`, returning the whole :class:`~devupai.APIResponse`."""
437
+
438
+ @overload
439
+ async def create(
440
+ self,
441
+ *,
442
+ file: FileInput,
443
+ model: str | None = None,
444
+ language: str | None = None,
445
+ response_format: TextTranscriptionFormat,
446
+ prompt: str | None = None,
447
+ temperature: float | None = None,
448
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
449
+ extra_headers: Mapping[str, str] | None = None,
450
+ timeout: float | None = None,
451
+ max_retries: int | None = None,
452
+ ) -> APIResponse[str]: ...
453
+ @overload
454
+ async def create(
455
+ self,
456
+ *,
457
+ file: FileInput,
458
+ model: str | None = None,
459
+ language: str | None = None,
460
+ response_format: Literal["verbose_json"],
461
+ prompt: str | None = None,
462
+ temperature: float | None = None,
463
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
464
+ extra_headers: Mapping[str, str] | None = None,
465
+ timeout: float | None = None,
466
+ max_retries: int | None = None,
467
+ ) -> APIResponse[TranscriptionVerboseResponse]: ...
468
+ @overload
469
+ async def create(
470
+ self,
471
+ *,
472
+ file: FileInput,
473
+ model: str | None = None,
474
+ language: str | None = None,
475
+ response_format: Literal["json"] | None = None,
476
+ prompt: str | None = None,
477
+ temperature: float | None = None,
478
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
479
+ extra_headers: Mapping[str, str] | None = None,
480
+ timeout: float | None = None,
481
+ max_retries: int | None = None,
482
+ ) -> APIResponse[TranscriptionResponse]: ...
483
+ @overload
484
+ async def create(
485
+ self,
486
+ *,
487
+ file: FileInput,
488
+ model: str | None = None,
489
+ language: str | None = None,
490
+ response_format: TranscriptionFormat | None = None,
491
+ prompt: str | None = None,
492
+ temperature: float | None = None,
493
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
494
+ extra_headers: Mapping[str, str] | None = None,
495
+ timeout: float | None = None,
496
+ max_retries: int | None = None,
497
+ ) -> APIResponse[TranscriptionResponse] | APIResponse[TranscriptionVerboseResponse] | APIResponse[str]: ...
498
+ @endpoint("POST", _TRANSCRIPTIONS_PATH, billed=True)
499
+ async def create(
500
+ self,
501
+ *,
502
+ file: FileInput,
503
+ model: str | None = None,
504
+ language: str | None = None,
505
+ response_format: TranscriptionFormat | None = None,
506
+ prompt: str | None = None,
507
+ temperature: float | None = None,
508
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
509
+ extra_headers: Mapping[str, str] | None = None,
510
+ timeout: float | None = None,
511
+ max_retries: int | None = None,
512
+ ) -> APIResponse[TranscriptionResponse] | APIResponse[TranscriptionVerboseResponse] | APIResponse[str]:
513
+ """Transcribes an audio file; see :meth:`AsyncTranscriptions.create`."""
514
+ parts = _transcription_parts(
515
+ file=file,
516
+ model=model,
517
+ language=language,
518
+ response_format=response_format,
519
+ prompt=prompt,
520
+ temperature=temperature,
521
+ timestamp_granularities=timestamp_granularities,
522
+ )
523
+ response = await self._send(
524
+ "POST",
525
+ _TRANSCRIPTIONS_PATH,
526
+ multipart=parts,
527
+ retry=billed_post_retry(),
528
+ kind=_transcription_kind(response_format),
529
+ options=options(timeout=timeout, max_retries=max_retries, extra_headers=extra_headers),
530
+ )
531
+ return _transcription_result(response, response_format)
532
+
533
+
534
+ class AsyncTranscriptions(AsyncAPIResource):
535
+ """Speech to text: ``POST /audio/transcriptions`` (multipart)."""
536
+
537
+ @property
538
+ def with_response(self) -> AsyncTranscriptionsWithResponse:
539
+ """The same methods, returning the whole :class:`~devupai.APIResponse` (headers, request id, warnings)."""
540
+ return AsyncTranscriptionsWithResponse(self._client)
541
+
542
+ @overload
543
+ async def create(
544
+ self,
545
+ *,
546
+ file: FileInput,
547
+ model: str | None = None,
548
+ language: str | None = None,
549
+ response_format: TextTranscriptionFormat,
550
+ prompt: str | None = None,
551
+ temperature: float | None = None,
552
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
553
+ extra_headers: Mapping[str, str] | None = None,
554
+ timeout: float | None = None,
555
+ max_retries: int | None = None,
556
+ ) -> str: ...
557
+ @overload
558
+ async def create(
559
+ self,
560
+ *,
561
+ file: FileInput,
562
+ model: str | None = None,
563
+ language: str | None = None,
564
+ response_format: Literal["verbose_json"],
565
+ prompt: str | None = None,
566
+ temperature: float | None = None,
567
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
568
+ extra_headers: Mapping[str, str] | None = None,
569
+ timeout: float | None = None,
570
+ max_retries: int | None = None,
571
+ ) -> TranscriptionVerboseResponse: ...
572
+ @overload
573
+ async def create(
574
+ self,
575
+ *,
576
+ file: FileInput,
577
+ model: str | None = None,
578
+ language: str | None = None,
579
+ response_format: Literal["json"] | None = None,
580
+ prompt: str | None = None,
581
+ temperature: float | None = None,
582
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
583
+ extra_headers: Mapping[str, str] | None = None,
584
+ timeout: float | None = None,
585
+ max_retries: int | None = None,
586
+ ) -> TranscriptionResponse: ...
587
+ @overload
588
+ async def create(
589
+ self,
590
+ *,
591
+ file: FileInput,
592
+ model: str | None = None,
593
+ language: str | None = None,
594
+ response_format: TranscriptionFormat | None = None,
595
+ prompt: str | None = None,
596
+ temperature: float | None = None,
597
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
598
+ extra_headers: Mapping[str, str] | None = None,
599
+ timeout: float | None = None,
600
+ max_retries: int | None = None,
601
+ ) -> TranscriptionResponse | TranscriptionVerboseResponse | str: ...
602
+ @endpoint("POST", _TRANSCRIPTIONS_PATH, billed=True)
603
+ async def create(
604
+ self,
605
+ *,
606
+ file: FileInput,
607
+ model: str | None = None,
608
+ language: str | None = None,
609
+ response_format: TranscriptionFormat | None = None,
610
+ prompt: str | None = None,
611
+ temperature: float | None = None,
612
+ timestamp_granularities: Sequence[TimestampGranularity] | None = None,
613
+ extra_headers: Mapping[str, str] | None = None,
614
+ timeout: float | None = None,
615
+ max_retries: int | None = None,
616
+ ) -> TranscriptionResponse | TranscriptionVerboseResponse | str:
617
+ """Transcribes an audio file.
618
+
619
+ The file and the fields are sent as ``multipart/form-data``: ``file`` (bytes, a path, an open binary file
620
+ or a ``(filename, content[, content_type])`` tuple), then ``model``, ``language``, ``response_format``,
621
+ ``prompt``, ``temperature`` and one ``timestamp_granularities[]`` part per value. A field left as ``None``
622
+ is not sent. A path is opened and closed by the SDK; an open file is read and left open.
623
+
624
+ The result follows ``response_format``: ``text``, ``srt`` and ``vtt`` return a ``str``; ``verbose_json``
625
+ returns a :class:`~devupai.types.TranscriptionVerboseResponse`; ``json`` (the default) returns a
626
+ :class:`~devupai.types.TranscriptionResponse`.
627
+
628
+ ``extra_headers`` adds request headers, and ``timeout`` and ``max_retries`` apply to this call only.
629
+
630
+ Example::
631
+
632
+ from pathlib import Path
633
+
634
+ result = await client.audio.transcriptions.create(file=Path("clip.wav"), model="<model-id>")
635
+ print(result.text)
636
+ """
637
+ response = await self.with_response.create(
638
+ file=file,
639
+ model=model,
640
+ language=language,
641
+ response_format=response_format,
642
+ prompt=prompt,
643
+ temperature=temperature,
644
+ timestamp_granularities=timestamp_granularities,
645
+ extra_headers=extra_headers,
646
+ timeout=timeout,
647
+ max_retries=max_retries,
648
+ )
649
+ return response.data
650
+
651
+
652
+ class AsyncAudio(AsyncAPIResource):
653
+ """Audio resources."""
654
+
655
+ speech: AsyncSpeech
656
+ """Text to speech."""
657
+ transcriptions: AsyncTranscriptions
658
+ """Speech to text."""
659
+
660
+ def __init__(self, client: AsyncDevupAI) -> None:
661
+ super().__init__(client)
662
+ self.speech = AsyncSpeech(client)
663
+ self.transcriptions = AsyncTranscriptions(client)