Spaces:
Sleeping
Sleeping
Replace default demo with SuperKart UI
Browse files- streamlit_app.py +78 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, json, requests, streamlit as st
|
| 2 |
+
API_URL = os.getenv("API_URL","https://angadsi-superkart-sales-forecast-api.hf.space/predict")
|
| 3 |
+
|
| 4 |
+
st.set_page_config(page_title="SuperKart Sales Forecast", page_icon="🛒", layout="centered")
|
| 5 |
+
st.title("🛒 SuperKart Sales Forecast")
|
| 6 |
+
st.caption("Backend: Flask on HF Spaces • Frontend: Streamlit")
|
| 7 |
+
|
| 8 |
+
with st.form("predict_form"):
|
| 9 |
+
c1, c2 = st.columns(2)
|
| 10 |
+
with c1:
|
| 11 |
+
product_id = st.text_input("Product_Id", "ME1234")
|
| 12 |
+
product_weight = st.number_input("Product_Weight", 0.0, value=0.75, step=0.01)
|
| 13 |
+
sugar = st.selectbox("Product_Sugar_Content", ["low sugar","regular","no sugar"], index=1)
|
| 14 |
+
alloc_area = st.number_input("Product_Allocated_Area (0-1)", 0.0, 1.0, value=0.012, step=0.001)
|
| 15 |
+
product_type = st.selectbox("Product_Type", [
|
| 16 |
+
"meat","snack foods","hard drinks","dairy","canned","soft drinks","health and hygiene",
|
| 17 |
+
"baking goods","bread","breakfast","frozen foods","fruits and vegetables","household",
|
| 18 |
+
"seafood","starchy foods","others"
|
| 19 |
+
])
|
| 20 |
+
with c2:
|
| 21 |
+
mrp = st.number_input("Product_MRP", 0.0, value=199.0, step=1.0)
|
| 22 |
+
store_id = st.text_input("Store_Id", "S104")
|
| 23 |
+
est_year = st.number_input("Store_Establishment_Year", 1900, 2100, value=2012, step=1)
|
| 24 |
+
store_size = st.selectbox("Store_Size", ["High","Medium","Low"])
|
| 25 |
+
city_tier = st.selectbox("Store_Location_City_Type", ["Tier 1","Tier 2","Tier 3"])
|
| 26 |
+
store_type = st.selectbox("Store_Type", ["Departmental Store","Supermarket Type 1","Supermarket Type 2","Food Mart"])
|
| 27 |
+
submitted = st.form_submit_button("Predict")
|
| 28 |
+
|
| 29 |
+
st.markdown("### Or paste JSON for batch prediction")
|
| 30 |
+
default_batch = [
|
| 31 |
+
{"Product_Id":"ME1234","Product_Weight":0.75,"Product_Sugar_Content":"regular",
|
| 32 |
+
"Product_Allocated_Area":0.012,"Product_Type":"meat","Product_MRP":199.0,
|
| 33 |
+
"Store_Id":"S104","Store_Establishment_Year":2012,"Store_Size":"High",
|
| 34 |
+
"Store_Location_City_Type":"Tier 1","Store_Type":"Supermarket Type 1"}
|
| 35 |
+
]
|
| 36 |
+
raw = st.text_area("JSON list (optional)", value=json.dumps(default_batch, indent=2), height=180)
|
| 37 |
+
go_batch = st.button("Predict (batch JSON)")
|
| 38 |
+
|
| 39 |
+
def call_api(payload):
|
| 40 |
+
r = requests.post(API_URL, json=payload, timeout=30)
|
| 41 |
+
try:
|
| 42 |
+
return r.status_code, r.json()
|
| 43 |
+
except Exception:
|
| 44 |
+
return r.status_code, {"error": r.text[:300]}
|
| 45 |
+
|
| 46 |
+
if submitted:
|
| 47 |
+
payload = {
|
| 48 |
+
"Product_Id": product_id,
|
| 49 |
+
"Product_Weight": float(product_weight),
|
| 50 |
+
"Product_Sugar_Content": sugar,
|
| 51 |
+
"Product_Allocated_Area": float(alloc_area),
|
| 52 |
+
"Product_Type": product_type,
|
| 53 |
+
"Product_MRP": float(mrp),
|
| 54 |
+
"Store_Id": store_id,
|
| 55 |
+
"Store_Establishment_Year": int(est_year),
|
| 56 |
+
"Store_Size": store_size,
|
| 57 |
+
"Store_Location_City_Type": city_tier,
|
| 58 |
+
"Store_Type": store_type
|
| 59 |
+
}
|
| 60 |
+
with st.spinner("Calling backend..."):
|
| 61 |
+
code, resp = call_api(payload)
|
| 62 |
+
if code == 200 and "predictions" in resp:
|
| 63 |
+
st.success(f"Prediction: **{resp['predictions'][0]:,.2f}**")
|
| 64 |
+
else:
|
| 65 |
+
st.error(f"{code}: {resp}")
|
| 66 |
+
|
| 67 |
+
if go_batch:
|
| 68 |
+
try:
|
| 69 |
+
payload = json.loads(raw)
|
| 70 |
+
with st.spinner("Calling backend..."):
|
| 71 |
+
code, resp = call_api(payload)
|
| 72 |
+
if code == 200 and "predictions" in resp:
|
| 73 |
+
st.success("Predictions:")
|
| 74 |
+
st.write(resp["predictions"])
|
| 75 |
+
else:
|
| 76 |
+
st.error(f"{code}: {resp}")
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"Invalid JSON: {e}")
|