Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,80 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
def home():
|
| 13 |
-
return "Sales Forecasting Backend is running!"
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
if __name__ == '__main__':
|
| 38 |
-
# Running on 0.0.0.0 makes it accessible externally, useful for deployment
|
| 39 |
-
app.run(host='0.0.0.0', port=5000)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import streamlit as st
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="SuperKart Sales Forecast", layout="centered")
|
| 7 |
+
st.title("๐ SuperKart Sales Forecast")
|
| 8 |
+
|
| 9 |
+
# Read backend URL from secret or sidebar input
|
| 10 |
+
DEFAULT_BACKEND = os.getenv("BACKEND_URL", "").strip()
|
| 11 |
+
backend_url = st.sidebar.text_input(
|
| 12 |
+
"Backend /predict URL",
|
| 13 |
+
value=DEFAULT_BACKEND,
|
| 14 |
+
help="From BACKEND_URL secret; you can override here."
|
| 15 |
+
)
|
| 16 |
+
st.sidebar.caption("Example: https://rizwan9--backend.hf.space/predict")
|
| 17 |
|
| 18 |
+
# Health check button
|
| 19 |
+
if st.sidebar.button("๐ Check Backend Connection"):
|
| 20 |
+
if not backend_url:
|
| 21 |
+
st.sidebar.error("Please enter backend URL first.")
|
| 22 |
+
else:
|
| 23 |
+
try:
|
| 24 |
+
health_url = backend_url.replace("/predict", "/")
|
| 25 |
+
r = requests.get(health_url, timeout=10)
|
| 26 |
+
if r.status_code == 200:
|
| 27 |
+
st.sidebar.success("โ
Backend is reachable!")
|
| 28 |
+
else:
|
| 29 |
+
st.sidebar.warning(f"โ ๏ธ Backend responded with {r.status_code}")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
st.sidebar.error(f"โ Could not connect: {e}")
|
| 32 |
|
| 33 |
+
st.markdown("Enter product & store details, then click **Predict**.")
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
col1, col2 = st.columns(2)
|
| 36 |
+
with col1:
|
| 37 |
+
Product_Weight = st.number_input("Product_Weight", min_value=0.0, value=12.65)
|
| 38 |
+
Product_Allocated_Area = st.number_input("Product_Allocated_Area", min_value=0.0, value=0.07)
|
| 39 |
+
Product_MRP = st.number_input("Product_MRP", min_value=0.0, value=147.0)
|
| 40 |
+
Store_Establishment_Year = st.number_input("Store_Establishment_Year", min_value=1900, max_value=2100, value=2005)
|
| 41 |
+
with col2:
|
| 42 |
+
Product_Sugar_Content = st.selectbox("Product_Sugar_Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 43 |
+
Product_Type = st.text_input("Product_Type", "Dairy")
|
| 44 |
+
Store_Size = st.selectbox("Store_Size", ["High", "Medium", "Low"])
|
| 45 |
+
Store_Location_City_Type = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 46 |
+
Store_Type = st.selectbox("Store_Type", ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"])
|
| 47 |
|
| 48 |
+
payload = {
|
| 49 |
+
"Product_Weight": Product_Weight,
|
| 50 |
+
"Product_Sugar_Content": Product_Sugar_Content,
|
| 51 |
+
"Product_Allocated_Area": Product_Allocated_Area,
|
| 52 |
+
"Product_Type": Product_Type,
|
| 53 |
+
"Product_MRP": Product_MRP,
|
| 54 |
+
"Store_Establishment_Year": int(Store_Establishment_Year),
|
| 55 |
+
"Store_Size": Store_Size,
|
| 56 |
+
"Store_Location_City_Type": Store_Location_City_Type,
|
| 57 |
+
"Store_Type": Store_Type
|
| 58 |
+
}
|
| 59 |
|
| 60 |
+
st.markdown("#### Request Payload (Preview)")
|
| 61 |
+
st.code(pd.DataFrame([payload]).to_json(orient="records", indent=2))
|
| 62 |
|
| 63 |
+
if st.button("๐ฎ Predict"):
|
| 64 |
+
if not backend_url:
|
| 65 |
+
st.error("Please set backend URL in sidebar.")
|
| 66 |
+
else:
|
| 67 |
+
try:
|
| 68 |
+
resp = requests.post(backend_url, json=[payload], timeout=20)
|
| 69 |
+
if resp.ok:
|
| 70 |
+
data = resp.json()
|
| 71 |
+
preds = data.get("predictions") or data.get("prediction")
|
| 72 |
+
if isinstance(preds, list):
|
| 73 |
+
st.success(f"**Predicted Sales:** {preds[0]:,.2f}")
|
| 74 |
+
else:
|
| 75 |
+
st.success(f"**Prediction:** {preds}")
|
| 76 |
+
else:
|
| 77 |
+
st.error(f"Backend error {resp.status_code}: {resp.text}")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Request failed: {e}")
|
| 80 |
|
|
|
|
|
|
|
|
|