#!/usr/bin/env python3 """ Debug script to test HF Spaces environment """ import os import sys import subprocess import time import requests from pathlib import Path def check_files(): """Check if required files exist""" print("šŸ” Checking required files...") files_to_check = [ "dispatcher.py", "worker.py", "utils.py", "latent_stats.json", "static/index.html", "requirements.txt" ] for file in files_to_check: if os.path.exists(file): print(f"āœ… {file} exists") else: print(f"āŒ {file} missing") print(f"šŸ“ Current directory: {os.getcwd()}") print(f"šŸ“‹ Directory contents:") for item in os.listdir('.'): print(f" {item}") def test_imports(): """Test if imports work""" print("\nšŸ” Testing imports...") try: import torch print(f"āœ… torch {torch.__version__}") except Exception as e: print(f"āŒ torch failed: {e}") try: from utils import initialize_model print("āœ… utils imports work") except Exception as e: print(f"āŒ utils import failed: {e}") try: import fastapi print(f"āœ… fastapi {fastapi.__version__}") except Exception as e: print(f"āŒ fastapi failed: {e}") def test_dispatcher(): """Test dispatcher startup""" print("\nšŸ” Testing dispatcher...") try: # Try to start dispatcher proc = subprocess.Popen([ sys.executable, "dispatcher.py", "--port", "7860" ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Wait a bit time.sleep(3) # Check if it's running if proc.poll() is None: print("āœ… Dispatcher process is running") # Test HTTP request try: response = requests.get("http://localhost:7860/", timeout=5) if response.status_code == 200: print("āœ… Dispatcher HTTP response OK") else: print(f"āš ļø Dispatcher HTTP status: {response.status_code}") except Exception as e: print(f"āŒ Dispatcher HTTP test failed: {e}") else: print("āŒ Dispatcher process died") stdout, stderr = proc.communicate() print(f"šŸ“‹ Stdout: {stdout.decode()}") print(f"šŸ“‹ Stderr: {stderr.decode()}") # Clean up proc.terminate() proc.wait() except Exception as e: print(f"āŒ Failed to test dispatcher: {e}") def main(): print("šŸš€ HF Spaces Debug Script") print("=" * 50) check_files() test_imports() test_dispatcher() print("\nšŸ“Š Environment info:") print(f"Python: {sys.version}") print(f"Platform: {sys.platform}") print(f"CWD: {os.getcwd()}") # Check for GPU try: import torch if torch.cuda.is_available(): print(f"šŸŽ® CUDA available: {torch.cuda.device_count()} devices") else: print("šŸ’» Running on CPU") except: print("ā“ Cannot check GPU status") if __name__ == "__main__": main()