SuperKart-App / streamlit_app.py
dhani10's picture
Update streamlit_app.py
43fee50 verified
import streamlit as st
import pandas as pd
import numpy as np
import requests
# Hugging Face backend endpoint
BACKEND_URL = "https://dhani10-SuperKart-Backend.hf.space/predict"
# Load dropdown values from SuperKart.csv
@st.cache_data
def load_dropdown_data():
df = pd.read_csv("SuperKart.csv")
# product_ids = sorted(df['Product_Id'].dropna().unique())
# store_ids = sorted(df['Store_Id'].dropna().unique())
# years = sorted(df['Store_Establishment_Year'].dropna().unique())
product_ids = sorted(df['Product_Id'].dropna().astype(str).unique())
store_ids = sorted(df['Store_Id'].dropna().astype(str).unique())
years = sorted(df['Store_Establishment_Year'].dropna().astype(int).unique())
return product_ids, store_ids, years, df
# Page Title
st.title('SuperKart Sales Forecasting')
# Load CSV data
product_ids, store_ids, est_years, df = load_dropdown_data()
# Input Form
with st.form("prediction_form"):
st.header("Product & Store Details")
col1, col2 = st.columns(2)
with col1:
product_id = st.selectbox("Product ID", options=product_ids)
product_weight = st.number_input("Product Weight", min_value=6.0, max_value=500.0)
sugar_content = st.selectbox("Product Sugar Content", ["No Sugar", "Low Sugar", "Reg", "Regular"])
allocated_area = st.number_input("Allocated Area Ratio", min_value=0.0, max_value=0.03)
product_type = st.selectbox("Product Type", [
"Baking Goods", "Bread", "Breakfast", "Canned", "Dairy",
"Frozen Foods", "Fruits and Vegetables", "Hard Drinks",
"Health and Hygiene", "Household", "Meat", "Others",
"Seafood", "Snack Foods", "Soft Drinks", "Starchy Foods"])
mrp = st.number_input("MRP ($)", min_value=0.0, value=5.99)
with col2:
store_id = st.selectbox("Store ID", options=store_ids)
establishment_year = st.selectbox("Establishment Year", options=est_years)
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
city_type = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"])
store_type = st.selectbox("Store Type", [
"Departmental Store", "Supermarket Type 1",
"Supermarket Type 2", "Food Mart"])
submit = st.form_submit_button("Predict Sales")
# Prediction logic
if submit:
# data = {
# "Product_Id": product_id,
# "Product_Weight": product_weight,
# "Product_Sugar_Content": sugar_content,
# "Product_Allocated_Area": allocated_area,
# "Product_Type": product_type,
# "Product_MRP": mrp,
# "Store_Id": store_id,
# "Store_Establishment_Year": establishment_year,
# "Store_Size": store_size,
# "Store_Location_City_Type": city_type,
# "Store_Type": store_type
# }
data = {
"Product_Id": str(product_id),
"Product_Weight": float(product_weight),
"Product_Sugar_Content": str(sugar_content),
"Product_Allocated_Area": float(allocated_area),
"Product_Type": str(product_type),
"Product_MRP": float(mrp),
"Store_Id": str(store_id),
"Store_Establishment_Year": int(establishment_year), # FIXED LINE
"Store_Size": str(store_size),
"Store_Location_City_Type": str(city_type),
"Store_Type": str(store_type)
}
try:
response = requests.post(BACKEND_URL, json=data)
if response.status_code == 200:
prediction = response.json().get('prediction')
if prediction is not None:
st.success(f"**Predicted Sales:** ${prediction:,.2f}")
st.subheader("Inventory Recommendations")
if prediction > 1000:
st.info("โœ… **High-demand product** - Increase stock by 30%")
elif prediction > 500:
st.info("๐Ÿ”„ **Moderate-demand product** - Maintain current stock levels")
else:
st.info("โš ๏ธ **Low-demand product** - Reduce stock by 20%")
else:
st.error("Unexpected response from backend: 'prediction' key missing")
else:
st.error(f"Backend error: {response.status_code} - {response.text}")
except Exception as e:
st.error(f"Connection error: {str(e)}")
# Business insights section
st.markdown("---")
st.header("Business Insights")
st.write("""
- **Premium products** in Tier 1 cities show 35% higher sales
- **Health & Hygiene** category growing at 18% YoY
- **Supermarket Type 1** stores have highest sales/sq.ft
- Reduce waste by 22% using accurate forecasts
""")
# Optional: Show sample data
if st.checkbox("Show Product Database"):
st.dataframe(df[['Product_Id', 'Product_Type', 'Product_Weight', 'Product_MRP']].drop_duplicates().head(10))
if st.checkbox("Show Store Database"):
st.dataframe(df[['Store_Id', 'Store_Location_City_Type', 'Store_Size', 'Store_Type']].drop_duplicates().head(10))