35 lines
1.5 KiB
Python
35 lines
1.5 KiB
Python
# Shared helpers: makes the CUDA DLLs findable and loads the Whisper model.
|
|
import glob
|
|
import os
|
|
import 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 site_packages in site.getsitepackages():
|
|
for bin_dir in glob.glob(os.path.join(site_packages, "nvidia", "*", "bin")):
|
|
os.add_dll_directory(bin_dir)
|
|
os.environ["PATH"] = bin_dir + 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 # noqa: E402
|
|
|
|
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:
|
|
whisper_model = WhisperModel(name, device="cuda", compute_type="default")
|
|
# One second of silence forces an actual decode, not just a model load.
|
|
list(whisper_model.transcribe(np.zeros(16000, np.float32))[0])
|
|
return whisper_model, "cuda"
|
|
except Exception as error:
|
|
print("cuda failed, using cpu:", error, flush=True)
|
|
# CPU fallback: int8 quantisation, CORES threads.
|
|
return WhisperModel(name, device="cpu", compute_type="int8", cpu_threads=CORES), "cpu"
|