|
1 | | -from typing import Any, Dict, List, cast, Union, Optional |
| 1 | +from typing import Any, Dict, List, cast, Union, Optional, overload |
2 | 2 | from typing_extensions import NotRequired, TypedDict |
3 | 3 | from .request import Request, RequestConfig |
4 | 4 | from .async_request import AsyncRequest, AsyncRequestConfig |
5 | 5 | from ._config import ClientConfig |
6 | 6 | from typing import Any, Dict, List, cast |
7 | 7 | from typing_extensions import NotRequired, TypedDict |
8 | 8 | from .custom_typing import SupportedAccents |
| 9 | +from .helpers import build_path |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TextToSpeechParams(TypedDict): |
@@ -64,16 +65,41 @@ def __init__( |
64 | 65 | disable_request_logging=disable_request_logging, |
65 | 66 | ) |
66 | 67 |
|
67 | | - def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse: |
68 | | - path = "/ai/transcribe" |
| 68 | + @overload |
| 69 | + def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse: ... |
| 70 | + @overload |
| 71 | + def speech_to_text(self, file: bytes, options: Optional[SpeechToTextParams] = None) -> SpeechToTextResponse: ... |
| 72 | + |
| 73 | + def speech_to_text( |
| 74 | + self, |
| 75 | + blob: Union[SpeechToTextParams, bytes], |
| 76 | + options: Optional[SpeechToTextParams] = None, |
| 77 | + ) -> SpeechToTextResponse: |
| 78 | + if isinstance(blob, dict): # If params is provided as a dict, we assume it's the first argument |
| 79 | + resp = Request( |
| 80 | + config=self.config, |
| 81 | + path="/ai/transcribe", |
| 82 | + params=cast(Dict[Any, Any], blob), |
| 83 | + verb="post", |
| 84 | + ).perform_with_content() |
| 85 | + return resp |
| 86 | + |
| 87 | + options = options or {} |
| 88 | + path = build_path(base_path="/ai/transcribe", params=options) |
| 89 | + content_type = options.get("content_type", "application/octet-stream") |
| 90 | + headers = {"Content-Type": content_type} |
| 91 | + |
69 | 92 | resp = Request( |
70 | 93 | config=self.config, |
71 | 94 | path=path, |
72 | | - params=cast(Dict[Any, Any], params), |
| 95 | + params=options, |
| 96 | + data=blob, |
| 97 | + headers=headers, |
73 | 98 | verb="post", |
74 | 99 | ).perform_with_content() |
75 | 100 | return resp |
76 | 101 |
|
| 102 | + |
77 | 103 | def text_to_speech(self, params: TextToSpeechParams) -> TextToSpeechResponse: |
78 | 104 | path = "/ai/tts" |
79 | 105 | resp = Request( |
@@ -111,12 +137,36 @@ def __init__( |
111 | 137 | disable_request_logging=disable_request_logging, |
112 | 138 | ) |
113 | 139 |
|
114 | | - async def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse: |
115 | | - path = "/ai/transcribe" |
| 140 | + @overload |
| 141 | + async def speech_to_text(self, params: SpeechToTextParams) -> SpeechToTextResponse: ... |
| 142 | + @overload |
| 143 | + async def speech_to_text(self, file: bytes, options: Optional[SpeechToTextParams] = None) -> SpeechToTextResponse: ... |
| 144 | + |
| 145 | + async def speech_to_text( |
| 146 | + self, |
| 147 | + blob: Union[SpeechToTextParams, bytes], |
| 148 | + options: Optional[SpeechToTextParams] = None, |
| 149 | + ) -> SpeechToTextResponse: |
| 150 | + if isinstance(blob, dict): |
| 151 | + resp = await AsyncRequest( |
| 152 | + config=self.config, |
| 153 | + path="/ai/transcribe", |
| 154 | + params=cast(Dict[Any, Any], blob), |
| 155 | + verb="post", |
| 156 | + ).perform_with_content() |
| 157 | + return resp |
| 158 | + |
| 159 | + options = options or {} |
| 160 | + path = build_path(base_path="/ai/transcribe", params=options) |
| 161 | + content_type = options.get("content_type", "application/octet-stream") |
| 162 | + headers = {"Content-Type": content_type} |
| 163 | + |
116 | 164 | resp = await AsyncRequest( |
117 | 165 | config=self.config, |
118 | 166 | path=path, |
119 | | - params=cast(Dict[Any, Any], params), |
| 167 | + params=options, |
| 168 | + data=blob, |
| 169 | + headers=headers, |
120 | 170 | verb="post", |
121 | 171 | ).perform_with_content() |
122 | 172 | return resp |
|
0 commit comments