Update main.py
Browse files
main.py
CHANGED
|
@@ -10,22 +10,32 @@ os.makedirs(hermes_dir, exist_ok=True)
|
|
| 10 |
|
| 11 |
print("[1] Setting up configs and skills...")
|
| 12 |
|
| 13 |
-
# --- Secrets Load
|
| 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 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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={
|
| 29 |
|
| 30 |
# --- Write config.yaml (NVIDIA Primary) ---
|
| 31 |
with open(os.path.join(hermes_dir, "config.yaml"), "w") as f:
|
|
@@ -73,9 +83,9 @@ 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
|
| 77 |
|
| 78 |
-
#
|
| 79 |
process = subprocess.Popen(
|
| 80 |
[hermes_bin, "gateway", "start"],
|
| 81 |
env=os.environ,
|
|
@@ -85,10 +95,8 @@ process = subprocess.Popen(
|
|
| 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)
|
|
@@ -96,4 +104,3 @@ try:
|
|
| 96 |
except KeyboardInterrupt:
|
| 97 |
print("๐ Stopping server.")
|
| 98 |
process.terminate()
|
| 99 |
-
|
|
|
|
| 10 |
|
| 11 |
print("[1] Setting up configs and skills...")
|
| 12 |
|
| 13 |
+
# --- Secrets Load ---
|
| 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 |
+
# Debugging lines taaki exact pata chale kaunsa secret nahi mil raha
|
| 20 |
+
if not OR_KEY: print("โ ๏ธ Warning: OPENROUTER_API_KEY is empty or missing!")
|
| 21 |
+
if not NV_KEY: print("โ ๏ธ Warning: NVIDIA_API_KEY is empty or missing!")
|
| 22 |
+
if not TG_TOKEN: print("โ ๏ธ Warning: TELEGRAM_BOT_TOKEN is empty or missing!")
|
| 23 |
+
if not TG_USER: print("โ ๏ธ Warning: TELEGRAM_ALLOWED_USERS is empty or missing!")
|
| 24 |
+
|
| 25 |
+
# Strict crash hata kar fallback check lagaya hai
|
| 26 |
+
if not TG_TOKEN or not TG_USER:
|
| 27 |
+
print("โ ERROR: Main Telegram credentials missing hain! Check your Hugging Face Secrets.")
|
| 28 |
sys.exit(1)
|
| 29 |
|
| 30 |
+
# Ensure string conversion for safe file writing
|
| 31 |
+
TG_USER_STR = str(TG_USER).strip()
|
| 32 |
+
|
| 33 |
# --- Write .env ---
|
| 34 |
with open(os.path.join(hermes_dir, ".env"), "w") as f:
|
| 35 |
f.write(f"OPENROUTER_API_KEY={OR_KEY}\n")
|
| 36 |
f.write(f"NVIDIA_API_KEY={NV_KEY}\n")
|
| 37 |
f.write(f"TELEGRAM_BOT_TOKEN={TG_TOKEN}\n")
|
| 38 |
+
f.write(f"TELEGRAM_ALLOWED_USERS={TG_USER_STR}\n")
|
| 39 |
|
| 40 |
# --- Write config.yaml (NVIDIA Primary) ---
|
| 41 |
with open(os.path.join(hermes_dir, "config.yaml"), "w") as f:
|
|
|
|
| 83 |
|
| 84 |
# --- Start Hermes Gateway ---
|
| 85 |
print("[2] Launching Hermes Telegram Gateway...")
|
| 86 |
+
hermes_bin = shutil.which("hermes") or "/usr/local/bin/hermes"
|
| 87 |
|
| 88 |
+
# Process running
|
| 89 |
process = subprocess.Popen(
|
| 90 |
[hermes_bin, "gateway", "start"],
|
| 91 |
env=os.environ,
|
|
|
|
| 95 |
|
| 96 |
print("๐ Bot is live on Hugging Face Server! Keeping process alive...")
|
| 97 |
|
|
|
|
| 98 |
try:
|
| 99 |
while True:
|
|
|
|
| 100 |
if process.poll() is not None:
|
| 101 |
print("โ ๏ธ Hermes process ended unexpectedly. Restarting...")
|
| 102 |
process = subprocess.Popen([hermes_bin, "gateway", "start"], env=os.environ, stdout=sys.stdout, stderr=sys.stderr)
|
|
|
|
| 104 |
except KeyboardInterrupt:
|
| 105 |
print("๐ Stopping server.")
|
| 106 |
process.terminate()
|
|
|