From 303f63bbe3686fa31693e157e1d1d780c083ffd8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 20 Feb 2026 23:47:15 -0800 Subject: [PATCH] Fix #2806: handle ClusterBackend import failure with clear error message Replace bare except/pass with proper fallback initialization and a warning log when speaker-related modules fail to import. At the usage site, raise an informative ImportError instead of a cryptic NameError so users understand which dependency is missing. --- funasr/auto/auto_model.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/funasr/auto/auto_model.py b/funasr/auto/auto_model.py index 9bf1ad57e..e9c275626 100644 --- a/funasr/auto/auto_model.py +++ b/funasr/auto/auto_model.py @@ -40,11 +40,12 @@ def _resolve_ncpu(config, fallback=4): value = fallback return max(value, 1) +sv_chunk = postprocess = distribute_spk = ClusterBackend = None try: from funasr.models.campplus.utils import sv_chunk, postprocess, distribute_spk from funasr.models.campplus.cluster_backend import ClusterBackend -except: - pass +except Exception as e: + logging.warning(f"Failed to import speaker-related modules: {e}. Speaker diarization will be unavailable.") def prepare_data_iterator(data_in, input_len=None, data_type=None, key=None): @@ -169,6 +170,12 @@ def __init__(self, **kwargs): spk_kwargs["device"] = kwargs["device"] spk_kwargs.setdefault("ncpu", kwargs.get("ncpu", 4)) spk_model, spk_kwargs = self.build_model(**spk_kwargs) + if ClusterBackend is None: + raise ImportError( + "ClusterBackend is not available. This is likely because required dependencies " + "(e.g. scikit-learn with HDBSCAN support, scipy) failed to import. " + "Please ensure scikit-learn>=1.3.0 and scipy are properly installed." + ) self.cb_model = ClusterBackend(**cb_kwargs).to(kwargs["device"]) spk_mode = kwargs.get("spk_mode", "punc_segment") if spk_mode not in ["default", "vad_segment", "punc_segment"]: