""" WidgeTDC Cortex - Runtime Log Monitor Monitors HF Space logs in real-time to verify system health """ import time import requests from huggingface_hub import HfApi # Configuration SPACE_NAME = "widgetdc-cortex" USERNAME = "Kraft102" REPO_ID = f"{USERNAME}/{SPACE_NAME}" SPACE_URL = f"https://{USERNAME.lower()}-{SPACE_NAME}.hf.space" print("=" * 70) print(" WIDGETTDC CORTEX - RUNTIME LOG MONITOR") print("=" * 70) print() # Initialize API api = HfApi() def check_build_status(): """Check if Space is running""" try: runtime = api.get_space_runtime(repo_id=REPO_ID) return runtime.stage, runtime.hardware except Exception as e: return "UNKNOWN", None def test_health_endpoint(): """Test /health endpoint""" try: response = requests.get(f"{SPACE_URL}/health", timeout=10) return response.status_code == 200, response.json() if response.status_code == 200 else None except Exception as e: return False, str(e) def monitor_runtime(): """Monitor runtime status and logs""" print("[1/4] Checking Space status...") stage, hardware = check_build_status() print(f" Stage: {stage}") print(f" Hardware: {hardware}") print() if stage == "RUNNING": print("✅ Space is RUNNING!") print() print("[2/4] Testing health endpoint...") healthy, data = test_health_endpoint() if healthy: print(f"✅ Health check PASSED!") print(f" Response: {data}") else: print(f"❌ Health check FAILED!") print(f" Error: {data}") print() print("[3/4] Testing API endpoints...") # Test MCP route try: response = requests.post( f"{SPACE_URL}/api/mcp/route", json={"method": "initialize", "params": {}}, timeout=10 ) print(f" MCP Route: {response.status_code}") except Exception as e: print(f" MCP Route: ERROR - {e}") print() print("[4/4] System Health Summary:") print(" 🟢 Space: RUNNING") print(f" {'🟢' if healthy else '🔴'} Health: {'OK' if healthy else 'FAILED'}") print(f" 🔵 URL: {SPACE_URL}") elif stage == "BUILDING": print("⏳ Space is still BUILDING...") print(" Run this script again in 1-2 minutes") elif stage == "BUILD_ERROR": print("❌ Space has BUILD_ERROR!") print(" Check logs at:") print(f" https://huggingface.co/spaces/{REPO_ID}/logs") else: print(f"⚠️ Unknown status: {stage}") if __name__ == "__main__": monitor_runtime() print() print("=" * 70) print(" MONITORING COMPLETE") print("=" * 70)