#!/usr/bin/env python3 """ OpenClaw Agent Army - Hugging Face Spaces Entry Point """ import os import sys import time import subprocess from pathlib import Path # Set environment os.environ['OPENCLAWDIR'] = '/app/.openclaw' def check_and_install_deps(): """Check and install missing dependencies""" print("šŸ” Checking dependencies...") # Check if we're in the correct directory if not os.path.exists('supervisor.py'): print("āŒ Not in the correct directory. supervisor.py not found.") return False # Create .openclaw structure if missing os.makedirs('.openclaw', exist_ok=True) os.makedirs('.openclaw/workspace', exist_ok=True) os.makedirs('.openclaw/agents', exist_ok=True) # Copy config to .openclaw if needed if os.path.exists('config.json') and not os.path.exists('.openclaw/workspace/config.json'): import shutil shutil.copy('config.json', '.openclaw/workspace/config.json') # Copy agent files if os.path.exists('agent_army/agents') and not os.path.exists('.openclaw/agents/gig1'): import shutil for agent_file in Path('agent_army/agents').glob('*.py'): if agent_file.name not in ['__init__.py', 'common.py']: shutil.copy(agent_file, f'.openclaw/agents/{agent_file.name}') # Copy skills if os.path.exists('skills') and not os.path.exists('.openclaw/workspace/skills'): import shutil shutil.copytree('skills', '.openclaw/workspace/skills', dirs_exist_ok=True) return True def start_agent_supervisor(): """Start the agent supervisor""" print("šŸš€ Starting Agent Army Supervisor...") # Check if openclaw command exists try: subprocess.run(['openclaw', '--version'], capture_output=True, check=True) # Use openclaw CLI if available os.system('openclaw gateway --port 7860 --host 0.0.0.0') except (subprocess.CalledProcessError, FileNotFoundError): # Fall back to direct supervisor os.system('python3 supervisor.py') def start_xvfb(): """Start Xvfb for headless Chrome""" try: print("šŸ–„ļø Starting Xvfb virtual display...") subprocess.Popen(['Xvfb', ':99', '-screen', '0', '1920x1080x24', '-nolisten', 'tcp', '-fbdir', '/var/tmp']) time.sleep(2) # Give Xvfb time to start print("āœ… Xvfb started") except Exception as e: print(f"āš ļø Xvfb start failed: {e}") print("āš ļø Chrome may not work without virtual display") def main(): print("=" * 50) print("OPENCLAW AGENT ARMY - HUGGING FACE SPACE") print("=" * 50) # Check Python version print(f"Python version: {sys.version}") # Start Xvfb for headless Chrome (must be before agents) start_xvfb() # Install dependencies if needed print("šŸ“¦ Ensuring dependencies are installed...") try: subprocess.run([sys.executable, '-m', 'pip', 'install', '-q', '--upgrade', 'pip'], check=False, timeout=60) subprocess.run([sys.executable, '-m', 'pip', 'install', '-q', '-r', 'requirements.txt'], check=False, timeout=120) except subprocess.TimeoutExpired: print("āš ļø pip install timed out, continuing...") except Exception as e: print(f"āš ļø pip install error: {e}, continuing...") # Check structure if not check_and_install_deps(): print("āŒ Setup failed. Check logs.") sys.exit(1) print("āœ… Setup complete") print("\nšŸ“Š Agent Status:") # Check for running agents try: result = subprocess.run(['ps', 'aux'], capture_output=True, text=True) agent_count = result.stdout.count('gig_real.py') + result.stdout.count('trading_real.py') print(f" Active agent processes: {agent_count}") except: pass print("\n🌐 Starting services...") # Start supervisor in foreground start_agent_supervisor() if __name__ == "__main__": try: main() except KeyboardInterrupt: print("\nšŸ‘‹ Shutting down...") sys.exit(0) except Exception as e: print(f"āŒ Error: {e}") import traceback traceback.print_exc() sys.exit(1)