Mtkhang90's picture
Rename streamlit_app.py to app.py
895e107 verified
import streamlit as st
import requests
import csv
import io
# -------------------------------
# BuildSmart Estimator using Groq
# -------------------------------
# Page Configuration
st.set_page_config(page_title="BuildSmart Estimator", page_icon="πŸ—οΈ")
st.title("πŸ—οΈ BuildSmart Estimator")
st.subheader("Estimate construction materials based on your project details")
# Load Groq API Key securely from secrets
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
GROQ_MODEL = "llama3-70b-8192" # You can change this if Groq updates their models
# Form for user input
with st.form("estimator_form"):
total_area = st.number_input("Total Area (in square feet)", min_value=100, step=50)
floors = st.number_input("Number of Floors", min_value=1, step=1)
structure_type = st.selectbox("Structure Type", ["Residential", "Commercial", "Industrial"])
material_pref = st.selectbox("Material Preference", ["Cement & Bricks", "Steel & Concrete"])
location = st.text_input("Location", placeholder="e.g., Lahore, Karachi, etc.")
submitted = st.form_submit_button("Estimate Materials")
# Prompt builder for Groq
def build_prompt(area, floors, structure, material, loc):
return f"""
You are a construction estimator bot. Based on the following user inputs, estimate the quantity of construction materials needed.
Project:
- Total Area: {area} sq ft
- Floors: {floors}
- Structure: {structure}
- Material Preference: {material}
- Location: {loc}
Return the estimates in this format only:
Cement (bags):
Sand (cubic feet):
Bricks (units):
Steel (kg):
Crush (cubic feet):
Rori (cubic feet):
"""
# Call Groq API
def call_groq_api(prompt):
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": GROQ_MODEL,
"messages": [
{"role": "user", "content": prompt}
]
}
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
return f"❌ Error: {response.status_code} - {response.text}"
# When the form is submitted
if submitted:
prompt = build_prompt(total_area, floors, structure_type, material_pref, location)
with st.spinner("Generating estimate..."):
result = call_groq_api(prompt)
# Display result
st.markdown("### πŸ“¦ Estimated Material Requirements")
st.text(result)
# CSV Download
csv_buffer = io.StringIO()
writer = csv.writer(csv_buffer)
writer.writerow(["Material", "Quantity"])
for line in result.strip().splitlines():
if ":" in line:
parts = line.split(":", 1)
writer.writerow([parts[0].strip(), parts[1].strip()])
st.download_button(
label="πŸ“₯ Download Estimate",
data=csv_buffer.getvalue(),
file_name="buildsmart_estimate.csv",
mime="text/csv"
)
# Footer
st.markdown("---")
st.caption("Built with ❀️ using Groq + Streamlit. Customize this app by editing `/streamlit_app.py`.")