Update app.py
Browse files
app.py
CHANGED
|
@@ -3,74 +3,72 @@ import streamlit as st
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
# ------------------ Page Setup ------------------
|
| 7 |
st.set_page_config(page_title="π SuperKart Sales Forecast", layout="centered")
|
| 8 |
|
| 9 |
st.title("SuperKart Sales Forecast")
|
| 10 |
st.caption("Frontend powered by Streamlit β calls Flask backend for predictions")
|
| 11 |
|
| 12 |
-
# ------------------
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
env_url = os.getenv("BACKEND_URL")
|
| 16 |
if env_url:
|
| 17 |
return env_url.strip()
|
| 18 |
-
|
| 19 |
-
# 2) Optional: Streamlit secrets (if .streamlit/secrets.toml exists)
|
| 20 |
try:
|
| 21 |
-
return st.secrets["BACKEND_URL"].strip()
|
| 22 |
except Exception:
|
| 23 |
pass
|
| 24 |
-
|
| 25 |
-
# 3) Fallback placeholder (change this to your real backend if you prefer)
|
| 26 |
-
return "https://<your-backend-space>.hf.space"
|
| 27 |
|
| 28 |
BACKEND_URL = resolve_backend_url()
|
| 29 |
|
| 30 |
-
# ------------------
|
|
|
|
|
|
|
| 31 |
st.sidebar.title("βοΈ Backend")
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
st.sidebar.markdown(f"**URL:** [{BACKEND_URL}]({BACKEND_URL})")
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def check_backend_health(url: str):
|
| 38 |
try:
|
| 39 |
-
r = requests.get(f"{url}/health", timeout=
|
| 40 |
return r.status_code, r.text
|
| 41 |
except Exception as e:
|
| 42 |
return None, str(e)
|
| 43 |
|
| 44 |
-
# Auto-check toggle
|
| 45 |
auto_check = st.sidebar.toggle("Auto check health on load", value=True)
|
| 46 |
-
|
| 47 |
status_area = st.sidebar.empty()
|
|
|
|
| 48 |
if auto_check:
|
| 49 |
-
|
|
|
|
| 50 |
if code == 200:
|
| 51 |
-
status_area.success(
|
| 52 |
elif code is None:
|
| 53 |
status_area.error(f"β Unreachable\n{msg}")
|
| 54 |
else:
|
| 55 |
status_area.warning(f"β οΈ Status {code}\n{msg}")
|
| 56 |
|
| 57 |
-
# Manual check button
|
| 58 |
if st.sidebar.button("π Check Health Now"):
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
if code == 200:
|
| 61 |
-
|
| 62 |
elif code is None:
|
| 63 |
-
|
| 64 |
else:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# Config guidance/warning
|
| 68 |
-
if "<your-backend-space>" in BACKEND_URL:
|
| 69 |
-
st.sidebar.warning("Set BACKEND_URL in Space settings β Variables and secrets.")
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
st.divider()
|
| 72 |
-
|
| 73 |
-
# ------------------ Form: Inputs ------------------
|
| 74 |
st.subheader("Enter Product and Store Details")
|
| 75 |
|
| 76 |
with st.form("input_form"):
|
|
@@ -93,7 +91,9 @@ with st.form("input_form"):
|
|
| 93 |
|
| 94 |
submitted = st.form_submit_button("π Predict Sales")
|
| 95 |
|
| 96 |
-
# ------------------
|
|
|
|
|
|
|
| 97 |
if submitted:
|
| 98 |
payload = {
|
| 99 |
"Product_Weight": Product_Weight,
|
|
@@ -106,10 +106,13 @@ if submitted:
|
|
| 106 |
"Store_Location_City_Type": Store_Location_City_Type,
|
| 107 |
"Store_Type": Store_Type
|
| 108 |
}
|
|
|
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 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}")
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
|
|
|
| 6 |
st.set_page_config(page_title="π SuperKart Sales Forecast", layout="centered")
|
| 7 |
|
| 8 |
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():
|
| 15 |
env_url = os.getenv("BACKEND_URL")
|
| 16 |
if env_url:
|
| 17 |
return env_url.strip()
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
return st.secrets["BACKEND_URL"].strip()
|
| 20 |
except Exception:
|
| 21 |
pass
|
| 22 |
+
return "https://rizwan9-backend.hf.space" # fallback if not set
|
|
|
|
|
|
|
| 23 |
|
| 24 |
BACKEND_URL = resolve_backend_url()
|
| 25 |
|
| 26 |
+
# -----------------------------
|
| 27 |
+
# Sidebar: Backend Health
|
| 28 |
+
# -----------------------------
|
| 29 |
st.sidebar.title("βοΈ Backend")
|
| 30 |
+
|
| 31 |
+
default_url = BACKEND_URL
|
| 32 |
+
backend_url_input = st.sidebar.text_input("Backend URL", value=default_url)
|
| 33 |
+
BACKEND_URL = backend_url_input.strip() or default_url
|
| 34 |
st.sidebar.markdown(f"**URL:** [{BACKEND_URL}]({BACKEND_URL})")
|
| 35 |
|
| 36 |
+
@st.cache_data(ttl=60, show_spinner=False)
|
| 37 |
+
def check_backend_health(url: str, timeout: int = 45):
|
|
|
|
| 38 |
try:
|
| 39 |
+
r = requests.get(f"{url}/health", timeout=timeout)
|
| 40 |
return r.status_code, r.text
|
| 41 |
except Exception as e:
|
| 42 |
return None, str(e)
|
| 43 |
|
|
|
|
| 44 |
auto_check = st.sidebar.toggle("Auto check health on load", value=True)
|
|
|
|
| 45 |
status_area = st.sidebar.empty()
|
| 46 |
+
|
| 47 |
if auto_check:
|
| 48 |
+
with status_area, st.spinner("Checking backend health..."):
|
| 49 |
+
code, msg = check_backend_health(BACKEND_URL)
|
| 50 |
if code == 200:
|
| 51 |
+
status_area.success("β
Healthy (200)")
|
| 52 |
elif code is None:
|
| 53 |
status_area.error(f"β Unreachable\n{msg}")
|
| 54 |
else:
|
| 55 |
status_area.warning(f"β οΈ Status {code}\n{msg}")
|
| 56 |
|
|
|
|
| 57 |
if st.sidebar.button("π Check Health Now"):
|
| 58 |
+
check_backend_health.clear()
|
| 59 |
+
with status_area, st.spinner("Re-checking..."):
|
| 60 |
+
code, msg = check_backend_health(BACKEND_URL)
|
| 61 |
if code == 200:
|
| 62 |
+
status_area.success("β
Healthy (200)")
|
| 63 |
elif code is None:
|
| 64 |
+
status_area.error(f"β Unreachable\n{msg}")
|
| 65 |
else:
|
| 66 |
+
status_area.warning(f"β οΈ Status {code}\n{msg}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# -----------------------------
|
| 69 |
+
# Main Form
|
| 70 |
+
# -----------------------------
|
| 71 |
st.divider()
|
|
|
|
|
|
|
| 72 |
st.subheader("Enter Product and Store Details")
|
| 73 |
|
| 74 |
with st.form("input_form"):
|
|
|
|
| 91 |
|
| 92 |
submitted = st.form_submit_button("π Predict Sales")
|
| 93 |
|
| 94 |
+
# -----------------------------
|
| 95 |
+
# Prediction
|
| 96 |
+
# -----------------------------
|
| 97 |
if submitted:
|
| 98 |
payload = {
|
| 99 |
"Product_Weight": Product_Weight,
|
|
|
|
| 106 |
"Store_Location_City_Type": Store_Location_City_Type,
|
| 107 |
"Store_Type": Store_Type
|
| 108 |
}
|
| 109 |
+
|
| 110 |
try:
|
| 111 |
+
with st.spinner("Fetching prediction from backend..."):
|
| 112 |
+
r = requests.post(f"{BACKEND_URL}/predict", json=payload, timeout=60)
|
| 113 |
+
r.raise_for_status()
|
| 114 |
+
prediction = r.json()["predictions"][0]
|
| 115 |
st.success(f"π Predicted Product Store Sales Total: **{prediction:,.2f}**")
|
| 116 |
except Exception as e:
|
| 117 |
st.error(f"β Prediction failed:\n\n{e}")
|
| 118 |
+
|