113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""[NUMES 1113] Watch numes $0 aliases by REAL probe (catalog $0 pricing flaps, probe is truth).
|
|
|
|
Live = HTTP 200 on a tiny chat completion through api.numes.ai.
|
|
Unpark channel numes-pool + enable abilities for live aliases; park when none live.
|
|
Runs on the gateway host (cron every 10 min).
|
|
"""
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
KEYS = os.environ.get("NUMES_KEYS", "/opt/numes-shim/keys.json")
|
|
BASE = "https://api.numes.ai/api/v1"
|
|
CHANNEL = "numes-pool"
|
|
STATE = "/opt/numes-shim/watch_state.json"
|
|
|
|
MAP = {
|
|
"glm-5.3": "z-ai/glm-5.3",
|
|
"glm-5.3-flash": "dealignai/glm-5.3-flash-uncensored-fp8",
|
|
"glm-5.3-uncensored": "dealignai/glm-5.3-uncensored-fp8",
|
|
"kimi-k3.1-pro": "moonshotai/kimi-k3.1-pro",
|
|
"kimi-k3-abliterated": "uniboshi/kimi-k3-abliterated-v1",
|
|
"deepseek-v4.1-flash": "deepseek/deepseek-v4.1-flash",
|
|
"deepseek-v4.1-flash-uncensored": "dealignai/deepseek-v4.1-flash-uncensored-fp8",
|
|
}
|
|
|
|
|
|
def log(msg):
|
|
sys.stdout.write("%s %s\n" % (time.strftime("%F %T"), msg))
|
|
sys.stdout.flush()
|
|
|
|
|
|
def first_key():
|
|
try:
|
|
d = json.load(open(KEYS))
|
|
if isinstance(d, dict):
|
|
d = d.get("keys", [])
|
|
return d[0] if d else None
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def probe(item):
|
|
alias, slug = item
|
|
k = first_key()
|
|
if not k:
|
|
return alias, 0, 0.0
|
|
body = json.dumps({"model": slug,
|
|
"messages": [{"role": "user", "content": "ping"}],
|
|
"max_tokens": 16})
|
|
t0 = time.time()
|
|
r = subprocess.run(
|
|
["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", "-m", "90",
|
|
BASE + "/chat/completions", "-H", "Authorization: Bearer " + k,
|
|
"-H", "Content-Type: application/json", "-d", body],
|
|
capture_output=True, text=True, timeout=120)
|
|
try:
|
|
code = int((r.stdout or "0").strip() or 0)
|
|
except Exception:
|
|
code = 0
|
|
return alias, code, round(time.time() - t0, 1)
|
|
|
|
|
|
def psql(sql):
|
|
p = "/tmp/numes_probe_%d.sql" % int(time.time() * 1000)
|
|
open(p, "w").write(sql + "\n")
|
|
os.chmod(p, 0o644)
|
|
r = subprocess.run(["su", "postgres", "-c",
|
|
"psql -d newapi -v ON_ERROR_STOP=1 -At -f " + p],
|
|
capture_output=True, text=True, cwd="/tmp", timeout=180)
|
|
try:
|
|
os.unlink(p)
|
|
except Exception:
|
|
pass
|
|
if r.returncode != 0:
|
|
log("psql fail: " + (r.stderr or "")[:200])
|
|
return r.stdout.strip()
|
|
|
|
|
|
def main():
|
|
with ThreadPoolExecutor(max_workers=4) as ex:
|
|
res = list(ex.map(probe, MAP.items()))
|
|
live = {a for a, code, _s in res if code == 200}
|
|
log("probe: " + " ".join("%s=%s/%.0fs" % (a, c, s) for a, c, s in res))
|
|
|
|
try:
|
|
prev = json.load(open(STATE)).get("live", [])
|
|
except Exception:
|
|
prev = []
|
|
|
|
if live:
|
|
psql("update channels set status=1 where name='%s';" % CHANNEL)
|
|
psql("update abilities set enabled=true where channel_id=(select id from channels where name='%s') and model in (%s);"
|
|
% (CHANNEL, ",".join("'%s'" % m for m in sorted(live))))
|
|
dead = sorted(set(MAP) - live)
|
|
if dead:
|
|
psql("update abilities set enabled=false where channel_id=(select id from channels where name='%s') and model in (%s);"
|
|
% (CHANNEL, ",".join("'%s'" % m for m in dead)))
|
|
if not live:
|
|
psql("update channels set status=2 where name='%s';" % CHANNEL)
|
|
|
|
changed = sorted(live) != sorted(prev)
|
|
json.dump({"live": sorted(live), "ts": int(time.time())}, open(STATE, "w"))
|
|
log("live=%s%s" % (sorted(live), " CHANGED" if changed else ""))
|
|
if changed:
|
|
log("ALERT numes live set: %s" % sorted(live))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |