9
9
APIRouter ,
10
10
Form ,
11
11
Query ,
12
+ Request ,
12
13
Response ,
13
14
UploadFile ,
14
15
WebSocket ,
30
31
from faster_whisper_server .core import Segment , segments_to_srt , segments_to_text , segments_to_vtt
31
32
from faster_whisper_server .dependencies import ConfigDependency , ModelManagerDependency , get_config
32
33
from faster_whisper_server .server_models import (
34
+ DEFAULT_TIMESTAMP_GRANULARITIES ,
35
+ TIMESTAMP_GRANULARITIES_COMBINATIONS ,
33
36
TimestampGranularities ,
34
37
TranscriptionJsonResponse ,
35
38
TranscriptionVerboseJsonResponse ,
@@ -150,6 +153,18 @@ def translate_file(
150
153
return segments_to_response (segments , transcription_info , response_format )
151
154
152
155
156
+ # HACK: Since Form() doesn't support `alias`, we need to use a workaround.
157
+ async def get_timestamp_granularities (request : Request ) -> TimestampGranularities :
158
+ form = await request .form ()
159
+ if form .get ("timestamp_granularities[]" ) is None :
160
+ return DEFAULT_TIMESTAMP_GRANULARITIES
161
+ timestamp_granularities = form .getlist ("timestamp_granularities[]" )
162
+ assert (
163
+ timestamp_granularities in TIMESTAMP_GRANULARITIES_COMBINATIONS
164
+ ), f"{ timestamp_granularities } is not a valid value for `timestamp_granularities[]`."
165
+ return timestamp_granularities
166
+
167
+
153
168
# https://platform.openai.com/docs/api-reference/audio/createTranscription
154
169
# https://github.com/openai/openai-openapi/blob/master/openapi.yaml#L8915
155
170
@router .post (
@@ -159,6 +174,7 @@ def translate_file(
159
174
def transcribe_file (
160
175
config : ConfigDependency ,
161
176
model_manager : ModelManagerDependency ,
177
+ request : Request ,
162
178
file : Annotated [UploadFile , Form ()],
163
179
model : Annotated [ModelName | None , Form ()] = None ,
164
180
language : Annotated [Language | None , Form ()] = None ,
@@ -167,6 +183,7 @@ def transcribe_file(
167
183
temperature : Annotated [float , Form ()] = 0.0 ,
168
184
timestamp_granularities : Annotated [
169
185
TimestampGranularities ,
186
+ # WARN: `alias` doesn't actually work.
170
187
Form (alias = "timestamp_granularities[]" ),
171
188
] = ["segment" ],
172
189
stream : Annotated [bool , Form ()] = False ,
@@ -178,6 +195,11 @@ def transcribe_file(
178
195
language = config .default_language
179
196
if response_format is None :
180
197
response_format = config .default_response_format
198
+ timestamp_granularities = asyncio .run (get_timestamp_granularities (request ))
199
+ if timestamp_granularities != DEFAULT_TIMESTAMP_GRANULARITIES and response_format != ResponseFormat .VERBOSE_JSON :
200
+ logger .warning (
201
+ "It only makes sense to provide `timestamp_granularities[]` when `response_format` is set to `verbose_json`. See https://platform.openai.com/docs/api-reference/audio/createTranscription#audio-createtranscription-timestamp_granularities." # noqa: E501
202
+ )
181
203
whisper = model_manager .load_model (model )
182
204
segments , transcription_info = whisper .transcribe (
183
205
file .file ,
0 commit comments