Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,80 +4,76 @@ import csv
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
-
# BuildSmart Estimator
|
| 8 |
# -------------------------------
|
| 9 |
|
| 10 |
-
# Set page config
|
| 11 |
st.set_page_config(page_title="BuildSmart Estimator", page_icon="ποΈ")
|
| 12 |
-
|
| 13 |
-
# App title
|
| 14 |
st.title("ποΈ BuildSmart Estimator")
|
| 15 |
st.subheader("Estimate construction materials based on project details")
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with st.form("estimator_form"):
|
| 19 |
total_area = st.number_input("Total Area (in square feet)", min_value=100, step=50)
|
| 20 |
floors = st.number_input("Number of Floors", min_value=1, step=1)
|
| 21 |
structure_type = st.selectbox("Structure Type", ["Residential", "Commercial", "Industrial"])
|
| 22 |
material_pref = st.selectbox("Material Preference", ["Cement & Bricks", "Steel & Concrete"])
|
| 23 |
location = st.text_input("Location", placeholder="e.g., Lahore, Karachi, etc.")
|
| 24 |
-
|
| 25 |
-
# Submit button
|
| 26 |
submitted = st.form_submit_button("Estimate Materials")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
def build_prompt(area, floors, structure, material, loc):
|
| 30 |
-
return
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
def
|
| 39 |
-
API_URL = "https://api-inference.huggingface.co/models/your-username/your-model-name" # π Replace this
|
| 40 |
headers = {
|
| 41 |
-
"Authorization": "Bearer
|
|
|
|
| 42 |
}
|
| 43 |
payload = {
|
| 44 |
-
"
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
-
response = requests.post(
|
|
|
|
| 47 |
if response.status_code == 200:
|
| 48 |
-
return response.json()[0][
|
| 49 |
else:
|
| 50 |
-
return f"β
|
| 51 |
|
| 52 |
-
#
|
| 53 |
if submitted:
|
| 54 |
prompt = build_prompt(total_area, floors, structure_type, material_pref, location)
|
| 55 |
with st.spinner("Estimating materials..."):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Display results
|
| 59 |
-
st.markdown("### π¦ Estimated Material Requirements")
|
| 60 |
-
st.text(response_text)
|
| 61 |
-
|
| 62 |
-
# Optional: Convert response into CSV format
|
| 63 |
-
csv_buffer = io.StringIO()
|
| 64 |
-
writer = csv.writer(csv_buffer)
|
| 65 |
-
writer.writerow(["Material", "Quantity"])
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
parts = line.split(":", 1)
|
| 71 |
-
writer.writerow([parts[0].strip(), parts[1].strip()])
|
| 72 |
-
|
| 73 |
-
# Download button
|
| 74 |
-
st.download_button(
|
| 75 |
-
label="π₯ Download Estimate",
|
| 76 |
-
data=csv_buffer.getvalue(),
|
| 77 |
-
file_name="material_estimate.csv",
|
| 78 |
-
mime="text/csv"
|
| 79 |
-
)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
st.caption("Developed for educational purposes. Replace placeholder API values before deployment.")
|
|
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
# -------------------------------
|
| 7 |
+
# BuildSmart Estimator using Groq
|
| 8 |
# -------------------------------
|
| 9 |
|
|
|
|
| 10 |
st.set_page_config(page_title="BuildSmart Estimator", page_icon="ποΈ")
|
|
|
|
|
|
|
| 11 |
st.title("ποΈ BuildSmart Estimator")
|
| 12 |
st.subheader("Estimate construction materials based on project details")
|
| 13 |
|
| 14 |
+
# Securely fetch Groq API key from Streamlit secrets
|
| 15 |
+
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
| 16 |
+
GROQ_MODEL = "llama3-70b-8192" # Recommended and supported
|
| 17 |
+
|
| 18 |
+
# Form inputs
|
| 19 |
with st.form("estimator_form"):
|
| 20 |
total_area = st.number_input("Total Area (in square feet)", min_value=100, step=50)
|
| 21 |
floors = st.number_input("Number of Floors", min_value=1, step=1)
|
| 22 |
structure_type = st.selectbox("Structure Type", ["Residential", "Commercial", "Industrial"])
|
| 23 |
material_pref = st.selectbox("Material Preference", ["Cement & Bricks", "Steel & Concrete"])
|
| 24 |
location = st.text_input("Location", placeholder="e.g., Lahore, Karachi, etc.")
|
|
|
|
|
|
|
| 25 |
submitted = st.form_submit_button("Estimate Materials")
|
| 26 |
|
| 27 |
+
# Prompt constructor
|
| 28 |
def build_prompt(area, floors, structure, material, loc):
|
| 29 |
+
return f"""
|
| 30 |
+
You are a construction cost estimator bot. Based on the following user inputs, estimate the quantity of construction materials needed.
|
| 31 |
+
|
| 32 |
+
Project Details:
|
| 33 |
+
- Total Area: {area} sq ft
|
| 34 |
+
- Number of Floors: {floors}
|
| 35 |
+
- Structure Type: {structure}
|
| 36 |
+
- Material Preference: {material}
|
| 37 |
+
- Location: {loc}
|
| 38 |
+
|
| 39 |
+
Return only the estimated material quantities in this format:
|
| 40 |
+
|
| 41 |
+
Cement (bags):
|
| 42 |
+
Sand (cubic feet):
|
| 43 |
+
Bricks (units):
|
| 44 |
+
Steel (kg):
|
| 45 |
+
Crush (cubic feet):
|
| 46 |
+
Rori (cubic feet):
|
| 47 |
+
"""
|
| 48 |
|
| 49 |
+
# Groq API function
|
| 50 |
+
def call_groq_api(prompt):
|
|
|
|
| 51 |
headers = {
|
| 52 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 53 |
+
"Content-Type": "application/json"
|
| 54 |
}
|
| 55 |
payload = {
|
| 56 |
+
"model": GROQ_MODEL,
|
| 57 |
+
"messages": [
|
| 58 |
+
{"role": "user", "content": prompt}
|
| 59 |
+
]
|
| 60 |
}
|
| 61 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
|
| 62 |
+
|
| 63 |
if response.status_code == 200:
|
| 64 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 65 |
else:
|
| 66 |
+
return f"β Error: {response.status_code} - {response.text}"
|
| 67 |
|
| 68 |
+
# Run on form submission
|
| 69 |
if submitted:
|
| 70 |
prompt = build_prompt(total_area, floors, structure_type, material_pref, location)
|
| 71 |
with st.spinner("Estimating materials..."):
|
| 72 |
+
result = call_groq_api(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Display result
|
| 75 |
+
st.markdown("### π¦ Estimated Material Requirements")
|
| 76 |
+
st.text(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# CSV export
|
| 79 |
+
csv_buffer = io.String_
|
|
|