Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import xgboost as xgb
|
| 5 |
-
import joblib
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
|
| 8 |
-
# Set page config for a professional look
|
| 9 |
-
st.set_page_config(page_title="Sales Forecaster", layout="centered")
|
| 10 |
-
|
| 11 |
-
@st.cache_resource
|
| 12 |
-
def load_assets():
|
| 13 |
-
# Initialize the Regressor (Must match the training model type)
|
| 14 |
-
model = xgb.XGBRegressor()
|
| 15 |
-
model.load_model("xgboost_model.json")
|
| 16 |
-
|
| 17 |
-
# Load the column list we saved during training
|
| 18 |
-
model_columns = joblib.load("model_columns.pkl")
|
| 19 |
-
return model, model_columns
|
| 20 |
-
|
| 21 |
-
# Load assets
|
| 22 |
-
try:
|
| 23 |
-
model, model_columns = load_assets()
|
| 24 |
-
except Exception as e:
|
| 25 |
-
st.error(f"Error loading model assets: {e}")
|
| 26 |
-
st.stop()
|
| 27 |
-
|
| 28 |
-
st.title("π Kaggle Sticker Sales Predictor")
|
| 29 |
-
st.markdown("""
|
| 30 |
-
This application predicts sales volume for stickers, mugs, and t-shirts across various European stores.
|
| 31 |
-
**Current Model MAPE:** 6.42%
|
| 32 |
-
""")
|
| 33 |
-
|
| 34 |
-
# User Inputs
|
| 35 |
-
with st.sidebar:
|
| 36 |
-
st.header("Input Parameters")
|
| 37 |
-
date = st.date_input("Select Date", datetime(2022, 1, 1))
|
| 38 |
-
country = st.selectbox("Country", ["Finland", "Italy", "Norway", "Spain", "Sweden"])
|
| 39 |
-
store = st.selectbox("Store", ["Kagglart", "Kaggle Rama"])
|
| 40 |
-
product = st.selectbox("Product", ["Kaggle Mug", "Kaggle Sticker", "Kaggle T-shirt"])
|
| 41 |
-
|
| 42 |
-
# Prediction Logic
|
| 43 |
-
if st.button("Predict Sales"):
|
| 44 |
-
# 1. Create a dataframe from input
|
| 45 |
-
input_data = pd.DataFrame({
|
| 46 |
-
'date': [pd.to_datetime(date)],
|
| 47 |
-
'country': [country],
|
| 48 |
-
'store': [store],
|
| 49 |
-
'product': [product]
|
| 50 |
-
})
|
| 51 |
-
|
| 52 |
-
# 2. Feature Engineering (Exact same as training)
|
| 53 |
-
input_data['year'] = input_data['date'].dt.year
|
| 54 |
-
input_data['month'] = input_data['date'].dt.month
|
| 55 |
-
input_data['day'] = input_data['date'].dt.day
|
| 56 |
-
input_data['dayofweek'] = input_data['date'].dt.dayofweek
|
| 57 |
-
input_data['is_weekend'] = input_data['dayofweek'].apply(lambda x: 1 if x >= 5 else 0)
|
| 58 |
-
|
| 59 |
-
# 3. One-Hot Encoding
|
| 60 |
-
# This creates columns like 'country_Italy', 'store_Kagglart', etc.
|
| 61 |
-
input_data = pd.get_dummies(input_data, columns=['country', 'store', 'product'])
|
| 62 |
-
|
| 63 |
-
# 4. Align with training columns (The "model_columns" check)
|
| 64 |
-
# This ensures columns are in the right order and missing dummies are set to 0
|
| 65 |
-
final_input = pd.DataFrame(columns=model_columns)
|
| 66 |
-
for col in model_columns:
|
| 67 |
-
if col in input_data.columns:
|
| 68 |
-
final_input.loc[0, col] = input_data.loc[0, col]
|
| 69 |
-
else:
|
| 70 |
-
final_input.loc[0, col] = 0
|
| 71 |
-
|
| 72 |
-
# Ensure all columns are numeric
|
| 73 |
-
final_input = final_input.astype(float)
|
| 74 |
-
|
| 75 |
-
# 5. Prediction (Reverse the Log Transformation)
|
| 76 |
-
# The model predicts log(1+sales), so we use expm1 to get the real number
|
| 77 |
-
log_pred = model.predict(final_input)
|
| 78 |
-
prediction = np.expm1(log_pred)[0]
|
| 79 |
-
|
| 80 |
-
# Result Display
|
| 81 |
-
st.divider()
|
| 82 |
-
cols = st.columns(2)
|
| 83 |
-
cols[0].metric("Predicted Sales", f"{int(np.round(prediction))} units")
|
| 84 |
-
|
| 85 |
-
# Contextual Insights
|
| 86 |
-
if input_data['is_weekend'].values[0] == 1:
|
| 87 |
-
st.info("π
**Weekend Effect:** Sales are typically higher on this day.")
|
| 88 |
-
|
| 89 |
-
if input_data['month'].values[0] == 12:
|
| 90 |
-
st.warning("π **Holiday Season:** Expect high volatility in December sales.")
|
| 91 |
-
|
| 92 |
-
st.markdown("---")
|
| 93 |
-
st.caption("Developed by Basak Tamer | Data Science Portfolio Project")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|