run_streamlit_validator.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. """
  3. Streamlit OCR校验工具启动脚本
  4. """
  5. import subprocess
  6. import sys
  7. import os
  8. from pathlib import Path
  9. def check_streamlit():
  10. """检查Streamlit是否已安装"""
  11. try:
  12. import streamlit
  13. return True
  14. except ImportError:
  15. return False
  16. def install_dependencies():
  17. """安装必要的依赖"""
  18. print("📦 安装Streamlit依赖...")
  19. dependencies = [
  20. "streamlit",
  21. "plotly",
  22. "pandas",
  23. "pillow",
  24. "opencv-python"
  25. ]
  26. for dep in dependencies:
  27. try:
  28. print(f" 安装 {dep}...")
  29. subprocess.check_call([sys.executable, "-m", "pip", "install", dep],
  30. stdout=subprocess.DEVNULL,
  31. stderr=subprocess.DEVNULL)
  32. print(f" ✅ {dep} 安装成功")
  33. except subprocess.CalledProcessError:
  34. print(f" ❌ {dep} 安装失败")
  35. return False
  36. return True
  37. def main():
  38. """主函数"""
  39. print("🚀 启动Streamlit OCR校验工具")
  40. print("=" * 50)
  41. # 检查Streamlit
  42. if not check_streamlit():
  43. print("❌ Streamlit未安装")
  44. choice = input("是否自动安装依赖?(y/n): ")
  45. if choice.lower() == 'y':
  46. if not install_dependencies():
  47. print("❌ 依赖安装失败,请手动安装:")
  48. print("pip install streamlit plotly pandas pillow opencv-python")
  49. return
  50. else:
  51. print("请手动安装依赖:")
  52. print("pip install streamlit plotly pandas pillow opencv-python")
  53. return
  54. # 检查OCR数据
  55. output_dir = Path("output")
  56. if not output_dir.exists() or not any(output_dir.rglob("*.json")):
  57. print("⚠️ 未找到OCR数据文件")
  58. print(" 请先运行OCR处理生成数据文件")
  59. print(" python3 ocr_by_vlm.py sample_data/your_image.png")
  60. print("")
  61. # 启动Streamlit
  62. print("🌐 启动Streamlit应用...")
  63. print(" 浏览器将自动打开 http://localhost:8501")
  64. print(" 按 Ctrl+C 停止应用")
  65. print("")
  66. try:
  67. # 启动Streamlit应用
  68. subprocess.run([
  69. sys.executable, "-m", "streamlit", "run",
  70. "streamlit_ocr_validator.py",
  71. "--server.headless", "false",
  72. "--server.port", "8501"
  73. ])
  74. except KeyboardInterrupt:
  75. print("\n👋 应用已停止")
  76. except FileNotFoundError:
  77. print("❌ streamlit_ocr_validator.py 文件不存在")
  78. except Exception as e:
  79. print(f"❌ 启动失败: {e}")
  80. if __name__ == "__main__":
  81. main()