""" PaddleX 适配器自动加载钩子 放置在 Python 搜索路径中,会在任何 Python 进程启动时自动执行 """ import os import sys def load_paddlex_adapters(): """自动加载 PaddleX 适配器""" # 只有设置了环境变量才激活 if os.getenv("PADDLEX_ENABLE_TABLE_ADAPTER", "").lower() not in ("true", "1", "yes"): return try: # 确保适配器路径在 sys.path adapter_base = os.path.dirname(os.path.abspath(__file__)) adapters_path = os.path.join(adapter_base, 'adapters') if adapters_path not in sys.path: sys.path.insert(0, adapters_path) # 延迟导入,只有在实际使用时才加载 def activate_adapter(): try: from adapters.table_recognition_adapter import apply_table_recognition_adapter, restore_original_function if apply_table_recognition_adapter(): print("✅ [sitecustomize] Table recognition adapter activated") except Exception as e: print(f"⚠️ [sitecustomize] Failed to activate adapter: {e}") # 监听 PaddleX 导入事件 import importlib.abc import importlib.machinery class AdapterLoader(importlib.abc.MetaPathFinder): def find_module(self, fullname, path=None): if fullname == "paddlex.inference.pipelines.table_recognition.pipeline_v2": # 在目标模块导入后激活适配器 activate_adapter() return None sys.meta_path.insert(0, AdapterLoader()) except Exception as e: print(f"⚠️ [sitecustomize] Error setting up adapter hook: {e}") # 自动执行 load_paddlex_adapters()