Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# ---------------------- App Title ----------------------
|
| 6 |
+
st.title("🏗️ BuildSmart Estimator")
|
| 7 |
+
st.markdown("Estimate construction materials using a Mistral-powered model via Hugging Face.")
|
| 8 |
+
|
| 9 |
+
# ---------------------- User Inputs ----------------------
|
| 10 |
+
st.header("📋 Project Details")
|
| 11 |
+
|
| 12 |
+
area = st.number_input("Total Area (in square feet)", min_value=100, max_value=100000, step=100)
|
| 13 |
+
floors = st.number_input("Number of Floors", min_value=1, max_value=100, step=1)
|
| 14 |
+
structure_type = st.selectbox("Structure Type", ["Residential", "Commercial", "Industrial"])
|
| 15 |
+
material_preference = st.selectbox("Material Preference", ["Cement & Bricks", "Steel & Concrete"])
|
| 16 |
+
location = st.text_input("Location")
|
| 17 |
+
|
| 18 |
+
# ---------------------- Hugging Face Config ----------------------
|
| 19 |
+
HUGGINGFACE_API_TOKEN = st.secrets["api_token"]
|
| 20 |
+
HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 21 |
+
|
| 22 |
+
headers = {
|
| 23 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
| 24 |
+
"Content-Type": "application/json"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# ---------------------- Build Prompt ----------------------
|
| 28 |
+
def build_prompt(area, floors, structure_type, material_pref, location):
|
| 29 |
+
return (
|
| 30 |
+
f"[INST] Estimate construction materials for the following project:\n"
|
| 31 |
+
f"- Area: {area} sqft\n"
|
| 32 |
+
f"- Floors: {floors}\n"
|
| 33 |
+
f"- Structure type: {structure_type}\n"
|
| 34 |
+
f"- Material preference: {material_pref}\n"
|
| 35 |
+
f"- Location: {location}\n\n"
|
| 36 |
+
f"Return in this format:\n"
|
| 37 |
+
f"Cement (bags), Sand (cubic feet), Bricks (units), Steel (kg), Crush (cubic feet), Rori (cubic feet). [/INST]"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# ---------------------- Call API ----------------------
|
| 41 |
+
def query_mistral(prompt):
|
| 42 |
+
response = requests.post(
|
| 43 |
+
HUGGINGFACE_API_URL,
|
| 44 |
+
headers=headers,
|
| 45 |
+
json={"inputs": prompt}
|
| 46 |
+
)
|
| 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 |
+
# ---------------------- Submit Button ----------------------
|
| 53 |
+
if st.button("Estimate Materials"):
|
| 54 |
+
if not location:
|
| 55 |
+
st.warning("Please enter a location before submitting.")
|
| 56 |
+
else:
|
| 57 |
+
with st.spinner("Estimating materials using Mistral..."):
|
| 58 |
+
prompt = build_prompt(area, floors, structure_type, material_preference, location)
|
| 59 |
+
result_text = query_mistral(prompt)
|
| 60 |
+
|
| 61 |
+
st.subheader("📦 Estimated Materials")
|
| 62 |
+
st.text(result_text)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
lines = result_text.strip().split(",")
|
| 66 |
+
data = []
|
| 67 |
+
for line in lines:
|
| 68 |
+
if ":" in line:
|
| 69 |
+
key, value = line.split(":", 1)
|
| 70 |
+
else:
|
| 71 |
+
parts = line.strip().split()
|
| 72 |
+
key = " ".join(parts[:-1])
|
| 73 |
+
value = parts[-1]
|
| 74 |
+
data.append([key.strip(), value.strip()])
|
| 75 |
+
df = pd.DataFrame(data, columns=["Material", "Estimated Quantity"])
|
| 76 |
+
st.dataframe(df)
|
| 77 |
+
|
| 78 |
+
csv = df.to_csv(index=False)
|
| 79 |
+
st.download_button("📥 Download as CSV", csv, "material_estimate.csv", "text/csv")
|
| 80 |
+
except Exception as e:
|
| 81 |
+
st.error("❗ Could not parse the model output.")
|
| 82 |
+
st.exception(e)
|
| 83 |
+
|
| 84 |
+
st.markdown("---")
|
| 85 |
+
st.caption("Powered by Mistral via Hugging Face Inference API")
|