Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,107 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(page_title="Construction Estimator", layout="centered")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return
|
| 30 |
-
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
for
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if st.button("Calculate BOQ & Cost"):
|
| 105 |
-
scaled_qty_df = calculate_scaled_quantities(qty_df, covered_area, floors)
|
| 106 |
-
cost_df = calculate_costs(scaled_qty_df, rate_df)
|
| 107 |
-
boq_table = generate_boq_table(cost_df)
|
| 108 |
-
|
| 109 |
-
st.subheader("Scaled Material Quantities & Cost Estimate")
|
| 110 |
-
st.dataframe(boq_table.style.format({"Qty": "{:.2f}", "Rate (PKR)": "β¨ {:,.2f}", "Amount (PKR)": "β¨ {:,.2f}"}))
|
| 111 |
-
|
| 112 |
-
total_cost = boq_table['Amount (PKR)'].sum()
|
| 113 |
-
st.markdown(f"### Total Estimated Cost: β¨ {total_cost:,.2f}")
|
| 114 |
-
|
| 115 |
-
buf = draw_floor_plan(rooms, baths, living, car_porch, covered_area)
|
| 116 |
-
st.subheader("Tentative Floor Plan Sketch")
|
| 117 |
-
st.image(buf, use_column_width=True)
|
| 118 |
-
st.download_button("Download Floor Plan Sketch", buf, file_name="floor_plan.png", mime="image/png")
|
| 119 |
-
|
| 120 |
-
except Exception as e:
|
| 121 |
-
st.error(f"Error processing files: {e}")
|
| 122 |
-
|
| 123 |
-
if __name__ == "__main__":
|
| 124 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import io
|
| 5 |
+
import os
|
| 6 |
+
import replicate
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
+
from openai import OpenAI
|
| 10 |
|
| 11 |
st.set_page_config(page_title="Construction Estimator", layout="centered")
|
| 12 |
|
| 13 |
+
client = OpenAI(
|
| 14 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
| 15 |
+
base_url="https://api.groq.com/openai/v1"
|
| 16 |
+
)
|
| 17 |
+
GROQ_MODEL = "llama3-8b-8192"
|
| 18 |
+
|
| 19 |
+
@st.cache_data
|
| 20 |
+
def load_excel(file):
|
| 21 |
+
return pd.read_excel(file)
|
| 22 |
+
|
| 23 |
+
@st.cache_resource
|
| 24 |
+
def embed_chunks(chunks):
|
| 25 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 26 |
+
embeddings = model.encode(chunks)
|
| 27 |
+
return embeddings, model
|
| 28 |
+
|
| 29 |
+
def query_embedding(user_query, chunks, embeddings, model):
|
| 30 |
+
query_vec = model.encode([user_query])
|
| 31 |
+
similarities = cosine_similarity(query_vec, embeddings)[0]
|
| 32 |
+
top_idx = np.argmax(similarities)
|
| 33 |
+
return chunks[top_idx]
|
| 34 |
+
|
| 35 |
+
def generate_estimate(context, user_input):
|
| 36 |
+
prompt = f"""You are a construction estimator in Pakistan. Using the following schedule:
|
| 37 |
+
|
| 38 |
+
{context}
|
| 39 |
+
|
| 40 |
+
Generate a BOQ with item number, description, quantity, unit, rate, and total amount for:
|
| 41 |
+
{user_input}
|
| 42 |
+
|
| 43 |
+
Output as a markdown table."""
|
| 44 |
+
|
| 45 |
+
response = client.chat.completions.create(
|
| 46 |
+
model=GROQ_MODEL,
|
| 47 |
+
messages=[{"role": "user", "content": prompt}]
|
| 48 |
+
)
|
| 49 |
+
return response.choices[0].message.content
|
| 50 |
+
|
| 51 |
+
def compute_total_quantities(base_df, covered_area, floors):
|
| 52 |
+
base_df = base_df.copy()
|
| 53 |
+
base_df["Adjusted Qty"] = base_df["Qty per 1000 sft"] * (covered_area / 1000)
|
| 54 |
+
floor_factor = 1 + max(0, floors - 1) * 0.8
|
| 55 |
+
base_df["Total Qty"] = base_df["Adjusted Qty"] * floor_factor
|
| 56 |
+
return base_df
|
| 57 |
+
|
| 58 |
+
def generate_realistic_plan(rooms, baths, living, car_porch):
|
| 59 |
+
prompt = f"floor plan for {rooms} rooms, {baths} bathrooms, {living} living rooms, and {car_porch} car porch in modern style"
|
| 60 |
+
output = replicate.run(
|
| 61 |
+
"cjwbw/floor-plan-generator",
|
| 62 |
+
input={"prompt": prompt}
|
| 63 |
+
)
|
| 64 |
+
return output
|
| 65 |
+
|
| 66 |
+
st.title("ποΈ Construction Estimator (Material + Cost + BOQ + Sketch)")
|
| 67 |
+
|
| 68 |
+
quantity_file = st.file_uploader("Upload Material Quantities Excel (per 1000 sft)", type=["xlsx"])
|
| 69 |
+
cost_file = st.file_uploader("Upload Material Costs Excel", type=["xlsx"])
|
| 70 |
+
|
| 71 |
+
if quantity_file and cost_file:
|
| 72 |
+
base_df = load_excel(quantity_file)
|
| 73 |
+
cost_df = load_excel(cost_file)
|
| 74 |
+
|
| 75 |
+
if "Material" in base_df.columns and "Qty per 1000 sft" in base_df.columns:
|
| 76 |
+
st.success("Files loaded successfully.")
|
| 77 |
+
|
| 78 |
+
rooms = st.number_input("Number of Rooms", min_value=1, value=3)
|
| 79 |
+
baths = st.number_input("Number of Bathrooms", min_value=1, value=2)
|
| 80 |
+
living = st.number_input("Number of Living Rooms", min_value=0, value=1)
|
| 81 |
+
car_porch = st.number_input("Number of Car Porches", min_value=0, value=1)
|
| 82 |
+
covered_area = st.number_input("Total Covered Area (sft)", min_value=100, value=1200)
|
| 83 |
+
floors = st.number_input("Number of Floors", min_value=1, value=1)
|
| 84 |
+
|
| 85 |
+
if st.button("Generate Estimate"):
|
| 86 |
+
computed_df = compute_total_quantities(base_df, covered_area, floors)
|
| 87 |
+
boq = computed_df.merge(cost_df, on="Material", how="left")
|
| 88 |
+
boq["Amount"] = boq["Total Qty"] * boq["Rate"]
|
| 89 |
+
|
| 90 |
+
st.subheader("π Bill of Quantities (BOQ)")
|
| 91 |
+
st.dataframe(boq[["Material", "Total Qty", "Unit", "Rate", "Amount"]])
|
| 92 |
+
|
| 93 |
+
total_cost = boq["Amount"].sum()
|
| 94 |
+
st.metric("Total Estimated Cost (Rs)", f"{total_cost:,.0f}")
|
| 95 |
+
|
| 96 |
+
st.subheader("π AI-Generated Floor Plan Sketch")
|
| 97 |
+
try:
|
| 98 |
+
image_url = generate_realistic_plan(rooms, baths, living, car_porch)
|
| 99 |
+
st.image(image_url, caption="Generated by AI (Replicate)", use_column_width=True)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
st.warning("Could not generate sketch. Please check Replicate API setup.")
|
| 102 |
+
st.text(str(e))
|
| 103 |
+
|
| 104 |
+
else:
|
| 105 |
+
st.error("Quantity sheet must contain 'Material' and 'Qty per 1000 sft' columns.")
|
| 106 |
+
else:
|
| 107 |
+
st.info("Please upload both the quantity and cost Excel sheets.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|