Mtkhang90 commited on
Commit
0c3fc34
·
verified ·
1 Parent(s): 92466dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -7,15 +7,16 @@ import io
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)
@@ -24,29 +25,28 @@ with st.form("estimator_form"):
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}",
@@ -59,28 +59,27 @@ def call_groq_api(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.StringIO()
80
  writer = csv.writer(csv_buffer)
81
  writer.writerow(["Material", "Quantity"])
82
 
83
- for line in result.splitlines():
84
  if ":" in line:
85
  parts = line.split(":", 1)
86
  writer.writerow([parts[0].strip(), parts[1].strip()])
@@ -94,4 +93,4 @@ if submitted:
94
 
95
  # Footer
96
  st.markdown("---")
97
- st.caption("Powered by Groq · Securely using Streamlit secrets")
 
7
  # BuildSmart Estimator using Groq
8
  # -------------------------------
9
 
10
+ # Page Configuration
11
  st.set_page_config(page_title="BuildSmart Estimator", page_icon="🏗️")
12
  st.title("🏗️ BuildSmart Estimator")
13
+ st.subheader("Estimate construction materials based on your project details")
14
 
15
+ # Load Groq API Key securely from secrets
16
  GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
17
+ GROQ_MODEL = "llama3-70b-8192" # You can change this if Groq updates their models
18
 
19
+ # Form for user input
20
  with st.form("estimator_form"):
21
  total_area = st.number_input("Total Area (in square feet)", min_value=100, step=50)
22
  floors = st.number_input("Number of Floors", min_value=1, step=1)
 
25
  location = st.text_input("Location", placeholder="e.g., Lahore, Karachi, etc.")
26
  submitted = st.form_submit_button("Estimate Materials")
27
 
28
+ # Prompt builder for Groq
29
  def build_prompt(area, floors, structure, material, loc):
30
  return f"""
31
+ You are a construction estimator bot. Based on the following user inputs, estimate the quantity of construction materials needed.
32
 
33
+ Project:
34
  - Total Area: {area} sq ft
35
+ - Floors: {floors}
36
+ - Structure: {structure}
37
  - Material Preference: {material}
38
  - Location: {loc}
39
 
40
+ Return the estimates in this format only:
41
+ Cement (bags):
42
+ Sand (cubic feet):
43
+ Bricks (units):
44
+ Steel (kg):
45
+ Crush (cubic feet):
 
46
  Rori (cubic feet):
47
  """
48
 
49
+ # Call Groq API
50
  def call_groq_api(prompt):
51
  headers = {
52
  "Authorization": f"Bearer {GROQ_API_KEY}",
 
59
  ]
60
  }
61
  response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
 
62
  if response.status_code == 200:
63
  return response.json()["choices"][0]["message"]["content"]
64
  else:
65
  return f"❌ Error: {response.status_code} - {response.text}"
66
 
67
+ # When the form is submitted
68
  if submitted:
69
  prompt = build_prompt(total_area, floors, structure_type, material_pref, location)
70
+ with st.spinner("Generating estimate..."):
71
  result = call_groq_api(prompt)
72
 
73
  # Display result
74
  st.markdown("### 📦 Estimated Material Requirements")
75
  st.text(result)
76
 
77
+ # CSV Download
78
  csv_buffer = io.StringIO()
79
  writer = csv.writer(csv_buffer)
80
  writer.writerow(["Material", "Quantity"])
81
 
82
+ for line in result.strip().splitlines():
83
  if ":" in line:
84
  parts = line.split(":", 1)
85
  writer.writerow([parts[0].strip(), parts[1].strip()])
 
93
 
94
  # Footer
95
  st.markdown("---")
96
+ st.caption("Built with ❤️ using Groq + Streamlit. Customize this app by editing `/streamlit_app.py`.")