Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions terminal/serve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4297,6 +4297,8 @@ def __init__(self, engine: Engine, tokenizer, template: ChatTemplate, model_name
# "*" = any), and every name it answers to, which serve() works out from the address it listens on
self.allowed_hosts: list[str] = []
self.host_names: set[str] = set(LOOPBACK_NAMES)
# the PC's real network IP (what another device types in), shown in About > This PC; worked out once at serve()
self.server_ip: str | None = None
self.status = {"busy": False, "queued": 0} # GET /status: what the model is doing right now
self.rate = collections.deque(maxlen=32) # (time, generated) samples for the live tok/s window
# "parallel" (the engine's batch slots): every running request's own status and rate window; self.status
Expand Down Expand Up @@ -5754,11 +5756,13 @@ def metrics(self, all_requests=False) -> dict:
engine["vision_note"] = self.vision.cpu_note
tel = self.telemetry.snapshot() if getattr(self, "telemetry", None) else {"now": {}, "history": {}, "static": {}}
parked = self.conv_log.poll(getattr(self.engine, "log_path", None), getattr(self.engine, "log_start", None))
static = dict(tel["static"])
if self.server_ip: # About > This PC: the PC's real network IP (no port)
static["server_ip"] = self.server_ip
return {"engine": engine, "live": live, "requests": hist[::-1][:None if all_requests else 12],
"conversation_cache": conversation_cache_view(engine, hist, totals, parked),
"requests_kept": len(hist), "totals": totals, "hardware": tel["now"],
"hardware_static":
tel["static"], "history": tel["history"], "time": now}
"hardware_static": static, "history": tel["history"], "time": now}

def prometheus(self) -> str:
"""GET /metrics/prometheus: the totals and what is running in Prometheus' text format, under llama-server's
Expand Down Expand Up @@ -9106,6 +9110,13 @@ def lan_addresses() -> list[str]:
return ([first] if ok(first) else []) + sorted(ip for ip in ips if ok(ip) and ip != first)


def machine_ip() -> str | None:
"""The PC's real network IP (what another device types in): its IPv4 addresses on the networks, without loopback
or link-local. None when the machine has none (only loopback, or only IPv6)."""
ips = lan_addresses()
return ", ".join(ips) if ips else None


def host_names_for(bind_host: str, allowed_hosts=(), trusted_origins=()) -> set[str]:
"""Every name this server answers to besides an IP address: localhost, the address it listens on, the config's
allowed_hosts and trusted_origins' hosts, and - listening beyond this PC (0.0.0.0 or a LAN address) - this PC's
Expand All @@ -9128,6 +9139,7 @@ def host_names_for(bind_host: str, allowed_hosts=(), trusted_origins=()) -> set[

def serve(svc: Service, host="127.0.0.1", port=8095) -> ThreadingHTTPServer:
svc.host_names = host_names_for(host, svc.allowed_hosts, svc.trusted_origins)
svc.server_ip = machine_ip() # About > This PC: the PC's real IP, worked out once at start
svc.start_telemetry()
httpd = Server((host, port), make_handler(svc))
httpd.svc = svc
Expand Down
18 changes: 18 additions & 0 deletions terminal/serve/test_web_monitor_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def test_about_shows_the_reason_beside_a_named_gpu(self):
"console.log(JSON.stringify(rows['facts-hw'].find(r => r[0] === 'GPU')[1]));")
self.assertEqual(run_node(script), f"Apple M5 Max, 122 GB · {MAC_NOTE}")

SERVER_IP = "192.168.1.42"

def _about_hw(self, st):
app = (WEB / "app.js").read_text(encoding="utf-8")
return (STUBS + "const rows = {}; const facts = (el, r) => { rows[el.id] = r; }; const projectionText = () => null;\n"
"globalThis.location = {origin: 'http://127.0.0.1:8080'};\n" + function_source(app, "renderAbout") +
f"renderAbout({{}}, {json.dumps({})}, {json.dumps(st)});\n"
"const row = rows['facts-hw'].find(r => r[0] === 'Server');\n"
"console.log(JSON.stringify(row ? row[1] : null));")

def test_about_shows_the_server_ip(self):
cards = run_node(self._about_hw({"gpu_name": "NVIDIA GeForce RTX 4090", "server_ip": self.SERVER_IP}))
self.assertEqual(cards, self.SERVER_IP, "the PC's real network IP, beside GPU/CPU/RAM")

def test_about_hides_the_server_row_without_an_ip(self):
cards = run_node(self._about_hw({"gpu_name": "NVIDIA GeForce RTX 4090"}))
self.assertIsNone(cards, "no server_ip from an older server: the row is left out")


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions terminal/serve/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,8 @@ function renderAbout(eng, hw, st) {
["GPU", st.gpu_name ? `${st.gpu_name}${hw.gpu_mem_total ? `, ${gb(hw.gpu_mem_total, 0)} GB` : ""}${st.gpu_note ? ` · ${st.gpu_note}` : ""}` : (st.gpu_note || "not readable (NVML)")],
["CPU", st.cpu_name ? `${st.cpu_name}${st.threads ? `, ${st.threads} threads` : ""}` : null],
["RAM", hw.ram_total ? `${gb(hw.ram_total, 0)} GB` : null],
// the PC's real network IP (no port - that is in the base URLs above); the server sends nothing when it has none
["Server", st.server_ip || null],
]);
const base = location.origin;
facts($("facts-api"), [
Expand Down
Loading