Create main.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import osimport subprocessimport shutilimport timeimport sys
home = os.path.expanduser("~")hermes_dir = os.path.join(home, ".hermes")os.makedirs(hermes_dir, exist_ok=True)
print("[1] Setting up configs and skills...")
# --- Secrets Load (Hugging Face Env Variables se) ---OR_KEY = os.getenv('OPENROUTER_API_KEY')NV_KEY = os.getenv('NVIDIA_API_KEY')TG_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')TG_USER = os.getenv('TELEGRAM_ALLOWED_USERS')
if not all([OR_KEY, NV_KEY, TG_TOKEN, TG_USER]): print("β ERROR: Hugging Face Spaces me Repository Secrets/Env Variables missing hain!") sys.exit(1)
# --- Write .env ---with open(os.path.join(hermes_dir, ".env"), "w") as f: f.write(f"OPENROUTER_API_KEY={OR_KEY}\n") f.write(f"NVIDIA_API_KEY={NV_KEY}\n") f.write(f"TELEGRAM_BOT_TOKEN={TG_TOKEN}\n") f.write(f"TELEGRAM_ALLOWED_USERS={TG_USER}\n")
# --- Write config.yaml (NVIDIA Primary) ---with open(os.path.join(hermes_dir, "config.yaml"), "w") as f: f.write("""model: provider: nvidia model: meta/llama-3.1-405b-instruct
providers: nvidia: base_url: "https://integrate.api.nvidia.com/v1" api_key: "${NVIDIA_API_KEY}" openrouter: api_key: "${OPENROUTER_API_KEY}"
terminal: backend: local
memory: enabled: true
display: tool_progress: "off"
compression: enabled: true threshold: 0.60""")
# --- Write skills.md ---skills_path = os.path.join(hermes_dir, "skills.md")with open(skills_path, "w") as f: f.write("""# Hermes Agent Skills## Core Instructions- You are a helpful AI assistant with access to local terminal tools.- ALWAYS use available tools if the user task requires it.- Be concise and efficient.
## Core Behavior- NEVER say "I cannot do this" or "I am just an AI".- You have access to this Docker environment.- You can create, edit, delete, and execute files via commands.""")
print("β
Config and Skills created successfully.")
# --- Start Hermes Gateway ---print("[2] Launching Hermes Telegram Gateway...")hermes_bin = shutil.which("hermes") or os.path.expanduser("~/.local/bin/hermes")
# Live log output taaki Hugging Face logs me sab dikheprocess = subprocess.Popen( [hermes_bin, "gateway", "start"], env=os.environ, stdout=sys.stdout, stderr=sys.stderr)
print("π Bot is live on Hugging Face Server! Keeping process alive...")
# Main execution loop jo container ko running rakhega bina freeze huetry: while True: # Check if process died if process.poll() is not None: print("β οΈ Hermes process ended unexpectedly. Restarting...") process = subprocess.Popen([hermes_bin, "gateway", "start"], env=os.environ, stdout=sys.stdout, stderr=sys.stderr) time.sleep(30)except KeyboardInterrupt: print("π Stopping server.") process.terminate() main.pyAdded+99