28 lines
1.4 KiB
Python
28 lines
1.4 KiB
Python
# Shared helpers: makes the CUDA DLLs findable and loads the Whisper model.
|
|
import glob, os, site
|
|
import numpy as np
|
|
|
|
# pip nvidia-* wheels ship cublas/cudnn DLLs; ctranslate2 needs them on PATH (Windows)
|
|
# Each nvidia/<lib>/bin folder from the pip wheels is added to the DLL search path and PATH.
|
|
for sp in site.getsitepackages():
|
|
for b in glob.glob(os.path.join(sp, "nvidia", "*", "bin")):
|
|
os.add_dll_directory(b); os.environ["PATH"] = b + os.pathsep + os.environ["PATH"]
|
|
|
|
# Import only after the DLL path is set up above, or CUDA libs fail to load.
|
|
from faster_whisper import WhisperModel
|
|
|
|
CORES = max(1, (os.cpu_count() or 2) // 2) # ponytail: logical/2 ~ physical cores (no psutil); exact count if hybrid CPUs need it
|
|
|
|
def load_model(name):
|
|
"""CUDA if it really works (probe one decode), else CPU int8."""
|
|
# Try CUDA first; the probe below forces the real CUDA libs to load now, so a missing DLL fails here.
|
|
try:
|
|
m = WhisperModel(name, device="cuda", compute_type="default")
|
|
# One second of silence forces an actual decode, not just a model load.
|
|
list(m.transcribe(np.zeros(16000, np.float32))[0])
|
|
return m, "cuda"
|
|
except Exception as e:
|
|
print("cuda failed, using cpu:", e, flush=True)
|
|
# CPU fallback: int8 quantisation, CORES threads.
|
|
return WhisperModel(name, device="cpu", compute_type="int8", cpu_threads=CORES), "cpu"
|