basaktamer commited on
Commit
6ac22d7
·
verified ·
1 Parent(s): 8031c41

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import xgboost as xgb
5
+ import pickle
6
+ import datetime
7
+
8
+ # --- PAGE CONFIG ---
9
+ st.set_page_config(page_title="Sticker Sales Predictor", layout="centered")
10
+
11
+ @st.cache_resource
12
+ def load_assets():
13
+ # Load the XGBoost model from the .ubj file
14
+ model = xgb.XGBRegressor()
15
+ model.load_model("model.ubj")
16
+
17
+ # Load the exact column names used during training
18
+ with open("model_columns.pkl", "rb") as f:
19
+ model_columns = pickle.load(f)
20
+
21
+ return model, model_columns
22
+
23
+ model, model_columns = load_assets()
24
+
25
+ # --- UI ELEMENTS ---
26
+ st.title("📊 Sticker Sales Forecasting")
27
+ st.write("Predict the number of stickers sold based on date, location, and product type.")
28
+
29
+ # --- INPUT SECTION ---
30
+ with st.container():
31
+ col1, col2 = st.columns(2)
32
+
33
+ with col1:
34
+ date = st.date_input("Select Date", datetime.date(2026, 5, 4))
35
+ country = st.selectbox("Country", ["Finland", "Italy", "Norway"])
36
+
37
+ with col2:
38
+ store = st.selectbox("Store", ["KaggleMart", "KaggleRama"])
39
+ product = st.selectbox("Product", ["Kaggle Mug", "Kaggle Hat", "Kaggle Sticker"])
40
+
41
+ # --- PREPROCESSING ---
42
+ def get_prediction():
43
+ # Create initial DataFrame
44
+ input_date = pd.to_datetime(date)
45
+ data = {
46
+ 'year': [input_date.year],
47
+ 'month': [input_date.month],
48
+ 'day': [input_date.day],
49
+ 'dayofweek': [input_date.dayofweek],
50
+ 'is_weekend': [1 if input_date.dayofweek >= 5 else 0]
51
+ }
52
+
53
+ df = pd.DataFrame(data)
54
+
55
+ # Apply One-Hot Encoding manually to match training columns
56
+ # We create a dataframe of zeros with the same columns as the model
57
+ features_df = pd.DataFrame(0, index=[0], columns=model_columns)
58
+
59
+ # Fill in the time features
60
+ for col in data.keys():
61
+ if col in features_df.columns:
62
+ features_df[col] = data[col]
63
+
64
+ # Set the One-Hot features to 1
65
+ country_col = f"country_{country}"
66
+ store_col = f"store_{store}"
67
+ product_col = f"product_{product}"
68
+
69
+ for col in [country_col, store_col, product_col]:
70
+ if col in features_df.columns:
71
+ features_df[col] = 1
72
+
73
+ # Ensure column order is EXACTLY as it was in model_columns.pkl
74
+ features_df = features_df[model_columns]
75
+
76
+ # Predict and inverse the log transformation (np.expm1)
77
+ prediction_log = model.predict(features_df)
78
+ return np.expm1(prediction_log)[0]
79
+
80
+ # --- RESULTS ---
81
+ if st.button("Predict Sales"):
82
+ result = get_prediction()
83
+ st.metric(label="Predicted Units Sold", value=f"{int(result)}")
84
+ st.balloons()