| import streamlit as st |
| import pandas as pd |
| import requests |
| import json |
|
|
|
|
| BACKEND_URL = "https://Himadri1102-SuperKart-Model-Backend11.hf.space" |
| SINGLE_PREDICT_ENDPOINT = f"{BACKEND_URL}/v1/sales" |
| BATCH_PREDICT_ENDPOINT = f"{BACKEND_URL}/v1/salesbatch" |
|
|
| |
| st.title("SuperKart Sales Revenue Forecast") |
| st.markdown("Predicts the total sales revenue for a specific product and store combination.") |
|
|
| st.subheader("1. Product and Store Inputs") |
| st.markdown("⚠️ **WARNING**: The model requires ALL 29+ features (scaled/encoded). This UI collects only key features and defaults the rest for demonstration.") |
|
|
| |
| product_mrp = st.number_input("Product MRP (Maximum Retail Price)", min_value=30.0, max_value=300.0, value=150.0, step=5.0) |
| product_weight = st.number_input("Product Weight", min_value=4.0, max_value=22.0, value=12.0, step=0.1) |
| store_age = st.number_input("Store Age (Years)", min_value=5, max_value=40, value=20) |
| store_size = st.selectbox("Store Size", ["Small", "Medium", "High"]) |
|
|
| |
|
|
| |
| store_size_encoded = {'Small': 0, 'Medium': 1, 'High': 2}[store_size] |
|
|
|
|
| def create_payload(mrp, weight, age, size_enc): |
| """ |
| Creates the full, complete feature payload required by the XGBoost pipeline. |
| All missing OHE features are set to False (0) or a continuous feature is set to its mean. |
| """ |
| |
| payload = { |
| |
| 'Product_MRP': mrp, |
| 'Product_Weight': weight, |
| 'Store_Age': age, |
| 'Store_Size_Encoded': size_enc, |
|
|
| |
| |
| 'Product_Allocated_Area': 0.0687, |
|
|
| |
| |
| |
|
|
| |
| 'Product_Sugar_Content_No Sugar': False, |
| 'Product_Sugar_Content_Regular': True, |
| 'Product_Sugar_Content_Low Sugar': False, |
| |
|
|
| |
| 'Product_Type_Baking Goods': False, |
| 'Product_Type_Breads': False, |
| 'Product_Type_Breakfast': False, |
| 'Product_Type_Canned': False, |
| 'Product_Type_Dairy': False, |
| 'Product_Type_Frozen Foods': False, |
| 'Product_Type_Fruits and Vegetables': False, |
| 'Product_Type_Hard Drinks': False, |
| 'Product_Type_Health and Hygiene': False, |
| 'Product_Type_Household': False, |
| 'Product_Type_Meat': False, |
| 'Product_Type_Others': False, |
| 'Product_Type_Seafood': False, |
| 'Product_Type_Snack Foods': True, |
| 'Product_Type_Soft Drinks': False, |
| 'Product_Type_Starchy Foods': False, |
|
|
| |
| 'Store_Location_City_Type_Tier 1': False, |
| 'Store_Location_City_Type_Tier 2': True, |
| 'Store_Location_City_Type_Tier 3': False, |
|
|
| |
| 'Store_Type_Departmental Store': False, |
| 'Store_Type_Food Mart': False, |
| 'Store_Type_Supermarket Type1': True, |
| 'Store_Type_Supermarket Type2': False, |
| } |
|
|
| |
| |
| if store_size == 'High': |
| |
| payload['Store_Type_Supermarket Type1'] = False |
| payload['Store_Type_Supermarket Type2'] = True |
|
|
| return payload |
|
|
|
|
| |
| if st.button("Predict Sales Revenue"): |
|
|
| |
| input_payload = create_payload(product_mrp, product_weight, store_age, store_size_encoded) |
|
|
| |
| response = requests.post(SINGLE_PREDICT_ENDPOINT, json=input_payload) |
|
|
| if response.status_code == 200: |
| try: |
| prediction = response.json()['Predicted Total Sales'] |
| st.success(f"📈 Predicted Sales Revenue: **${prediction:,.2f}**") |
| except KeyError: |
| st.error("Prediction successful but key 'Predicted Total Sales' was missing from API response.") |
| st.json(response.json()) |
| else: |
| st.error(f"Error making prediction. Status Code: {response.status_code}") |
| st.json(response.json()) |
|
|
|
|
| |
| st.subheader("2. Batch Prediction (CSV Upload)") |
|
|
| uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"]) |
|
|
| if uploaded_file is not None: |
| if st.button("Predict Batch Sales"): |
| response = requests.post(BATCH_PREDICT_ENDPOINT, files={"file": uploaded_file}) |
|
|
| if response.status_code == 200: |
| predictions = response.json() |
| st.success("Batch predictions completed!") |
| st.dataframe(pd.DataFrame(list(predictions.items()), columns=['ID', 'Predicted Sales'])) |
| else: |
| st.error(f"Error making batch prediction. Status Code: {response.status_code}") |
| st.json(response.json()) |
|
|