Mtkhang90 commited on
Commit
4246afd
Β·
verified Β·
1 Parent(s): 5000cd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -49
app.py CHANGED
@@ -4,80 +4,76 @@ import csv
4
  import io
5
 
6
  # -------------------------------
7
- # BuildSmart Estimator: Streamlit Web App
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
- # Input form for user
 
 
 
 
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
- # Function to build the prompt
29
  def build_prompt(area, floors, structure, material, loc):
30
- return (
31
- f"Estimate the construction material required for a {structure} building "
32
- f"with {floors} floor(s), total area of {area} square feet, located in {loc}, "
33
- f"using {material}. Return quantities for: Cement (bags), Sand (cubic feet), Bricks (units), "
34
- f"Steel (kg), Crush (cubic feet), and Rori (cubic feet)."
35
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Function to call Hugging Face Inference API
38
- def query_huggingface_api(prompt):
39
- API_URL = "https://api-inference.huggingface.co/models/your-username/your-model-name" # πŸ” Replace this
40
  headers = {
41
- "Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN" # πŸ” Replace this
 
42
  }
43
  payload = {
44
- "inputs": prompt
 
 
 
45
  }
46
- response = requests.post(API_URL, headers=headers, json=payload)
 
47
  if response.status_code == 200:
48
- return response.json()[0]['generated_text']
49
  else:
50
- return f"❌ API Error: {response.status_code} - {response.text}"
51
 
52
- # Process form submission
53
  if submitted:
54
  prompt = build_prompt(total_area, floors, structure_type, material_pref, location)
55
  with st.spinner("Estimating materials..."):
56
- response_text = query_huggingface_api(prompt)
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
- # Parse simple line-based format (adjust if your model returns structured JSON)
68
- for line in response_text.splitlines():
69
- if ":" in line:
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
- # Footer
82
- st.markdown("---")
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_