start_paddlex_with_adapter.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. """
  3. PaddleX 服务启动包装器 - 自动激活适配器
  4. 用于启动 PaddleX HTTP API 服务并自动激活增强适配器。
  5. 适配器位于同一目录下的 adapters/ 子目录中。
  6. 使用方法:
  7. python start_paddlex_with_adapter.py --serve --port 8111 --device gpu --pipeline config.yaml
  8. """
  9. import os
  10. import sys
  11. from pathlib import Path
  12. def main():
  13. # 🎯 在当前进程中激活适配器
  14. if os.getenv("PADDLEX_ENABLE_ADAPTER", "").lower() in ("true", "1", "yes"):
  15. # 适配器位于同一目录下的 adapters/ 子目录
  16. script_dir = Path(__file__).parent
  17. adapters_path = script_dir / "adapters"
  18. if str(adapters_path) not in sys.path:
  19. sys.path.insert(0, str(adapters_path))
  20. try:
  21. from adapters import apply_table_recognition_adapter, apply_enhanced_doc_preprocessor
  22. if apply_table_recognition_adapter() and apply_enhanced_doc_preprocessor():
  23. print("✅ Adapter activated in wrapper process")
  24. else:
  25. print("⚠️ Failed to activate adapter")
  26. except Exception as e:
  27. print(f"⚠️ Failed to activate adapter: {e}")
  28. # 🎯 解析命令行参数
  29. args = sys.argv[1:] # 去掉脚本名
  30. # 🎯 导入 PaddleX CLI 主函数并在当前进程执行
  31. from paddlex.paddlex_cli import main as paddlex_main
  32. # 替换 sys.argv 以传递参数
  33. sys.argv = ['paddlex'] + args
  34. # 执行 PaddleX
  35. paddlex_main()
  36. if __name__ == "__main__":
  37. main()