Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,90 +2,75 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 4 |
|
| 5 |
-
# Load the base
|
| 6 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 8 |
-
model_id,
|
| 9 |
-
torch_dtype=torch.float16,
|
| 10 |
-
safety_checker=None
|
| 11 |
)
|
| 12 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 13 |
-
pipe.enable_attention_slicing()
|
| 14 |
|
| 15 |
-
# Move
|
| 16 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
|
| 18 |
-
# Load
|
| 19 |
pipe.load_lora_weights(
|
| 20 |
-
"ejazhabibdar/sd-FloorPlan-model",
|
| 21 |
-
weight_name="pytorch_lora_weights.safetensors"
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
# (Optional) Enable xFormers for faster/less memory if installed
|
| 25 |
-
# try:
|
| 26 |
-
# pipe.enable_xformers_memory_efficient_attention()
|
| 27 |
-
# except Exception as e:
|
| 28 |
-
# pass
|
| 29 |
|
| 30 |
-
#
|
| 31 |
def generate_floorplan(bedrooms, bathrooms, floors, plot_size):
|
| 32 |
-
# Construct a text prompt from the structured inputs:
|
| 33 |
-
# We use the LoRA's recommended style: "Floor plan of a [small/big] house, few/many rooms, one/multiple bathrooms, ...":contentReference[oaicite:2]{index=2}
|
| 34 |
prompt_parts = []
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
else:
|
| 41 |
-
prompt_parts.append("house")
|
| 42 |
else:
|
| 43 |
prompt_parts.append("house")
|
| 44 |
-
|
| 45 |
if bedrooms <= 2:
|
| 46 |
prompt_parts.append("few rooms")
|
| 47 |
elif bedrooms >= 5:
|
| 48 |
prompt_parts.append("many rooms")
|
| 49 |
else:
|
| 50 |
prompt_parts.append(f"{bedrooms} rooms")
|
| 51 |
-
|
| 52 |
if bathrooms == 1:
|
| 53 |
prompt_parts.append("one bathroom")
|
| 54 |
else:
|
| 55 |
prompt_parts.append(f"{bathrooms} bathrooms")
|
| 56 |
-
|
| 57 |
if floors == 1:
|
| 58 |
-
# single floor might not need explicit mention
|
| 59 |
prompt_parts.append("one floor")
|
| 60 |
else:
|
| 61 |
prompt_parts.append(f"{floors} floors")
|
| 62 |
-
|
| 63 |
prompt = "Floor plan of a " + ", ".join(prompt_parts) + "."
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
image = pipe(prompt,
|
| 67 |
-
num_inference_steps=25, # number of diffusion steps
|
| 68 |
-
guidance_scale=7.5 # classifier-free guidance scale
|
| 69 |
-
# You can adjust the LoRA strength via cross_attention_kwargs if needed, e.g. {"scale": 0.8}
|
| 70 |
-
).images[0]
|
| 71 |
return image
|
| 72 |
|
| 73 |
-
#
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
-
gr.Markdown("## 🏠 AI Floor Plan Generator
|
| 76 |
with gr.Row():
|
| 77 |
-
bedrooms = gr.Slider(label="Bedrooms", minimum=1, maximum=10, value=3
|
| 78 |
-
bathrooms = gr.Slider(label="Bathrooms", minimum=1, maximum=5, value=2
|
| 79 |
with gr.Row():
|
| 80 |
-
floors = gr.Slider(label="Floors", minimum=1, maximum=3, value=1
|
| 81 |
plot_size = gr.Dropdown(label="Plot Size", choices=["Small", "Medium", "Large"], value="Medium")
|
|
|
|
| 82 |
generate_btn = gr.Button("Generate Floor Plan")
|
| 83 |
output_image = gr.Image(label="Generated Floor Plan", type="pil")
|
| 84 |
-
|
| 85 |
-
# Connect the input components to the generation function
|
| 86 |
-
generate_btn.click(fn=generate_floorplan,
|
| 87 |
-
inputs=[bedrooms, bathrooms, floors, plot_size],
|
| 88 |
-
outputs=output_image)
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 4 |
|
| 5 |
+
# Load the base model
|
| 6 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 8 |
+
model_id,
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
safety_checker=None
|
| 11 |
)
|
| 12 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 13 |
+
pipe.enable_attention_slicing()
|
| 14 |
|
| 15 |
+
# Move pipeline to GPU or CPU
|
| 16 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
|
| 18 |
+
# Load FloorPlan LoRA weights
|
| 19 |
pipe.load_lora_weights(
|
| 20 |
+
"ejazhabibdar/sd-FloorPlan-model",
|
| 21 |
+
weight_name="pytorch_lora_weights.safetensors"
|
| 22 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Prompt generation logic
|
| 25 |
def generate_floorplan(bedrooms, bathrooms, floors, plot_size):
|
|
|
|
|
|
|
| 26 |
prompt_parts = []
|
| 27 |
+
|
| 28 |
+
size_map = {"small": "small", "medium": "", "large": "big"}
|
| 29 |
+
size_word = size_map.get(plot_size.lower(), "")
|
| 30 |
+
if size_word:
|
| 31 |
+
prompt_parts.append(f"{size_word} house")
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
prompt_parts.append("house")
|
| 34 |
+
|
| 35 |
if bedrooms <= 2:
|
| 36 |
prompt_parts.append("few rooms")
|
| 37 |
elif bedrooms >= 5:
|
| 38 |
prompt_parts.append("many rooms")
|
| 39 |
else:
|
| 40 |
prompt_parts.append(f"{bedrooms} rooms")
|
| 41 |
+
|
| 42 |
if bathrooms == 1:
|
| 43 |
prompt_parts.append("one bathroom")
|
| 44 |
else:
|
| 45 |
prompt_parts.append(f"{bathrooms} bathrooms")
|
| 46 |
+
|
| 47 |
if floors == 1:
|
|
|
|
| 48 |
prompt_parts.append("one floor")
|
| 49 |
else:
|
| 50 |
prompt_parts.append(f"{floors} floors")
|
| 51 |
+
|
| 52 |
prompt = "Floor plan of a " + ", ".join(prompt_parts) + "."
|
| 53 |
+
|
| 54 |
+
image = pipe(prompt, num_inference_steps=25, guidance_scale=7.5).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return image
|
| 56 |
|
| 57 |
+
# Gradio UI
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("## 🏠 AI Floor Plan Generator")
|
| 60 |
with gr.Row():
|
| 61 |
+
bedrooms = gr.Slider(label="Bedrooms", minimum=1, maximum=10, value=3)
|
| 62 |
+
bathrooms = gr.Slider(label="Bathrooms", minimum=1, maximum=5, value=2)
|
| 63 |
with gr.Row():
|
| 64 |
+
floors = gr.Slider(label="Floors", minimum=1, maximum=3, value=1)
|
| 65 |
plot_size = gr.Dropdown(label="Plot Size", choices=["Small", "Medium", "Large"], value="Medium")
|
| 66 |
+
|
| 67 |
generate_btn = gr.Button("Generate Floor Plan")
|
| 68 |
output_image = gr.Image(label="Generated Floor Plan", type="pil")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
generate_btn.click(
|
| 71 |
+
fn=generate_floorplan,
|
| 72 |
+
inputs=[bedrooms, bathrooms, floors, plot_size],
|
| 73 |
+
outputs=output_image
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
demo.launch()
|