Rizwan9 commited on
Commit
6c5e526
Β·
verified Β·
1 Parent(s): 6b9140e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
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 Resolver
13
  # -----------------------------
14
  def resolve_backend_url() -> str:
15
- # Prefer secret, then env, then hardcoded fallback
16
- url = None
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() # clear cache
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
- # Use dataset's exact labels (note lowercase 'and')
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
- r.raise_for_status()
111
- data = r.json()
112
- prediction = data["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}")
 
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}")