Files
runware-autoreg/rw_models.py
T

112 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Probe which Runware models actually answer on a $2 trial key.
Reads the catalog from GET /v1/models, then sends one tiny chat request per
model, records HTTP code, latency and first characters of the answer.
Usage:
python rw_models.py --key <rw-key> [--only claude,gemini] [--max 40] [--json out.json]
"""
import argparse
import json
import time
import urllib.request
API = "https://api.runware.ai/v1"
def post(path, key, payload, timeout=90):
data = json.dumps(payload).encode()
req = urllib.request.Request(API + path, data=data, method="POST", headers={
"Authorization": "Bearer " + key,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0"})
t0 = time.time()
try:
with urllib.request.urlopen(req, timeout=timeout) as r:
body = r.read().decode("utf-8", "replace")
code = r.status
except Exception as e:
code = getattr(e, "code", 0) or 0
try:
body = e.read().decode("utf-8", "replace")
except Exception:
body = str(e)
return code, body, round(time.time() - t0, 2)
def get(path, key, timeout=60):
req = urllib.request.Request(API + path, headers={
"Authorization": "Bearer " + key, "User-Agent": "Mozilla/5.0"})
try:
with urllib.request.urlopen(req, timeout=timeout) as r:
return r.status, r.read().decode("utf-8", "replace")
except Exception as e:
code = getattr(e, "code", 0) or 0
try:
return code, e.read().decode("utf-8", "replace")
except Exception:
return code, str(e)
def catalog(key):
code, body = get("/models", key)
if code != 200:
return [], code, body[:200]
try:
j = json.loads(body)
except Exception:
return [], code, body[:200]
data = j.get("data") or j
ids = []
for item in data:
mid = item.get("id") or item.get("model") or item.get("name")
if mid:
ids.append(mid)
return sorted(set(ids)), code, ""
def probe(key, model):
payload = {"model": model,
"messages": [{"role": "user", "content": "reply with the single word ok"}],
"max_tokens": 16}
code, body, dt = post("/chat/completions", key, payload)
text = ""
try:
j = json.loads(body)
ch = (j.get("choices") or [{}])[0]
text = ((ch.get("message") or {}).get("content") or "")[:60]
if not text:
text = json.dumps(j)[:60]
except Exception:
text = body[:80].replace("\n", " ")
return {"model": model, "code": code, "sec": dt, "text": text}
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--key", required=True)
ap.add_argument("--only", default="")
ap.add_argument("--max", type=int, default=40)
ap.add_argument("--json", default="")
a = ap.parse_args()
ids, code, err = catalog(a.key)
print("catalog http=%s models=%d %s" % (code, len(ids), err))
if a.only:
keys = [k.strip().lower() for k in a.only.split(",") if k.strip()]
ids = [m for m in ids if any(k in m.lower() for k in keys)]
ids = ids[:a.max]
out = []
for m in ids:
rec = probe(a.key, m)
print("%-46s %s %5.2fs %s" % (rec["model"], rec["code"], rec["sec"], rec["text"]))
out.append(rec)
ok = [r for r in out if r["code"] == 200]
print("\nanswered %d/%d" % (len(ok), len(out)))
if a.json:
json.dump(out, open(a.json, "w", encoding="utf-8"), ensure_ascii=False, indent=1)
return 0
if __name__ == "__main__":
raise SystemExit(main())