GPT-designed layouts: Vastu-correct, diverse, no overlaps
Browse filesGPT designs each plan's structure (Vastu zones, proportions, column
arrangement); the deterministic tiler + repair pipeline renders exact
rectangles and enforces every rule. Pooja always NE/N/E and smallest,
living never smaller than a washroom, master biggest bedroom, no overlaps
or out-of-box rooms, and 10-12 genuinely distinct layouts per 20. Falls
back to templates when OPENAI_API_KEY is not configured.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- core/gpt_layout.py +150 -0
- core/pdf_templates.py +185 -25
- core/pdf_templates_2bhk.py +27 -1
- core/pdf_templates_3bhk.py +17 -0
- core/pdf_templates_4bhk.py +17 -0
core/gpt_layout.py
CHANGED
|
@@ -20,6 +20,9 @@ from core.constants import ROOM_SIZE_RANGES
|
|
| 20 |
|
| 21 |
|
| 22 |
MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def _load_api_key() -> str | None:
|
|
@@ -28,6 +31,23 @@ def _load_api_key() -> str | None:
|
|
| 28 |
return os.getenv("OPENAI_API_KEY")
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def gpt_available() -> bool:
|
| 32 |
return bool(_load_api_key())
|
| 33 |
|
|
@@ -76,6 +96,136 @@ def get_style_hint(index: int) -> str:
|
|
| 76 |
return STYLE_VARIANTS[index % len(STYLE_VARIANTS)]
|
| 77 |
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def propose_layout(
|
| 80 |
room_names: Iterable[str],
|
| 81 |
plot_w: float,
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
|
| 23 |
+
# Model used for the structural column-layout generation (the good path). A
|
| 24 |
+
# strong reasoning model follows the Vastu + proportion constraints far better.
|
| 25 |
+
LAYOUT_MODEL = os.getenv("OPENAI_LAYOUT_MODEL", "gpt-5.2")
|
| 26 |
|
| 27 |
|
| 28 |
def _load_api_key() -> str | None:
|
|
|
|
| 31 |
return os.getenv("OPENAI_API_KEY")
|
| 32 |
|
| 33 |
|
| 34 |
+
def _chat_json(client, model, system, user, temperature=0.7, timeout=None):
|
| 35 |
+
"""Call chat.completions expecting a JSON object. Retries without
|
| 36 |
+
temperature for models (o-series / some gpt-5) that reject it."""
|
| 37 |
+
kw = dict(model=model,
|
| 38 |
+
response_format={"type": "json_object"},
|
| 39 |
+
messages=[{"role": "system", "content": system},
|
| 40 |
+
{"role": "user", "content": user}])
|
| 41 |
+
try:
|
| 42 |
+
r = client.chat.completions.create(temperature=temperature, **kw)
|
| 43 |
+
except Exception as exc:
|
| 44 |
+
if "temperature" in str(exc).lower():
|
| 45 |
+
r = client.chat.completions.create(**kw)
|
| 46 |
+
else:
|
| 47 |
+
raise
|
| 48 |
+
return json.loads(r.choices[0].message.content or "{}")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
def gpt_available() -> bool:
|
| 52 |
return bool(_load_api_key())
|
| 53 |
|
|
|
|
| 96 |
return STYLE_VARIANTS[index % len(STYLE_VARIANTS)]
|
| 97 |
|
| 98 |
|
| 99 |
+
_LAYOUT_SYSTEM = (
|
| 100 |
+
"You are an expert architect laying out Indian residential floor plans on a "
|
| 101 |
+
"rectangular plot, following Vastu Shastra. You design the STRUCTURE of the "
|
| 102 |
+
"plan as vertical COLUMNS; a deterministic engine will turn your structure "
|
| 103 |
+
"into exact rectangles, so you only choose which room goes where and its "
|
| 104 |
+
"relative size β never pixel coordinates.\n\n"
|
| 105 |
+
"FRAME: the ROAD is at the BOTTOM (south). x grows EAST (right), y grows "
|
| 106 |
+
"NORTH (rear). A layout is a left(WEST)->right(EAST) list of columns. Within "
|
| 107 |
+
"each column, rooms are stacked from the ROAD (bottom/south) to the REAR "
|
| 108 |
+
"(top/north): the FIRST room in a column touches the road, the LAST touches "
|
| 109 |
+
"the rear.\n\n"
|
| 110 |
+
"VASTU placement (must follow):\n"
|
| 111 |
+
"- external_staircase: on the road (first room of a side column).\n"
|
| 112 |
+
"- foyer: on the road, next to the staircase (the entry).\n"
|
| 113 |
+
"- balcony: on the road frontage (first room of a column).\n"
|
| 114 |
+
"- living_room: front/near the foyer, a LARGE room.\n"
|
| 115 |
+
"- kitchen: SOUTH-EAST β first/second room of the EAST (rightmost) column.\n"
|
| 116 |
+
"- utility_area: touching the kitchen (east), small.\n"
|
| 117 |
+
"- dining_room: between kitchen and living.\n"
|
| 118 |
+
"- master_bedroom: SOUTH-WEST β first room of the WEST (leftmost) column, the "
|
| 119 |
+
"LARGEST bedroom.\n"
|
| 120 |
+
"- other bedrooms: the REAR (last rows), medium size, all smaller than master.\n"
|
| 121 |
+
"- common_bathroom / attached_bathroom: NORTH-WEST wet core, SMALL; they must "
|
| 122 |
+
"NOT be placed next to the pooja room.\n"
|
| 123 |
+
"- pooja_room: NORTH-EAST β the LAST (top) room of the EAST (rightmost) "
|
| 124 |
+
"column, the SMALLEST room; it must never touch a bathroom, utility or "
|
| 125 |
+
"kitchen.\n"
|
| 126 |
+
"- study: flexible filler.\n\n"
|
| 127 |
+
"PROPORTIONS (approx share of the built area β keep the ordering, the engine "
|
| 128 |
+
"clamps exact sizes): master_bedroom ~17%, living_room ~15%, each other "
|
| 129 |
+
"bedroom ~11%, kitchen ~7%, dining_room ~7%, study ~5%, staircase ~12%, "
|
| 130 |
+
"washroom ~3%, utility ~2%, pooja ~2%. The master must be the biggest room, "
|
| 131 |
+
"the living room clearly bigger than any washroom, and pooja the smallest.\n\n"
|
| 132 |
+
"OUTPUT: ONLY JSON, no markdown:\n"
|
| 133 |
+
'{"layouts":[{"columns":[{"width":<0..1>,"rooms":[{"name":<room>,'
|
| 134 |
+
'"height":<0..1>}, ...]}, ...]}, ...]}\n'
|
| 135 |
+
"Rules: column widths in a layout sum to ~1.0; room heights in a column sum "
|
| 136 |
+
"to ~1.0; use 2-3 columns; include EVERY requested room EXACTLY once and add "
|
| 137 |
+
"no others (internal_corridor is optional). Make each layout STRUCTURALLY "
|
| 138 |
+
"DIFFERENT from the others (move rooms between columns, change column count "
|
| 139 |
+
"and widths) while keeping every Vastu rule above."
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def propose_column_layouts(required_rooms, optional_rooms, plot_w, plot_h,
|
| 144 |
+
road_side, n_variants=6, timeout=45.0):
|
| 145 |
+
"""Ask GPT for `n_variants` structurally-distinct COLUMN layouts.
|
| 146 |
+
|
| 147 |
+
Returns a list of specs, each a list of columns
|
| 148 |
+
``[{"width": float, "rooms": [{"name": str, "height": float}, ...]}, ...]``
|
| 149 |
+
OR None on failure. Only layouts containing every required room exactly once
|
| 150 |
+
survive; the caller renders them with the deterministic tiler + repair
|
| 151 |
+
pipeline (which guarantees no overlaps/gaps and enforces Vastu/proportions).
|
| 152 |
+
"""
|
| 153 |
+
api_key = _load_api_key()
|
| 154 |
+
if not api_key:
|
| 155 |
+
return None
|
| 156 |
+
try:
|
| 157 |
+
from openai import OpenAI
|
| 158 |
+
except Exception:
|
| 159 |
+
return None
|
| 160 |
+
|
| 161 |
+
required = list(dict.fromkeys(required_rooms))
|
| 162 |
+
optional = [r for r in dict.fromkeys(optional_rooms) if r not in required]
|
| 163 |
+
rooms_all = required + optional
|
| 164 |
+
guides = "\n".join(_room_range_text(r) for r in rooms_all)
|
| 165 |
+
user = json.dumps({
|
| 166 |
+
"plot": {"width_ft": plot_w, "height_ft": plot_h, "road_side": road_side},
|
| 167 |
+
"required_rooms": required,
|
| 168 |
+
"optional_rooms": optional,
|
| 169 |
+
"room_size_guides_ft": guides,
|
| 170 |
+
"n_layouts": n_variants,
|
| 171 |
+
})
|
| 172 |
+
try:
|
| 173 |
+
client = OpenAI(api_key=api_key, timeout=timeout)
|
| 174 |
+
data = _chat_json(client, os.getenv("OPENAI_LAYOUT_MODEL", LAYOUT_MODEL),
|
| 175 |
+
_LAYOUT_SYSTEM, user, temperature=0.9)
|
| 176 |
+
except Exception:
|
| 177 |
+
return None
|
| 178 |
+
|
| 179 |
+
raw = data.get("layouts")
|
| 180 |
+
if not isinstance(raw, list):
|
| 181 |
+
# some models return a single {"columns": [...]}
|
| 182 |
+
raw = [data] if "columns" in data else None
|
| 183 |
+
if not raw:
|
| 184 |
+
return None
|
| 185 |
+
|
| 186 |
+
allowed = set(rooms_all) | {"internal_corridor"}
|
| 187 |
+
req_set = set(required)
|
| 188 |
+
out = []
|
| 189 |
+
for lay in raw:
|
| 190 |
+
cols = lay.get("columns") if isinstance(lay, dict) else None
|
| 191 |
+
if not isinstance(cols, list) or len(cols) < 2:
|
| 192 |
+
continue
|
| 193 |
+
spec, names = [], []
|
| 194 |
+
ok = True
|
| 195 |
+
for col in cols:
|
| 196 |
+
crooms = col.get("rooms") if isinstance(col, dict) else None
|
| 197 |
+
try:
|
| 198 |
+
cw = float(col.get("width", 0))
|
| 199 |
+
except Exception:
|
| 200 |
+
cw = 0
|
| 201 |
+
if not isinstance(crooms, list) or cw <= 0:
|
| 202 |
+
ok = False
|
| 203 |
+
break
|
| 204 |
+
rows = []
|
| 205 |
+
for rm in crooms:
|
| 206 |
+
nm = rm.get("name") if isinstance(rm, dict) else None
|
| 207 |
+
if nm not in allowed:
|
| 208 |
+
continue # silently drop stray rooms
|
| 209 |
+
try:
|
| 210 |
+
hh = float(rm.get("height", 0))
|
| 211 |
+
except Exception:
|
| 212 |
+
hh = 0
|
| 213 |
+
if hh <= 0:
|
| 214 |
+
hh = 0.1
|
| 215 |
+
rows.append({"name": nm, "height": hh})
|
| 216 |
+
names.append(nm)
|
| 217 |
+
if rows:
|
| 218 |
+
spec.append({"width": cw, "rooms": rows})
|
| 219 |
+
if not ok or not spec:
|
| 220 |
+
continue
|
| 221 |
+
# must contain every REQUIRED room at least once, and no duplicate of a
|
| 222 |
+
# singular room beyond what the caller allows (dedupe handled downstream)
|
| 223 |
+
if not req_set.issubset(set(names)):
|
| 224 |
+
continue
|
| 225 |
+
out.append(spec)
|
| 226 |
+
return out or None
|
| 227 |
+
|
| 228 |
+
|
| 229 |
def propose_layout(
|
| 230 |
room_names: Iterable[str],
|
| 231 |
plot_w: float,
|
core/pdf_templates.py
CHANGED
|
@@ -1263,6 +1263,13 @@ def build_from_template(tpl: dict, plot_w: float, plot_h: float,
|
|
| 1263 |
# staircase is one contiguous box first.
|
| 1264 |
rooms = _merge_fragments(rooms)
|
| 1265 |
rooms = _snap_staircase(rooms, ix0, iy0, ix1, iy1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1266 |
if jitter > 0:
|
| 1267 |
rooms = _jitter(rooms, ix0, iy0, ix1, iy1, jitter, rng)
|
| 1268 |
rooms = _resolve_overlaps(rooms)
|
|
@@ -1554,12 +1561,6 @@ def build_from_template(tpl: dict, plot_w: float, plot_h: float,
|
|
| 1554 |
# (the largest non-fixed box is relabelled master and vice-versa), so it
|
| 1555 |
# cannot move geometry, reopen a gap, or create an overlap.
|
| 1556 |
rooms = _promote_master_to_largest(rooms)
|
| 1557 |
-
# Per-plan proportional variation so the cycled templates don't render as
|
| 1558 |
-
# identical plans. Only the OPTIMISED plan (jitter == 0) is varied; the
|
| 1559 |
-
# raw plan already varies via its own jitter. Safe: validated + reverted.
|
| 1560 |
-
if jitter == 0 and seed is not None:
|
| 1561 |
-
sl, sr, sb, st = get_setbacks(road_side)
|
| 1562 |
-
rooms = _vary_layout(rooms, seed, sl, sb, plot_w - sr, plot_h - st)
|
| 1563 |
return rooms
|
| 1564 |
|
| 1565 |
|
|
@@ -2733,6 +2734,142 @@ def apply_template(index: int, plot_w: float, plot_h: float,
|
|
| 2733 |
core=_CORE, optional=_OPTIONAL)
|
| 2734 |
|
| 2735 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2736 |
def _has_overlap_or_spill(boxes, ix0, iy0, ix1, iy1):
|
| 2737 |
for i in range(len(boxes)):
|
| 2738 |
a = boxes[i]
|
|
@@ -2762,31 +2899,29 @@ def _vary_layout(boxes, seed, ix0, iy0, ix1, iy1, amp=2.6, minw=4.0):
|
|
| 2762 |
return boxes
|
| 2763 |
rng = random.Random(seed * 2654435761 & 0xFFFFFFFF)
|
| 2764 |
snap = copy.deepcopy(boxes)
|
| 2765 |
-
PROT = {"external_staircase", "pooja_room"}
|
| 2766 |
|
| 2767 |
-
def
|
| 2768 |
-
|
| 2769 |
-
|
|
|
|
|
|
|
|
|
|
| 2770 |
size = "w" if axis == "x" else "h"
|
| 2771 |
-
|
| 2772 |
-
|
| 2773 |
-
lines = sorted({round(pos(b), 1) for b in boxes} |
|
| 2774 |
-
{round(pos(b) + b[size], 1) for b in boxes})
|
| 2775 |
for L in lines:
|
| 2776 |
if L <= lo_bound + 0.2 or L >= hi_bound - 0.2:
|
| 2777 |
continue
|
| 2778 |
-
left = [b for b in
|
| 2779 |
-
right = [b for b in
|
| 2780 |
if not left or not right:
|
| 2781 |
continue
|
| 2782 |
-
#
|
| 2783 |
-
if any(
|
| 2784 |
-
for b in
|
| 2785 |
continue
|
| 2786 |
-
|
| 2787 |
-
|
| 2788 |
-
hi = min(b[size] for b in right) - minw
|
| 2789 |
-
loo = -(min(b[size] for b in left) - minw)
|
| 2790 |
if hi <= 0.1 or loo >= -0.1:
|
| 2791 |
continue
|
| 2792 |
d = max(loo, min(hi, rng.uniform(-amp, amp)))
|
|
@@ -2798,8 +2933,18 @@ def _vary_layout(boxes, seed, ix0, iy0, ix1, iy1, amp=2.6, minw=4.0):
|
|
| 2798 |
b[axis] += d
|
| 2799 |
b[size] -= d
|
| 2800 |
|
| 2801 |
-
|
| 2802 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2803 |
if _has_overlap_or_spill(boxes, ix0, iy0, ix1, iy1):
|
| 2804 |
boxes[:] = snap
|
| 2805 |
return boxes
|
|
@@ -2903,6 +3048,21 @@ def generate_bhk1_pdf_population(plot_w: float, plot_h: float,
|
|
| 2903 |
|
| 2904 |
The result plugs straight into `generator.rank_plans(pairs, ...)`.
|
| 2905 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2906 |
pairs = []
|
| 2907 |
# pdf_p6 (index 5) is a structurally fragmented source drawing; it creates
|
| 2908 |
# duplicate pooja/corridor fragments and a persistent void. Keep it in the
|
|
|
|
| 1263 |
# staircase is one contiguous box first.
|
| 1264 |
rooms = _merge_fragments(rooms)
|
| 1265 |
rooms = _snap_staircase(rooms, ix0, iy0, ix1, iy1)
|
| 1266 |
+
# PER-PLAN PROPORTIONAL VARIATION β applied on the CLEAN template tiling
|
| 1267 |
+
# (a guillotine layout, so sliding a cut-line is always gapless and
|
| 1268 |
+
# overlap-free) BEFORE the fills run. This is what stops the cycled templates
|
| 1269 |
+
# rendering as identical plans. Only the OPTIMISED plan (jitter == 0) is
|
| 1270 |
+
# varied; the raw plan already varies via its own jitter.
|
| 1271 |
+
if jitter == 0 and seed is not None:
|
| 1272 |
+
rooms = _vary_layout(rooms, seed, ix0, iy0, ix1, iy1)
|
| 1273 |
if jitter > 0:
|
| 1274 |
rooms = _jitter(rooms, ix0, iy0, ix1, iy1, jitter, rng)
|
| 1275 |
rooms = _resolve_overlaps(rooms)
|
|
|
|
| 1561 |
# (the largest non-fixed box is relabelled master and vice-versa), so it
|
| 1562 |
# cannot move geometry, reopen a gap, or create an overlap.
|
| 1563 |
rooms = _promote_master_to_largest(rooms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1564 |
return rooms
|
| 1565 |
|
| 1566 |
|
|
|
|
| 2734 |
core=_CORE, optional=_OPTIONAL)
|
| 2735 |
|
| 2736 |
|
| 2737 |
+
def _spec_to_rooms(spec, ref_w, ref_h):
|
| 2738 |
+
"""Convert a GPT column-spec into a GAPLESS guillotine tiling (same idea as
|
| 2739 |
+
the hand-authored `_cols` templates). Columns left->right; rooms bottom->top
|
| 2740 |
+
within a column. Fills ref_w x ref_h exactly β no gaps, no overlaps."""
|
| 2741 |
+
rooms = []
|
| 2742 |
+
wsum = sum(max(float(c.get("width", 0)), 1e-6) for c in spec) or 1.0
|
| 2743 |
+
x = 0.0
|
| 2744 |
+
for k, col in enumerate(spec):
|
| 2745 |
+
cw = ref_w * (max(float(col.get("width", 0)), 1e-6) / wsum)
|
| 2746 |
+
if k == len(spec) - 1:
|
| 2747 |
+
cw = ref_w - x
|
| 2748 |
+
crooms = col.get("rooms", [])
|
| 2749 |
+
hsum = sum(max(float(r.get("height", 0)), 1e-6) for r in crooms) or 1.0
|
| 2750 |
+
y = 0.0
|
| 2751 |
+
for m, r in enumerate(crooms):
|
| 2752 |
+
ch = ref_h * (max(float(r.get("height", 0)), 1e-6) / hsum)
|
| 2753 |
+
if m == len(crooms) - 1:
|
| 2754 |
+
ch = ref_h - y
|
| 2755 |
+
rooms.append({"name": r["name"], "x": round(x, 2), "y": round(y, 2),
|
| 2756 |
+
"w": round(cw, 2), "h": round(max(ch, 0.5), 2)})
|
| 2757 |
+
y += ch
|
| 2758 |
+
x += cw
|
| 2759 |
+
return rooms
|
| 2760 |
+
|
| 2761 |
+
|
| 2762 |
+
def build_from_spec(spec, name, plot_w, plot_h, road_side, selected_optional,
|
| 2763 |
+
jitter, seed, fill_gaps, core=None, optional=None):
|
| 2764 |
+
"""Render a GPT column-spec into a finished plan through the SAME repair
|
| 2765 |
+
pipeline the templates use (so pooja lands NE, proportions are enforced,
|
| 2766 |
+
living is never a strip, and there are never overlaps/spills)."""
|
| 2767 |
+
layout_w = plot_h if road_side in ("E", "W") else plot_w
|
| 2768 |
+
layout_h = plot_w if road_side in ("E", "W") else plot_h
|
| 2769 |
+
left, right, bottom, top = get_setbacks("S")
|
| 2770 |
+
ref_w = layout_w - left - right
|
| 2771 |
+
ref_h = layout_h - bottom - top
|
| 2772 |
+
rooms = _spec_to_rooms(spec, ref_w, ref_h)
|
| 2773 |
+
tpl = {"name": name, "entry": "top", "ref_w": ref_w, "ref_h": ref_h,
|
| 2774 |
+
"rooms": rooms}
|
| 2775 |
+
return build_from_template(tpl, plot_w, plot_h, road_side, selected_optional,
|
| 2776 |
+
jitter, seed, fill_gaps, core=core,
|
| 2777 |
+
optional=optional)
|
| 2778 |
+
|
| 2779 |
+
|
| 2780 |
+
_GPT_SPEC_CACHE = {}
|
| 2781 |
+
|
| 2782 |
+
|
| 2783 |
+
def _plan_is_good(boxes, plot_w, plot_h, road_side, n_beds):
|
| 2784 |
+
"""Accept a rendered plan only if it is architecturally sound: no overlaps,
|
| 2785 |
+
nothing outside the setback box, the right bedroom count, living bigger than
|
| 2786 |
+
every washroom, pooja in NE/N/E, and a well-filled plan."""
|
| 2787 |
+
sl, sr, sb, st = get_setbacks(road_side)
|
| 2788 |
+
ix0, iy0, ix1, iy1 = sl, sb, plot_w - sr, plot_h - st
|
| 2789 |
+
inner = max((ix1 - ix0) * (iy1 - iy0), 1.0)
|
| 2790 |
+
# Judge on the clamped geometry (make_floorplan clamps too), so a hairline
|
| 2791 |
+
# over-edge sliver doesn't reject an otherwise-sound plan.
|
| 2792 |
+
boxes = _clamp_to_box(copy.deepcopy(boxes), ix0, iy0, ix1, iy1)
|
| 2793 |
+
if _has_overlap_or_spill(boxes, ix0, iy0, ix1, iy1):
|
| 2794 |
+
return False
|
| 2795 |
+
per = {}
|
| 2796 |
+
for b in boxes:
|
| 2797 |
+
per.setdefault(b["name"], []).append(b["w"] * b["h"])
|
| 2798 |
+
bedkeys = ("master_bedroom", "bedroom_01", "bedroom_02", "bedroom_03")[:n_beds]
|
| 2799 |
+
if any(k not in per for k in bedkeys):
|
| 2800 |
+
return False
|
| 2801 |
+
lr = sum(per.get("living_room", [0]))
|
| 2802 |
+
mw = max([max(per.get(k, [0])) for k in
|
| 2803 |
+
("common_bathroom", "attached_bathroom", "utility_area")] or [0])
|
| 2804 |
+
if lr <= mw:
|
| 2805 |
+
return False
|
| 2806 |
+
pj = next((b for b in boxes if b["name"] == "pooja_room"), None)
|
| 2807 |
+
if pj is not None and _zone_of(pj, plot_w, plot_h) not in ("NE", "N", "E"):
|
| 2808 |
+
return False
|
| 2809 |
+
fill = sum(b["w"] * b["h"] for b in boxes) / inner
|
| 2810 |
+
if fill > 1.06 or fill < 0.85:
|
| 2811 |
+
return False
|
| 2812 |
+
return True
|
| 2813 |
+
|
| 2814 |
+
|
| 2815 |
+
def generate_gpt_population(bhk_tag, required_rooms, optional_rooms,
|
| 2816 |
+
plot_w, plot_h, road_side, n, selected_optional,
|
| 2817 |
+
core, optional_set, raw_jitter=1.4, base_seed=0):
|
| 2818 |
+
"""Produce up to `n` (raw, optimised) FloorPlan pairs from GPT-designed
|
| 2819 |
+
column layouts, rendered through the deterministic repair pipeline and
|
| 2820 |
+
validated. Returns a list of pairs (possibly < n good structures, which the
|
| 2821 |
+
caller pads with per-plan variation) or None if GPT is unavailable / failed.
|
| 2822 |
+
"""
|
| 2823 |
+
try:
|
| 2824 |
+
from core import gpt_layout
|
| 2825 |
+
except Exception:
|
| 2826 |
+
return None
|
| 2827 |
+
if not gpt_layout.gpt_available():
|
| 2828 |
+
return None
|
| 2829 |
+
|
| 2830 |
+
key = (bhk_tag, round(plot_w, 1), round(plot_h, 1), road_side,
|
| 2831 |
+
tuple(sorted(selected_optional or [])))
|
| 2832 |
+
specs = _GPT_SPEC_CACHE.get(key)
|
| 2833 |
+
if specs is None:
|
| 2834 |
+
specs = gpt_layout.propose_column_layouts(
|
| 2835 |
+
required_rooms, optional_rooms, plot_w, plot_h, road_side,
|
| 2836 |
+
n_variants=min(14, max(10, n)))
|
| 2837 |
+
_GPT_SPEC_CACHE[key] = specs or []
|
| 2838 |
+
if not specs:
|
| 2839 |
+
return None
|
| 2840 |
+
|
| 2841 |
+
n_beds = sum(1 for k in ("master_bedroom", "bedroom_01", "bedroom_02",
|
| 2842 |
+
"bedroom_03") if k in required_rooms)
|
| 2843 |
+
# Render each spec once (clean) and keep only the sound ones.
|
| 2844 |
+
good = []
|
| 2845 |
+
for i, spec in enumerate(specs):
|
| 2846 |
+
opt_boxes = build_from_spec(spec, f"{bhk_tag}_gpt_{i}", plot_w, plot_h,
|
| 2847 |
+
road_side, selected_optional, 0.0,
|
| 2848 |
+
base_seed + i * 101, True, core, optional_set)
|
| 2849 |
+
if _plan_is_good(opt_boxes, plot_w, plot_h, road_side, n_beds):
|
| 2850 |
+
good.append((i, spec))
|
| 2851 |
+
if not good:
|
| 2852 |
+
return None
|
| 2853 |
+
|
| 2854 |
+
pairs = []
|
| 2855 |
+
for j in range(n):
|
| 2856 |
+
idx, spec = good[j % len(good)]
|
| 2857 |
+
seed = base_seed + j * 137 + 11
|
| 2858 |
+
raw = build_from_spec(spec, f"{bhk_tag}_gpt_{idx}", plot_w, plot_h,
|
| 2859 |
+
road_side, selected_optional, raw_jitter,
|
| 2860 |
+
seed, True, core, optional_set)
|
| 2861 |
+
opt = build_from_spec(spec, f"{bhk_tag}_gpt_{idx}", plot_w, plot_h,
|
| 2862 |
+
road_side, selected_optional, 0.0,
|
| 2863 |
+
seed, True, core, optional_set)
|
| 2864 |
+
raw_fp = make_floorplan(raw, plot_w, plot_h, f"{bhk_tag}_gpt_{idx}",
|
| 2865 |
+
road_side)
|
| 2866 |
+
opt_fp = make_floorplan(opt, plot_w, plot_h, f"{bhk_tag}_gpt_{idx}",
|
| 2867 |
+
road_side)
|
| 2868 |
+
raw_fp.template_name = opt_fp.template_name
|
| 2869 |
+
pairs.append((raw_fp, opt_fp))
|
| 2870 |
+
return pairs
|
| 2871 |
+
|
| 2872 |
+
|
| 2873 |
def _has_overlap_or_spill(boxes, ix0, iy0, ix1, iy1):
|
| 2874 |
for i in range(len(boxes)):
|
| 2875 |
a = boxes[i]
|
|
|
|
| 2899 |
return boxes
|
| 2900 |
rng = random.Random(seed * 2654435761 & 0xFFFFFFFF)
|
| 2901 |
snap = copy.deepcopy(boxes)
|
|
|
|
| 2902 |
|
| 2903 |
+
def _slide_lines(group, axis, lo_bound, hi_bound, minsz):
|
| 2904 |
+
"""Slide clean interior cut-lines within ``group`` along ``axis``.
|
| 2905 |
+
Every box whose edge lies on a line moves together, so the tiling stays
|
| 2906 |
+
gapless + overlap-free. Nothing is 'protected' β the staircase size and
|
| 2907 |
+
the pooja are re-fixed by later pipeline passes, so they may flex here.
|
| 2908 |
+
"""
|
| 2909 |
size = "w" if axis == "x" else "h"
|
| 2910 |
+
lines = sorted({round(b[axis], 1) for b in group} |
|
| 2911 |
+
{round(b[axis] + b[size], 1) for b in group})
|
|
|
|
|
|
|
| 2912 |
for L in lines:
|
| 2913 |
if L <= lo_bound + 0.2 or L >= hi_bound - 0.2:
|
| 2914 |
continue
|
| 2915 |
+
left = [b for b in group if abs(b[axis] + b[size] - L) < 0.25]
|
| 2916 |
+
right = [b for b in group if abs(b[axis] - L) < 0.25]
|
| 2917 |
if not left or not right:
|
| 2918 |
continue
|
| 2919 |
+
# clean cut: no box in the group may straddle the line
|
| 2920 |
+
if any(b[axis] < L - 0.25 and b[axis] + b[size] > L + 0.25
|
| 2921 |
+
for b in group):
|
| 2922 |
continue
|
| 2923 |
+
hi = min(b[size] for b in right) - minsz
|
| 2924 |
+
loo = -(min(b[size] for b in left) - minsz)
|
|
|
|
|
|
|
| 2925 |
if hi <= 0.1 or loo >= -0.1:
|
| 2926 |
continue
|
| 2927 |
d = max(loo, min(hi, rng.uniform(-amp, amp)))
|
|
|
|
| 2933 |
b[axis] += d
|
| 2934 |
b[size] -= d
|
| 2935 |
|
| 2936 |
+
# 1) Slide the vertical COLUMN boundaries (plot-wide clean cuts) β varies the
|
| 2937 |
+
# column widths from plan to plan.
|
| 2938 |
+
_slide_lines(boxes, "x", ix0, ix1, minw)
|
| 2939 |
+
# 2) Within EACH column (boxes sharing an x-span), slide the horizontal ROW
|
| 2940 |
+
# boundaries β varies the room heights. Done per column so a neighbouring
|
| 2941 |
+
# column never blocks the cut.
|
| 2942 |
+
cols = {}
|
| 2943 |
+
for b in boxes:
|
| 2944 |
+
cols.setdefault((round(b["x"], 1), round(b["x"] + b["w"], 1)), []).append(b)
|
| 2945 |
+
for group in cols.values():
|
| 2946 |
+
if len(group) >= 2:
|
| 2947 |
+
_slide_lines(group, "y", iy0, iy1, 3.0)
|
| 2948 |
if _has_overlap_or_spill(boxes, ix0, iy0, ix1, iy1):
|
| 2949 |
boxes[:] = snap
|
| 2950 |
return boxes
|
|
|
|
| 3048 |
|
| 3049 |
The result plugs straight into `generator.rank_plans(pairs, ...)`.
|
| 3050 |
"""
|
| 3051 |
+
# ββ GPT-designed layouts (primary path when an API key is configured) ββ
|
| 3052 |
+
try:
|
| 3053 |
+
required = ["external_staircase", "foyer", "living_room", "kitchen",
|
| 3054 |
+
"master_bedroom", "common_bathroom"]
|
| 3055 |
+
gopt = [r for r in ("dining_room", "pooja_room", "utility_area",
|
| 3056 |
+
"balcony", "study") if r in set(selected_optional or [])]
|
| 3057 |
+
gpt_pairs = generate_gpt_population(
|
| 3058 |
+
"1bhk", required, gopt, plot_w, plot_h, road_side, n,
|
| 3059 |
+
selected_optional, _CORE, _OPTIONAL,
|
| 3060 |
+
raw_jitter=raw_jitter, base_seed=base_seed)
|
| 3061 |
+
if gpt_pairs:
|
| 3062 |
+
return gpt_pairs
|
| 3063 |
+
except Exception as exc:
|
| 3064 |
+
print(f"[1bhk] GPT layout path unavailable, using templates: {exc}")
|
| 3065 |
+
|
| 3066 |
pairs = []
|
| 3067 |
# pdf_p6 (index 5) is a structurally fragmented source drawing; it creates
|
| 3068 |
# duplicate pooja/corridor fragments and a persistent void. Keep it in the
|
core/pdf_templates_2bhk.py
CHANGED
|
@@ -381,9 +381,35 @@ def generate_bhk2_pdf_population(plot_w: float, plot_h: float,
|
|
| 381 |
# tall plots (40x60 / 30x60): p1, p2, p3, p4
|
| 382 |
# compact plots (30x40 / 40x45): p2, p4, p9
|
| 383 |
# (p5/p6/p7 overlap or oversize a washroom on these plots, so stay excluded.)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
tall = (plot_h / max(plot_w, 1e-6)) >= 1.35 if road_side in ("S", "N") \
|
| 385 |
else (plot_w / max(plot_h, 1e-6)) >= 1.35
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
pairs = []
|
| 389 |
for i in range(n):
|
|
|
|
| 381 |
# tall plots (40x60 / 30x60): p1, p2, p3, p4
|
| 382 |
# compact plots (30x40 / 40x45): p2, p4, p9
|
| 383 |
# (p5/p6/p7 overlap or oversize a washroom on these plots, so stay excluded.)
|
| 384 |
+
# ββ GPT-designed layouts (primary path when an API key is configured) ββ
|
| 385 |
+
# GPT designs the room STRUCTURE (Vastu zones + proportions + real variety);
|
| 386 |
+
# the deterministic tiler + repair pipeline renders it with no overlaps/gaps
|
| 387 |
+
# and enforces every hard rule. Falls back to the templates below if GPT is
|
| 388 |
+
# unavailable or returns too few sound layouts.
|
| 389 |
+
try:
|
| 390 |
+
from .pdf_templates import generate_gpt_population
|
| 391 |
+
required = ["external_staircase", "foyer", "living_room", "kitchen",
|
| 392 |
+
"master_bedroom", "bedroom_01", "common_bathroom",
|
| 393 |
+
"attached_bathroom"]
|
| 394 |
+
gopt = [r for r in ("dining_room", "pooja_room", "utility_area",
|
| 395 |
+
"balcony", "study") if r in set(selected_optional or [])]
|
| 396 |
+
gpt_pairs = generate_gpt_population(
|
| 397 |
+
"2bhk", required, gopt, plot_w, plot_h, road_side, n,
|
| 398 |
+
selected_optional, _CORE_2BHK, _OPTIONAL_2BHK,
|
| 399 |
+
raw_jitter=raw_jitter, base_seed=base_seed)
|
| 400 |
+
if gpt_pairs:
|
| 401 |
+
return gpt_pairs
|
| 402 |
+
except Exception as exc:
|
| 403 |
+
print(f"[2bhk] GPT layout path unavailable, using templates: {exc}")
|
| 404 |
+
|
| 405 |
tall = (plot_h / max(plot_w, 1e-6)) >= 1.35 if road_side in ("S", "N") \
|
| 406 |
else (plot_w / max(plot_h, 1e-6)) >= 1.35
|
| 407 |
+
# Templates validated to tile cleanly (no overlap, living > washroom, 2 real
|
| 408 |
+
# bedrooms) on each plot class. The old compact set [1, 3, 8] was WRONG β
|
| 409 |
+
# on a 30x40 plot p2/p4 collapse the living room below the washroom. p1/p6/p9
|
| 410 |
+
# are the schemes that hold up on a compact plot; per-plan variation then
|
| 411 |
+
# keeps the cycled plans looking different.
|
| 412 |
+
order = [0, 1, 2, 3, 9, 10] if tall else [0, 5, 8]
|
| 413 |
|
| 414 |
pairs = []
|
| 415 |
for i in range(n):
|
core/pdf_templates_3bhk.py
CHANGED
|
@@ -215,6 +215,23 @@ def generate_bhk3_pdf_population(plot_w: float, plot_h: float,
|
|
| 215 |
optimised_jitter: float = 0.0) -> List[Tuple]:
|
| 216 |
# OPTIMISED uses ZERO jitter so it is always gapless AND overlap-free;
|
| 217 |
# variety comes from the four distinct templates, not from jittering.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
pairs = []
|
| 219 |
tall = (plot_h / max(plot_w, 1e-6)) >= 1.45 if road_side in ("S", "N") \
|
| 220 |
else (plot_w / max(plot_h, 1e-6)) >= 1.45
|
|
|
|
| 215 |
optimised_jitter: float = 0.0) -> List[Tuple]:
|
| 216 |
# OPTIMISED uses ZERO jitter so it is always gapless AND overlap-free;
|
| 217 |
# variety comes from the four distinct templates, not from jittering.
|
| 218 |
+
# ββ GPT-designed layouts (primary path when an API key is configured) ββ
|
| 219 |
+
try:
|
| 220 |
+
from .pdf_templates import generate_gpt_population
|
| 221 |
+
required = ["external_staircase", "foyer", "living_room", "kitchen",
|
| 222 |
+
"master_bedroom", "bedroom_01", "bedroom_02",
|
| 223 |
+
"common_bathroom", "attached_bathroom"]
|
| 224 |
+
gopt = [r for r in ("dining_room", "pooja_room", "utility_area",
|
| 225 |
+
"balcony", "study") if r in set(selected_optional or [])]
|
| 226 |
+
gpt_pairs = generate_gpt_population(
|
| 227 |
+
"3bhk", required, gopt, plot_w, plot_h, road_side, n,
|
| 228 |
+
selected_optional, _CORE_3BHK, _OPTIONAL_3BHK,
|
| 229 |
+
raw_jitter=raw_jitter, base_seed=base_seed)
|
| 230 |
+
if gpt_pairs:
|
| 231 |
+
return gpt_pairs
|
| 232 |
+
except Exception as exc:
|
| 233 |
+
print(f"[3bhk] GPT layout path unavailable, using templates: {exc}")
|
| 234 |
+
|
| 235 |
pairs = []
|
| 236 |
tall = (plot_h / max(plot_w, 1e-6)) >= 1.45 if road_side in ("S", "N") \
|
| 237 |
else (plot_w / max(plot_h, 1e-6)) >= 1.45
|
core/pdf_templates_4bhk.py
CHANGED
|
@@ -200,6 +200,23 @@ def generate_bhk4_pdf_population(plot_w: float, plot_h: float,
|
|
| 200 |
optimised_jitter: float = 0.0) -> List[Tuple]:
|
| 201 |
# OPTIMISED uses ZERO jitter so it is always gapless AND overlap-free;
|
| 202 |
# variety comes from the four distinct templates, not from jittering.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
pairs = []
|
| 204 |
tall = (plot_h / max(plot_w, 1e-6)) >= 1.35 if road_side in ("S", "N") \
|
| 205 |
else (plot_w / max(plot_h, 1e-6)) >= 1.35
|
|
|
|
| 200 |
optimised_jitter: float = 0.0) -> List[Tuple]:
|
| 201 |
# OPTIMISED uses ZERO jitter so it is always gapless AND overlap-free;
|
| 202 |
# variety comes from the four distinct templates, not from jittering.
|
| 203 |
+
# ββ GPT-designed layouts (primary path when an API key is configured) ββ
|
| 204 |
+
try:
|
| 205 |
+
from .pdf_templates import generate_gpt_population
|
| 206 |
+
required = ["external_staircase", "foyer", "living_room", "kitchen",
|
| 207 |
+
"master_bedroom", "bedroom_01", "bedroom_02", "bedroom_03",
|
| 208 |
+
"common_bathroom", "attached_bathroom"]
|
| 209 |
+
gopt = [r for r in ("dining_room", "pooja_room", "utility_area",
|
| 210 |
+
"balcony", "study") if r in set(selected_optional or [])]
|
| 211 |
+
gpt_pairs = generate_gpt_population(
|
| 212 |
+
"4bhk", required, gopt, plot_w, plot_h, road_side, n,
|
| 213 |
+
selected_optional, _CORE_4BHK, _OPTIONAL_4BHK,
|
| 214 |
+
raw_jitter=raw_jitter, base_seed=base_seed)
|
| 215 |
+
if gpt_pairs:
|
| 216 |
+
return gpt_pairs
|
| 217 |
+
except Exception as exc:
|
| 218 |
+
print(f"[4bhk] GPT layout path unavailable, using templates: {exc}")
|
| 219 |
+
|
| 220 |
pairs = []
|
| 221 |
tall = (plot_h / max(plot_w, 1e-6)) >= 1.35 if road_side in ("S", "N") \
|
| 222 |
else (plot_w / max(plot_h, 1e-6)) >= 1.35
|