Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
from shapely.geometry import Point, Polygon, box
|
| 4 |
+
import ezdxf
|
| 5 |
+
|
| 6 |
+
def generate_floor_plan(plot_size, num_rooms, building_type):
|
| 7 |
+
# Parse plot dimensions
|
| 8 |
+
try:
|
| 9 |
+
plot_width, plot_height = map(float, plot_size.split('x'))
|
| 10 |
+
except ValueError:
|
| 11 |
+
return "Error: Plot size must be in 'width x height' format (e.g., 50x30)."
|
| 12 |
+
|
| 13 |
+
# Create plot boundary
|
| 14 |
+
plot_boundary = box(0, 0, plot_width, plot_height)
|
| 15 |
+
|
| 16 |
+
# Calculate room sizes
|
| 17 |
+
room_width = plot_width / (num_rooms ** 0.5)
|
| 18 |
+
room_height = plot_height / (num_rooms ** 0.5)
|
| 19 |
+
|
| 20 |
+
# Generate room layout
|
| 21 |
+
rooms = []
|
| 22 |
+
x, y = 0, 0
|
| 23 |
+
for _ in range(int(num_rooms ** 0.5)):
|
| 24 |
+
for _ in range(int(num_rooms ** 0.5)):
|
| 25 |
+
room = box(x, y, x + room_width, y + room_height)
|
| 26 |
+
rooms.append(room)
|
| 27 |
+
x += room_width
|
| 28 |
+
x = 0
|
| 29 |
+
y += room_height
|
| 30 |
+
|
| 31 |
+
# Add courtyard (10% of plot area at center)
|
| 32 |
+
courtyard_size = (plot_width * plot_height) * 0.1
|
| 33 |
+
courtyard_side = courtyard_size ** 0.5
|
| 34 |
+
courtyard = box(
|
| 35 |
+
(plot_width - courtyard_side) / 2,
|
| 36 |
+
(plot_height - courtyard_side) / 2,
|
| 37 |
+
(plot_width + courtyard_side) / 2,
|
| 38 |
+
(plot_height + courtyard_side) / 2,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Visualization
|
| 42 |
+
fig, ax = plt.subplots()
|
| 43 |
+
ax.set_xlim(0, plot_width)
|
| 44 |
+
ax.set_ylim(0, plot_height)
|
| 45 |
+
|
| 46 |
+
# Plot elements
|
| 47 |
+
ax.add_patch(plt.Rectangle((0, 0), plot_width, plot_height, edgecolor="black", fill=False, lw=2)) # Plot boundary
|
| 48 |
+
for room in rooms:
|
| 49 |
+
ax.add_patch(plt.Rectangle((room.bounds[0], room.bounds[1]), room_width, room_height, edgecolor="blue", fill=False))
|
| 50 |
+
ax.add_patch(plt.Rectangle((courtyard.bounds[0], courtyard.bounds[1]), courtyard_side, courtyard_side, edgecolor="green", fill=False, lw=2)) # Courtyard
|
| 51 |
+
|
| 52 |
+
plt.axis("off")
|
| 53 |
+
plt.close(fig)
|
| 54 |
+
|
| 55 |
+
# Save the plot as an image
|
| 56 |
+
img_path = "floor_plan.png"
|
| 57 |
+
fig.savefig(img_path)
|
| 58 |
+
|
| 59 |
+
return img_path
|
| 60 |
+
|
| 61 |
+
# Gradio Interface
|
| 62 |
+
interface = gr.Interface(
|
| 63 |
+
fn=generate_floor_plan,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Textbox(label="Plot Size (e.g., 50x30)"),
|
| 66 |
+
gr.Number(label="Number of Rooms", value=4),
|
| 67 |
+
gr.Radio(["Residential", "Commercial"], label="Building Type"),
|
| 68 |
+
],
|
| 69 |
+
outputs="image",
|
| 70 |
+
title="Floor Plan Generator",
|
| 71 |
+
description="Enter plot size, number of rooms, and building type to generate a floor plan."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
interface.launch()
|