Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
Improve auto-registration import diagnostics
游雁2ca745e5d11a
diff --git a/funasr/__init__.py b/funasr/__init__.pyindex 8fa29d0e1..e466840f9 100644--- a/funasr/__init__.py+++ b/funasr/__init__.py@@ -1,6 +1,10 @@"""Initialize funasr package."""+import importlibimport os+import pkgutil+import traceback+dirname = os.path.dirname(__file__)version_file = os.path.join(dirname, "version.txt")@@ -8,8 +12,25 @@ with open(version_file, "r") as f:__version__ = f.read().strip()-import importlib-import pkgutil+_IMPORT_ERRORS = {}+_IMPORT_ERROR_TRACEBACKS = {}+_IMPORT_DEBUG = os.environ.get("FUNASR_IMPORT_DEBUG") == "1"+_STRICT_IMPORT = os.environ.get("FUNASR_STRICT_IMPORT") == "1"+++def _record_import_error(name, error):+ _IMPORT_ERRORS[name] = f"{error.__class__.__name__}: {error}"+ _IMPORT_ERROR_TRACEBACKS[name] = traceback.format_exc()+ if _IMPORT_DEBUG:+ print(f"Failed to import {name}: {_IMPORT_ERRORS[name]}")+++def get_import_errors():+ return dict(_IMPORT_ERRORS)+++def get_import_error_tracebacks():+ return dict(_IMPORT_ERROR_TRACEBACKS)def import_submodules(package, recursive=True):@@ -17,18 +38,20 @@ def import_submodules(package, recursive=True):try:package = importlib.import_module(package)except Exception as e:- # 如果想要看到导入错误的具体信息,可以取消注释下面的行- # print(f"Failed to import {package}: {e}")- pass+ _record_import_error(package, e)+ if _STRICT_IMPORT:+ raise+ return {}results = {}if not isinstance(package, str):for loader, name, is_pkg in pkgutil.walk_packages(package.__path__, package.__name__ + "."):try:results[name] = importlib.import_module(name)except Exception as e:- # 如果想要看到导入错误的具体信息,可以取消注释下面的行- # print(f"Failed to import {name}: {e}")- pass+ _record_import_error(name, e)+ if _STRICT_IMPORT:+ raise+ continueif recursive and is_pkg:results.update(import_submodules(name))return resultsdiff --git a/funasr/auto/auto_model.py b/funasr/auto/auto_model.pyindex 195fe336a..4b538b03d 100644--- a/funasr/auto/auto_model.py+++ b/funasr/auto/auto_model.py@@ -51,6 +51,48 @@ def _resolve_ncpu(config, fallback=4):return max(value, 1)+def _get_import_errors():+ try:+ import funasr+ except Exception:+ return {}+ get_import_errors = getattr(funasr, "get_import_errors", None)+ if get_import_errors is not None:+ return get_import_errors()+ return dict(getattr(funasr, "_IMPORT_ERRORS", {}))+++def _format_unregistered_component_error(component_type, component_name, registry):+ registered = sorted(registry.keys())+ preview = ", ".join(registered[:80])+ if len(registered) > 80:+ preview += f", ... ({len(registered)} total)"+ if not preview:+ preview = "(none)"++ import_errors = _get_import_errors()+ if import_errors:+ lines = [+ f" - {name}: {error}"+ for name, error in sorted(import_errors.items())[:50]+ ]+ remaining = len(import_errors) - len(lines)+ if remaining > 0:+ lines.append(f" ... {remaining} more import failures hidden")+ import_error_text = "\n".join(lines)+ else:+ import_error_text = " (none recorded)"++ return (+ f"{component_type} '{component_name}' is not registered.\n"+ f"Registered {component_type} keys ({len(registered)}): {preview}\n"+ "Some modules may have failed to import during auto-registration. "+ "Set FUNASR_IMPORT_DEBUG=1 to print failures during import, or "+ "FUNASR_STRICT_IMPORT=1 to fail fast.\n"+ f"Recorded import failures:\n{import_error_text}"+ )++try:from funasr.models.campplus.utils import sv_chunk, postprocess, distribute_spkfrom funasr.models.campplus.cluster_backend import ClusterBackend@@ -292,7 +334,12 @@ class AutoModel:kwargs["frontend"] = frontend# build modelmodel_class = tables.model_classes.get(kwargs["model"])- assert model_class is not None, f'{kwargs["model"]} is not registered'+ if model_class is None:+ raise RuntimeError(+ _format_unregistered_component_error(+ "model", kwargs["model"], tables.model_classes+ )+ )model_conf = {}deep_update(model_conf, kwargs.get("model_conf", {}))deep_update(model_conf, kwargs)diff --git a/tests/test_auto_model.py b/tests/test_auto_model.pyindex d17d9ab41..5418c3288 100644--- a/tests/test_auto_model.py+++ b/tests/test_auto_model.py@@ -1,15 +1,23 @@+import importlib+from pathlib import Path+import sys+import tempfileimport unittest-import torch+import numpy as np+import torch++import funasrfrom funasr.auto.auto_model import AutoModel+class TestAutoModel(unittest.TestCase):def setUp(self):self.base_kwargs = {"model": "damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch","vad_model": "fsmn-vad",- "punc_model":"ct-punc",+ "punc_model": "ct-punc","device": "cpu","batch_size": 1,"disable_update": True,@@ -55,6 +63,92 @@ class TestAutoModel(unittest.TestCase):self.assertEqual(len(progress), 2)self.assertEqual(progress, [(2, 3), (3, 3)])+ def test_import_submodules_records_failures(self):+ old_errors = dict(funasr._IMPORT_ERRORS)+ old_tracebacks = dict(funasr._IMPORT_ERROR_TRACEBACKS)+ package_name = "funasr_import_probe_pkg"++ with tempfile.TemporaryDirectory() as tmp_dir:+ package_dir = Path(tmp_dir) / package_name+ package_dir.mkdir()+ (package_dir / "__init__.py").write_text("", encoding="utf-8")+ (package_dir / "broken.py").write_text(+ 'raise RuntimeError("boom optional dependency")\n', encoding="utf-8"+ )+ sys.path.insert(0, tmp_dir)+ try:+ funasr._IMPORT_ERRORS.clear()+ funasr._IMPORT_ERROR_TRACEBACKS.clear()+ package = importlib.import_module(package_name)++ imported = funasr.import_submodules(package)++ failed_module = f"{package_name}.broken"+ self.assertEqual(imported, {})+ self.assertIn(failed_module, funasr._IMPORT_ERRORS)+ self.assertIn("boom optional dependency", funasr._IMPORT_ERRORS[failed_module])+ self.assertIn(failed_module, funasr._IMPORT_ERROR_TRACEBACKS)+ finally:+ sys.path.remove(tmp_dir)+ for module_name in list(sys.modules):+ if module_name == package_name or module_name.startswith(package_name + "."):+ sys.modules.pop(module_name, None)+ funasr._IMPORT_ERRORS.clear()+ funasr._IMPORT_ERRORS.update(old_errors)+ funasr._IMPORT_ERROR_TRACEBACKS.clear()+ funasr._IMPORT_ERROR_TRACEBACKS.update(old_tracebacks)++ def test_import_submodules_strict_import_raises(self):+ old_strict_import = funasr._STRICT_IMPORT+ package_name = "funasr_import_strict_probe_pkg"++ with tempfile.TemporaryDirectory() as tmp_dir:+ package_dir = Path(tmp_dir) / package_name+ package_dir.mkdir()+ (package_dir / "__init__.py").write_text("", encoding="utf-8")+ (package_dir / "broken.py").write_text(+ 'raise RuntimeError("strict optional dependency")\n', encoding="utf-8"+ )+ sys.path.insert(0, tmp_dir)+ try:+ funasr._STRICT_IMPORT = True+ package = importlib.import_module(package_name)++ with self.assertRaisesRegex(RuntimeError, "strict optional dependency"):+ funasr.import_submodules(package)+ finally:+ funasr._STRICT_IMPORT = old_strict_import+ sys.path.remove(tmp_dir)+ for module_name in list(sys.modules):+ if module_name == package_name or module_name.startswith(package_name + "."):+ sys.modules.pop(module_name, None)++ def test_unregistered_model_reports_import_failures(self):+ old_errors = dict(funasr._IMPORT_ERRORS)+ try:+ funasr._IMPORT_ERRORS.clear()+ funasr._IMPORT_ERRORS["funasr.models.fake_model"] = (+ "ModuleNotFoundError: No module named 'fake_dep'"+ )++ with self.assertRaises(RuntimeError) as context:+ AutoModel.build_model(+ model="missing_model",+ model_conf={},+ device="cpu",+ ncpu=1,+ )++ message = str(context.exception)+ self.assertIn("missing_model", message)+ self.assertIn("funasr.models.fake_model", message)+ self.assertIn("fake_dep", message)+ self.assertIn("FUNASR_IMPORT_DEBUG", message)+ self.assertIn("FUNASR_STRICT_IMPORT", message)+ finally:+ funasr._IMPORT_ERRORS.clear()+ funasr._IMPORT_ERRORS.update(old_errors)+if __name__ == '__main__':unittest.main()