Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ import pandas as pd
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
from fpdf import FPDF
|
| 7 |
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Constants
|
| 10 |
BRICK_VOLUME_CFT = (9/12) * (4.5/12) * (3/12)
|
|
@@ -78,16 +81,72 @@ def estimate(rooms, wall_thickness, openings, floors):
|
|
| 78 |
cement_bag_volume_cft = CEMENT_BAG_WEIGHT_KG / CEMENT_DENSITY_KG_PER_CFT
|
| 79 |
cement_bags = round(cement_volume / cement_bag_volume_cft)
|
| 80 |
|
| 81 |
-
return number_of_bricks, sand_volume, cement_bags
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
def generate_pdf(data_dict):
|
| 84 |
pdf = FPDF()
|
| 85 |
pdf.add_page()
|
| 86 |
-
pdf.set_font("Arial", size=
|
| 87 |
pdf.cell(200, 10, "Estimation Report", ln=True, align='C')
|
| 88 |
-
pdf.ln(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
for key, value in data_dict.items():
|
| 90 |
pdf.cell(200, 10, f"{key}: {value}", ln=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
tmp = NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 92 |
pdf.output(tmp.name)
|
| 93 |
return tmp.name
|
|
@@ -99,7 +158,7 @@ def plot_rooms(shapes):
|
|
| 99 |
height = (y1 - y0)
|
| 100 |
rect = plt.Rectangle((x0, y0), width, height, fill=False, edgecolor='blue', linewidth=2)
|
| 101 |
ax.add_patch(rect)
|
| 102 |
-
ax.set_title("
|
| 103 |
ax.set_aspect("equal")
|
| 104 |
ax.axis("off")
|
| 105 |
st.pyplot(fig)
|
|
@@ -111,9 +170,9 @@ if uploaded_file:
|
|
| 111 |
st.success(f"✔️ Parsed {len(rooms)} rooms and {len(openings)} openings across {floors} floor(s).")
|
| 112 |
st.write(f"📏 Wall Thickness: {wall_thickness} ft")
|
| 113 |
|
| 114 |
-
bricks, sand, cement = estimate(rooms, wall_thickness, openings, floors)
|
| 115 |
|
| 116 |
-
st.subheader("
|
| 117 |
df_rooms = pd.DataFrame(rooms, columns=["Length (ft)", "Width (ft)", "Height (ft)"])
|
| 118 |
st.dataframe(df_rooms)
|
| 119 |
|
|
@@ -127,7 +186,7 @@ if uploaded_file:
|
|
| 127 |
st.subheader("🖼️ 2D Floor Plan")
|
| 128 |
plot_rooms(room_shapes)
|
| 129 |
|
| 130 |
-
st.subheader("
|
| 131 |
col1, col2 = st.columns(2)
|
| 132 |
with col1:
|
| 133 |
st.download_button("⬇️ Download Excel", df_result.to_csv(index=False).encode(), "estimates.csv", "text/csv")
|
|
@@ -136,9 +195,7 @@ if uploaded_file:
|
|
| 136 |
"Bricks Required": f"{bricks:,}",
|
| 137 |
"Sand Volume": f"{sand:.2f} cft",
|
| 138 |
"Cement Bags": f"{cement} bags"
|
| 139 |
-
})
|
| 140 |
with open(pdf_file, "rb") as f:
|
| 141 |
st.download_button("⬇️ Download PDF", f.read(), "estimates.pdf", "application/pdf")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
from fpdf import FPDF
|
| 7 |
from tempfile import NamedTemporaryFile
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import matplotlib.patches as patches
|
| 10 |
+
import tempfile
|
| 11 |
|
| 12 |
# Constants
|
| 13 |
BRICK_VOLUME_CFT = (9/12) * (4.5/12) * (3/12)
|
|
|
|
| 81 |
cement_bag_volume_cft = CEMENT_BAG_WEIGHT_KG / CEMENT_DENSITY_KG_PER_CFT
|
| 82 |
cement_bags = round(cement_volume / cement_bag_volume_cft)
|
| 83 |
|
| 84 |
+
return number_of_bricks, sand_volume, cement_bags, {
|
| 85 |
+
"Wall Volume (cft)": wall_volume,
|
| 86 |
+
"Opening Volume (cft)": opening_volume,
|
| 87 |
+
"Beam Volume (cft)": beam_volume,
|
| 88 |
+
"Net Volume (cft)": net_volume,
|
| 89 |
+
"Mortar Volume (cft)": mortar_volume,
|
| 90 |
+
"Cement Volume (cft)": cement_volume,
|
| 91 |
+
"Sand Volume (cft)": sand_volume,
|
| 92 |
+
"Cement Bag Volume (cft)": cement_bag_volume_cft,
|
| 93 |
+
"Brick Volume (cft)": BRICK_VOLUME_CFT
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
def draw_plan_image(room_shapes):
|
| 97 |
+
fig, ax = plt.subplots()
|
| 98 |
+
for x0, y0, x1, y1 in room_shapes:
|
| 99 |
+
width = x1 - x0
|
| 100 |
+
height = y1 - y0
|
| 101 |
+
rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='black', facecolor='none')
|
| 102 |
+
ax.add_patch(rect)
|
| 103 |
+
ax.text(x0 + width / 2, y0 - 2, f"{round(width / 12, 1)} ft", ha='center', fontsize=8)
|
| 104 |
+
ax.text(x1 + 2, y0 + height / 2, f"{round(height / 12, 1)} ft", va='center', fontsize=8, rotation=90)
|
| 105 |
+
ax.set_aspect('equal')
|
| 106 |
+
ax.axis('off')
|
| 107 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 108 |
+
fig.savefig(temp_file.name, bbox_inches='tight')
|
| 109 |
+
plt.close(fig)
|
| 110 |
+
return temp_file.name
|
| 111 |
+
|
| 112 |
+
def generate_pdf(data_dict, calc_details, room_shapes):
|
| 113 |
+
image_path = draw_plan_image(room_shapes)
|
| 114 |
|
|
|
|
| 115 |
pdf = FPDF()
|
| 116 |
pdf.add_page()
|
| 117 |
+
pdf.set_font("Arial", 'B', size=14)
|
| 118 |
pdf.cell(200, 10, "Estimation Report", ln=True, align='C')
|
| 119 |
+
pdf.ln(5)
|
| 120 |
+
|
| 121 |
+
pdf.set_font("Arial", 'B', size=12)
|
| 122 |
+
pdf.cell(200, 10, "Summary of Quantities", ln=True)
|
| 123 |
+
pdf.set_font("Arial", '', size=11)
|
| 124 |
for key, value in data_dict.items():
|
| 125 |
pdf.cell(200, 10, f"{key}: {value}", ln=True)
|
| 126 |
+
|
| 127 |
+
pdf.ln(8)
|
| 128 |
+
pdf.set_font("Arial", 'B', size=12)
|
| 129 |
+
pdf.cell(200, 10, "Step-by-Step Calculations with Formulas", ln=True)
|
| 130 |
+
|
| 131 |
+
pdf.set_font("Arial", '', size=10)
|
| 132 |
+
pdf.multi_cell(0, 8, f"""
|
| 133 |
+
1. Wall Volume = 2 × (L + W) × H × t = {round(calc_details['Wall Volume (cft)'], 2)} cft
|
| 134 |
+
2. Opening Volume = L × H × t = {round(calc_details['Opening Volume (cft)'], 2)} cft
|
| 135 |
+
3. Beam Volume = (L+1) × 0.75 × 0.75 = {round(calc_details['Beam Volume (cft)'], 2)} cft
|
| 136 |
+
4. Net Volume = (Wall - Opening - Beam) × Floors = {round(calc_details['Net Volume (cft)'], 2)} cft
|
| 137 |
+
5. Brick Volume = 9" × 4.5" × 3" = {round(calc_details['Brick Volume (cft)'], 4)} cft
|
| 138 |
+
6. Bricks = Net Volume / Brick Vol × 1.05 = {data_dict['Bricks Required']}
|
| 139 |
+
7. Mortar = Net Volume × 0.25 = {round(calc_details['Mortar Volume (cft)'], 2)} cft
|
| 140 |
+
8. Cement = Mortar × 1/6 = {round(calc_details['Cement Volume (cft)'], 2)} cft
|
| 141 |
+
9. Sand = Mortar × 5/6 = {round(calc_details['Sand Volume (cft)'], 2)} cft
|
| 142 |
+
10. Cement Bags = Cement / Bag Volume = {data_dict['Cement Bags']}
|
| 143 |
+
""")
|
| 144 |
+
|
| 145 |
+
pdf.ln(5)
|
| 146 |
+
pdf.set_font("Arial", 'B', size=12)
|
| 147 |
+
pdf.cell(200, 10, "2D Plan with Dimensions (in ft)", ln=True)
|
| 148 |
+
pdf.image(image_path, x=10, y=None, w=180)
|
| 149 |
+
|
| 150 |
tmp = NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 151 |
pdf.output(tmp.name)
|
| 152 |
return tmp.name
|
|
|
|
| 158 |
height = (y1 - y0)
|
| 159 |
rect = plt.Rectangle((x0, y0), width, height, fill=False, edgecolor='blue', linewidth=2)
|
| 160 |
ax.add_patch(rect)
|
| 161 |
+
ax.set_title("🗏️ 2D Floor Plan")
|
| 162 |
ax.set_aspect("equal")
|
| 163 |
ax.axis("off")
|
| 164 |
st.pyplot(fig)
|
|
|
|
| 170 |
st.success(f"✔️ Parsed {len(rooms)} rooms and {len(openings)} openings across {floors} floor(s).")
|
| 171 |
st.write(f"📏 Wall Thickness: {wall_thickness} ft")
|
| 172 |
|
| 173 |
+
bricks, sand, cement, calc_details = estimate(rooms, wall_thickness, openings, floors)
|
| 174 |
|
| 175 |
+
st.subheader("📊 Room Dimensions")
|
| 176 |
df_rooms = pd.DataFrame(rooms, columns=["Length (ft)", "Width (ft)", "Height (ft)"])
|
| 177 |
st.dataframe(df_rooms)
|
| 178 |
|
|
|
|
| 186 |
st.subheader("🖼️ 2D Floor Plan")
|
| 187 |
plot_rooms(room_shapes)
|
| 188 |
|
| 189 |
+
st.subheader("📄 Export")
|
| 190 |
col1, col2 = st.columns(2)
|
| 191 |
with col1:
|
| 192 |
st.download_button("⬇️ Download Excel", df_result.to_csv(index=False).encode(), "estimates.csv", "text/csv")
|
|
|
|
| 195 |
"Bricks Required": f"{bricks:,}",
|
| 196 |
"Sand Volume": f"{sand:.2f} cft",
|
| 197 |
"Cement Bags": f"{cement} bags"
|
| 198 |
+
}, calc_details, room_shapes)
|
| 199 |
with open(pdf_file, "rb") as f:
|
| 200 |
st.download_button("⬇️ Download PDF", f.read(), "estimates.pdf", "application/pdf")
|
| 201 |
|
|
|
|
|
|