Spaces:
Running on Zero
Running on Zero
| """Validate / repair a model-emitted box list so the renderer never receives | |
| garbage. The freeform model is grammar-constrained to integers, but truncation | |
| or odd values can still slip through; this clamps sizes and drops unusable boxes.""" | |
| from __future__ import annotations | |
| _MAX = 8 | |
| _DEFAULT_COLOR = "#caa06f" | |
| def _int(v): | |
| try: | |
| return int(v) | |
| except (TypeError, ValueError): | |
| return None | |
| def repair_boxes(raw) -> list[dict]: | |
| if not isinstance(raw, list): | |
| return [] | |
| out: list[dict] = [] | |
| for b in raw: | |
| if not isinstance(b, dict): | |
| continue | |
| vals = {k: _int(b.get(k)) for k in ("x", "y", "z", "w", "h", "d")} | |
| if any(v is None for v in vals.values()): | |
| continue | |
| for k in ("w", "h", "d"): | |
| vals[k] = max(1, min(_MAX, vals[k])) | |
| color = b.get("color") | |
| if not isinstance(color, str) or not color.startswith("#"): | |
| color = _DEFAULT_COLOR | |
| out.append({**vals, "color": color}) | |
| return out | |