File size: 2,792 Bytes
9548cf1
 
 
 
c13c735
9548cf1
 
 
 
 
c13c735
 
9548cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c13c735
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
import streamlit as st
import requests
import csv
import io
import os

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 API key from environment variable (Docker-safe)
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
GROQ_MODEL = "llama3-70b-8192"

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")

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):
"""

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}"

if submitted:
    prompt = build_prompt(total_area, floors, structure_type, material_pref, location)
    with st.spinner("Generating estimate..."):
        result = call_groq_api(prompt)

    st.markdown("### ๐Ÿ“ฆ Estimated Material Requirements")
    st.text(result)

    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"
    )

st.markdown("---")
st.caption("Built with โค๏ธ using Groq + Streamlit.")