Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 4 |
+
|
| 5 |
+
# Load the base Stable Diffusion model (v1.5) in half-precision for efficiency
|
| 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 # disable safety checker for faster inference (floor plans are non-NSFW)
|
| 11 |
+
)
|
| 12 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 13 |
+
pipe.enable_attention_slicing() # reduce memory usage, good for running on limited GPU
|
| 14 |
+
|
| 15 |
+
# Move the pipeline to GPU (if available)
|
| 16 |
+
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
|
| 18 |
+
# Load the FloorPlan LoRA weights into the pipeline
|
| 19 |
+
pipe.load_lora_weights(
|
| 20 |
+
"ejazhabibdar/sd-FloorPlan-model", # Hugging Face model hub ID for the LoRA weights
|
| 21 |
+
weight_name="pytorch_lora_weights.safetensors" # file containing LoRA weights
|
| 22 |
+
):contentReference[oaicite:1]{index=1}
|
| 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 |
+
# Define a function to generate the floor plan image based on user inputs
|
| 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 |
+
# Determine house size descriptor
|
| 36 |
+
if plot_size.lower() in ["small", "medium", "large"]:
|
| 37 |
+
size_word = "small" if plot_size.lower() == "small" else ("big" if plot_size.lower() == "large" else "")
|
| 38 |
+
if size_word:
|
| 39 |
+
prompt_parts.append(f"{size_word} house")
|
| 40 |
+
else:
|
| 41 |
+
prompt_parts.append("house")
|
| 42 |
+
else:
|
| 43 |
+
prompt_parts.append("house")
|
| 44 |
+
# Rooms descriptor based on bedrooms count
|
| 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 |
+
# Bathrooms descriptor
|
| 52 |
+
if bathrooms == 1:
|
| 53 |
+
prompt_parts.append("one bathroom")
|
| 54 |
+
else:
|
| 55 |
+
prompt_parts.append(f"{bathrooms} bathrooms")
|
| 56 |
+
# Floors descriptor
|
| 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 |
+
# You can add more prompt details (kitchen size, windows) if desired, but we'll keep it simple
|
| 63 |
+
prompt = "Floor plan of a " + ", ".join(prompt_parts) + "."
|
| 64 |
+
|
| 65 |
+
# Generate the image with the pipeline
|
| 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 |
+
# Build the Gradio interface
|
| 74 |
+
with gr.Blocks() as demo:
|
| 75 |
+
gr.Markdown("## 🏠 AI Floor Plan Generator\nEnter your desired house parameters and click **Generate** to create a floor plan:")
|
| 76 |
+
with gr.Row():
|
| 77 |
+
bedrooms = gr.Slider(label="Bedrooms", minimum=1, maximum=10, value=3, step=1)
|
| 78 |
+
bathrooms = gr.Slider(label="Bathrooms", minimum=1, maximum=5, value=2, step=1)
|
| 79 |
+
with gr.Row():
|
| 80 |
+
floors = gr.Slider(label="Floors", minimum=1, maximum=3, value=1, step=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 |
+
# Launch the app (not strictly required on Spaces, but okay to include)
|
| 91 |
+
demo.launch()
|