Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -5,12 +5,19 @@ import joblib
|
|
| 5 |
# Load model and data
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model_and_data():
|
| 8 |
-
model = joblib.load("Quarterly_Sales_Revenue_model_v1_0.joblib")
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
return model, data
|
| 11 |
|
| 12 |
# Prediction logic
|
| 13 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
feature_cols = [
|
| 15 |
'Product_Id', 'Store_Id', 'Product_Type', 'Product_Sugar_Content',
|
| 16 |
'Store_Type', 'Store_Location_City_Type', 'Store_Size',
|
|
@@ -18,31 +25,60 @@ def predict_total_sales_by_store(store_ids, df, model):
|
|
| 18 |
'Store_Establishment_Year', 'Total_Historical_Sales',
|
| 19 |
'Num_Past_Transactions', 'Average_Historical_Sales'
|
| 20 |
]
|
| 21 |
-
|
| 22 |
for store_id in store_ids:
|
| 23 |
store_df = df[df['Store_Id'] == store_id].copy()
|
|
|
|
| 24 |
if store_df.empty:
|
| 25 |
-
|
| 26 |
continue
|
|
|
|
| 27 |
X_input = store_df[feature_cols]
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
model, df = load_model_and_data()
|
| 34 |
|
| 35 |
# UI
|
| 36 |
-
st.title("
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
if not store_ids:
|
| 41 |
st.warning("Please select at least one Store ID.")
|
| 42 |
else:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Load model and data
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model_and_data():
|
| 8 |
+
model = joblib.load("deployment_files/Quarterly_Sales_Revenue_model_v1_0.joblib")
|
| 9 |
+
#model = joblib.load("Quarterly_Sales_Revenue_model_v1_0.joblib")
|
| 10 |
+
|
| 11 |
+
data = pd.read_csv("deployment_files/SuperKart_local.csv")
|
| 12 |
+
|
| 13 |
return model, data
|
| 14 |
|
| 15 |
# Prediction logic
|
| 16 |
+
def predict_quarterly_sales_by_store_and_product(store_ids, df, model):
|
| 17 |
+
store_results = []
|
| 18 |
+
product_results = []
|
| 19 |
+
total_sales = 0.0
|
| 20 |
+
|
| 21 |
feature_cols = [
|
| 22 |
'Product_Id', 'Store_Id', 'Product_Type', 'Product_Sugar_Content',
|
| 23 |
'Store_Type', 'Store_Location_City_Type', 'Store_Size',
|
|
|
|
| 25 |
'Store_Establishment_Year', 'Total_Historical_Sales',
|
| 26 |
'Num_Past_Transactions', 'Average_Historical_Sales'
|
| 27 |
]
|
| 28 |
+
|
| 29 |
for store_id in store_ids:
|
| 30 |
store_df = df[df['Store_Id'] == store_id].copy()
|
| 31 |
+
|
| 32 |
if store_df.empty:
|
| 33 |
+
store_results.append((store_id, 0.0))
|
| 34 |
continue
|
| 35 |
+
|
| 36 |
X_input = store_df[feature_cols]
|
| 37 |
+
y_pred = model.predict(X_input)
|
| 38 |
+
|
| 39 |
+
store_total_sales = sum(y_pred)
|
| 40 |
+
store_results.append((store_id, round(store_total_sales, 2)))
|
| 41 |
+
total_sales += store_total_sales
|
| 42 |
|
| 43 |
+
for product_id, sales in zip(store_df['Product_Id'], y_pred):
|
| 44 |
+
product_results.append((store_id, product_id, round(sales, 2)))
|
| 45 |
+
|
| 46 |
+
return store_results, product_results, round(total_sales, 2)
|
| 47 |
+
|
| 48 |
+
# Load model and data
|
| 49 |
model, df = load_model_and_data()
|
| 50 |
|
| 51 |
# UI
|
| 52 |
+
st.title("π Predict Quarterly Sales by Store and Product")
|
| 53 |
+
|
| 54 |
+
# Dropdowns
|
| 55 |
+
store_ids = st.multiselect("π¬ Select Store ID(s)", sorted(df["Store_Id"].unique()))
|
| 56 |
+
product_ids = st.multiselect("π¦ Select Product ID(s) (optional)", sorted(df["Product_Id"].unique()))
|
| 57 |
|
| 58 |
+
# Predict button
|
| 59 |
+
if st.button("Predict Quarterly Sales"):
|
| 60 |
if not store_ids:
|
| 61 |
st.warning("Please select at least one Store ID.")
|
| 62 |
else:
|
| 63 |
+
store_results, product_results, total_sales = predict_quarterly_sales_by_store_and_product(store_ids, df, model)
|
| 64 |
+
|
| 65 |
+
# Display Store-wise sales
|
| 66 |
+
st.subheader("πͺ Store-wise Total Quarterly Sales")
|
| 67 |
+
for store, sales in store_results:
|
| 68 |
+
st.write(f"π Store {store}: βΉ{sales:,.2f}")
|
| 69 |
+
|
| 70 |
+
# Display Product-wise sales (optional filter)
|
| 71 |
+
st.subheader("π¦ Product-wise Sales (Filtered by Selected Stores)")
|
| 72 |
+
filtered_products = product_results
|
| 73 |
+
if product_ids:
|
| 74 |
+
filtered_products = [r for r in product_results if r[1] in product_ids]
|
| 75 |
+
|
| 76 |
+
if filtered_products:
|
| 77 |
+
product_df = pd.DataFrame(filtered_products, columns=["Store_Id", "Product_Id", "Predicted_Sales"])
|
| 78 |
+
st.dataframe(product_df)
|
| 79 |
+
else:
|
| 80 |
+
st.info("No product sales to display for selected filters.")
|
| 81 |
|
| 82 |
+
# Display total sales
|
| 83 |
+
st.subheader("π Total Quarterly Sales Across All Selected Stores")
|
| 84 |
+
st.success(f"βΉ{total_sales:,.2f}")
|