Apply ruff lint and rename cryptic identifiers to readable names

This commit is contained in:
Stefano Zini
2026-11-05 09:09:20 +01:00
parent 19de4654ff
commit ead33657c7
24 changed files with 4459 additions and 1856 deletions
+18 -11
View File
@@ -1,27 +1,34 @@
# Shared helpers: makes the CUDA DLLs findable and loads the Whisper model.
import glob, os, site
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 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"]
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
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
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")
whisper_model = 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)
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"