Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ import xgboost as xgb
|
|
| 5 |
import pickle
|
| 6 |
import datetime
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# --- ASSET LOADING ---
|
| 9 |
@st.cache_resource
|
| 10 |
def load_assets():
|
|
@@ -18,26 +21,26 @@ def load_assets():
|
|
| 18 |
|
| 19 |
return model, model_columns
|
| 20 |
|
|
|
|
| 21 |
model, model_columns = load_assets()
|
| 22 |
|
| 23 |
# --- INTERFACE ---
|
| 24 |
-
st.set_page_config(page_title="Kaggle Sticker Sales Forecast", layout="centered")
|
| 25 |
st.title("📊 Sticker Sales Forecasting")
|
| 26 |
-
st.write("
|
| 27 |
|
| 28 |
-
# --- INPUT SECTION
|
| 29 |
with st.sidebar:
|
| 30 |
st.header("Input Features")
|
| 31 |
date = st.date_input("Select Date", datetime.date(2026, 5, 4))
|
| 32 |
|
| 33 |
-
#
|
| 34 |
country = st.selectbox("Country", ["Finland", "Italy", "Singapore", "Norway", "Canada", "Kenya"])
|
| 35 |
store = st.selectbox("Store", ["Premium Sticker Mart", "Stickers for Less", "Discount Stickers"])
|
| 36 |
product = st.selectbox("Product", ["Kaggle", "Kaggle Tiers", "Kerneler Dark Mode", "Kerneler", "Holographic Goose"])
|
| 37 |
|
| 38 |
# --- PREDICTION LOGIC ---
|
| 39 |
if st.button("Predict Units Sold"):
|
| 40 |
-
# 1.
|
| 41 |
input_date = pd.to_datetime(date)
|
| 42 |
data = {
|
| 43 |
'year': input_date.year,
|
|
@@ -47,42 +50,31 @@ if st.button("Predict Units Sold"):
|
|
| 47 |
'is_weekend': 1 if input_date.dayofweek >= 5 else 0
|
| 48 |
}
|
| 49 |
|
| 50 |
-
# 2. Build the full feature row
|
| 51 |
input_df = pd.DataFrame(0, index=[0], columns=model_columns)
|
| 52 |
|
| 53 |
-
# 3.
|
| 54 |
for key, value in data.items():
|
| 55 |
if key in input_df.columns:
|
| 56 |
input_df.at[0, key] = value
|
| 57 |
|
| 58 |
-
# 4.
|
| 59 |
-
|
| 60 |
-
country_col = f"country_{country}"
|
| 61 |
-
store_col = f"store_{store}"
|
| 62 |
-
product_col = f"product_{product}"
|
| 63 |
-
|
| 64 |
-
for feat in [country_col, store_col, product_col]:
|
| 65 |
if feat in input_df.columns:
|
| 66 |
input_df.at[0, feat] = 1
|
| 67 |
-
else:
|
| 68 |
-
# Troubleshooting tip: If a column isn't found, it prints to the HF logs
|
| 69 |
-
print(f"Warning: {feat} not found in model_columns.pkl")
|
| 70 |
|
| 71 |
-
# 5.
|
| 72 |
raw_pred = model.predict(input_df)
|
| 73 |
|
| 74 |
-
#
|
| 75 |
final_prediction = np.expm1(raw_pred)[0]
|
| 76 |
|
| 77 |
-
# --- RESULTS
|
| 78 |
-
if final_prediction < 0: final_prediction = 0
|
| 79 |
-
|
| 80 |
st.markdown("---")
|
| 81 |
-
|
| 82 |
st.metric(label="Predicted Units Sold", value=f"{int(final_prediction)}")
|
| 83 |
|
| 84 |
-
# Visual feedback
|
| 85 |
if final_prediction > 0:
|
| 86 |
-
st.success("
|
| 87 |
else:
|
| 88 |
-
st.warning("The model
|
|
|
|
| 5 |
import pickle
|
| 6 |
import datetime
|
| 7 |
|
| 8 |
+
# 1. MUST BE THE FIRST STREAMLIT COMMAND
|
| 9 |
+
st.set_page_config(page_title="Kaggle Sticker Sales Forecast", layout="centered")
|
| 10 |
+
|
| 11 |
# --- ASSET LOADING ---
|
| 12 |
@st.cache_resource
|
| 13 |
def load_assets():
|
|
|
|
| 21 |
|
| 22 |
return model, model_columns
|
| 23 |
|
| 24 |
+
# Load model and columns
|
| 25 |
model, model_columns = load_assets()
|
| 26 |
|
| 27 |
# --- INTERFACE ---
|
|
|
|
| 28 |
st.title("📊 Sticker Sales Forecasting")
|
| 29 |
+
st.write("Predict sales volume based on historical training data patterns.")
|
| 30 |
|
| 31 |
+
# --- INPUT SECTION ---
|
| 32 |
with st.sidebar:
|
| 33 |
st.header("Input Features")
|
| 34 |
date = st.date_input("Select Date", datetime.date(2026, 5, 4))
|
| 35 |
|
| 36 |
+
# Categories based on your data summary
|
| 37 |
country = st.selectbox("Country", ["Finland", "Italy", "Singapore", "Norway", "Canada", "Kenya"])
|
| 38 |
store = st.selectbox("Store", ["Premium Sticker Mart", "Stickers for Less", "Discount Stickers"])
|
| 39 |
product = st.selectbox("Product", ["Kaggle", "Kaggle Tiers", "Kerneler Dark Mode", "Kerneler", "Holographic Goose"])
|
| 40 |
|
| 41 |
# --- PREDICTION LOGIC ---
|
| 42 |
if st.button("Predict Units Sold"):
|
| 43 |
+
# 1. Feature Engineering (Match your training steps)
|
| 44 |
input_date = pd.to_datetime(date)
|
| 45 |
data = {
|
| 46 |
'year': input_date.year,
|
|
|
|
| 50 |
'is_weekend': 1 if input_date.dayofweek >= 5 else 0
|
| 51 |
}
|
| 52 |
|
| 53 |
+
# 2. Build the full feature row using the loaded columns
|
| 54 |
input_df = pd.DataFrame(0, index=[0], columns=model_columns)
|
| 55 |
|
| 56 |
+
# 3. Fill Time Features
|
| 57 |
for key, value in data.items():
|
| 58 |
if key in input_df.columns:
|
| 59 |
input_df.at[0, key] = value
|
| 60 |
|
| 61 |
+
# 4. Fill One-Hot Features
|
| 62 |
+
for feat in [f"country_{country}", f"store_{store}", f"product_{product}"]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if feat in input_df.columns:
|
| 64 |
input_df.at[0, feat] = 1
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# 5. Model Inference
|
| 67 |
raw_pred = model.predict(input_df)
|
| 68 |
|
| 69 |
+
# Reverse log transformation if used during training (np.expm1)
|
| 70 |
final_prediction = np.expm1(raw_pred)[0]
|
| 71 |
|
| 72 |
+
# --- RESULTS ---
|
|
|
|
|
|
|
| 73 |
st.markdown("---")
|
| 74 |
+
if final_prediction < 0: final_prediction = 0
|
| 75 |
st.metric(label="Predicted Units Sold", value=f"{int(final_prediction)}")
|
| 76 |
|
|
|
|
| 77 |
if final_prediction > 0:
|
| 78 |
+
st.success("Calculation complete.")
|
| 79 |
else:
|
| 80 |
+
st.warning("The model returned a near-zero prediction. Check feature alignment.")
|