Sovereign-endpoint probe: boot wait + tok/s
Browse files- tests/probe_llm_space.py +68 -0
tests/probe_llm_space.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Probe the sovereign LLM Space: wait for boot, then measure /v1/chat/completions."""
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
import urllib.request
|
| 6 |
+
import urllib.error
|
| 7 |
+
|
| 8 |
+
BASE = "https://lovelogicai-freestream-llm.hf.space"
|
| 9 |
+
KEY = "" # LLM_API_KEY secret not set yet → open endpoint
|
| 10 |
+
|
| 11 |
+
print("[probe] waiting for the Space to build + boot (model download on first boot)...",
|
| 12 |
+
flush=True)
|
| 13 |
+
deadline = time.time() + 900
|
| 14 |
+
up = False
|
| 15 |
+
while time.time() < deadline:
|
| 16 |
+
try:
|
| 17 |
+
req = urllib.request.Request(f"{BASE}/health")
|
| 18 |
+
with urllib.request.urlopen(req, timeout=10) as r:
|
| 19 |
+
if r.status == 200:
|
| 20 |
+
up = True
|
| 21 |
+
break
|
| 22 |
+
except urllib.error.HTTPError as e:
|
| 23 |
+
if e.code == 200:
|
| 24 |
+
up = True
|
| 25 |
+
break
|
| 26 |
+
print(f" health: HTTP {e.code}", flush=True)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f" health: {type(e).__name__}", flush=True)
|
| 29 |
+
time.sleep(15)
|
| 30 |
+
|
| 31 |
+
if not up:
|
| 32 |
+
print("[probe] Space did not come up within 15 min — check the build log", flush=True)
|
| 33 |
+
raise SystemExit(1)
|
| 34 |
+
|
| 35 |
+
print("[probe] llama-server /health OK", flush=True)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def chat(messages, max_tokens=80):
|
| 39 |
+
payload = json.dumps({"model": "local", "messages": messages,
|
| 40 |
+
"max_tokens": max_tokens, "temperature": 0.8}).encode()
|
| 41 |
+
req = urllib.request.Request(
|
| 42 |
+
f"{BASE}/v1/chat/completions", data=payload, method="POST",
|
| 43 |
+
headers={"Content-Type": "application/json",
|
| 44 |
+
**({"Authorization": f"Bearer {KEY}"} if KEY else {})})
|
| 45 |
+
t0 = time.time()
|
| 46 |
+
with urllib.request.urlopen(req, timeout=300) as r:
|
| 47 |
+
data = json.loads(r.read())
|
| 48 |
+
dt = time.time() - t0
|
| 49 |
+
text = data["choices"][0]["message"]["content"]
|
| 50 |
+
n = data.get("usage", {}).get("completion_tokens", 0)
|
| 51 |
+
return text, dt, n
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# warmup (model load, mmap)
|
| 55 |
+
text, dt, n = chat([{"role": "user", "content": "Say 'bridge online'."}], max_tokens=16)
|
| 56 |
+
print(f"[probe] warmup: {dt:.1f}s -> '{text[:60]}'", flush=True)
|
| 57 |
+
|
| 58 |
+
# roleplay-style turn, measured twice
|
| 59 |
+
for rep in range(2):
|
| 60 |
+
text, dt, n = chat([
|
| 61 |
+
{"role": "system", "content": "You are Captain Vera Steel, captain of the "
|
| 62 |
+
"freighter Long Dawn. Reply in 2 short spoken sentences."},
|
| 63 |
+
{"role": "user", "content": "Captain, what's our cargo and destination?"}], max_tokens=80)
|
| 64 |
+
tps = (n / dt) if n else 0
|
| 65 |
+
print(f"[probe] turn rep{rep}: {dt:.1f}s, {n} tokens ({tps:.1f} tok/s) "
|
| 66 |
+
f"-> '{text[:100]}'", flush=True)
|
| 67 |
+
|
| 68 |
+
print("[probe] done", flush=True)
|