constructelligence commited on
Commit
53f4112
·
verified ·
1 Parent(s): b9b09fd

Upload console_server.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. console_server.py +411 -0
console_server.py ADDED
@@ -0,0 +1,411 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Operator console: photo in, reviewed and approved paint work order out.
3
+
4
+ A local web app for the person standing next to the robot. Upload (or take) a photo
5
+ of the wall; the kit shows where it will paint, what it keeps out of, which damage
6
+ needs prep first, and why the safety gate would stop. The operator can:
7
+
8
+ * toggle layers (paint area, keep-out, uncertain wall, touch-up, damage, tool paths);
9
+ * correct the plan with a brush: **paint here** / **do not paint**;
10
+ * sign the confirmations the gate allows a person to make (not wallpaper, depth
11
+ waived, uncertain areas reviewed), each recorded with their name;
12
+ * re-plan, then **approve**. An approved order is frozen: the job keeps the photo,
13
+ masks, edits and work order with their sha256, and an entry is appended to the
14
+ hash-linked ``registry.jsonl`` (``run_registry.py verify`` checks it).
15
+
16
+ Production notes: binds to 127.0.0.1 unless ``--host`` says otherwise; one model
17
+ loaded once, inference serialised behind a lock; uploads capped (``MAX_UPLOAD``)
18
+ and decoded before anything is written; job ids are random and file serving is an
19
+ allowlist, so no path from a request reaches the filesystem. stdlib + the kit's own
20
+ dependencies only: nothing extra to install on the robot PC.
21
+
22
+ Usage::
23
+
24
+ python3 console_server.py --checkpoint artifacts/best.pt --jobs-dir jobs
25
+ # open http://127.0.0.1:8765
26
+ """
27
+ from __future__ import annotations
28
+
29
+ import argparse
30
+ import base64
31
+ import hashlib
32
+ import io
33
+ import json
34
+ import re
35
+ import secrets
36
+ import threading
37
+ import time
38
+ from http import HTTPStatus
39
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
40
+ from pathlib import Path
41
+ from urllib.parse import parse_qs, urlparse
42
+
43
+ import numpy as np
44
+ from PIL import Image
45
+
46
+ from work_order import CONFIRMATIONS, EDIT_ADD, EDIT_EXCLUDE, apply_edits, build_work_order, scale_from_prediction
47
+
48
+ VERSION = "1.0"
49
+ STATIC = Path(__file__).with_name("console")
50
+ MAX_UPLOAD = 30 * 1024 * 1024
51
+ MAX_SIDE = 2048 # frames are normalised to this long side before analysis
52
+ JOB_ID = re.compile(r"^[0-9a-f]{12}$")
53
+ LAYER_COLOURS = {"paint": (46, 204, 113), "keepout": (231, 76, 60), "uncertain": (241, 196, 15),
54
+ "touchup": (0, 210, 255)}
55
+ JOB_FILES = {"photo.jpg": "image/jpeg", "work_order.json": "application/json", "result.json": "application/json",
56
+ "approval.json": "application/json", "layer_edits.png": "image/png",
57
+ **{f"layer_{k}.png": "image/png" for k in LAYER_COLOURS}}
58
+ STATIC_FILES = {"index.html": "text/html; charset=utf-8", "app.js": "text/javascript; charset=utf-8",
59
+ "style.css": "text/css; charset=utf-8"}
60
+
61
+
62
+ def sha256_bytes(data):
63
+ return hashlib.sha256(data).hexdigest()
64
+
65
+
66
+ def now():
67
+ return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
68
+
69
+
70
+ def save_mask(path, mask):
71
+ Image.fromarray(np.asarray(mask, bool).astype(np.uint8) * 255).save(path)
72
+
73
+
74
+ def load_mask(path):
75
+ return np.asarray(Image.open(path).convert("L")) > 127
76
+
77
+
78
+ def layer_png(mask, colour, alpha=150):
79
+ mask = np.asarray(mask, bool)
80
+ rgba = np.zeros(mask.shape + (4,), np.uint8)
81
+ rgba[mask] = (*colour, alpha)
82
+ return Image.fromarray(rgba)
83
+
84
+
85
+ def decode_edits(data_url, shape):
86
+ """Edit canvas (PNG data URL) -> 0/1/2 layer: green = paint here, red = do not paint."""
87
+ if not data_url:
88
+ return None
89
+ match = re.match(r"^data:image/png;base64,([A-Za-z0-9+/=]+)$", data_url)
90
+ if not match:
91
+ raise ValueError("edits must be a PNG data URL")
92
+ image = Image.open(io.BytesIO(base64.b64decode(match.group(1)))).convert("RGBA")
93
+ if image.size != (shape[1], shape[0]):
94
+ image = image.resize((shape[1], shape[0]), Image.Resampling.NEAREST)
95
+ rgba = np.asarray(image)
96
+ painted = rgba[..., 3] > 0
97
+ edits = np.zeros(shape, np.uint8)
98
+ edits[painted & (rgba[..., 1] > rgba[..., 0])] = EDIT_ADD
99
+ edits[painted & (rgba[..., 0] >= rgba[..., 1])] = EDIT_EXCLUDE
100
+ return edits
101
+
102
+
103
+ class Console:
104
+ """Jobs on disk + the analyser. ``analyze_fn(rgb) -> (summary, masks)`` is injected (tests fake it)."""
105
+
106
+ def __init__(self, jobs_dir, analyze_fn, model_info, defaults=None):
107
+ self.jobs = Path(jobs_dir)
108
+ self.jobs.mkdir(parents=True, exist_ok=True)
109
+ self.analyze_fn = analyze_fn
110
+ self.model_info = model_info
111
+ self.defaults = defaults or {}
112
+ self.lock = threading.Lock() # one inference at a time; also serialises job writes
113
+
114
+ # ---------------------------------------------------------------- jobs
115
+ def job_dir(self, job_id):
116
+ if not JOB_ID.match(job_id or ""):
117
+ raise KeyError(job_id)
118
+ path = self.jobs / job_id
119
+ if not path.is_dir():
120
+ raise KeyError(job_id)
121
+ return path
122
+
123
+ def read_job(self, job_id):
124
+ path = self.job_dir(job_id)
125
+ job = json.loads((path / "job.json").read_text())
126
+ order_path = path / "work_order.json"
127
+ job["work_order"] = json.loads(order_path.read_text()) if order_path.is_file() else None
128
+ result = json.loads((path / "result.json").read_text())
129
+ job["regions"] = (result.get("surface_condition") or {}).get("regions", [])
130
+ job["measurements"] = result.get("measurements")
131
+ job["completion"] = result.get("completion")
132
+ job["paint_color"] = result.get("paint_color")
133
+ job["site_objects"] = result.get("site_objects")
134
+ job["has_edits"] = (path / "layer_edits.png").is_file()
135
+ return job
136
+
137
+ def list_jobs(self):
138
+ jobs = []
139
+ for path in sorted(self.jobs.iterdir(), key=lambda p: p.stat().st_mtime, reverse=True):
140
+ if JOB_ID.match(path.name) and (path / "job.json").is_file():
141
+ jobs.append(json.loads((path / "job.json").read_text()))
142
+ return jobs[:200]
143
+
144
+ def create_job(self, data, filename="", wall_id=""):
145
+ if len(data) > MAX_UPLOAD:
146
+ raise ValueError(f"image larger than {MAX_UPLOAD // (1024 * 1024)} MB")
147
+ try:
148
+ image = Image.open(io.BytesIO(data))
149
+ image.load()
150
+ except Exception as exc:
151
+ raise ValueError(f"not a readable image: {exc}") from exc
152
+ from PIL import ImageOps
153
+ image = ImageOps.exif_transpose(image).convert("RGB")
154
+ if max(image.size) > MAX_SIDE:
155
+ image.thumbnail((MAX_SIDE, MAX_SIDE), Image.Resampling.LANCZOS)
156
+ rgb = np.asarray(image)
157
+ job_id = secrets.token_hex(6)
158
+ path = self.jobs / job_id
159
+ path.mkdir()
160
+ image.save(path / "photo.jpg", quality=92)
161
+ with self.lock:
162
+ started = time.time()
163
+ summary, masks = self.analyze_fn(rgb)
164
+ seconds = round(time.time() - started, 2)
165
+ (path / "result.json").write_text(json.dumps(summary, indent=2) + "\n")
166
+ save_mask(path / "paintable.png", masks["safe_paintable"])
167
+ save_mask(path / "keepout.png", masks["hazards"])
168
+ save_mask(path / "touchup.png", masks["touch_up"])
169
+ save_mask(path / "wall.png", masks["wall"])
170
+ layer_png(masks["uncertain"], LAYER_COLOURS["uncertain"]).save(path / "layer_uncertain.png")
171
+ job = {"id": job_id, "created": now(), "filename": Path(filename).name[:120], "wall_id": wall_id[:80],
172
+ "width": image.size[0], "height": image.size[1], "photo_sha256": sha256_bytes(
173
+ (path / "photo.jpg").read_bytes()), "analysis_seconds": seconds, "model": self.model_info,
174
+ "status": None, "approved": None}
175
+ (path / "job.json").write_text(json.dumps(job, indent=2) + "\n")
176
+ self.plan(job_id, {})
177
+ return self.read_job(job_id)
178
+
179
+ def plan(self, job_id, request):
180
+ path = self.job_dir(job_id)
181
+ job = json.loads((path / "job.json").read_text())
182
+ if job.get("approved"):
183
+ raise PermissionError("job is approved and frozen; start a new job to change the plan")
184
+ prediction = json.loads((path / "result.json").read_text())
185
+ paintable, keepout = load_mask(path / "paintable.png"), load_mask(path / "keepout.png")
186
+ touchup, wall = load_mask(path / "touchup.png"), load_mask(path / "wall.png")
187
+ # edits_png absent: keep the saved edits; "" clears them; a data URL replaces them.
188
+ if "edits_png" in request:
189
+ edits = decode_edits(request["edits_png"], paintable.shape)
190
+ if edits is not None and edits.any():
191
+ Image.fromarray(edits).save(path / "edits.png")
192
+ elif (path / "edits.png").is_file():
193
+ (path / "edits.png").unlink()
194
+ edits = np.asarray(Image.open(path / "edits.png")) if (path / "edits.png").is_file() else None
195
+ edit_stats = None
196
+ if edits is not None:
197
+ paintable, keepout, edit_stats = apply_edits(paintable, keepout, edits, wall=wall)
198
+ rgba = np.zeros(edits.shape + (4,), np.uint8)
199
+ rgba[edits == EDIT_ADD] = (0, 255, 0, 255)
200
+ rgba[edits == EDIT_EXCLUDE] = (255, 0, 0, 255)
201
+ Image.fromarray(rgba).save(path / "layer_edits.png")
202
+ elif (path / "layer_edits.png").is_file():
203
+ (path / "layer_edits.png").unlink()
204
+ operator = (request.get("operator") or "").strip()[:80]
205
+ confirmations = {}
206
+ for name in request.get("confirmations") or []:
207
+ if name not in CONFIRMATIONS:
208
+ raise ValueError(f"unknown confirmation {name!r}")
209
+ if not operator:
210
+ raise ValueError("enter the operator's name to sign a confirmation")
211
+ confirmations[name] = {"by": operator, "at": now()}
212
+ depth = request.get("depth_report")
213
+ mm_per_px = request.get("mm_per_px") or self.defaults.get("mm_per_px")
214
+ source = "operator" if request.get("mm_per_px") else ("default" if mm_per_px else None)
215
+ if not mm_per_px:
216
+ mm_per_px, source = scale_from_prediction(prediction)
217
+ tools_mm = {**{"roller": 230.0, "trim": 60.0, "prep": 125.0, "margin": 40.0}, **(request.get("tools_mm") or {})}
218
+ order, _ = build_work_order(prediction, paintable, keepout, touchup, depth=depth,
219
+ require_depth=self.defaults.get("require_depth", True),
220
+ mm_per_px=float(mm_per_px) if mm_per_px else None, scale_source=source,
221
+ tools_mm=tools_mm, confirmations=confirmations, edit_stats=edit_stats)
222
+ order["job_id"], order["planned_at"], order["model"] = job_id, now(), self.model_info
223
+ (path / "work_order.json").write_text(json.dumps(order, indent=2) + "\n")
224
+ layer_png(paintable & ~keepout, LAYER_COLOURS["paint"], 120).save(path / "layer_paint.png")
225
+ layer_png(keepout & wall, LAYER_COLOURS["keepout"], 120).save(path / "layer_keepout.png")
226
+ # Touch-up patches only matter on a signed-off wall; otherwise the coat covers them.
227
+ signed_off = (prediction.get("completion") or {}).get("status") == "complete"
228
+ layer_png(touchup & ~keepout & signed_off, LAYER_COLOURS["touchup"], 170).save(path / "layer_touchup.png")
229
+ job.update(status=order["status"], summary=order.get("summary"), updated=now())
230
+ (path / "job.json").write_text(json.dumps(job, indent=2) + "\n")
231
+ return self.read_job(job_id)
232
+
233
+ def approve(self, job_id, request):
234
+ from run_registry import append
235
+ operator = (request.get("operator") or "").strip()[:80]
236
+ if not operator:
237
+ raise ValueError("enter the operator's name to approve")
238
+ with self.lock:
239
+ path = self.job_dir(job_id)
240
+ job = json.loads((path / "job.json").read_text())
241
+ if job.get("approved"):
242
+ raise PermissionError("already approved")
243
+ order_bytes = (path / "work_order.json").read_bytes()
244
+ order = json.loads(order_bytes)
245
+ if order["status"] == "blocked":
246
+ raise PermissionError("the safety gate blocks this order: " + "; ".join(order["gate"]["reasons"]))
247
+ hashes = {name: sha256_bytes((path / name).read_bytes())
248
+ for name in ("photo.jpg", "result.json", "paintable.png", "keepout.png", "edits.png")
249
+ if (path / name).is_file()}
250
+ approval = {"job_id": job_id, "operator": operator, "note": (request.get("note") or "")[:500],
251
+ "approved_at": now(), "work_order_sha256": sha256_bytes(order_bytes), "inputs_sha256": hashes,
252
+ "status": order["status"], "confirmations": order.get("confirmations"),
253
+ "operator_edits": order.get("operator_edits"), "model": self.model_info}
254
+ record = append(self.jobs / "registry.jsonl", "work_order_approval",
255
+ checkpoint_sha256=(self.model_info or {}).get("sha256"), run_id=job_id,
256
+ config={"work_order_sha256": approval["work_order_sha256"]},
257
+ extra={"operator": operator, "work_order_sha256": approval["work_order_sha256"]})
258
+ approval["registry_hash"] = record["hash"]
259
+ (path / "approval.json").write_text(json.dumps(approval, indent=2) + "\n")
260
+ job.update(approved={"by": operator, "at": approval["approved_at"], "registry_hash": record["hash"]})
261
+ (path / "job.json").write_text(json.dumps(job, indent=2) + "\n")
262
+ return self.read_job(job_id)
263
+
264
+
265
+ def make_handler(console):
266
+ class Handler(BaseHTTPRequestHandler):
267
+ server_version = f"PaintingVisionConsole/{VERSION}"
268
+
269
+ def log_message(self, fmt, *args): # quieter, one line per request
270
+ print(f"{self.address_string()} {fmt % args}", flush=True)
271
+
272
+ def send(self, status, body, content_type="application/json", extra=None):
273
+ if isinstance(body, (dict, list)):
274
+ body = json.dumps(body).encode()
275
+ elif isinstance(body, str):
276
+ body = body.encode()
277
+ self.send_response(status)
278
+ self.send_header("Content-Type", content_type)
279
+ self.send_header("Content-Length", str(len(body)))
280
+ self.send_header("Cache-Control", "no-store")
281
+ self.send_header("X-Content-Type-Options", "nosniff")
282
+ self.send_header("Content-Security-Policy", "default-src 'self'; img-src 'self' data: blob:; "
283
+ "style-src 'self'; script-src 'self'")
284
+ for key, value in (extra or {}).items():
285
+ self.send_header(key, value)
286
+ self.end_headers()
287
+ self.wfile.write(body)
288
+
289
+ def error(self, status, message):
290
+ self.send(status, {"error": message})
291
+
292
+ def body(self):
293
+ length = int(self.headers.get("Content-Length") or 0)
294
+ if length > MAX_UPLOAD:
295
+ raise ValueError("request too large")
296
+ return self.rfile.read(length) if length else b""
297
+
298
+ def do_GET(self):
299
+ url = urlparse(self.path)
300
+ parts = [p for p in url.path.split("/") if p]
301
+ try:
302
+ if not parts:
303
+ return self.send(200, (STATIC / "index.html").read_bytes(), STATIC_FILES["index.html"])
304
+ if parts == ["api", "health"]:
305
+ return self.send(200, {"ok": True, "version": VERSION, "model": console.model_info,
306
+ "confirmations": {k: v[1] for k, v in CONFIRMATIONS.items()}})
307
+ if parts == ["api", "jobs"]:
308
+ return self.send(200, console.list_jobs())
309
+ if len(parts) == 3 and parts[:2] == ["api", "jobs"]:
310
+ return self.send(200, console.read_job(parts[2]))
311
+ if len(parts) == 4 and parts[:2] == ["api", "jobs"] and parts[3] in JOB_FILES:
312
+ path = console.job_dir(parts[2]) / parts[3]
313
+ if not path.is_file():
314
+ return self.error(404, "not found")
315
+ extra = ({"Content-Disposition": f'attachment; filename="{parts[2]}_{parts[3]}"'}
316
+ if "download" in parse_qs(url.query) else None)
317
+ return self.send(200, path.read_bytes(), JOB_FILES[parts[3]], extra)
318
+ if len(parts) == 1 and parts[0] in STATIC_FILES:
319
+ return self.send(200, (STATIC / parts[0]).read_bytes(), STATIC_FILES[parts[0]])
320
+ return self.error(404, "not found")
321
+ except KeyError:
322
+ return self.error(404, "no such job")
323
+
324
+ def do_POST(self):
325
+ url = urlparse(self.path)
326
+ parts = [p for p in url.path.split("/") if p]
327
+ try:
328
+ if parts == ["api", "jobs"]:
329
+ query = parse_qs(url.query)
330
+ job = console.create_job(self.body(), query.get("filename", [""])[0], query.get("wall_id", [""])[0])
331
+ return self.send(201, job)
332
+ if len(parts) == 4 and parts[:2] == ["api", "jobs"] and parts[3] in ("plan", "approve"):
333
+ request = json.loads(self.body() or b"{}")
334
+ if not isinstance(request, dict):
335
+ raise ValueError("expected a JSON object")
336
+ action = console.plan if parts[3] == "plan" else console.approve
337
+ return self.send(200, action(parts[2], request))
338
+ return self.error(404, "not found")
339
+ except KeyError:
340
+ return self.error(404, "no such job")
341
+ except PermissionError as exc:
342
+ return self.error(409, str(exc))
343
+ except (ValueError, json.JSONDecodeError) as exc:
344
+ return self.error(400, str(exc))
345
+ except Exception as exc: # keep the console up; the operator sees the error
346
+ import traceback
347
+ traceback.print_exc()
348
+ return self.error(500, f"{type(exc).__name__}: {exc}")
349
+
350
+ return Handler
351
+
352
+
353
+ def model_analyzer(checkpoint_path, device_name=None):
354
+ """Load the checkpoint once; return ``(analyze_fn, model_info)``."""
355
+ import cv2
356
+ import torch
357
+
358
+ from hub import resolve_checkpoint
359
+ from predict import analyze, build_parser
360
+ from train import load_checkpoint_model
361
+
362
+ path = Path(resolve_checkpoint(str(checkpoint_path)))
363
+ device = torch.device(device_name or ("cuda" if torch.cuda.is_available() else "cpu"))
364
+ model, checkpoint = load_checkpoint_model(path, device)
365
+ args = build_parser().parse_args(["unused.jpg"])
366
+ args.depth_map, args.focal_px = None, None
367
+ warnings = []
368
+ if checkpoint.get("completion_threshold") is None:
369
+ warnings.append("sign-off is disabled: this checkpoint has no calibrated completion threshold")
370
+ if not checkpoint.get("condition_operating_point"):
371
+ warnings.append("damage flags use uncalibrated defaults (run calibrate_condition.py --write)")
372
+ if "temperature" not in checkpoint:
373
+ warnings.append("confidences are not temperature-calibrated (run train.py --calibrate-checkpoint)")
374
+ info = {"checkpoint": path.name, "sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
375
+ "arch": checkpoint.get("arch"), "device": str(device),
376
+ "completion_threshold": checkpoint.get("completion_threshold"),
377
+ "condition_operating_point": checkpoint.get("condition_operating_point"), "warnings": warnings}
378
+
379
+ def analyze_fn(rgb):
380
+ return analyze(cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR), model, checkpoint, device, args)
381
+
382
+ return analyze_fn, info
383
+
384
+
385
+ def main():
386
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
387
+ ap.add_argument("--checkpoint", default="artifacts/best.pt", help="local .pt or hf://owner/repo")
388
+ ap.add_argument("--jobs-dir", type=Path, default=Path("jobs"))
389
+ ap.add_argument("--host", default="127.0.0.1", help="0.0.0.0 exposes the console to the network: only on a "
390
+ "trusted site network")
391
+ ap.add_argument("--port", type=int, default=8765)
392
+ ap.add_argument("--device")
393
+ ap.add_argument("--mm-per-px", type=float, help="default scale when a job gives none")
394
+ ap.add_argument("--no-require-depth", action="store_true",
395
+ help="do not require depth confirmation (operators can still waive it per job)")
396
+ args = ap.parse_args()
397
+ analyze_fn, info = model_analyzer(args.checkpoint, args.device)
398
+ console = Console(args.jobs_dir, analyze_fn, info,
399
+ {"mm_per_px": args.mm_per_px, "require_depth": not args.no_require_depth})
400
+ server = ThreadingHTTPServer((args.host, args.port), make_handler(console))
401
+ for warning in info["warnings"]:
402
+ print("warning:", warning)
403
+ print(f"console on http://{args.host}:{args.port} (model {info['checkpoint']}, jobs in {args.jobs_dir})", flush=True)
404
+ try:
405
+ server.serve_forever()
406
+ except KeyboardInterrupt:
407
+ pass
408
+
409
+
410
+ if __name__ == "__main__":
411
+ main()