Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,115 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
model = joblib.load('best_sales_forecasting_model.pkl')
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
@
|
| 16 |
-
def
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Assuming the incoming data is a list of dictionaries, where each dictionary is a data point
|
| 21 |
-
input_data = pd.DataFrame(data)
|
| 22 |
-
|
| 23 |
-
# Ensure the columns are in the same order as the training data
|
| 24 |
-
# This assumes you have access to the columns from your training data (X_train)
|
| 25 |
-
# You might need to adjust this part based on how you handle feature ordering
|
| 26 |
-
# For demonstration, let's assume the input data has the same columns in the same order
|
| 27 |
-
# In a real application, you might need to reorder columns or handle missing ones
|
| 28 |
-
|
| 29 |
-
# Make predictions
|
| 30 |
-
predictions = model.predict(input_data)
|
| 31 |
-
|
| 32 |
-
# Return predictions as a JSON response
|
| 33 |
-
return jsonify(predictions.tolist())
|
| 34 |
except Exception as e:
|
| 35 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 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() -> 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 |
|
| 26 |
+
# -----------------------------
|
| 27 |
+
# Sidebar: Backend Health
|
| 28 |
+
# -----------------------------
|
| 29 |
+
st.sidebar.title("βοΈ Backend")
|
| 30 |
+
backend_url_input = st.sidebar.text_input("Backend URL", value=BACKEND_URL)
|
| 31 |
+
BACKEND_URL = backend_url_input.strip() or BACKEND_URL
|
| 32 |
+
st.sidebar.markdown(f"**URL:** [{BACKEND_URL}]({BACKEND_URL})")
|
| 33 |
|
| 34 |
+
@st.cache_data(ttl=60, show_spinner=False)
|
| 35 |
+
def check_backend_health(url: str, timeout: int = 45):
|
| 36 |
try:
|
| 37 |
+
r = requests.get(f"{url}/health", timeout=timeout)
|
| 38 |
+
return r.status_code, r.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
+
return None, str(e)
|
| 41 |
+
|
| 42 |
+
status_box = st.sidebar.empty()
|
| 43 |
+
auto_check = st.sidebar.toggle("Auto check health on load", value=True)
|
| 44 |
+
|
| 45 |
+
def run_health_check():
|
| 46 |
+
with status_box, st.spinner("Checking backend health..."):
|
| 47 |
+
code, msg = check_backend_health(BACKEND_URL)
|
| 48 |
+
if code == 200:
|
| 49 |
+
status_box.success("β
Healthy (200)")
|
| 50 |
+
elif code is None:
|
| 51 |
+
status_box.error(f"β Unreachable\n{msg}")
|
| 52 |
+
else:
|
| 53 |
+
status_box.warning(f"β οΈ Status {code}\n{msg}")
|
| 54 |
+
|
| 55 |
+
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 |
+
|
| 71 |
+
with col1:
|
| 72 |
+
Product_Weight = st.number_input("Product Weight", min_value=0.0, step=0.1)
|
| 73 |
+
Product_Allocated_Area = st.number_input("Product Allocated Area", min_value=0.0, step=0.001, format="%.3f")
|
| 74 |
+
Product_MRP = st.number_input("Product MRP", min_value=0.0, step=0.5)
|
| 75 |
+
Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1950, max_value=2025, step=1)
|
| 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",
|
| 83 |
+
"Seafood","Starchy Foods","Others"
|
| 84 |
+
])
|
| 85 |
+
Store_Size = st.selectbox("Store Size", ["Low","Medium","High"])
|
| 86 |
+
Store_Location_City_Type = st.selectbox("City Tier", ["Tier 1","Tier 2","Tier 3"])
|
| 87 |
+
Store_Type = st.selectbox("Store Type", ["Departmental Store","Supermarket Type 1","Supermarket Type 2","Food Mart"])
|
| 88 |
+
|
| 89 |
+
submitted = st.form_submit_button("π Predict Sales")
|
| 90 |
+
|
| 91 |
+
# -----------------------------
|
| 92 |
+
# Call backend
|
| 93 |
+
# -----------------------------
|
| 94 |
+
if submitted:
|
| 95 |
+
payload = {
|
| 96 |
+
"Product_Weight": Product_Weight,
|
| 97 |
+
"Product_Sugar_Content": Product_Sugar_Content,
|
| 98 |
+
"Product_Allocated_Area": Product_Allocated_Area,
|
| 99 |
+
"Product_Type": Product_Type,
|
| 100 |
+
"Product_MRP": Product_MRP,
|
| 101 |
+
"Store_Establishment_Year": int(Store_Establishment_Year),
|
| 102 |
+
"Store_Size": Store_Size,
|
| 103 |
+
"Store_Location_City_Type": Store_Location_City_Type,
|
| 104 |
+
"Store_Type": Store_Type
|
| 105 |
+
}
|
| 106 |
+
|
| 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}")
|