File size: 2,781 Bytes
1a7ee60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Backend Diagnostic Tool
=======================
Quick script to check if the backend is running and healthy.
"""

import requests
import sys
import time
from typing import Optional

class Colors:
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'

def check_backend(url: str = "http://localhost:7860", timeout: int = 30) -> bool:
    """Check if backend is running and healthy."""
    health_url = f"{url}/health"
    
    print(f"{Colors.BOLD}Checking backend at {url}...{Colors.ENDC}\n")
    
    # Wait for backend to start
    print(f"Waiting for server to start (timeout: {timeout}s)...")
    start_time = time.time()
    
    while time.time() - start_time < timeout:
        try:
            response = requests.get(health_url, timeout=2)
            if response.status_code == 200:
                data = response.json()
                print(f"\n{Colors.OKGREEN}✓ Backend is running!{Colors.ENDC}")
                print(f"{Colors.OKGREEN}✓ Status: {data.get('status')}{Colors.ENDC}")
                print(f"{Colors.OKGREEN}✓ Models loaded: {data.get('models_loaded')}{Colors.ENDC}")
                print(f"{Colors.OKGREEN}✓ Version: {data.get('version')}{Colors.ENDC}")
                
                print(f"\n{Colors.BOLD}Available endpoints:{Colors.ENDC}")
                print(f"  • Root: {url}/")
                print(f"  • Health: {url}/health")
                print(f"  • Process: {url}/process")
                print(f"  • API Docs: {url}/docs")
                
                return True
        except requests.exceptions.RequestException:
            # Server not ready yet
            pass
        
        # Show progress
        elapsed = int(time.time() - start_time)
        print(f"\rWaiting... ({elapsed}s/{timeout}s)", end="", flush=True)
        time.sleep(1)
    
    print(f"\n\n{Colors.FAIL}✗ Backend is not responding{Colors.ENDC}")
    print(f"{Colors.WARNING}Possible issues:{Colors.ENDC}")
    print("  1. Backend server not started")
    print("  2. Taking longer than expected to load ML models")
    print("  3. Port 7860 is blocked or in use")
    print("  4. Missing dependencies")
    
    print(f"\n{Colors.BOLD}Troubleshooting steps:{Colors.ENDC}")
    print("  1. Check if backend is running: ps aux | grep api_server.py")
    print("  2. Check backend logs in the terminal where you started it")
    print("  3. Try starting manually: python api_server.py")
    print("  4. Check port availability: lsof -i :7860")
    
    return False

def main():
    """Main function."""
    # Check default backend
    if check_backend():
        sys.exit(0)
    else:
        sys.exit(1)

if __name__ == "__main__":
    main()