Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import shutil
|
| 4 |
+
import time
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
home = os.path.expanduser("~")
|
| 8 |
+
hermes_dir = os.path.join(home, ".hermes")
|
| 9 |
+
os.makedirs(hermes_dir, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
print("[1] Setting up configs and skills...")
|
| 12 |
+
|
| 13 |
+
# --- Secrets Load (Hugging Face Env Variables se) ---
|
| 14 |
+
OR_KEY = os.getenv('OPENROUTER_API_KEY')
|
| 15 |
+
NV_KEY = os.getenv('NVIDIA_API_KEY')
|
| 16 |
+
TG_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
| 17 |
+
TG_USER = os.getenv('TELEGRAM_ALLOWED_USERS')
|
| 18 |
+
|
| 19 |
+
if not all([OR_KEY, NV_KEY, TG_TOKEN, TG_USER]):
|
| 20 |
+
print("β ERROR: Hugging Face Spaces me Repository Secrets/Env Variables missing hain!")
|
| 21 |
+
sys.exit(1)
|
| 22 |
+
|
| 23 |
+
# --- Write .env ---
|
| 24 |
+
with open(os.path.join(hermes_dir, ".env"), "w") as f:
|
| 25 |
+
f.write(f"OPENROUTER_API_KEY={OR_KEY}\n")
|
| 26 |
+
f.write(f"NVIDIA_API_KEY={NV_KEY}\n")
|
| 27 |
+
f.write(f"TELEGRAM_BOT_TOKEN={TG_TOKEN}\n")
|
| 28 |
+
f.write(f"TELEGRAM_ALLOWED_USERS={TG_USER}\n")
|
| 29 |
+
|
| 30 |
+
# --- Write config.yaml (NVIDIA Primary) ---
|
| 31 |
+
with open(os.path.join(hermes_dir, "config.yaml"), "w") as f:
|
| 32 |
+
f.write("""model:
|
| 33 |
+
provider: nvidia
|
| 34 |
+
model: meta/llama-3.1-405b-instruct
|
| 35 |
+
|
| 36 |
+
providers:
|
| 37 |
+
nvidia:
|
| 38 |
+
base_url: "https://integrate.api.nvidia.com/v1"
|
| 39 |
+
api_key: "${NVIDIA_API_KEY}"
|
| 40 |
+
openrouter:
|
| 41 |
+
api_key: "${OPENROUTER_API_KEY}"
|
| 42 |
+
|
| 43 |
+
terminal:
|
| 44 |
+
backend: local
|
| 45 |
+
|
| 46 |
+
memory:
|
| 47 |
+
enabled: true
|
| 48 |
+
|
| 49 |
+
display:
|
| 50 |
+
tool_progress: "off"
|
| 51 |
+
|
| 52 |
+
compression:
|
| 53 |
+
enabled: true
|
| 54 |
+
threshold: 0.60
|
| 55 |
+
""")
|
| 56 |
+
|
| 57 |
+
# --- Write skills.md ---
|
| 58 |
+
skills_path = os.path.join(hermes_dir, "skills.md")
|
| 59 |
+
with open(skills_path, "w") as f:
|
| 60 |
+
f.write("""# Hermes Agent Skills
|
| 61 |
+
## Core Instructions
|
| 62 |
+
- You are a helpful AI assistant with access to local terminal tools.
|
| 63 |
+
- ALWAYS use available tools if the user task requires it.
|
| 64 |
+
- Be concise and efficient.
|
| 65 |
+
|
| 66 |
+
## Core Behavior
|
| 67 |
+
- NEVER say "I cannot do this" or "I am just an AI".
|
| 68 |
+
- You have access to this Docker environment.
|
| 69 |
+
- You can create, edit, delete, and execute files via commands.
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
+
print("β
Config and Skills created successfully.")
|
| 73 |
+
|
| 74 |
+
# --- Start Hermes Gateway ---
|
| 75 |
+
print("[2] Launching Hermes Telegram Gateway...")
|
| 76 |
+
hermes_bin = shutil.which("hermes") or os.path.expanduser("~/.local/bin/hermes")
|
| 77 |
+
|
| 78 |
+
# Live log output taaki Hugging Face logs me sab dikhe
|
| 79 |
+
process = subprocess.Popen(
|
| 80 |
+
[hermes_bin, "gateway", "start"],
|
| 81 |
+
env=os.environ,
|
| 82 |
+
stdout=sys.stdout,
|
| 83 |
+
stderr=sys.stderr
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
print("π Bot is live on Hugging Face Server! Keeping process alive...")
|
| 87 |
+
|
| 88 |
+
# Main execution loop jo container ko running rakhega bina freeze hue
|
| 89 |
+
try:
|
| 90 |
+
while True:
|
| 91 |
+
# Check if process died
|
| 92 |
+
if process.poll() is not None:
|
| 93 |
+
print("β οΈ Hermes process ended unexpectedly. Restarting...")
|
| 94 |
+
process = subprocess.Popen([hermes_bin, "gateway", "start"], env=os.environ, stdout=sys.stdout, stderr=sys.stderr)
|
| 95 |
+
time.sleep(30)
|
| 96 |
+
except KeyboardInterrupt:
|
| 97 |
+
print("π Stopping server.")
|
| 98 |
+
process.terminate()
|
| 99 |
+
|