File size: 3,931 Bytes
43f8a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94

import streamlit as st
import pandas as pd
import joblib
import numpy as np

# --- Streamlit Page Configuration ---
st.set_page_config(
    page_title="SuperKart Sales Forecaster",
    page_icon="๐Ÿ›’",
    layout="wide"
)
# Load the trained model pipeline
# The @st.cache_resource decorator ensures the model is loaded only once
@st.cache_resource
def load_model():
    """Loads the serialized model pipeline from disk."""
    # CORRECTED LINE: Changed the filename to match your model
    return joblib.load("superkart_prediction_model_v1_0.joblib")

# Load the model
model = load_model()



# --- App Title and Description ---
st.title("๐Ÿ›’ SuperKart Sales Forecaster")
st.markdown("This application predicts the total sales revenue for a product in a specific store based on its characteristics.")

# --- Helper lists for dropdown menus ---
PRODUCT_TYPES = sorted(['Snack Foods', 'Meat', 'Fruits and Vegetables', 'Household', 'Baking Goods', 'Frozen Foods', 'Dairy', 'Canned', 'Health and Hygiene', 'Soft Drinks', 'Breads', 'Hard Drinks', 'Others', 'Starchy Foods', 'Breakfast', 'Seafood'])
STORE_TYPES = sorted(['Supermarket Type1', 'Supermarket Type2', 'Departmental Store', 'Food Mart'])
STORE_LOCATIONS = sorted(['Tier 1', 'Tier 2', 'Tier 3'])
SUGAR_CONTENT_OPTIONS = ['Low Sugar', 'Regular']
STORE_SIZE_OPTIONS = ['Small', 'Medium', 'High']

# --- Main Prediction Form ---
with st.form("prediction_form"):
    st.header("Enter Product and Store Details")

    # Create columns for a cleaner layout
    col1, col2, col3 = st.columns(3)

    with col1:
        st.subheader("Product Details")
        product_type = st.selectbox("Product Type", options=PRODUCT_TYPES)
        product_weight = st.slider("Product Weight", min_value=4.0, max_value=22.0, value=12.5, step=0.1)
        product_sugar_content = st.selectbox("Product Sugar Content", options=SUGAR_CONTENT_OPTIONS)

    with col2:
        st.subheader("Pricing and Display")
        product_mrp = st.slider("Product MRP ($)", min_value=30.0, max_value=270.0, value=140.0, step=0.5)
        product_allocated_area = st.slider("Product Allocated Area (Ratio)", min_value=0.0, max_value=0.30, value=0.07, step=0.001, format="%.3f")

    with col3:
        st.subheader("Store Details")
        store_type = st.selectbox("Store Type", options=STORE_TYPES)
        store_size = st.selectbox("Store Size", options=STORE_SIZE_OPTIONS)
        store_location_city_type = st.selectbox("Store Location City Type", options=STORE_LOCATIONS)
        # Corrected the key in the input_data dictionary to match the model's feature name
        store_age = st.slider("Store Age (in years)", min_value=16, max_value=38, value=22, step=1)

    # Submit button for the form
    submitted = st.form_submit_button("Predict Sales Revenue")

# --- Prediction Logic ---
if submitted:
    # Create a DataFrame from the user inputs
    # The column names must match exactly what the model was trained on
    input_data = pd.DataFrame([{
        'Product_Weight': product_weight,
        'Product_Sugar_Content': product_sugar_content,
        'Product_Allocated_Area': product_allocated_area,
        'Product_Type': product_type,
        'Product_MRP': product_mrp,
        'Store_Size': store_size,
        'Store_Location_City_Type': store_location_city_type,
        'Store_Type': store_type,
        # The key here was 'Store_Age' in your previous code, but your feature list shows 'store_age'.
        # Python is case-sensitive, so it's safer to match the feature list exactly.
        'store_age': store_age
    }])

    try:
        # Use the loaded pipeline to make a prediction
        with st.spinner("Forecasting..."):
            prediction = model.predict(input_data)

            # Display the prediction
            st.success(f"**Predicted Sales Revenue: ${prediction[0]:,.2f}**")

    except Exception as e:
        st.error(f"An error occurred during prediction: {e}")