File size: 2,693 Bytes
6ac22d7
 
 
 
 
 
 
86f232f
 
 
83855bc
6ac22d7
 
89d166f
6ac22d7
83855bc
6ac22d7
83855bc
6ac22d7
 
 
 
 
86f232f
6ac22d7
 
83855bc
6ac22d7
86f232f
6ac22d7
86f232f
89d166f
 
83855bc
89d166f
86f232f
89d166f
 
 
6ac22d7
83855bc
89d166f
86f232f
6ac22d7
 
83855bc
 
 
 
 
6ac22d7
 
86f232f
83855bc
6ac22d7
86f232f
83855bc
 
 
6ac22d7
86f232f
 
83855bc
 
 
86f232f
83855bc
89d166f
86f232f
83855bc
6ac22d7
86f232f
89d166f
86f232f
89d166f
 
 
86f232f
89d166f
86f232f
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
import streamlit as st
import pandas as pd
import numpy as np
import xgboost as xgb
import pickle
import datetime

# 1. MUST BE THE FIRST STREAMLIT COMMAND
st.set_page_config(page_title="Kaggle Sticker Sales Forecast", layout="centered")

# --- ASSET LOADING ---
@st.cache_resource
def load_assets():
    # Load the XGBoost model
    model = xgb.XGBRegressor()
    model.load_model("model.ubj") 
    
    # Load the exact column list from your training phase
    with open("model_columns.pkl", "rb") as f:
        model_columns = pickle.load(f)
        
    return model, model_columns

# Load model and columns
model, model_columns = load_assets()

# --- INTERFACE ---
st.title("📊 Sticker Sales Forecasting")
st.write("Predict sales volume based on historical training data patterns.")

# --- INPUT SECTION ---
with st.sidebar:
    st.header("Input Features")
    date = st.date_input("Select Date", datetime.date(2026, 5, 4))
    
    # Categories based on your data summary
    country = st.selectbox("Country", ["Finland", "Italy", "Singapore", "Norway", "Canada", "Kenya"])
    store = st.selectbox("Store", ["Premium Sticker Mart", "Stickers for Less", "Discount Stickers"])
    product = st.selectbox("Product", ["Kaggle", "Kaggle Tiers", "Kerneler Dark Mode", "Kerneler", "Holographic Goose"])

# --- PREDICTION LOGIC ---
if st.button("Predict Units Sold"):
    # 1. Feature Engineering (Match your training steps)
    input_date = pd.to_datetime(date)
    data = {
        'year': input_date.year,
        'month': input_date.month,
        'day': input_date.day,
        'dayofweek': input_date.dayofweek,
        'is_weekend': 1 if input_date.dayofweek >= 5 else 0
    }
    
    # 2. Build the full feature row using the loaded columns
    input_df = pd.DataFrame(0, index=[0], columns=model_columns)
    
    # 3. Fill Time Features
    for key, value in data.items():
        if key in input_df.columns:
            input_df.at[0, key] = value
            
    # 4. Fill One-Hot Features
    for feat in [f"country_{country}", f"store_{store}", f"product_{product}"]:
        if feat in input_df.columns:
            input_df.at[0, feat] = 1

    # 5. Model Inference
    raw_pred = model.predict(input_df)
    
    # Reverse log transformation if used during training (np.expm1)
    final_prediction = np.expm1(raw_pred)[0]

    # --- RESULTS ---
    st.markdown("---")
    if final_prediction < 0: final_prediction = 0
    st.metric(label="Predicted Units Sold", value=f"{int(final_prediction)}")
    
    if final_prediction > 0:
        st.success("Calculation complete.")
    else:
        st.warning("The model returned a near-zero prediction. Check feature alignment.")