2Dand3DModel / app.py
sialnoman318's picture
Update app.py
4635f0a verified
import gradio as gr
import open3d as o3d
import matplotlib.pyplot as plt
import numpy as np
import os
from datasets import load_dataset
ds = load_dataset("OmarAmir2001/floor-plans-dataset")
# Ensure directories exist
os.makedirs("outputs/2d_drawings", exist_ok=True)
os.makedirs("outputs/3d_models", exist_ok=True)
# Parse plot size
def parse_plot_size(plot_size):
try:
dimensions = plot_size.lower().replace("feet", "").strip().split("x")
if len(dimensions) != 2:
raise ValueError("Invalid plot size format. Use '30x40 feet'.")
width, length = map(float, dimensions)
return width, length
except Exception as e:
raise ValueError(f"Error parsing plot size: {e}")
# Generate 2D drawing
def generate_2d_drawing(plot_size, num_floors, storey_height):
try:
width, length = parse_plot_size(plot_size)
fig, ax = plt.subplots(figsize=(8, 6))
for i in range(num_floors):
ax.plot([0, width], [i * storey_height, i * storey_height], 'b-')
ax.text(width / 2, i * storey_height + 1, f"Floor {i+1}", ha='center')
ax.plot([0, width, width, 0, 0], [0, 0, num_floors * storey_height, num_floors * storey_height, 0], 'r--')
ax.set_title("2D Drawing")
ax.set_xlabel("Width (ft)")
ax.set_ylabel("Height (ft)")
output_path = os.path.join("outputs/2d_drawings", "2d_drawing.png")
plt.savefig(output_path)
plt.close()
return output_path
except Exception as e:
print(f"Error in generate_2d_drawing: {e}")
raise e
# Generate 3D model
def generate_3d_model(plot_size, num_floors, storey_height):
try:
width, length = parse_plot_size(plot_size)
height = num_floors * storey_height
mesh = o3d.geometry.TriangleMesh.create_box(width, length, height)
mesh.compute_vertex_normals()
output_path = os.path.join("outputs/3d_models", "3d_model.obj")
o3d.io.write_triangle_mesh(output_path, mesh)
return output_path
except Exception as e:
print(f"Error in generate_3d_model: {e}")
raise e
# Main function
def generate_building(plot_size, storey_height, num_floors, additional_reqs):
drawing_path = generate_2d_drawing(plot_size, num_floors, storey_height)
model_path = generate_3d_model(plot_size, num_floors, storey_height)
return drawing_path, model_path
# Gradio interface
iface = gr.Interface(
fn=generate_building,
inputs=[
gr.Textbox(label="Plot Size (e.g., 30x40 feet)", placeholder="Enter plot dimensions"),
gr.Number(label="Storey Height (ft)", value=10),
gr.Number(label="Number of Floors", value=3),
gr.Textbox(label="Additional Requirements", placeholder="Optional")
],
outputs=[
gr.Image(label="2D Drawing"),
gr.File(label="3D Model (Download)")
],
title="Building Design Generator",
description="Generates 2D drawings and 3D models based on user inputs."
)
if __name__ == "__main__":
iface.launch()