Download src/buddy_fusion/boxes.py from build-small-hackathon/hatchimera: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/build-small-hackathon/hatchimera/resolve/main/src/buddy_fusion/boxes.py
- Command line
-
hf download hf://spaces/build-small-hackathon/hatchimera/src/buddy_fusion/boxes.py
-
curl -L -o boxes.py https://huggingface.co/spaces/build-small-hackathon/hatchimera/resolve/main/src/buddy_fusion/boxes.py
1.03 kB
| """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 | |