1- from typing import Any , Dict , List , cast , Union , Optional
1+ from typing import Any , Dict , List , cast , Union , Optional , overload
22from typing_extensions import NotRequired , TypedDict
33from .request import Request , RequestConfig
44from .async_request import AsyncRequest , AsyncRequestConfig
55from ._config import ClientConfig
66from typing import Any , Dict , List , cast
77from typing_extensions import NotRequired , TypedDict
88from .custom_typing import SupportedAccents
9+ from .helpers import build_path
910
1011
1112class TextToSpeechParams (TypedDict ):
@@ -29,6 +30,7 @@ class SpeechToTextParams(TypedDict):
2930 by_speaker : NotRequired [bool ]
3031 webhook_url : NotRequired [str ]
3132 batch_size : NotRequired [int ]
33+ chunk_duration : NotRequired [int ]
3234
3335
3436class ChunkParams (TypedDict ):
@@ -63,16 +65,41 @@ def __init__(
6365 disable_request_logging = disable_request_logging ,
6466 )
6567
66- def speech_to_text (self , params : SpeechToTextParams ) -> SpeechToTextResponse :
67- 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+
6892 resp = Request (
6993 config = self .config ,
7094 path = path ,
71- params = cast (Dict [Any , Any ], params ),
95+ params = options ,
96+ data = blob ,
97+ headers = headers ,
7298 verb = "post" ,
7399 ).perform_with_content ()
74100 return resp
75101
102+
76103 def text_to_speech (self , params : TextToSpeechParams ) -> TextToSpeechResponse :
77104 path = "/ai/tts"
78105 resp = Request (
@@ -110,12 +137,36 @@ def __init__(
110137 disable_request_logging = disable_request_logging ,
111138 )
112139
113- async def speech_to_text (self , params : SpeechToTextParams ) -> SpeechToTextResponse :
114- 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+
115164 resp = await AsyncRequest (
116165 config = self .config ,
117166 path = path ,
118- params = cast (Dict [Any , Any ], params ),
167+ params = options ,
168+ data = blob ,
169+ headers = headers ,
119170 verb = "post" ,
120171 ).perform_with_content ()
121172 return resp
0 commit comments