Update inference script for user's schema
Browse files- generate.py +82 -25
generate.py
CHANGED
|
@@ -1,41 +1,68 @@
|
|
| 1 |
"""
|
| 2 |
Inference script for parametric floorplan generation.
|
| 3 |
-
|
| 4 |
-
Usage:
|
| 5 |
-
python generate.py --room_count 4 --total_area 100 --room_types Bedroom Bathroom Kitchen LivingRoom
|
| 6 |
"""
|
| 7 |
import json
|
| 8 |
import argparse
|
| 9 |
import torch
|
| 10 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 11 |
|
| 12 |
-
def build_prompt(
|
|
|
|
| 13 |
lines = [
|
| 14 |
-
f"Generate a floor plan
|
| 15 |
-
f"
|
|
|
|
|
|
|
|
|
|
| 16 |
]
|
| 17 |
-
if
|
| 18 |
-
lines.append("
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if
|
| 22 |
-
lines.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return "\n".join(lines)
|
| 24 |
|
| 25 |
-
def generate_floorplan(model_id, prompt, max_new_tokens
|
|
|
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 27 |
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
-
model_id,
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
if tokenizer.pad_token is None:
|
| 31 |
tokenizer.pad_token = tokenizer.eos_token
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
messages = [
|
| 34 |
-
{"role": "system", "content":
|
| 35 |
{"role": "user", "content": prompt},
|
| 36 |
]
|
|
|
|
| 37 |
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 38 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=
|
| 39 |
|
| 40 |
with torch.no_grad():
|
| 41 |
outputs = model.generate(
|
|
@@ -46,27 +73,57 @@ def generate_floorplan(model_id, prompt, max_new_tokens=1024, temperature=0.7, t
|
|
| 46 |
do_sample=True,
|
| 47 |
pad_token_id=tokenizer.pad_token_id,
|
| 48 |
)
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def main():
|
| 52 |
-
parser = argparse.ArgumentParser()
|
| 53 |
parser.add_argument("--model_id", type=str, default="Karthik8nitt/parametric-floorplan-generator")
|
| 54 |
-
parser.add_argument("--
|
| 55 |
-
parser.add_argument("--
|
| 56 |
-
parser.add_argument("--
|
| 57 |
-
parser.add_argument("--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
parser.add_argument("--temperature", type=float, default=0.7)
|
| 59 |
parser.add_argument("--top_p", type=float, default=0.9)
|
| 60 |
args = parser.parse_args()
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
print("Prompt:\n", prompt)
|
| 64 |
print("\n--- Generating floorplan ---\n")
|
|
|
|
| 65 |
result = generate_floorplan(args.model_id, prompt, args.max_new_tokens, args.temperature, args.top_p)
|
| 66 |
print(result)
|
|
|
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
print(f"\nWarning: could not parse as JSON: {e}")
|
| 72 |
|
|
|
|
| 1 |
"""
|
| 2 |
Inference script for parametric floorplan generation.
|
| 3 |
+
Generates a JSON floorplan from parametric constraints using a fine-tuned model.
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
import json
|
| 6 |
import argparse
|
| 7 |
import torch
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
|
| 10 |
+
def build_prompt(params: dict) -> str:
|
| 11 |
+
"""Build natural-language prompt from ProjectCreate-like parameters."""
|
| 12 |
lines = [
|
| 13 |
+
f"Generate a floor plan for project '{params.get('name', 'Project')}.'",
|
| 14 |
+
f"Plot dimensions: {params['plot_length']}m x {params['plot_width']}m, shape: {params.get('plot_shape', 'rectangular')}.",
|
| 15 |
+
f"Setbacks: front={params['setback_front']}m, rear={params['setback_rear']}m, left={params['setback_left']}m, right={params['setback_right']}m.",
|
| 16 |
+
f"Road side: {params['road_side']}, North direction: {params.get('north_direction', 'N')}.",
|
| 17 |
+
f"Requirements: {params['num_bedrooms']} bedrooms, {params['toilets']} toilets.",
|
| 18 |
]
|
| 19 |
+
if params.get("parking"):
|
| 20 |
+
lines.append("Parking is required.")
|
| 21 |
+
if params.get("has_pooja"):
|
| 22 |
+
lines.append("Include a Pooja room.")
|
| 23 |
+
if params.get("has_study"):
|
| 24 |
+
lines.append("Include a Study room.")
|
| 25 |
+
if params.get("has_balcony"):
|
| 26 |
+
lines.append("Include a Balcony.")
|
| 27 |
+
if params.get("has_stilt"):
|
| 28 |
+
lines.append("Stilt parking required.")
|
| 29 |
+
if params.get("has_basement"):
|
| 30 |
+
lines.append("Include a basement.")
|
| 31 |
+
lines.append(f"Number of floors: {params.get('num_floors', 1)} (1=G, 2=G+1, 3=G+2).")
|
| 32 |
+
if params.get("vastu_enabled"):
|
| 33 |
+
lines.append("Vastu compliance is enabled.")
|
| 34 |
+
city = params.get("city", "other")
|
| 35 |
+
municipality = params.get("municipality")
|
| 36 |
+
lines.append(f"City: {city}, Municipality: {municipality or 'N/A'}.")
|
| 37 |
return "\n".join(lines)
|
| 38 |
|
| 39 |
+
def generate_floorplan(model_id: str, prompt: str, max_new_tokens: int = 2048,
|
| 40 |
+
temperature: float = 0.7, top_p: float = 0.9):
|
| 41 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 42 |
model = AutoModelForCausalLM.from_pretrained(
|
| 43 |
+
model_id,
|
| 44 |
+
torch_dtype=torch.bfloat16,
|
| 45 |
+
device_map="auto",
|
| 46 |
+
trust_remote_code=True,
|
| 47 |
)
|
| 48 |
if tokenizer.pad_token is None:
|
| 49 |
tokenizer.pad_token = tokenizer.eos_token
|
| 50 |
|
| 51 |
+
system_msg = (
|
| 52 |
+
"You are a parametric floorplan generator for Indian residential construction. "
|
| 53 |
+
"Given plot dimensions, setbacks, road direction, number of bedrooms/toilets, "
|
| 54 |
+
"and optional rooms (pooja, study, balcony, parking, basement, stilt), "
|
| 55 |
+
"output a valid JSON floorplan with plot boundary, buildable boundary, rooms as polygons "
|
| 56 |
+
"with dimensions and positions, doors, windows, and area summaries."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
messages = [
|
| 60 |
+
{"role": "system", "content": system_msg},
|
| 61 |
{"role": "user", "content": prompt},
|
| 62 |
]
|
| 63 |
+
|
| 64 |
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 65 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=4096).to(model.device)
|
| 66 |
|
| 67 |
with torch.no_grad():
|
| 68 |
outputs = model.generate(
|
|
|
|
| 73 |
do_sample=True,
|
| 74 |
pad_token_id=tokenizer.pad_token_id,
|
| 75 |
)
|
| 76 |
+
|
| 77 |
+
generated = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 78 |
+
return generated
|
| 79 |
|
| 80 |
def main():
|
| 81 |
+
parser = argparse.ArgumentParser(description="Generate a floorplan from parametric input")
|
| 82 |
parser.add_argument("--model_id", type=str, default="Karthik8nitt/parametric-floorplan-generator")
|
| 83 |
+
parser.add_argument("--name", type=str, default="MyHouse")
|
| 84 |
+
parser.add_argument("--plot_length", type=float, default=15.0)
|
| 85 |
+
parser.add_argument("--plot_width", type=float, default=12.0)
|
| 86 |
+
parser.add_argument("--setback_front", type=float, default=1.5)
|
| 87 |
+
parser.add_argument("--setback_rear", type=float, default=1.0)
|
| 88 |
+
parser.add_argument("--setback_left", type=float, default=1.0)
|
| 89 |
+
parser.add_argument("--setback_right", type=float, default=1.0)
|
| 90 |
+
parser.add_argument("--road_side", type=str, default="N", choices=["N","S","E","W"])
|
| 91 |
+
parser.add_argument("--north_direction", type=str, default="N", choices=["N","S","E","W"])
|
| 92 |
+
parser.add_argument("--num_bedrooms", type=int, default=3)
|
| 93 |
+
parser.add_argument("--toilets", type=int, default=3)
|
| 94 |
+
parser.add_argument("--parking", action="store_true")
|
| 95 |
+
parser.add_argument("--has_pooja", action="store_true")
|
| 96 |
+
parser.add_argument("--has_study", action="store_true")
|
| 97 |
+
parser.add_argument("--has_balcony", action="store_true")
|
| 98 |
+
parser.add_argument("--has_stilt", action="store_true")
|
| 99 |
+
parser.add_argument("--has_basement", action="store_true")
|
| 100 |
+
parser.add_argument("--num_floors", type=int, default=1, choices=[1,2,3])
|
| 101 |
+
parser.add_argument("--vastu_enabled", action="store_true")
|
| 102 |
+
parser.add_argument("--city", type=str, default="Delhi")
|
| 103 |
+
parser.add_argument("--municipality", type=str, default=None)
|
| 104 |
+
parser.add_argument("--max_new_tokens", type=int, default=2048)
|
| 105 |
parser.add_argument("--temperature", type=float, default=0.7)
|
| 106 |
parser.add_argument("--top_p", type=float, default=0.9)
|
| 107 |
args = parser.parse_args()
|
| 108 |
|
| 109 |
+
params = vars(args)
|
| 110 |
+
prompt = build_prompt(params)
|
| 111 |
print("Prompt:\n", prompt)
|
| 112 |
print("\n--- Generating floorplan ---\n")
|
| 113 |
+
|
| 114 |
result = generate_floorplan(args.model_id, prompt, args.max_new_tokens, args.temperature, args.top_p)
|
| 115 |
print(result)
|
| 116 |
+
|
| 117 |
try:
|
| 118 |
+
data = json.loads(result)
|
| 119 |
+
print("\n--- Parsed JSON (summary) ---")
|
| 120 |
+
print(f"Project: {data['project_name']}")
|
| 121 |
+
print(f"Plot shape: {data['plot']['shape']}")
|
| 122 |
+
print(f"Rooms: {len(data['rooms'])}")
|
| 123 |
+
print(f"Doors: {len(data['doors'])}")
|
| 124 |
+
print(f"Windows: {len(data['windows'])}")
|
| 125 |
+
print(f"Total built-up area: {data['dimensions']['total_built_up_area_sqm']} m²")
|
| 126 |
+
print(f"Total carpet area: {data['dimensions']['total_carpet_area_sqm']} m²")
|
| 127 |
except Exception as e:
|
| 128 |
print(f"\nWarning: could not parse as JSON: {e}")
|
| 129 |
|