Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,11 @@ st.title("SuperKart Sales Forecast")
|
|
| 9 |
st.caption("Frontend powered by Streamlit β calls Flask backend for predictions")
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
-
# Backend URL
|
| 13 |
# -----------------------------
|
| 14 |
def resolve_backend_url() -> str:
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
try:
|
| 18 |
-
url = st.secrets.get("BACKEND_URL")
|
| 19 |
-
except Exception:
|
| 20 |
-
pass
|
| 21 |
-
url = url or os.getenv("BACKEND_URL") or "https://rizwan9-backend.hf.space"
|
| 22 |
-
return url.strip()
|
| 23 |
|
| 24 |
BACKEND_URL = resolve_backend_url()
|
| 25 |
|
|
@@ -56,15 +50,15 @@ if auto_check:
|
|
| 56 |
run_health_check()
|
| 57 |
|
| 58 |
if st.sidebar.button("π Check Health Now"):
|
| 59 |
-
check_backend_health.clear()
|
| 60 |
run_health_check()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
st.divider()
|
| 63 |
st.subheader("Enter Product and Store Details")
|
| 64 |
|
| 65 |
-
# -----------------------------
|
| 66 |
-
# Main Form
|
| 67 |
-
# -----------------------------
|
| 68 |
with st.form("input_form"):
|
| 69 |
col1, col2 = st.columns(2)
|
| 70 |
|
|
@@ -76,7 +70,7 @@ with st.form("input_form"):
|
|
| 76 |
|
| 77 |
with col2:
|
| 78 |
Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 79 |
-
#
|
| 80 |
Product_Type = st.selectbox("Product Type", [
|
| 81 |
"Meat","Snack Foods","Hard Drinks","Dairy","Canned","Soft Drinks","Health and Hygiene",
|
| 82 |
"Baking Goods","Bread","Breakfast","Frozen Foods","Fruits and Vegetables","Household",
|
|
@@ -107,9 +101,15 @@ if submitted:
|
|
| 107 |
try:
|
| 108 |
with st.spinner("Fetching prediction from backend..."):
|
| 109 |
r = requests.post(f"{BACKEND_URL}/predict", json=payload, timeout=60)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
st.error(f"β Prediction failed:\n\n{e}")
|
|
|
|
| 9 |
st.caption("Frontend powered by Streamlit β calls Flask backend for predictions")
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
+
# Backend URL (no st.secrets)
|
| 13 |
# -----------------------------
|
| 14 |
def resolve_backend_url() -> str:
|
| 15 |
+
# Use HF Space variable if set, else fallback to your backend
|
| 16 |
+
return (os.getenv("BACKEND_URL") or "https://rizwan9-backend.hf.space").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
BACKEND_URL = resolve_backend_url()
|
| 19 |
|
|
|
|
| 50 |
run_health_check()
|
| 51 |
|
| 52 |
if st.sidebar.button("π Check Health Now"):
|
| 53 |
+
check_backend_health.clear()
|
| 54 |
run_health_check()
|
| 55 |
|
| 56 |
+
# -----------------------------
|
| 57 |
+
# Form
|
| 58 |
+
# -----------------------------
|
| 59 |
st.divider()
|
| 60 |
st.subheader("Enter Product and Store Details")
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
with st.form("input_form"):
|
| 63 |
col1, col2 = st.columns(2)
|
| 64 |
|
|
|
|
| 70 |
|
| 71 |
with col2:
|
| 72 |
Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 73 |
+
# note the lowercase "and" to match dataset
|
| 74 |
Product_Type = st.selectbox("Product Type", [
|
| 75 |
"Meat","Snack Foods","Hard Drinks","Dairy","Canned","Soft Drinks","Health and Hygiene",
|
| 76 |
"Baking Goods","Bread","Breakfast","Frozen Foods","Fruits and Vegetables","Household",
|
|
|
|
| 101 |
try:
|
| 102 |
with st.spinner("Fetching prediction from backend..."):
|
| 103 |
r = requests.post(f"{BACKEND_URL}/predict", json=payload, timeout=60)
|
| 104 |
+
|
| 105 |
+
if r.status_code != 200:
|
| 106 |
+
# show backend's detailed message to make debugging easy
|
| 107 |
+
try:
|
| 108 |
+
st.error(f"β Prediction failed ({r.status_code}):\n\n{r.json()}")
|
| 109 |
+
except Exception:
|
| 110 |
+
st.error(f"β Prediction failed ({r.status_code}):\n\n{r.text}")
|
| 111 |
+
else:
|
| 112 |
+
prediction = r.json()["predictions"][0]
|
| 113 |
+
st.success(f"π Predicted Product Store Sales Total: **{prediction:,.2f}**")
|
| 114 |
except Exception as e:
|
| 115 |
st.error(f"β Prediction failed:\n\n{e}")
|