Warmup functionality: add timeout option (for VM not connected to internet); False option to disable warmup
Browse files
whisper_fastapi_online_server.py
CHANGED
@@ -10,7 +10,7 @@ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|
10 |
from fastapi.responses import HTMLResponse
|
11 |
from fastapi.middleware.cors import CORSMiddleware
|
12 |
|
13 |
-
from whisper_streaming_custom.whisper_online import backend_factory, online_factory, add_shared_args,warmup_asr
|
14 |
from timed_objects import ASRToken
|
15 |
|
16 |
import math
|
@@ -42,8 +42,13 @@ parser.add_argument(
|
|
42 |
parser.add_argument(
|
43 |
"--warmup-file",
|
44 |
type=str,
|
|
|
45 |
dest="warmup_file",
|
46 |
-
help="
|
|
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
parser.add_argument(
|
|
|
10 |
from fastapi.responses import HTMLResponse
|
11 |
from fastapi.middleware.cors import CORSMiddleware
|
12 |
|
13 |
+
from whisper_streaming_custom.whisper_online import backend_factory, online_factory, add_shared_args, warmup_asr
|
14 |
from timed_objects import ASRToken
|
15 |
|
16 |
import math
|
|
|
42 |
parser.add_argument(
|
43 |
"--warmup-file",
|
44 |
type=str,
|
45 |
+
default=None,
|
46 |
dest="warmup_file",
|
47 |
+
help="""
|
48 |
+
The path to a speech audio wav file to warm up Whisper so that the very first chunk processing is fast.
|
49 |
+
If not set, uses https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav.
|
50 |
+
If False, no warmup is performed.
|
51 |
+
""",
|
52 |
)
|
53 |
|
54 |
parser.add_argument(
|
whisper_streaming_custom/whisper_online.py
CHANGED
@@ -227,34 +227,57 @@ def asr_factory(args, logfile=sys.stderr):
|
|
227 |
online = online_factory(args, asr, tokenizer, logfile=logfile)
|
228 |
return asr, online
|
229 |
|
230 |
-
def warmup_asr(asr, warmup_file=None):
|
231 |
"""
|
232 |
Warmup the ASR model by transcribing a short audio file.
|
233 |
"""
|
234 |
-
|
235 |
-
|
236 |
-
|
|
|
|
|
237 |
# Download JFK sample if not already present
|
238 |
-
import tempfile
|
239 |
-
import os
|
240 |
-
|
241 |
-
|
242 |
jfk_url = "https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav"
|
243 |
temp_dir = tempfile.gettempdir()
|
244 |
warmup_file = os.path.join(temp_dir, "whisper_warmup_jfk.wav")
|
245 |
|
246 |
if not os.path.exists(warmup_file):
|
247 |
logger.debug(f"Downloading warmup file from {jfk_url}")
|
|
|
|
|
248 |
import urllib.request
|
249 |
-
urllib.
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
# Process the audio
|
256 |
asr.transcribe(audio)
|
257 |
-
|
258 |
|
259 |
logger.info("Whisper is warmed up")
|
260 |
|
|
|
227 |
online = online_factory(args, asr, tokenizer, logfile=logfile)
|
228 |
return asr, online
|
229 |
|
230 |
+
def warmup_asr(asr, warmup_file=None, timeout=5):
|
231 |
"""
|
232 |
Warmup the ASR model by transcribing a short audio file.
|
233 |
"""
|
234 |
+
import os
|
235 |
+
import tempfile
|
236 |
+
|
237 |
+
|
238 |
+
if warmup_file is None:
|
239 |
# Download JFK sample if not already present
|
|
|
|
|
|
|
|
|
240 |
jfk_url = "https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav"
|
241 |
temp_dir = tempfile.gettempdir()
|
242 |
warmup_file = os.path.join(temp_dir, "whisper_warmup_jfk.wav")
|
243 |
|
244 |
if not os.path.exists(warmup_file):
|
245 |
logger.debug(f"Downloading warmup file from {jfk_url}")
|
246 |
+
print(f"Downloading warmup file from {jfk_url}")
|
247 |
+
import time
|
248 |
import urllib.request
|
249 |
+
import urllib.error
|
250 |
+
import socket
|
251 |
+
|
252 |
+
original_timeout = socket.getdefaulttimeout()
|
253 |
+
socket.setdefaulttimeout(timeout)
|
254 |
+
|
255 |
+
start_time = time.time()
|
256 |
+
try:
|
257 |
+
urllib.request.urlretrieve(jfk_url, warmup_file)
|
258 |
+
logger.debug(f"Download successful in {time.time() - start_time:.2f}s")
|
259 |
+
except (urllib.error.URLError, socket.timeout) as e:
|
260 |
+
logger.warning(f"Download failed: {e}. Proceeding without warmup.")
|
261 |
+
return False
|
262 |
+
finally:
|
263 |
+
socket.setdefaulttimeout(original_timeout)
|
264 |
+
elif not warmup_file:
|
265 |
+
return False
|
266 |
+
|
267 |
+
if not warmup_file or not os.path.exists(warmup_file) or os.path.getsize(warmup_file) == 0:
|
268 |
+
logger.warning(f"Warmup file {warmup_file} invalid or missing.")
|
269 |
+
return False
|
270 |
+
|
271 |
+
print(f"Warmping up Whisper with {warmup_file}")
|
272 |
+
try:
|
273 |
+
import librosa
|
274 |
+
audio, sr = librosa.load(warmup_file, sr=16000)
|
275 |
+
except Exception as e:
|
276 |
+
logger.warning(f"Failed to load audio file: {e}")
|
277 |
+
return False
|
278 |
+
|
279 |
# Process the audio
|
280 |
asr.transcribe(audio)
|
|
|
281 |
|
282 |
logger.info("Whisper is warmed up")
|
283 |
|