rahmad7/hermes-openmodel
0
1#!/usr/bin/env python32"""Boot test + health + proxy for HermesFace on HF Spaces."""3import os, sys, time, subprocess, shutil, http.server, socketserver, urllib.request4 5GATEWAY = "http://127.0.0.1:7861"6 7print("=== BOOT TEST ===", flush=True)8print(f"HERMES_HOME={os.environ.get('HERMES_HOME')}", flush=True)9 10hermes = shutil.which("hermes")11print(f"hermes binary: {hermes}", flush=True)12 13# Source .env14env_file = "/opt/data/.env"15if os.path.exists(env_file):16 with open(env_file) as f:17 for line in f:18 line = line.strip()19 if line and not line.startswith('#') and '=' in line:20 k, v = line.split('=', 1)21 os.environ[k] = v22 print(f"[ENV] Loaded .env — TELEGRAM_BOT_TOKEN={'YES' if os.environ.get('TELEGRAM_BOT_TOKEN') else 'NO'}", flush=True)23 24# Build config25print("=== Running build_config ===", flush=True)26subprocess.run([sys.executable, "/opt/data/scripts/build_config.py"], check=False)27 28# Proxy + Health server on 786029class Proxy(http.server.BaseHTTPRequestHandler):30 def _forward(self, method, body=None):31 try:32 url = GATEWAY + self.path33 req = urllib.request.Request(url, data=body, method=method)34 for k, v in self.headers.items():35 if k.lower() not in ('host', 'content-length', 'connection', 'transfer-encoding'):36 req.add_header(k, v)37 resp = urllib.request.urlopen(req, timeout=30)38 rbody = resp.read()39 self.send_response(resp.status)40 for k, v in resp.getheaders():41 if k.lower() not in ('transfer-encoding', 'connection'):42 self.send_header(k, v)43 self.send_header('Access-Control-Allow-Origin', '*')44 self.end_headers()45 self.wfile.write(rbody)46 except Exception:47 self.send_response(200)48 self.send_header('Content-type', 'text/plain')49 self.end_headers()50 self.wfile.write(b'HERMES OK')51 52 def do_GET(self):53 self._forward('GET')54 55 def do_POST(self):56 cl = int(self.headers.get('Content-Length', 0))57 body = self.rfile.read(cl) if cl else b''58 self._forward('POST', body)59 60 def do_OPTIONS(self):61 self.send_response(200)62 self.send_header('Access-Control-Allow-Origin', '*')63 self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')64 self.send_header('Access-Control-Allow-Headers', '*')65 self.end_headers()66 67 def log_message(self, *a):68 pass69 70# Start proxy server71server = socketserver.ThreadingTCPServer(('0.0.0.0', 7860), Proxy)72print("[PROXY] Serving on 0.0.0.0:7860 -> 127.0.0.1:7861", flush=True)73 74# Start gateway on 786175if hermes:76 env = os.environ.copy()77 env["PORT"] = "7861"78 env["GATEWAY_ALLOW_ALL_USERS"] = "true"79 print("[GATEWAY] Starting on port 7861...", flush=True)80 subprocess.Popen([hermes, "gateway", "run"], env=env)81 print("[GATEWAY] Process spawned", flush=True)82else:83 print("[ERROR] hermes binary not found!", flush=True)84 85print("[READY] Health proxy + Gateway starting...", flush=True)86server.serve_forever()87 