Spaces:
Sleeping
Sleeping
Ajay D commited on
ff
Browse files
app.py
CHANGED
|
@@ -4,27 +4,28 @@ from diffusers import StableDiffusionPipeline
|
|
| 4 |
|
| 5 |
|
| 6 |
# ===============================
|
| 7 |
-
#
|
| 8 |
# ===============================
|
| 9 |
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
print("Loading Model... Please wait...")
|
| 15 |
|
| 16 |
|
| 17 |
# ===============================
|
| 18 |
-
# LOAD MODEL
|
| 19 |
# ===============================
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 22 |
-
|
| 23 |
-
torch_dtype=torch.float32
|
| 24 |
-
safety_checker=None
|
| 25 |
)
|
| 26 |
|
| 27 |
-
pipe = pipe.to(
|
| 28 |
|
| 29 |
pipe.enable_attention_slicing()
|
| 30 |
|
|
@@ -32,85 +33,78 @@ print("Model Loaded Successfully")
|
|
| 32 |
|
| 33 |
|
| 34 |
# ===============================
|
| 35 |
-
#
|
| 36 |
-
# ===============================
|
| 37 |
-
|
| 38 |
-
VASTU = {
|
| 39 |
-
"bedroom": "South-West",
|
| 40 |
-
"kitchen": "South-East",
|
| 41 |
-
"puja": "North-East",
|
| 42 |
-
"hall": "Center",
|
| 43 |
-
"toilet": "North-West",
|
| 44 |
-
"stairs": "South"
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# ===============================
|
| 49 |
-
# PROMPT
|
| 50 |
# ===============================
|
| 51 |
|
| 52 |
def build_prompt(width, height, entrance):
|
| 53 |
|
| 54 |
-
|
| 55 |
professional architectural blueprint,
|
| 56 |
-
|
| 57 |
-
black and white
|
| 58 |
-
technical
|
| 59 |
top view layout,
|
| 60 |
thin lines,
|
| 61 |
-
no
|
| 62 |
no shading,
|
| 63 |
labeled rooms,
|
|
|
|
| 64 |
|
| 65 |
plot size {width} x {height} feet,
|
| 66 |
entrance facing {entrance},
|
| 67 |
|
| 68 |
-
bedroom in
|
| 69 |
-
kitchen in
|
| 70 |
-
|
| 71 |
-
living hall in
|
| 72 |
-
toilet in
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
printable blueprint,
|
| 77 |
-
scale plan
|
| 78 |
"""
|
| 79 |
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
"""
|
| 87 |
|
| 88 |
|
| 89 |
# ===============================
|
| 90 |
-
# GENERATE
|
| 91 |
# ===============================
|
| 92 |
|
| 93 |
-
def
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
prompt = build_prompt(width, height, entrance)
|
| 96 |
|
|
|
|
|
|
|
| 97 |
image = pipe(
|
| 98 |
prompt=prompt,
|
| 99 |
-
negative_prompt=
|
| 100 |
num_inference_steps=28,
|
| 101 |
-
guidance_scale=
|
| 102 |
height=768,
|
| 103 |
width=768
|
| 104 |
).images[0]
|
| 105 |
|
| 106 |
-
return image
|
| 107 |
|
| 108 |
|
| 109 |
# ===============================
|
| 110 |
-
# UI
|
| 111 |
# ===============================
|
| 112 |
|
| 113 |
-
with gr.Blocks() as demo:
|
| 114 |
|
| 115 |
gr.Markdown(
|
| 116 |
"""
|
|
@@ -121,20 +115,40 @@ with gr.Blocks() as demo:
|
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
)
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
-
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
# ===============================
|
| 7 |
+
# DEVICE
|
| 8 |
# ===============================
|
| 9 |
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
+
print("Using device:", device)
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
# ===============================
|
| 16 |
+
# LOAD MODEL (ONCE)
|
| 17 |
# ===============================
|
| 18 |
|
| 19 |
+
print("Loading Stable Diffusion Model...")
|
| 20 |
+
|
| 21 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 22 |
+
|
| 23 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 24 |
+
model_id,
|
| 25 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
+
pipe = pipe.to(device)
|
| 29 |
|
| 30 |
pipe.enable_attention_slicing()
|
| 31 |
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
# ===============================
|
| 36 |
+
# PROMPT BUILDER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# ===============================
|
| 38 |
|
| 39 |
def build_prompt(width, height, entrance):
|
| 40 |
|
| 41 |
+
prompt = f"""
|
| 42 |
professional architectural blueprint,
|
| 43 |
+
clean 2D house floor plan,
|
| 44 |
+
black and white,
|
| 45 |
+
CAD technical drawing,
|
| 46 |
top view layout,
|
| 47 |
thin lines,
|
| 48 |
+
no colors,
|
| 49 |
no shading,
|
| 50 |
labeled rooms,
|
| 51 |
+
scale drawing,
|
| 52 |
|
| 53 |
plot size {width} x {height} feet,
|
| 54 |
entrance facing {entrance},
|
| 55 |
|
| 56 |
+
bedroom in south west,
|
| 57 |
+
kitchen in south east,
|
| 58 |
+
pooja room in north east,
|
| 59 |
+
living hall in center,
|
| 60 |
+
toilet in north west,
|
| 61 |
+
staircase in south,
|
| 62 |
|
| 63 |
+
printable house plan
|
|
|
|
|
|
|
| 64 |
"""
|
| 65 |
|
| 66 |
+
return prompt
|
| 67 |
|
| 68 |
+
|
| 69 |
+
negative_prompt = """
|
| 70 |
+
3d, color, colorful, render, painting,
|
| 71 |
+
sketch, cartoon, anime, fantasy,
|
| 72 |
+
photo, realistic, texture,
|
| 73 |
+
shadow, shading, blur, messy,
|
| 74 |
+
low quality, watermark
|
| 75 |
"""
|
| 76 |
|
| 77 |
|
| 78 |
# ===============================
|
| 79 |
+
# GENERATE FUNCTION
|
| 80 |
# ===============================
|
| 81 |
|
| 82 |
+
def generate_plan(width, height, entrance):
|
| 83 |
+
|
| 84 |
+
if not width or not height:
|
| 85 |
+
return None, "❌ Enter plot size"
|
| 86 |
|
| 87 |
prompt = build_prompt(width, height, entrance)
|
| 88 |
|
| 89 |
+
print("Prompt:", prompt)
|
| 90 |
+
|
| 91 |
image = pipe(
|
| 92 |
prompt=prompt,
|
| 93 |
+
negative_prompt=negative_prompt,
|
| 94 |
num_inference_steps=28,
|
| 95 |
+
guidance_scale=9,
|
| 96 |
height=768,
|
| 97 |
width=768
|
| 98 |
).images[0]
|
| 99 |
|
| 100 |
+
return image, "✅ Plan Generated Successfully"
|
| 101 |
|
| 102 |
|
| 103 |
# ===============================
|
| 104 |
+
# UI (GRADIO)
|
| 105 |
# ===============================
|
| 106 |
|
| 107 |
+
with gr.Blocks(title="Vastu AI Floor Plan Generator") as demo:
|
| 108 |
|
| 109 |
gr.Markdown(
|
| 110 |
"""
|
|
|
|
| 115 |
|
| 116 |
with gr.Row():
|
| 117 |
|
| 118 |
+
width = gr.Textbox(
|
| 119 |
+
label="Plot Width (feet)",
|
| 120 |
+
placeholder="Example: 40"
|
| 121 |
+
)
|
| 122 |
|
| 123 |
+
height = gr.Textbox(
|
| 124 |
+
label="Plot Height (feet)",
|
| 125 |
+
placeholder="Example: 60"
|
| 126 |
+
)
|
|
|
|
| 127 |
|
| 128 |
+
entrance = gr.Dropdown(
|
| 129 |
+
["North", "South", "East", "West"],
|
| 130 |
+
label="Entrance Direction",
|
| 131 |
+
value="East"
|
| 132 |
+
)
|
| 133 |
|
| 134 |
+
generate_btn = gr.Button("Generate Plan 🚀")
|
| 135 |
|
| 136 |
+
status = gr.Textbox(label="Status")
|
| 137 |
|
| 138 |
+
output = gr.Image(
|
| 139 |
+
label="Generated Plan",
|
| 140 |
+
type="pil"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
generate_btn.click(
|
| 144 |
+
fn=generate_plan,
|
| 145 |
+
inputs=[width, height, entrance],
|
| 146 |
+
outputs=[output, status]
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
# ===============================
|
| 151 |
+
# LAUNCH
|
| 152 |
+
# ===============================
|
| 153 |
|
| 154 |
demo.launch()
|