arkai2025's picture
feat: freeform voxel generation (Gemma 4 12B) + freeform breeding
b90431b
Raw
History Blame Contribute Delete
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