Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions funasr/auto/auto_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."
)
Comment on lines +173 to +178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While adding a check for ClusterBackend is a great improvement to fail early and provide a clear error message, the other modules imported in the same try-except block (sv_chunk, postprocess, distribute_spk) are not checked.

If their import fails, they will also be None, and calling them later in inference_with_vad will result in a TypeError: 'NoneType' object is not callable.

To make the error handling more robust, I suggest checking for all these modules at once. This ensures that if any of the speaker-related dependencies are missing, the program fails early with a clear error message.

            if any(x is None for x in (sv_chunk, postprocess, distribute_spk, ClusterBackend)):
                raise ImportError("Speaker diarization dependencies failed to import. Check logs and ensure optional packages are installed (e.g., scikit-learn, scipy).")

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"]:
Expand Down