#!/usr/bin/env python3 """ Test script to verify the Hugging Face Spaces fixes work correctly. This script simulates the HF Spaces environment and tests the startup process. """ import os import sys import subprocess import tempfile from pathlib import Path def test_uvicorn_command(): """Test that uvicorn command works without --no-reload option.""" print("Testing uvicorn command...") # Test the corrected uvicorn command cmd = ["python", "-c", "import uvicorn; print('uvicorn import successful')"] try: result = subprocess.run(cmd, capture_output=True, text=True, timeout=10) if result.returncode == 0: print("✓ uvicorn import test passed") else: print(f"✗ uvicorn import test failed: {result.stderr}") return False except Exception as e: print(f"✗ uvicorn test failed: {e}") return False return True def test_permission_handling(): """Test that permission handling works without trying to chmod /tmp.""" print("Testing permission handling...") # Create a temporary directory to simulate /tmp with tempfile.TemporaryDirectory() as temp_dir: # Test creating subdirectories test_dirs = [ f"{temp_dir}/uploads", f"{temp_dir}/huggingface", f"{temp_dir}/torch", f"{temp_dir}/whisper" ] try: for dir_path in test_dirs: os.makedirs(dir_path, exist_ok=True) print(f"✓ Created directory: {dir_path}") # Test that we can write to these directories for dir_path in test_dirs: test_file = os.path.join(dir_path, "test.txt") with open(test_file, "w") as f: f.write("test") print(f"✓ Can write to: {dir_path}") return True except Exception as e: print(f"✗ Permission test failed: {e}") return False def test_app_import(): """Test that the app can be imported correctly.""" print("Testing app import...") try: # Add the ai_med_extract module to Python path current_dir = Path(__file__).parent ai_med_extract_path = current_dir / "services" / "ai-service" / "src" sys.path.insert(0, str(ai_med_extract_path)) # Also add the parent directory for proper module resolution sys.path.insert(0, str(ai_med_extract_path.parent)) # Set HF Spaces environment os.environ['HF_SPACES'] = 'true' # Try to import the app from ai_med_extract.app import create_app print("✓ App import successful") # Try to create the app (without initializing heavy components) app = create_app(initialize=False) print("✓ App creation successful") return True except Exception as e: print(f"✗ App import test failed: {e}") return False def main(): """Run all tests.""" print("Running Hugging Face Spaces fix tests...") print("=" * 50) tests = [ test_uvicorn_command, test_permission_handling, test_app_import ] passed = 0 total = len(tests) for test in tests: if test(): passed += 1 print() print("=" * 50) print(f"Tests passed: {passed}/{total}") if passed == total: print("✓ All tests passed! The fixes should work on Hugging Face Spaces.") else: print("✗ Some tests failed. Please check the issues above.") return passed == total if __name__ == "__main__": success = main() sys.exit(0 if success else 1)