#!/usr/bin/env python3 """ Monitor HF Space deployment status. Checks if the space is running and provides status updates. """ import requests import time import sys from datetime import datetime def check_space_status(base_url: str) -> dict: """Check if the HF Space is running and responding.""" try: # Try the root endpoint first response = requests.get(f"{base_url}/", timeout=10) if response.status_code == 200: return {"status": "running", "message": "Space is active and responding"} elif response.status_code == 404: # Check if it's the HF "space not found" 404 or our app's 404 if "huggingface" in response.text.lower(): return {"status": "not_found", "message": "Space not found or not public"} else: return {"status": "app_404", "message": "App is running but endpoint not found"} else: return {"status": "error", "message": f"Unexpected status code: {response.status_code}"} except requests.exceptions.Timeout: return {"status": "timeout", "message": "Space is not responding (timeout)"} except requests.exceptions.ConnectionError: return {"status": "connection_error", "message": "Cannot connect to space"} except Exception as e: return {"status": "error", "message": f"Error: {str(e)}"} def monitor_space(base_url: str, check_interval: int = 30, max_checks: int = 20): """Monitor the space status over time.""" print(f"šŸ” Monitoring HF Space: {base_url}") print(f"ā±ļø Check interval: {check_interval} seconds") print(f"šŸ”¢ Max checks: {max_checks}") print("-" * 60) for i in range(max_checks): timestamp = datetime.now().strftime("%H:%M:%S") print(f"[{timestamp}] Check {i+1}/{max_checks}:", end=" ") status = check_space_status(base_url) if status["status"] == "running": print(f"āœ… {status['message']}") print(f"šŸŽ‰ Space is running! You can now test the endpoints.") return True elif status["status"] == "app_404": print(f"āš ļø {status['message']}") print(f"šŸ’” App seems to be running, try the API endpoints directly.") return True else: print(f"āŒ {status['message']}") if i < max_checks - 1: print(f" Waiting {check_interval} seconds before next check...") time.sleep(check_interval) print(f"\nšŸ’„ Space did not start after {max_checks} checks.") print("šŸ”§ Check the HF Space logs for deployment errors.") return False if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description='Monitor HF Space deployment') parser.add_argument( '--url', default='https://ch404-cardserver.hf.space', help='Base URL of the HF Space' ) parser.add_argument( '--interval', type=int, default=30, help='Check interval in seconds' ) parser.add_argument( '--max-checks', type=int, default=20, help='Maximum number of checks before giving up' ) args = parser.parse_args() success = monitor_space(args.url, args.interval, args.max_checks) sys.exit(0 if success else 1)