m97 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
m97/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """A web dashboard and control panel for a gufo LLM server on AMD Strix Halo."""
2
+
3
+ __version__ = "0.1.0"
m97/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ """`python -m m97`, which is what the systemd unit runs."""
2
+
3
+ import sys
4
+
5
+ from .cli import main
6
+
7
+ sys.exit(main())
m97/access.py ADDED
@@ -0,0 +1,160 @@
1
+ """Who may press the buttons: anyone while the machine is on a trusted network, and elsewhere whoever knows the PIN.
2
+
3
+ Viewing is always open; only starting and stopping services is gated. A trusted network is recognized by the
4
+ UUID of the NetworkManager connection the machine is using, optionally narrowed by a subnet. NetworkManager
5
+ offers a saved Wi-Fi profile only to an access point with the same SSID *and* compatible security, so a WPA
6
+ profile is not matched by an open network that borrows its name.
7
+ """
8
+
9
+ import dataclasses
10
+ import hmac
11
+ import ipaddress
12
+ import logging
13
+ import subprocess
14
+ import time
15
+
16
+ __all__ = ["active_connections", "parse_active_connections", "parse_addresses",
17
+ "trusted_network",
18
+ "pin_matches", "make_rate_limiter",
19
+ "Decision", "decide"]
20
+
21
+ logger = logging.getLogger(__name__)
22
+
23
+
24
+ # --------------------------------------------------------------------------------
25
+ # What network the machine is on
26
+
27
+ def parse_active_connections(text):
28
+ """Parse `nmcli -t -f UUID,DEVICE connection show --active` into a list of `(uuid, device)`."""
29
+ connections = []
30
+ for line in text.splitlines():
31
+ uuid, sep, device = line.partition(":")
32
+ if sep and uuid:
33
+ connections.append((uuid, device))
34
+ return connections
35
+
36
+
37
+ def parse_addresses(text):
38
+ """Parse `nmcli -g IP4.ADDRESS device show <dev>` into a list of `ipaddress.IPv4Interface`.
39
+
40
+ nmcli separates several addresses with ` | `; anything that is not an address is skipped.
41
+ """
42
+ addresses = []
43
+ for item in text.replace("|", "\n").split():
44
+ try:
45
+ addresses.append(ipaddress.ip_interface(item))
46
+ except ValueError:
47
+ continue
48
+ return addresses
49
+
50
+
51
+ def _nmcli(*args):
52
+ result = subprocess.run(["nmcli", "-t", *args], capture_output=True, text=True, timeout=5)
53
+ result.check_returncode()
54
+ return result.stdout
55
+
56
+
57
+ def active_connections():
58
+ """Return the machine's active NetworkManager connections as a list of `(uuid, [addresses])`.
59
+
60
+ Empty if NetworkManager is not there or does not answer, which makes every network untrusted.
61
+ """
62
+ try:
63
+ connections = parse_active_connections(_nmcli("-f", "UUID,DEVICE", "connection", "show", "--active"))
64
+ return [(uuid, parse_addresses(_nmcli("-g", "IP4.ADDRESS", "device", "show", device)) if device else [])
65
+ for uuid, device in connections]
66
+ except (OSError, subprocess.SubprocessError) as exc:
67
+ logger.warning(f"could not ask NetworkManager for the active connections, so no network is trusted: {exc}")
68
+ return []
69
+
70
+
71
+ def trusted_network(networks, connections):
72
+ """Return the name of the trusted network the machine is on, or `None`.
73
+
74
+ `networks` is the `access.trusted_networks` setting: a list of tables with `name`, `connections` (a list
75
+ of connection UUIDs) and optionally `subnet`. `connections` is what `active_connections` returns. A
76
+ network matches when one of its UUIDs is active and, if it names a subnet, that connection has an
77
+ address inside it.
78
+ """
79
+ for network in networks:
80
+ uuids = set(network.get("connections", []))
81
+ maybe_subnet = network.get("subnet")
82
+ try:
83
+ subnet = ipaddress.ip_network(maybe_subnet, strict=False) if maybe_subnet else None
84
+ except ValueError:
85
+ logger.warning(f"trusted network {network.get('name')!r}: `subnet` {maybe_subnet!r} is not a subnet, "
86
+ f"so the network never matches")
87
+ continue
88
+ for uuid, addresses in connections:
89
+ if uuid not in uuids:
90
+ continue
91
+ if subnet is None or any(address.ip in subnet for address in addresses):
92
+ return network.get("name") or uuid
93
+ return None
94
+
95
+
96
+ # --------------------------------------------------------------------------------
97
+ # The PIN
98
+
99
+ def pin_matches(configured, given):
100
+ """Whether `given` is the configured PIN. An empty configured PIN matches nothing."""
101
+ if not configured:
102
+ return False
103
+ return hmac.compare_digest(configured.encode("utf-8"), (given or "").encode("utf-8"))
104
+
105
+
106
+ def make_rate_limiter(*, max_failures=5, window=300.0, clock=time.monotonic):
107
+ """Make a limiter for wrong PINs: at most `max_failures` per client within `window` seconds.
108
+
109
+ Returns `(allowed, record_failure)`. `allowed(client)` says whether the client may try now;
110
+ `record_failure(client)` counts a wrong PIN. `client` is any hashable key, e.g. the client's address.
111
+ """
112
+ # Against a four-digit PIN: 10^4 values, so 5,000 guesses on average; at the defaults, one guess a minute,
113
+ # that is 5,000 min ≈ 3.5 days per address, and a week to exhaust the space.
114
+ failures = {} # client -> monotonic times of recent failures
115
+
116
+ def recent(client):
117
+ now = clock()
118
+ times = [t for t in failures.get(client, []) if now - t < window]
119
+ if times:
120
+ failures[client] = times
121
+ else:
122
+ failures.pop(client, None)
123
+ return times
124
+
125
+ def allowed(client):
126
+ return len(recent(client)) < max_failures
127
+
128
+ def record_failure(client):
129
+ failures.setdefault(client, []).append(clock())
130
+
131
+ return allowed, record_failure
132
+
133
+
134
+ # --------------------------------------------------------------------------------
135
+ # The decision the page shows
136
+
137
+ @dataclasses.dataclass(frozen=True, kw_only=True)
138
+ class Decision:
139
+ """Whether the buttons are open, and a sentence for the page saying why.
140
+
141
+ `allowed`: open without a PIN. `pin_required`: a PIN would open them. Both false: locked, and `reason`
142
+ says what to configure.
143
+ """
144
+ allowed: bool
145
+ pin_required: bool
146
+ reason: str
147
+
148
+
149
+ def decide(access_settings, connections):
150
+ """Decide whether the buttons are open, given the `access` settings and the active connections."""
151
+ name = trusted_network(access_settings.get("trusted_networks", []), connections)
152
+ if name is not None:
153
+ return Decision(allowed=True, pin_required=False, reason=f"On the trusted network “{name}”.")
154
+ if access_settings.get("pin"):
155
+ return Decision(allowed=False, pin_required=True,
156
+ reason="Not on a trusted network: enter the PIN to use the buttons.")
157
+ return Decision(allowed=False, pin_required=False,
158
+ reason="Locked: this machine is not on a trusted network, and no PIN is set. To use the buttons "
159
+ "here, set `pin` under `[access]` in ~/.config/m97/settings.toml, or add this network "
160
+ "to `trusted_networks` there.")
m97/app.py ADDED
@@ -0,0 +1,351 @@
1
+ """The page: what the machine and its LLM server are doing, and buttons to start and stop the services."""
2
+
3
+ import datetime
4
+ import socket
5
+ import time
6
+
7
+ from nicegui import app, ui
8
+
9
+ from . import access, gufo
10
+
11
+ __all__ = ["build"]
12
+
13
+ # Badge (background, text) per status, chosen for contrast: white on green-700 is 5.0:1, black on amber
14
+ # 12:1, white on grey-700 6.2:1 (WCAG AA asks 4.5:1 for small text).
15
+ STATUS_COLORS = {"running": ("#15803d", "#ffffff"),
16
+ "starting": ("#f2c037", "#000000"),
17
+ "stopping": ("#f2c037", "#000000"),
18
+ "stopped": ("#616161", "#ffffff")}
19
+
20
+
21
+ def _fmt(value, spec, unit=""):
22
+ return "–" if value is None else f"{value:{spec}}{unit}"
23
+
24
+
25
+ def _spark(name, color, *, max_value=None):
26
+ """ECharts options for a small line chart with no axes, labels or legend."""
27
+ return {"animation": False,
28
+ "grid": {"left": 2, "right": 2, "top": 4, "bottom": 2},
29
+ "xAxis": {"type": "value", "show": False},
30
+ "yAxis": {"type": "value", "show": False, "min": 0, **({"max": max_value} if max_value else {})},
31
+ "tooltip": {"trigger": "axis", "valueFormatter": None},
32
+ "series": [{"name": name, "type": "line", "showSymbol": False, "data": [],
33
+ "lineStyle": {"width": 1.5, "color": color},
34
+ "areaStyle": {"color": color, "opacity": 0.15}}]}
35
+
36
+
37
+ def build(hub):
38
+ """Register the page, reading everything it shows from `hub`."""
39
+
40
+ @ui.page("/")
41
+ async def index(client):
42
+ ui.page_title(f"m97 · {socket.gethostname()}")
43
+ dark = ui.dark_mode(True)
44
+ # Quasar marks a disabled button only by fading it to 70 % opacity, which beside an enabled one reads as a
45
+ # slightly different shade of the same button. Grey it out instead.
46
+ ui.add_css(".q-btn.disabled { opacity: 0.3 !important; filter: grayscale(1); }")
47
+ ui.add_css(".q-card { border-radius: 10px; } .q-btn { border-radius: 8px; }")
48
+ # Quasar's red (#c10015) is 2.6:1 on the dark background; red-400 is 6.1:1 there, though too light on
49
+ # white. Its variable reaches the Stop buttons and the error text alike. `!important` because Quasar
50
+ # sets the variable as an inline style on <body>, which an ordinary rule cannot override.
51
+ ui.add_css(".body--dark { --q-negative: #f87171 !important; }")
52
+
53
+ # The viewer's choice of dark or light is kept in their browser, which may refuse storage (a private
54
+ # window, blocked site data); then the page simply opens dark.
55
+ def toggle_dark():
56
+ dark.set_value(not dark.value)
57
+ ui.run_javascript(f"try {{ localStorage.setItem('m97-dark', '{'1' if dark.value else '0'}') }} catch (e) {{}}")
58
+ unlocked = {"by_pin": False} # per page: a right PIN opens the buttons until the page is reloaded
59
+
60
+ with ui.header().classes("items-center justify-between px-4 py-2"):
61
+ ui.label(f"m97 · {socket.gethostname()}").classes("text-lg font-medium")
62
+ with ui.row().classes("items-center gap-2"):
63
+ clock_label = ui.label().classes("text-sm opacity-80")
64
+ ui.button(icon="contrast", on_click=toggle_dark).props(
65
+ "flat round dense color=white").tooltip("Dark or light")
66
+
67
+ with ui.column().classes("w-full max-w-6xl mx-auto p-4 gap-4"):
68
+
69
+ # ---- Services
70
+ with ui.card().props("flat bordered").classes("w-full"):
71
+ ui.label("Services").classes("text-base font-medium")
72
+ rows = {}
73
+ for key, service in hub.services.items():
74
+ with ui.row().classes("w-full items-center gap-3"):
75
+ ui.label(service.title).classes("w-28 font-medium")
76
+ badge = ui.badge("…")
77
+ spinner = ui.spinner(size="1.2em")
78
+ detail = ui.label().classes("text-sm opacity-70")
79
+ address = ui.link(new_tab=True).classes("text-sm font-mono")
80
+ address_plain = ui.label().classes("text-sm font-mono opacity-80")
81
+ scope = ui.label().classes("text-sm opacity-70")
82
+ ui.space()
83
+ start_button = ui.button("Start", on_click=lambda k=key: act(k, "start")).props("unelevated")
84
+ stop_button = ui.button("Stop", on_click=lambda k=key: act(k, "stop"),
85
+ color="negative").props("outline")
86
+ with ui.column().classes("w-full gap-0") as error:
87
+ error_summary = ui.label().classes("text-sm text-negative")
88
+ with ui.expansion("Details").props("dense").classes("text-sm w-full") as error_more:
89
+ error_details = ui.label().classes("text-xs font-mono whitespace-pre-wrap opacity-80")
90
+ rows[key] = (badge, spinner, detail, (address, address_plain, scope), start_button, stop_button,
91
+ (error, error_summary, error_more, error_details))
92
+ with ui.row().classes("w-full items-center gap-3"):
93
+ lock_note = ui.label().classes("text-sm opacity-70")
94
+ pin_input = ui.input("PIN", password=True).props("dense outlined").classes("w-32")
95
+
96
+ # ---- Model, speed, power, load, memory
97
+ with ui.grid(columns="repeat(auto-fit, minmax(17rem, 1fr))").classes("w-full gap-4"):
98
+ with ui.card().props("flat bordered"):
99
+ ui.label("Model").classes("text-base font-medium")
100
+ model_label = ui.label().classes("text-xl")
101
+ model_detail = ui.label().classes("text-sm opacity-70")
102
+ sessions_row = ui.row().classes("w-full gap-1 mt-2")
103
+ session_bars = [] # (bar, tooltip) per session, rebuilt when the count changes
104
+ sessions_label = ui.label().classes("text-sm opacity-70")
105
+
106
+ with ui.card().props("flat bordered"):
107
+ ui.label("Generation").classes("text-base font-medium")
108
+ speed_label = ui.label().classes("text-xl")
109
+ speed_chart = ui.echart(_spark("tok/s", "#7c6cf0")).classes("w-full h-16")
110
+ live_label = ui.label().classes("text-sm")
111
+ last_label = ui.label().classes("text-sm opacity-70")
112
+
113
+ with ui.card().props("flat bordered"):
114
+ ui.label("Power and temperatures").classes("text-base font-medium")
115
+ power_label = ui.label().classes("text-xl")
116
+ power_chart = ui.echart(_spark("W", "#e8833a")).classes("w-full h-16")
117
+ temp_label = ui.label().classes("text-sm opacity-70")
118
+
119
+ # CPU and GPU start alike, a readout over a load sparkline of the same height and scale, so the
120
+ # two sparklines sit level across the row and the same height means the same load in both.
121
+ with ui.card().props("flat bordered"):
122
+ ui.label("CPU").classes("text-base font-medium")
123
+ cpu_load_label = ui.label().classes("text-xl")
124
+ cpu_chart = ui.echart(_spark("CPU %", "#3b9ede", max_value=100)).classes("w-full h-16")
125
+ # One column per physical core, holding a bar per hardware thread of that core; 16 columns
126
+ # to a row.
127
+ thread_bars = {} # CPU number -> (fill, tooltip, core)
128
+ with ui.element("div").classes("w-full grid gap-x-[3px] gap-y-2").style(
129
+ f"grid-template-columns: repeat({min(16, len(hub.cpu_cores)) or 1}, minmax(0, 1fr))"):
130
+ for core, threads in enumerate(hub.cpu_cores):
131
+ with ui.element("div").classes("flex flex-col gap-[2px]"):
132
+ for cpu in threads:
133
+ with ui.element("div").classes("relative h-5 rounded-sm overflow-hidden").style(
134
+ "background: rgba(128,128,128,.2)"):
135
+ fill = ui.element("div").classes("absolute bottom-0 left-0 right-0").style(
136
+ "background: #3b9ede; height: 0%")
137
+ thread_bars[cpu] = (fill, ui.tooltip(), core)
138
+ cpu_label = ui.label().classes("text-sm opacity-70 mt-auto")
139
+
140
+ with ui.card().props("flat bordered"):
141
+ ui.label("GPU").classes("text-base font-medium")
142
+ gpu_load_label = ui.label().classes("text-xl")
143
+ gpu_chart = ui.echart(_spark("GPU %", "#2f9e72", max_value=100)).classes("w-full h-16")
144
+ gpu_label = ui.label().classes("text-sm opacity-70 mt-auto")
145
+
146
+ with ui.card().props("flat bordered"):
147
+ ui.label("Memory").classes("text-base font-medium")
148
+ ram_label = ui.label().classes("text-sm")
149
+ ram_bar = ui.linear_progress(show_value=False).props("rounded size=10px")
150
+ gtt_label = ui.label().classes("text-sm mt-2")
151
+ gtt_bar = ui.linear_progress(show_value=False, color="secondary").props("rounded size=10px")
152
+
153
+ # ---- Recent requests
154
+ with ui.card().props("flat bordered").classes("w-full"):
155
+ ui.label("Recent requests").classes("text-base font-medium")
156
+ columns = [{"name": "time", "label": "Time", "field": "time", "align": "left"},
157
+ {"name": "tokens", "label": "Prompt → generated", "field": "tokens", "align": "right"},
158
+ {"name": "output", "label": "Generation", "field": "output", "align": "right"},
159
+ {"name": "decode", "label": "Decoding", "field": "decode", "align": "right"},
160
+ {"name": "prefill", "label": "Prefill", "field": "prefill", "align": "right"},
161
+ {"name": "ttft", "label": "First token", "field": "ttft", "align": "right"},
162
+ {"name": "duration", "label": "Duration", "field": "duration", "align": "right"},
163
+ {"name": "finish", "label": "Finish", "field": "finish", "align": "left"}]
164
+ table = ui.table(columns=columns, rows=[], row_key="id").props("flat dense").classes("w-full")
165
+ no_requests = ui.label("No finished requests yet.").classes("text-sm opacity-70")
166
+
167
+ async def act(key, verb):
168
+ decision = await hub.decision()
169
+ if not decision.allowed and not unlocked["by_pin"]:
170
+ if not decision.pin_required:
171
+ ui.notify(decision.reason, type="warning", multi_line=True)
172
+ return
173
+ if not hub.pin_allowed(client.ip):
174
+ ui.notify("Too many wrong PINs. Try again in a few minutes.", type="negative")
175
+ return
176
+ if not access.pin_matches(hub.settings["access"]["pin"], pin_input.value):
177
+ hub.pin_failed(client.ip)
178
+ ui.notify("Wrong PIN.", type="negative")
179
+ return
180
+ unlocked["by_pin"] = True
181
+ pin_input.value = ""
182
+ hub.start_service(key) if verb == "start" else hub.stop_service(key)
183
+
184
+ def refresh_access():
185
+ decision = hub.access
186
+ open_ = decision.allowed or unlocked["by_pin"]
187
+ lock_note.text = "Unlocked with the PIN." if unlocked["by_pin"] and not decision.allowed else decision.reason
188
+ pin_input.visible = decision.pin_required and not open_
189
+ return open_ or decision.pin_required
190
+
191
+ def refresh():
192
+ clock_label.text = datetime.datetime.now().strftime("%H:%M:%S")
193
+ usable = refresh_access()
194
+
195
+ for key, (badge, spinner, detail, (address, address_plain, scope), start_button, stop_button,
196
+ (error, error_summary, error_more, error_details)) in rows.items():
197
+ status = hub.status(key)
198
+ badge.text = status
199
+ background, foreground = STATUS_COLORS[status]
200
+ badge.style(f"background: {background} !important; color: {foreground} !important")
201
+ spinner.visible = status in ("starting", "stopping")
202
+ state = hub.container[key]
203
+ detail.text = hub.note[key] or (f"container: {state}" if state not in (None, "running") else "")
204
+ service, where = hub.services[key], hub.scope[key]
205
+ host = socket.gethostname() if where == "all" else "127.0.0.1"
206
+ url = f"http://{host}:{service.port}{service.url_path}" if where else ""
207
+ address.text = url
208
+ address.props(f'href="{url}"')
209
+ address.visible = bool(url) and service.browsable
210
+ address_plain.text = url
211
+ address_plain.visible = bool(url) and not service.browsable
212
+ scope.text = {"all": "all interfaces", "local": "this machine only"}.get(where, "")
213
+ # Stop stays available while starting: it cancels the start and removes the container.
214
+ start_button.enabled = usable and status == "stopped"
215
+ stop_button.enabled = usable and status in ("running", "starting")
216
+ # A live error, or one remembered from before m97 restarted. What to read comes before the
217
+ # first blank line; the rest, usually log lines, is folded away.
218
+ message = hub.error[key] or hub.problem[key]
219
+ summary, _, details = message.partition("\n\n")
220
+ error_summary.text, error_details.text = summary, details
221
+ error_more.visible = bool(details)
222
+ error.visible = bool(message)
223
+
224
+ props = hub.props or {}
225
+ if hub.ready["gufo"]:
226
+ model_label.text = props.get("model_alias") or props.get("model") or "?"
227
+ n_ctx = (props.get("default_generation_settings") or {}).get("n_ctx")
228
+ version = (props.get("build_info") or "").split(" ")[0]
229
+ parts = [f"{n_ctx // 1024} Ki context" if n_ctx else "", f"gufo {version}" if version else ""]
230
+ model_detail.text = " · ".join(part for part in parts if part)
231
+ else:
232
+ model_label.text = {"starting": "loading…", "stopping": "stopping…"}.get(hub.status("gufo"), "not running")
233
+ model_detail.text = ""
234
+ if len(session_bars) != len(hub.slots):
235
+ sessions_row.clear()
236
+ session_bars.clear()
237
+ with sessions_row:
238
+ for _ in hub.slots:
239
+ with ui.element("div").classes("h-3 flex-1 rounded") as bar:
240
+ session_bars.append((bar, ui.tooltip()))
241
+ for (bar, tip), slot in zip(session_bars, hub.slots):
242
+ # A busy session's bar is filled as far as its context is: a request running away in a loop
243
+ # shows as a bar creeping across.
244
+ if not slot["busy"]:
245
+ background = "rgba(128,128,128,.25)"
246
+ elif slot["fill"] is None:
247
+ background = "#7c6cf0"
248
+ else:
249
+ edge = max(2.0, 100 * slot["fill"]) # a sliver even when nearly empty, to read as busy
250
+ background = f"linear-gradient(to right, #7c6cf0 {edge:.1f}%, rgba(124,108,240,.3) {edge:.1f}%)"
251
+ bar.style(f"background: {background}")
252
+ if slot["busy"]:
253
+ used = (slot["prompt_tokens"] or 0) + (slot["decoded"] or 0)
254
+ tip.text = (f"session {slot['id']}: generating at {_fmt(slot['tps'], '.1f', ' tok/s')}; "
255
+ f"context {used:,} of {_fmt(slot['n_ctx'], ',')} tokens "
256
+ f"({_fmt(None if slot['fill'] is None else 100 * slot['fill'], '.1f', ' %')}), "
257
+ f"{_fmt(slot['decoded'], ',')} generated")
258
+ else:
259
+ tip.text = f"session {slot['id']}: idle"
260
+ busy_slots = [s for s in hub.slots if s["busy"]]
261
+ fills = [s["fill"] for s in busy_slots if s["fill"] is not None]
262
+ deferred = hub.metrics.get("llamacpp:requests_deferred") or 0
263
+ sessions_label.text = " · ".join(part for part in (
264
+ f"{len(busy_slots)} of {len(hub.slots)} sessions busy" if hub.slots else "",
265
+ f"{deferred:.0f} waiting" if deferred else "",
266
+ # One decimal below 10 %, where whole percents would read "0 %" for a session that is busy.
267
+ f"fullest context {100 * max(fills):.{1 if max(fills) < 0.1 else 0}f} %" if fills else "") if part)
268
+
269
+ speed_label.text = _fmt(hub.gen_tps, ".1f", " tok/s") if hub.ready["gufo"] else "–"
270
+ live = [slot["tps"] for slot in hub.slots if slot["busy"] and slot["tps"] is not None]
271
+ live_label.text = f"Now, per session: {' · '.join(f'{tps:.1f}' for tps in live)} tok/s" if live else ""
272
+ live_label.visible = bool(live)
273
+ if hub.requests:
274
+ last = hub.requests[-1]
275
+ last_label.text = (f"Last request: {_fmt(gufo.output_tps(last), '.1f')} tok/s generation "
276
+ f"({_fmt(last.get('decode_tps'), '.1f')} decoding), "
277
+ f"{_fmt(last.get('prefill_tps'), '.0f')} tok/s prefill")
278
+ else:
279
+ last_label.text = ""
280
+
281
+ s = hub.latest
282
+ power_label.text = _fmt(s.get("ppt_w"), ".1f", " W")
283
+ temp_label.text = f"CPU {_fmt(s.get('cpu_temp_c'), '.0f', ' °C')} · GPU {_fmt(s.get('gpu_temp_c'), '.0f', ' °C')}"
284
+ cpu_load_label.text = _fmt(s.get("cpu_busy_pct"), ".0f", " %")
285
+ gpu_load_label.text = _fmt(s.get("gpu_busy_pct"), ".0f", " %")
286
+ gpu_label.text = f"clock {_fmt(s.get('gpu_mhz'), '.0f', ' MHz')}"
287
+ ram_used, ram_total = s.get("ram_used_gib"), s.get("ram_total_gib")
288
+ ram_label.text = f"RAM {_fmt(ram_used, '.1f')} of {_fmt(ram_total, '.0f', ' GiB')}"
289
+ ram_bar.value = ram_used / ram_total if ram_used is not None and ram_total else 0
290
+ gtt_used, gtt_total = s.get("gtt_used_gib"), s.get("gtt_total_gib")
291
+ gtt_label.text = f"GTT (mapped by the GPU) {_fmt(gtt_used, '.1f')} of {_fmt(gtt_total, '.0f', ' GiB')}"
292
+ gtt_bar.value = gtt_used / gtt_total if gtt_used is not None and gtt_total else 0
293
+
294
+ per_thread = s.get("cpu_thread_pct") or {}
295
+ for cpu, (fill, tip, core) in thread_bars.items():
296
+ pct = per_thread.get(cpu)
297
+ fill.style(f"background: #3b9ede; height: {0 if pct is None else min(100.0, pct):.0f}%")
298
+ tip.text = f"core {core}, CPU {cpu}: {_fmt(pct, '.0f', ' %')}"
299
+ busy = [pct for pct in per_thread.values() if pct is not None]
300
+ cpu_label.text = " · ".join(part for part in (
301
+ f"{len(hub.cpu_cores)} cores, {len(per_thread)} threads" if per_thread else "",
302
+ f"busiest {max(busy):.0f} %" if busy else "",
303
+ f"load {_fmt(s.get('load1'), '.2f')}") if part)
304
+
305
+ now = time.monotonic()
306
+ for chart, series in ((speed_chart, "gen_tps"), (power_chart, "ppt_w"), (cpu_chart, "cpu_busy_pct"),
307
+ (gpu_chart, "gpu_busy_pct")):
308
+ chart.options["xAxis"]["min"] = -hub.settings["history"]["seconds"]
309
+ chart.options["xAxis"]["max"] = 0
310
+ chart.options["series"][0]["data"] = [[round(t - now, 1), values[series]]
311
+ for t, values in hub.history if values[series] is not None]
312
+ chart.update()
313
+
314
+ table.rows = [{"id": r.get("request", i),
315
+ "time": r["time"].astimezone().strftime("%H:%M:%S"),
316
+ "tokens": f"{_fmt(r.get('prompt_tokens'), ',d')} → {_fmt(r.get('generated_tokens'), ',d')}",
317
+ "output": _fmt(gufo.output_tps(r), ".1f", " tok/s"),
318
+ "decode": _fmt(r.get("decode_tps"), ".1f", " tok/s"),
319
+ "prefill": _prefill_cell(r),
320
+ "ttft": _fmt(_ms_to_s(r.get("ttft_ms")), ".2f", " s"),
321
+ "duration": _fmt(_ms_to_s(r.get("duration_ms")), ".1f", " s"),
322
+ "finish": str(r.get("finish", "–"))}
323
+ for i, r in enumerate(reversed(hub.requests))]
324
+ table.visible = bool(hub.requests)
325
+ no_requests.visible = not hub.requests
326
+
327
+ refresh()
328
+ ui.timer(hub.interval, refresh)
329
+
330
+ await client.connected()
331
+ try:
332
+ stored = await ui.run_javascript("try { return localStorage.getItem('m97-dark') } catch (e) { return null }")
333
+ except TimeoutError:
334
+ stored = None
335
+ if stored in ("0", "1"):
336
+ dark.set_value(stored == "1")
337
+
338
+ app.on_startup(hub.start)
339
+ app.on_shutdown(hub.stop)
340
+
341
+
342
+ def _ms_to_s(value):
343
+ return value / 1000 if isinstance(value, (int, float)) else None
344
+
345
+
346
+ def _prefill_cell(record):
347
+ """Prefill speed for the recent-requests table, or "cached" when the whole prompt came from the cache."""
348
+ prompt, cached = record.get("prompt_tokens"), record.get("cached_tokens")
349
+ if isinstance(prompt, int) and isinstance(cached, int) and prompt > 0 and cached >= prompt:
350
+ return "cached"
351
+ return _fmt(record.get("prefill_tps"), ".0f", " tok/s")