Spaces:
Sleeping
Sleeping
Indentation Error Fix
Browse files- streamlit_app.py +44 -44
streamlit_app.py
CHANGED
|
@@ -7,98 +7,98 @@ import streamlit as st
|
|
| 7 |
# ------------------------
|
| 8 |
# Page & header
|
| 9 |
# ------------------------
|
| 10 |
-
st.set_page_config(page_title=
|
| 11 |
-
st.title(
|
| 12 |
-
st.caption(
|
| 13 |
|
| 14 |
# ------------------------
|
| 15 |
# Sidebar: backend URL
|
| 16 |
# ------------------------
|
| 17 |
-
default_api = os.environ.get(
|
| 18 |
api_base = st.sidebar.text_input(
|
| 19 |
-
|
| 20 |
value=default_api,
|
| 21 |
-
help=
|
| 22 |
)
|
| 23 |
|
| 24 |
-
st.sidebar.info(
|
| 25 |
|
| 26 |
# ------------------------
|
| 27 |
# Form: inputs
|
| 28 |
# ------------------------
|
| 29 |
-
with st.form(key=
|
| 30 |
-
st.subheader(
|
| 31 |
|
| 32 |
col1, col2 = st.columns(2)
|
| 33 |
|
| 34 |
with col1:
|
| 35 |
-
product_weight = st.number_input(
|
| 36 |
-
product_allocated_area = st.number_input(
|
| 37 |
-
product_mrp = st.number_input(
|
| 38 |
-
store_est_year = st.number_input(
|
| 39 |
|
| 40 |
with col2:
|
| 41 |
-
sugar = st.selectbox(
|
| 42 |
ptype = st.selectbox(
|
| 43 |
-
|
| 44 |
[
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
],
|
| 49 |
index=1,
|
| 50 |
)
|
| 51 |
-
store_id = st.selectbox(
|
| 52 |
-
store_size = st.selectbox(
|
| 53 |
-
city_type = st.selectbox(
|
| 54 |
store_type = st.selectbox(
|
| 55 |
-
|
| 56 |
-
[
|
| 57 |
index=2,
|
| 58 |
)
|
| 59 |
|
| 60 |
-
submitted = st.form_submit_button(
|
| 61 |
|
| 62 |
# ------------------------
|
| 63 |
# Build payload preview
|
| 64 |
# ------------------------
|
| 65 |
payload = {
|
| 66 |
-
|
| 67 |
{
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
}
|
| 79 |
]
|
| 80 |
}
|
| 81 |
-
st.markdown(
|
| 82 |
-
st.code(json.dumps(payload, indent=2), language=
|
| 83 |
|
| 84 |
# ------------------------
|
| 85 |
# Call backend on submit
|
| 86 |
# ------------------------
|
| 87 |
if submitted:
|
| 88 |
try:
|
| 89 |
-
url = api_base.rstrip(
|
| 90 |
-
with st.spinner(
|
| 91 |
t0 = time.time()
|
| 92 |
resp = requests.post(url, json=payload, timeout=45)
|
| 93 |
dt_ms = int((time.time() - t0) * 1000)
|
| 94 |
|
| 95 |
if resp.status_code == 200:
|
| 96 |
data = resp.json()
|
| 97 |
-
pred = data.get(
|
| 98 |
-
st.success(f
|
| 99 |
else:
|
| 100 |
-
st.error(f
|
| 101 |
except Exception as e:
|
| 102 |
-
st.error(f
|
| 103 |
|
| 104 |
-
st.caption(
|
|
|
|
| 7 |
# ------------------------
|
| 8 |
# Page & header
|
| 9 |
# ------------------------
|
| 10 |
+
st.set_page_config(page_title="SuperKart Sales Forecasting", page_icon="🛒", layout="centered")
|
| 11 |
+
st.title("🛒 SuperKart Sales Forecasting")
|
| 12 |
+
st.caption("Enter details, call your backend /predict endpoint, and see the forecasted Product_Store_Sales_Total.")
|
| 13 |
|
| 14 |
# ------------------------
|
| 15 |
# Sidebar: backend URL
|
| 16 |
# ------------------------
|
| 17 |
+
default_api = os.environ.get("SUPERKART_API_URL", "http://localhost:7860")
|
| 18 |
api_base = st.sidebar.text_input(
|
| 19 |
+
"Backend API base URL",
|
| 20 |
value=default_api,
|
| 21 |
+
help="Example: https://<username>-superkart-forecasting.hf.space or http://localhost:7860",
|
| 22 |
)
|
| 23 |
|
| 24 |
+
st.sidebar.info("Tip: Ensure backend is running and category labels match training (e.g., 'Supermarket Type2').")
|
| 25 |
|
| 26 |
# ------------------------
|
| 27 |
# Form: inputs
|
| 28 |
# ------------------------
|
| 29 |
+
with st.form(key="predict_form"):
|
| 30 |
+
st.subheader("Input Features")
|
| 31 |
|
| 32 |
col1, col2 = st.columns(2)
|
| 33 |
|
| 34 |
with col1:
|
| 35 |
+
product_weight = st.number_input("Product_Weight", min_value=0.0, value=12.5, step=0.1)
|
| 36 |
+
product_allocated_area = st.number_input("Product_Allocated_Area", min_value=0.0, max_value=1.0, value=0.08, step=0.01)
|
| 37 |
+
product_mrp = st.number_input("Product_MRP", min_value=0.0, value=150.0, step=1.0)
|
| 38 |
+
store_est_year = st.number_input("Store_Establishment_Year", min_value=1950, max_value=2025, value=2002, step=1)
|
| 39 |
|
| 40 |
with col2:
|
| 41 |
+
sugar = st.selectbox("Product_Sugar_Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 42 |
ptype = st.selectbox(
|
| 43 |
+
"Product_Type",
|
| 44 |
[
|
| 45 |
+
"Meat", "Snack Foods", "Hard Drinks", "Dairy", "Canned", "Soft Drinks",
|
| 46 |
+
"Health and Hygiene", "Baking Goods", "Bread", "Breakfast", "Frozen Foods",
|
| 47 |
+
"Fruits and Vegetables", "Household", "Seafood", "Starchy Foods", "Others",
|
| 48 |
],
|
| 49 |
index=1,
|
| 50 |
)
|
| 51 |
+
store_id = st.selectbox("Store_Id", ["OUT001", "OUT002", "OUT003", "OUT004"] , index=3)
|
| 52 |
+
store_size = st.selectbox("Store_Size", ["Low", "Medium", "High"] , index=1)
|
| 53 |
+
city_type = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"] , index=1)
|
| 54 |
store_type = st.selectbox(
|
| 55 |
+
"Store_Type",
|
| 56 |
+
["Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"] ,
|
| 57 |
index=2,
|
| 58 |
)
|
| 59 |
|
| 60 |
+
submitted = st.form_submit_button("🔮 Predict")
|
| 61 |
|
| 62 |
# ------------------------
|
| 63 |
# Build payload preview
|
| 64 |
# ------------------------
|
| 65 |
payload = {
|
| 66 |
+
"records": [
|
| 67 |
{
|
| 68 |
+
"Product_Weight": product_weight,
|
| 69 |
+
"Product_Allocated_Area": product_allocated_area,
|
| 70 |
+
"Product_MRP": product_mrp,
|
| 71 |
+
"Product_Sugar_Content": sugar,
|
| 72 |
+
"Product_Type": ptype,
|
| 73 |
+
"Store_Id": store_id,
|
| 74 |
+
"Store_Size": store_size,
|
| 75 |
+
"Store_Location_City_Type": city_type,
|
| 76 |
+
"Store_Type": store_type,
|
| 77 |
+
"Store_Establishment_Year": int(store_est_year),
|
| 78 |
}
|
| 79 |
]
|
| 80 |
}
|
| 81 |
+
st.markdown("#### Request Preview")
|
| 82 |
+
st.code(json.dumps(payload, indent=2), language="json")
|
| 83 |
|
| 84 |
# ------------------------
|
| 85 |
# Call backend on submit
|
| 86 |
# ------------------------
|
| 87 |
if submitted:
|
| 88 |
try:
|
| 89 |
+
url = api_base.rstrip("/") + "/predict"
|
| 90 |
+
with st.spinner("Calling backend…"):
|
| 91 |
t0 = time.time()
|
| 92 |
resp = requests.post(url, json=payload, timeout=45)
|
| 93 |
dt_ms = int((time.time() - t0) * 1000)
|
| 94 |
|
| 95 |
if resp.status_code == 200:
|
| 96 |
data = resp.json()
|
| 97 |
+
pred = data.get("predictions", [None])[0]
|
| 98 |
+
st.success(f"✅ Predicted Product_Store_Sales_Total: **{pred:.2f}** \\n⏱ Response time: {dt_ms} ms")
|
| 99 |
else:
|
| 100 |
+
st.error(f"❌ Request failed ({resp.status_code}): {resp.text}")
|
| 101 |
except Exception as e:
|
| 102 |
+
st.error(f"❌ Error contacting API: {e}")
|
| 103 |
|
| 104 |
+
st.caption("Keep category labels consistent with training (e.g., 'Supermarket Type2').")
|