11from __future__ import annotations
22
3- from typing import Any , Union , BinaryIO , Optional
43from pathlib import Path
4+ from typing import Any , BinaryIO , Dict , Optional , Tuple , Union
55
66from together .abstract import api_requestor
7- from together .together_response import TogetherResponse
87from together .types import (
9- AudioTranscriptionRequest ,
8+ AudioTimestampGranularities ,
109 AudioTranscriptionResponse ,
11- AudioTranscriptionVerboseResponse ,
1210 AudioTranscriptionResponseFormat ,
13- AudioTimestampGranularities ,
11+ AudioTranscriptionVerboseResponse ,
1412 TogetherClient ,
1513 TogetherRequest ,
1614)
@@ -29,7 +27,9 @@ def create(
2927 prompt : Optional [str ] = None ,
3028 response_format : Union [str , AudioTranscriptionResponseFormat ] = "json" ,
3129 temperature : float = 0.0 ,
32- timestamp_granularities : Optional [Union [str , AudioTimestampGranularities ]] = None ,
30+ timestamp_granularities : Optional [
31+ Union [str , AudioTimestampGranularities ]
32+ ] = None ,
3333 ** kwargs : Any ,
3434 ) -> Union [AudioTranscriptionResponse , AudioTranscriptionVerboseResponse ]:
3535 """
@@ -56,17 +56,17 @@ def create(
5656 Returns:
5757 The transcribed text in the requested format.
5858 """
59-
59+
6060 requestor = api_requestor .APIRequestor (
6161 client = self ._client ,
6262 )
6363
6464 # Handle file input - could be a path, URL, or file object
65- files_data = {}
65+ files_data : Dict [ str , Union [ Tuple [ None , str ], BinaryIO ]] = {}
6666 params_data = {}
67-
67+
6868 if isinstance (file , (str , Path )):
69- if isinstance (file , str ) and file .startswith ((' http://' , ' https://' )):
69+ if isinstance (file , str ) and file .startswith ((" http://" , " https://" )):
7070 # URL string - send as multipart field
7171 files_data ["file" ] = (None , file )
7272 else :
@@ -78,22 +78,29 @@ def create(
7878 files_data ["file" ] = file
7979
8080 # Build request parameters
81- params_data .update ({
82- "model" : model ,
83- "response_format" : response_format if isinstance (response_format , str ) else response_format .value ,
84- "temperature" : temperature ,
85- })
86-
81+ params_data .update (
82+ {
83+ "model" : model ,
84+ "response_format" : (
85+ response_format .value
86+ if hasattr (response_format , "value" )
87+ else response_format
88+ ),
89+ "temperature" : temperature ,
90+ }
91+ )
92+
8793 if language is not None :
8894 params_data ["language" ] = language
89-
95+
9096 if prompt is not None :
9197 params_data ["prompt" ] = prompt
92-
98+
9399 if timestamp_granularities is not None :
94100 params_data ["timestamp_granularities" ] = (
95- timestamp_granularities if isinstance (timestamp_granularities , str )
96- else timestamp_granularities .value
101+ timestamp_granularities .value
102+ if hasattr (timestamp_granularities , "value" )
103+ else timestamp_granularities
97104 )
98105
99106 # Add any additional kwargs
@@ -113,13 +120,17 @@ def create(
113120 if files_data and "file" in files_data :
114121 try :
115122 # Only close if it's a file object (not a tuple for URL)
116- if hasattr (files_data ["file" ], 'close' ):
117- files_data ["file" ].close ()
123+ file_obj = files_data ["file" ]
124+ if hasattr (file_obj , "close" ) and not isinstance (file_obj , tuple ):
125+ file_obj .close ()
118126 except :
119127 pass
120128
121129 # Parse response based on format
122- if response_format == "verbose_json" or response_format == AudioTranscriptionResponseFormat .VERBOSE_JSON :
130+ if (
131+ response_format == "verbose_json"
132+ or response_format == AudioTranscriptionResponseFormat .VERBOSE_JSON
133+ ):
123134 return AudioTranscriptionVerboseResponse (** response .data )
124135 else :
125136 return AudioTranscriptionResponse (** response .data )
@@ -133,12 +144,14 @@ async def create(
133144 self ,
134145 * ,
135146 file : Union [str , BinaryIO , Path ],
136- model : str = "openai/whisper-large-v3" ,
147+ model : str = "openai/whisper-large-v3" ,
137148 language : Optional [str ] = None ,
138149 prompt : Optional [str ] = None ,
139150 response_format : Union [str , AudioTranscriptionResponseFormat ] = "json" ,
140151 temperature : float = 0.0 ,
141- timestamp_granularities : Optional [Union [str , AudioTimestampGranularities ]] = None ,
152+ timestamp_granularities : Optional [
153+ Union [str , AudioTimestampGranularities ]
154+ ] = None ,
142155 ** kwargs : Any ,
143156 ) -> Union [AudioTranscriptionResponse , AudioTranscriptionVerboseResponse ]:
144157 """
@@ -165,17 +178,17 @@ async def create(
165178 Returns:
166179 The transcribed text in the requested format.
167180 """
168-
181+
169182 requestor = api_requestor .APIRequestor (
170183 client = self ._client ,
171184 )
172185
173186 # Handle file input - could be a path, URL, or file object
174- files_data = {}
187+ files_data : Dict [ str , Union [ Tuple [ None , str ], BinaryIO ]] = {}
175188 params_data = {}
176-
189+
177190 if isinstance (file , (str , Path )):
178- if isinstance (file , str ) and file .startswith ((' http://' , ' https://' )):
191+ if isinstance (file , str ) and file .startswith ((" http://" , " https://" )):
179192 # URL string - send as multipart field
180193 files_data ["file" ] = (None , file )
181194 else :
@@ -187,22 +200,37 @@ async def create(
187200 files_data ["file" ] = file
188201
189202 # Build request parameters
190- params_data .update ({
191- "model" : model ,
192- "response_format" : response_format if isinstance (response_format , str ) else response_format .value ,
193- "temperature" : temperature ,
194- })
195-
203+ params_data .update (
204+ {
205+ "model" : model ,
206+ "response_format" : (
207+ response_format
208+ if isinstance (response_format , str )
209+ else (
210+ response_format .value
211+ if hasattr (response_format , "value" )
212+ else response_format
213+ )
214+ ),
215+ "temperature" : temperature ,
216+ }
217+ )
218+
196219 if language is not None :
197220 params_data ["language" ] = language
198-
221+
199222 if prompt is not None :
200223 params_data ["prompt" ] = prompt
201-
224+
202225 if timestamp_granularities is not None :
203226 params_data ["timestamp_granularities" ] = (
204- timestamp_granularities if isinstance (timestamp_granularities , str )
205- else timestamp_granularities .value
227+ timestamp_granularities
228+ if isinstance (timestamp_granularities , str )
229+ else (
230+ timestamp_granularities .value
231+ if hasattr (timestamp_granularities , "value" )
232+ else timestamp_granularities
233+ )
206234 )
207235
208236 # Add any additional kwargs
@@ -222,13 +250,17 @@ async def create(
222250 if files_data and "file" in files_data :
223251 try :
224252 # Only close if it's a file object (not a tuple for URL)
225- if hasattr (files_data ["file" ], 'close' ):
226- files_data ["file" ].close ()
253+ file_obj = files_data ["file" ]
254+ if hasattr (file_obj , "close" ) and not isinstance (file_obj , tuple ):
255+ file_obj .close ()
227256 except :
228257 pass
229258
230259 # Parse response based on format
231- if response_format == "verbose_json" or response_format == AudioTranscriptionResponseFormat .VERBOSE_JSON :
260+ if (
261+ response_format == "verbose_json"
262+ or response_format == AudioTranscriptionResponseFormat .VERBOSE_JSON
263+ ):
232264 return AudioTranscriptionVerboseResponse (** response .data )
233265 else :
234- return AudioTranscriptionResponse (** response .data )
266+ return AudioTranscriptionResponse (** response .data )
0 commit comments