Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script for HF Spaces configuration | |
| ======================================= | |
| This script tests the HF Spaces configuration module to ensure it's working correctly. | |
| Run this script to verify that the configuration is properly set up. | |
| """ | |
| import os | |
| import sys | |
| from pathlib import Path | |
| def test_hf_spaces_config(): | |
| """Test the HF Spaces configuration""" | |
| print("π§ͺ Testing HF Spaces Configuration") | |
| print("=" * 50) | |
| try: | |
| # Import the configuration | |
| from hf_spaces_config import get_hf_config, is_hf_spaces | |
| print("β Successfully imported HF Spaces configuration") | |
| # Test environment detection | |
| print(f"\nπ Environment Detection:") | |
| print(f" Is HF Spaces: {is_hf_spaces()}") | |
| # Get configuration | |
| config = get_hf_config() | |
| print(f" Configuration loaded: {type(config).__name__}") | |
| # Test cache directories | |
| print(f"\nπ Cache Directories:") | |
| for name, path in config.cache_dirs.items(): | |
| exists = os.path.exists(path) | |
| print(f" {name}: {path} {'β ' if exists else 'β'}") | |
| # Test environment variables | |
| print(f"\nπ§ Environment Variables:") | |
| env_vars = config.env_vars | |
| for key, value in env_vars.items(): | |
| print(f" {key}: {value}") | |
| # Test model configuration | |
| print(f"\nπ€ Model Configuration:") | |
| model_config = config.get_model_config() | |
| for key, value in model_config.items(): | |
| print(f" {key}: {value}") | |
| # Test guard rail configuration | |
| print(f"\nπ‘οΈ Guard Rail Configuration:") | |
| guard_config = config.get_guard_rail_config() | |
| for key, value in guard_config.items(): | |
| print(f" {key}: {value}") | |
| # Test resource limits | |
| print(f"\nπ Resource Limits:") | |
| resource_limits = config.get_resource_limits() | |
| for key, value in resource_limits.items(): | |
| print(f" {key}: {value}") | |
| print(f"\nβ All tests passed!") | |
| return True | |
| except ImportError as e: | |
| print(f"β Import error: {e}") | |
| return False | |
| except Exception as e: | |
| print(f"β Configuration error: {e}") | |
| return False | |
| def test_cache_directories(): | |
| """Test cache directory creation""" | |
| print(f"\nπ§ Testing Cache Directory Creation") | |
| print("=" * 50) | |
| try: | |
| from hf_spaces_config import get_hf_config | |
| config = get_hf_config() | |
| # Test directory creation | |
| for name, path in config.cache_dirs.items(): | |
| try: | |
| Path(path).mkdir(parents=True, exist_ok=True) | |
| print(f"β Created: {name} -> {path}") | |
| except Exception as e: | |
| print(f"β Failed to create {name}: {e}") | |
| return True | |
| except Exception as e: | |
| print(f"β Cache directory test failed: {e}") | |
| return False | |
| def test_environment_variables(): | |
| """Test environment variable setup""" | |
| print(f"\nπ§ Testing Environment Variables") | |
| print("=" * 50) | |
| try: | |
| from hf_spaces_config import get_hf_config | |
| config = get_hf_config() | |
| # Check if environment variables are set | |
| for key, expected_value in config.env_vars.items(): | |
| actual_value = os.environ.get(key, "NOT_SET") | |
| status = "β " if actual_value == expected_value else "β" | |
| print(f" {key}: {actual_value} {status}") | |
| return True | |
| except Exception as e: | |
| print(f"β Environment variable test failed: {e}") | |
| return False | |
| def main(): | |
| """Run all tests""" | |
| print("π HF Spaces Configuration Test Suite") | |
| print("=" * 60) | |
| tests = [ | |
| ("Configuration Import", test_hf_spaces_config), | |
| ("Cache Directories", test_cache_directories), | |
| ("Environment Variables", test_environment_variables), | |
| ] | |
| results = [] | |
| for test_name, test_func in tests: | |
| print(f"\nπ§ͺ Running: {test_name}") | |
| result = test_func() | |
| results.append((test_name, result)) | |
| # Summary | |
| print(f"\nπ Test Summary") | |
| print("=" * 30) | |
| passed = sum(1 for _, result in results if result) | |
| total = len(results) | |
| for test_name, result in results: | |
| status = "β PASS" if result else "β FAIL" | |
| print(f" {test_name}: {status}") | |
| print(f"\nOverall: {passed}/{total} tests passed") | |
| if passed == total: | |
| print("π All tests passed! HF Spaces configuration is working correctly.") | |
| return 0 | |
| else: | |
| print("β οΈ Some tests failed. Please check the configuration.") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |