IDP-Machine-learning / check_backend.py
mrrobot2610's picture
Initial commit: IDP (Intelligent Document Processing) System
1a7ee60
Raw History Blame Contribute Delete
2.78 kB
#!/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()