File size: 3,380 Bytes
63b8c2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/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)