Spaces:
Paused
Paused
Download test_deployment.py from Sidd6555/Voucher-Generator: direct link, hf CLI and curl.
- Browser
- Download file 5.97 kB
-
https://huggingface.co/spaces/Sidd6555/Voucher-Generator/resolve/main/test_deployment.py
- Command line
-
hf download hf://spaces/Sidd6555/Voucher-Generator/test_deployment.py
-
curl -L -o test_deployment.py https://huggingface.co/spaces/Sidd6555/Voucher-Generator/resolve/main/test_deployment.py
5.97 kB
| #!/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()) | |