"""Probe the sovereign LLM Space: wait for boot, then measure /v1/chat/completions.""" import json import time import urllib.request import urllib.error BASE = "https://lovelogicai-freestream-llm.hf.space" KEY = "" # LLM_API_KEY secret not set yet → open endpoint print("[probe] waiting for the Space to build + boot (model download on first boot)...", flush=True) deadline = time.time() + 900 up = False while time.time() < deadline: try: req = urllib.request.Request(f"{BASE}/health") with urllib.request.urlopen(req, timeout=10) as r: if r.status == 200: up = True break except urllib.error.HTTPError as e: if e.code == 200: up = True break print(f" health: HTTP {e.code}", flush=True) except Exception as e: print(f" health: {type(e).__name__}", flush=True) time.sleep(15) if not up: print("[probe] Space did not come up within 15 min — check the build log", flush=True) raise SystemExit(1) print("[probe] llama-server /health OK", flush=True) def chat(messages, max_tokens=80): payload = json.dumps({"model": "local", "messages": messages, "max_tokens": max_tokens, "temperature": 0.8}).encode() req = urllib.request.Request( f"{BASE}/v1/chat/completions", data=payload, method="POST", headers={"Content-Type": "application/json", **({"Authorization": f"Bearer {KEY}"} if KEY else {})}) t0 = time.time() with urllib.request.urlopen(req, timeout=300) as r: data = json.loads(r.read()) dt = time.time() - t0 text = data["choices"][0]["message"]["content"] n = data.get("usage", {}).get("completion_tokens", 0) return text, dt, n # warmup (model load, mmap) text, dt, n = chat([{"role": "user", "content": "Say 'bridge online'."}], max_tokens=16) print(f"[probe] warmup: {dt:.1f}s -> '{text[:60]}'", flush=True) # roleplay-style turn, measured twice for rep in range(2): text, dt, n = chat([ {"role": "system", "content": "You are Captain Vera Steel, captain of the " "freighter Long Dawn. Reply in 2 short spoken sentences."}, {"role": "user", "content": "Captain, what's our cargo and destination?"}], max_tokens=80) tps = (n / dt) if n else 0 print(f"[probe] turn rep{rep}: {dt:.1f}s, {n} tokens ({tps:.1f} tok/s) " f"-> '{text[:100]}'", flush=True) print("[probe] done", flush=True)