-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaudio.py
More file actions
201 lines (165 loc) · 5.69 KB
/
audio.py
File metadata and controls
201 lines (165 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from typing import Any, Dict, List, Optional, Union, cast, overload
from typing_extensions import Literal, NotRequired, TypedDict
from ._config import ClientConfig
from ._types import BaseResponse
from .async_request import AsyncRequest, AsyncRequestConfig
from .request import Request, RequestConfig
class SpeechToTextParams(TypedDict):
url: NotRequired[str]
"""
the url of the audio file to transcribe, optional if file_store_key is provided
"""
file_store_key: NotRequired[str]
"""
the file store key of the audio file to transcribe, optional if url is provided
"""
language: NotRequired[Union[str, Literal["auto"]]]
"""
The language to transcribe or translate the file into. Use “auto” for automatic language detection, or specify a language code. If not specified, defaults to automatic detection. All supported language codes can be found
"""
translate: NotRequired[bool]
"""
When set to true, translates the content into English (or the specified language if language parameter is provided)
"""
by_speaker: NotRequired[bool]
"""
Identifies and separates different speakers in the audio file. When enabled, the response will include a speakers array with speaker-segmented transcripts.
"""
webhook_url: NotRequired[str]
"""
Webhook URL to send result to. When provided, the API will process asynchronously and send results to this URL when completed.
"""
batch_size: NotRequired[int]
"""
The batch size to return. Maximum value is 40. This controls how the audio is chunked for processing.
"""
chunk_duration: NotRequired[int]
"""
the duration of each chunk in seconds, maximum value is 15, defaults to 3
"""
class ChunkParams(TypedDict):
text: str
timestamp: tuple[int, int]
class BySpeakerParams(ChunkParams):
speaker: str
timestamp: tuple[int, int]
text: str
class SpeechToTextResponse(BaseResponse):
text: str
"""
the text of the transcription
"""
chunks: List[ChunkParams]
"""
the chunks of the transcription
"""
speakers: Optional[List[BySpeakerParams]]
"""
the speakers of the transcription, available if by_speaker is set to true
"""
language_detected: Optional[str]
"""
the language detected in the transcription, available if language is set to auto
"""
confidence: Optional[float]
"""
the confidence of the transcription language detection, available if language is set to auto
"""
class SpeechToTextWebhookResponse(BaseResponse):
status: Literal["processing", "error"]
"""
the status of the transcription process
"""
id: str
"""
the id of the transcription process
"""
class Audio(ClientConfig):
config: RequestConfig
def __init__(
self,
api_key: str,
base_url: str,
headers: Union[Dict[str, str], None] = None,
):
super().__init__(api_key, base_url, headers)
self.config = RequestConfig(base_url=base_url, api_key=api_key, headers=headers)
@overload
def speech_to_text(
self, params: SpeechToTextParams
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]: ...
@overload
def speech_to_text(
self, blob: bytes, options: Optional[SpeechToTextParams] = None
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]: ...
def speech_to_text(
self,
blob: Union[SpeechToTextParams, bytes],
options: Optional[SpeechToTextParams] = None,
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]:
options = options or {}
path = "/ai/transcribe"
if isinstance(blob, dict):
# URL or file_store_key based request
resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp
files = {"file": blob}
resp = Request(
config=self.config,
path=path,
params=options,
verb="post",
files=files,
).perform_with_content()
return resp
class AsyncAudio(ClientConfig):
config: AsyncRequestConfig
def __init__(
self,
api_key: str,
base_url: str,
headers: Union[Dict[str, str], None] = None,
):
super().__init__(api_key, base_url, headers)
self.config = AsyncRequestConfig(
base_url=base_url,
api_key=api_key,
headers=headers,
)
@overload
async def speech_to_text(
self, params: SpeechToTextParams
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]: ...
@overload
async def speech_to_text(
self, blob: bytes, options: Optional[SpeechToTextParams] = None
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]: ...
async def speech_to_text(
self,
blob: Union[SpeechToTextParams, bytes],
options: Optional[SpeechToTextParams] = None,
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]:
options = options or {}
path = "/ai/transcribe"
if isinstance(blob, dict):
resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], blob),
verb="post",
).perform_with_content()
return resp
files = {"file": blob}
resp = await AsyncRequest(
config=self.config,
path=path,
params=options,
verb="post",
files=files,
).perform_with_content()
return resp