#!/usr/bin/env python3 """ Deployment Test Script for HuggingFace Spaces Tests if the app can start without dependency errors """ import sys import subprocess import importlib from pathlib import Path def test_imports(): """Test that all critical imports work""" print("šŸ” Testing critical imports...") critical_imports = [ "gradio", "google.genai", "reportlab", "pypdf", "pandas", "PIL", "dotenv" ] failed = [] for module in critical_imports: try: importlib.import_module(module) print(f" āœ… {module}") except ImportError as e: print(f" āŒ {module}: {e}") failed.append((module, str(e))) if failed: print("\nāŒ Import test failed!") for module, error in failed: print(f" - {module}: {error}") return False print("āœ… All imports successful!\n") return True def test_gradio_version(): """Test that Gradio version matches README.md""" print("šŸ” Testing Gradio version compatibility...") import gradio as gr # Read README.md to get expected version readme_path = Path(__file__).parent / "README.md" expected_version = None if readme_path.exists(): with open(readme_path, 'r') as f: for line in f: if line.strip().startswith('sdk_version:'): expected_version = line.split(':')[1].strip() break actual_version = gr.__version__ if expected_version: print(f" Expected (README.md): {expected_version}") print(f" Actual (installed): {actual_version}") if actual_version == expected_version: print(" āœ… Versions match!") else: print(" āš ļø Version mismatch (may still work)") else: print(f" ā„¹ļø Installed version: {actual_version}") # Test built-in File component for PDF support try: test_file = gr.File(label="Test", file_types=[".pdf"]) print(" āœ… Gradio File component supports PDF files") except Exception as e: print(f" āŒ File component error: {e}") return False print() return True def test_app_syntax(): """Test that app.py has no syntax errors""" print("šŸ” Testing app.py syntax...") app_path = Path(__file__).parent / "app.py" if not app_path.exists(): print(" āŒ app.py not found!") return False try: with open(app_path, 'r') as f: compile(f.read(), str(app_path), 'exec') print(" āœ… No syntax errors\n") return True except SyntaxError as e: print(f" āŒ Syntax error: {e}\n") return False def test_dependencies(): """Test that requirements.txt dependencies can be installed""" print("šŸ” Testing requirements.txt...") req_path = Path(__file__).parent / "requirements.txt" if not req_path.exists(): print(" āŒ requirements.txt not found!") return False with open(req_path, 'r') as f: deps = [line.strip() for line in f if line.strip() and not line.startswith('#')] print(f" Found {len(deps)} dependencies:") for dep in deps: print(f" - {dep}") print(" āœ… Requirements file valid\n") return True def test_env_vars(): """Check for required environment variables""" print("šŸ” Checking environment setup...") import os required_vars = ["GEMINI_API_KEY"] optional_vars = ["SEARCH_API_KEY", "SEARCH_ENGINE_ID"] missing_required = [] for var in required_vars: if os.getenv(var): print(f" āœ… {var} is set") else: print(f" āš ļø {var} NOT set (REQUIRED for app to work)") missing_required.append(var) for var in optional_vars: if os.getenv(var): print(f" āœ… {var} is set") else: print(f" ā„¹ļø {var} not set (optional)") if missing_required: print(f"\n āš ļø Missing required variables: {', '.join(missing_required)}") print(" ā„¹ļø App will fail at runtime without these!") print() return len(missing_required) == 0 def test_output_directory(): """Check if output directory exists""" print("šŸ” Checking output directory...") output_dir = Path(__file__).parent / "output" if output_dir.exists(): print(f" āœ… Output directory exists: {output_dir}") else: print(f" ā„¹ļø Output directory will be created at runtime") print() return True def main(): """Run all deployment tests""" print("=" * 60) print("šŸš€ HuggingFace Spaces Deployment Test") print("=" * 60) print() tests = [ ("Dependencies", test_dependencies), ("Python Imports", test_imports), ("Gradio Version", test_gradio_version), ("App Syntax", test_app_syntax), ("Environment Variables", test_env_vars), ("Output Directory", test_output_directory), ] results = [] for name, test_func in tests: try: passed = test_func() results.append((name, passed)) except Exception as e: print(f" āŒ {name} test crashed: {e}\n") results.append((name, False)) # Summary print("=" * 60) print("šŸ“Š Test Summary") print("=" * 60) passed = sum(1 for _, p in results if p) total = len(results) for name, result in results: status = "āœ… PASS" if result else "āŒ FAIL" print(f" {status}: {name}") print() print(f"Results: {passed}/{total} tests passed") if passed == total: print("\nšŸŽ‰ All tests passed! Safe to deploy to HuggingFace Spaces.") return 0 else: print("\nāš ļø Some tests failed. Fix issues before deploying.") return 1 if __name__ == "__main__": sys.exit(main())