Mtkhang90 commited on
Commit
5000cd9
Β·
verified Β·
1 Parent(s): 7dcf5c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ 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.")