superkart-front / streamlit_app.py
ctroo's picture
Upload streamlit_app.py with huggingface_hub
954cbca verified
import streamlit as st
import requests
import json
# --- Configuration ---
BACKEND_URL = "https://ctroo-superkart-sales-backend.hf.space/predict"
# --- Page Setup ---
st.set_page_config(page_title="SuperKart Sales Forecaster", page_icon="🛒", layout="centered")
st.title("🛒 SuperKart Sales Forecaster")
st.markdown("Enter product and store details below to forecast expected sales revenue.")
# --- Input Form ---
with st.form("prediction_form"):
st.subheader("Product Details")
col1, col2 = st.columns(2)
with col1:
product_weight = st.number_input("Product Weight (kg)", min_value=0.0, value=12.0, step=0.1)
product_mrp = st.number_input("Product MRP ($)", min_value=0.0, value=150.0, step=1.0)
product_allocated_area = st.number_input("Allocated Area Ratio", min_value=0.0, max_value=1.0, value=0.06, step=0.01)
product_perishable = st.selectbox("Perishable?", options=[0, 1], format_func=lambda x: "Yes" if x == 1 else "No")
with col2:
product_sugar = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
product_type = st.selectbox("Product Type", [
'Fruits and Vegetables', 'Snack Foods', 'Household', 'Frozen Foods',
'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Soft Drinks',
'Meat', 'Breads', 'Hard Drinks', 'Others', 'Starchy Foods',
'Breakfast', 'Seafood'
])
st.subheader("Store Details")
col3, col4 = st.columns(2)
with col3:
store_id = st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004"])
store_type = st.selectbox("Store Type", [
"Supermarket Type1", "Supermarket Type2",
"Departmental Store", "Food Mart"
])
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
with col4:
store_location = st.selectbox("City Tier", ["Tier 1", "Tier 2", "Tier 3"])
store_age = st.number_input("Store Age (years)", min_value=1, max_value=100, value=20)
submitted = st.form_submit_button("Forecast Sales 🔮")
# --- Prediction ---
if submitted:
payload = {
"Product_Weight": product_weight,
"Product_Sugar_Content": product_sugar,
"Product_Allocated_Area": product_allocated_area,
"Product_Type": product_type,
"Product_MRP": product_mrp,
"Store_Id": store_id,
"Store_Size": store_size,
"Store_Location_City_Type": store_location,
"Store_Type": store_type,
"Store_Age": store_age,
"Product_Perishable": product_perishable
}
with st.spinner("Forecasting..."):
try:
response = requests.post(BACKEND_URL, json=payload, timeout=30)
result = response.json()
if result['status'] == 'success':
st.success(f"### Predicted Sales: **${result['predicted_sales']:,.2f}**")
st.json(payload)
else:
st.error(f"Prediction failed: {result.get('error', 'Unknown error')}")
except Exception as e:
st.error(f"Could not reach the backend API: {str(e)}")