File size: 2,877 Bytes
1522e41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
os.environ["STREAMLIT_SERVER_HEADLESS"] = "true"

import streamlit as st
import pandas as pd
from huggingface_hub import hf_hub_download
import joblib

# ---- Streamlit bootstrap ----
st.empty()
st.set_page_config(page_title="SuperKart Sales Prediction")
st.set_option("browser.gatherUsageStats", False)

# ---- Read secrets ----
Repo_ID = os.getenv("Repo_ID")
HF_TOKEN = os.getenv("HF_TOKEN")

if not Repo_ID:
    st.error("❌ Repo_ID secret is missing in HF Space")
    st.stop()

# ---- Render UI immediately ----
st.title("🛒 SuperKart Sales Prediction")
st.write("✅ UI rendered successfully")

# ---- Load model lazily ----
@st.cache_resource
def load_model():
    model_path = hf_hub_download(
        repo_id=Repo_ID,
        filename="best_superkart_sales_model_v1.joblib",
        repo_type="model",
        token=HF_TOKEN
    )
    return joblib.load(model_path)

# ---- Load model AFTER UI ----
try:
    with st.spinner("Loading ML model…"):
        model = load_model()
        st.success("✅ Model loaded successfully")
except Exception as e:
    st.error("❌ Model failed to load")
    st.exception(e)
    st.stop()

# ---- UI ----
st.write("""
This application predicts the **total product sales** for SuperKart
based on product characteristics and store attributes.
""")

product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
product_type = st.selectbox("Product Type", [
    "Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables",
    "Baking Goods", "Frozen Foods", "Health and Hygiene",
    "Canned", "Household", "Snack Foods", "Others"
])
store_id = st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004", "OUT005"])
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
store_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
store_type = st.selectbox("Store Type", ["Grocery Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"])

product_weight = st.number_input("Product Weight (kg)", 0.1, 50.0, 10.0)
product_allocated_area = st.number_input("Product Allocated Area", 0.001, 1.0, 0.05, step=0.001)
product_mrp = st.number_input("Product MRP", 1.0, 1000.0, 100.0)
store_est_year = st.number_input("Store Establishment Year", 1950, 2025, 2005)

input_data = pd.DataFrame([{
    "Product_Weight": product_weight,
    "Product_Allocated_Area": product_allocated_area,
    "Product_MRP": product_mrp,
    "Store_Establishment_Year": store_est_year,
    "Product_Sugar_Content": product_sugar_content,
    "Product_Type": product_type,
    "Store_Id": store_id,
    "Store_Size": store_size,
    "Store_Location_City_Type": store_city_type,
    "Store_Type": store_type
}])

if st.button("Predict Sales"):
    prediction = model.predict(input_data)[0]
    st.success(f"Estimated Product Sales: **₹ {prediction:,.2f}**")