test_local.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. """
  3. 本地测试 parse-service
  4. 先直接用 Python 测试解析,再启动 FastAPI 服务
  5. """
  6. import sys
  7. import os
  8. # 添加当前目录到路径
  9. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  10. from core.router import ParserFactory
  11. from utils.logger import log
  12. async def test_parse_file(file_path):
  13. """测试解析单个文件"""
  14. log.info(f"开始测试解析文件: {file_path}")
  15. if not os.path.exists(file_path):
  16. log.error(f"文件不存在: {file_path}")
  17. return None
  18. factory = ParserFactory()
  19. result = await factory.parse(file_path)
  20. log.info(f"解析完成! 文件类型: {result.file_type}")
  21. log.info(f"内容长度: {len(result.content)}")
  22. log.info(f"元数据: {result.metadata}")
  23. print("\n" + "="*80)
  24. print("解析结果:")
  25. print("="*80)
  26. print(result.content[:1000] + "..." if len(result.content) > 1000 else result.content)
  27. print("="*80)
  28. # 生成性能报告
  29. report = factory.generate_performance_report()
  30. print("\n" + report)
  31. return result
  32. if __name__ == "__main__":
  33. import asyncio
  34. # 先尝试用 duomotai 里的示例文件
  35. example_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "duomotai", "examples")
  36. if len(sys.argv) > 1:
  37. test_file = sys.argv[1]
  38. else:
  39. # 找一个示例文件
  40. test_file = None
  41. if os.path.exists(example_dir):
  42. for f in os.listdir(example_dir):
  43. if f.endswith(('.pdf', '.docx', '.xlsx', '.png', '.jpg')):
  44. test_file = os.path.join(example_dir, f)
  45. break
  46. if not test_file:
  47. print("用法: python test_local.py <文件路径>")
  48. print("或在 duomotai/examples 目录下放一个测试文件")
  49. sys.exit(1)
  50. print(f"测试文件: {test_file}")
  51. asyncio.run(test_parse_file(test_file))