Spaces:
Running
Running
| import streamlit as st | |
| import requests | |
| # Page Configuration | |
| st.set_page_config( | |
| page_title="SuperKart Sales Predictor", | |
| page_icon=":shopping_cart:", | |
| layout="centered" | |
| ) | |
| # App Title and User Guidance | |
| st.title("Ramakanth's SuperKart Sales Predictor") | |
| st.markdown(""" | |
| Welcome to the **SuperKart Sales Forecasting Tool**\! | |
| This application predicts the **total product-store sales revenue** for a given | |
| product and store combination using a trained **XGBoost model** deployed on | |
| Hugging Face Spaces. | |
| **How to use:** | |
| 1. Fill in the **Product Details** and **Store Details** below. | |
| 2. All fields are required. Hover over each label to see valid values and ranges. | |
| 3. Click **Predict Sales** to get an instant forecast. | |
| """) | |
| st.divider() | |
| # Input Form -- Product Details | |
| st.subheader("Product Details") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| Product_Weight = st.number_input( | |
| "Product Weight (kg)", | |
| min_value=4.0, max_value=22.0, value=12.66, step=0.01, | |
| help="Weight of the product in kilograms. Valid range: 4.0 to 22.0 kg." | |
| ) | |
| Product_Allocated_Area = st.number_input( | |
| "Product Allocated Area (ratio)", | |
| min_value=0.004, max_value=0.298, value=0.068, step=0.001, | |
| format="%.3f", | |
| help="Ratio of display area for this product to total store display. Range: 0.004 to 0.298." | |
| ) | |
| Product_MRP = st.number_input( | |
| "Product MRP (INR)", | |
| min_value=31.0, max_value=266.0, value=147.0, step=0.5, | |
| help="Maximum Retail Price of the product in Indian Rupees. Range: 31 to 266." | |
| ) | |
| with col2: | |
| Product_Sugar_Content = st.selectbox( | |
| "Sugar Content", | |
| ["Low Sugar", "Regular", "No Sugar"], | |
| help="Sugar content classification: Low Sugar / Regular / No Sugar." | |
| ) | |
| Product_Id_char = st.selectbox( | |
| "Product ID Prefix", | |
| ["FD", "DR", "NC"], | |
| help="Two-letter prefix of the Product ID: FD=Food, DR=Drinks, NC=Non-Consumable." | |
| ) | |
| Product_Type_Category = st.selectbox( | |
| "Product Type Category", | |
| ["Perishables", "Non Perishables"], | |
| help="Perishables: dairy, meat, fruits. Non-Perishables: canned, household, health." | |
| ) | |
| # Input Form -- Store Details | |
| st.subheader("Store Details") | |
| col3, col4 = st.columns(2) | |
| with col3: | |
| Store_Size = st.selectbox( | |
| "Store Size", | |
| ["High", "Medium", "Low"], | |
| index=1, | |
| help="Physical size: High=large supermarket, Medium=standard, Low=small food mart." | |
| ) | |
| Store_Location_City_Type = st.selectbox( | |
| "City Type", | |
| ["Tier 1", "Tier 2", "Tier 3"], | |
| index=1, | |
| help="Tier 1=metro cities, Tier 2=mid-size cities, Tier 3=smaller towns." | |
| ) | |
| with col4: | |
| Store_Type = st.selectbox( | |
| "Store Type", | |
| ["Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"], | |
| index=2, | |
| help="Store format based on product range offered." | |
| ) | |
| Store_Age_Years = st.number_input( | |
| "Store Age (Years)", | |
| min_value=0, max_value=100, value=26, step=1, | |
| help="Number of years since the store was established." | |
| ) | |
| st.divider() | |
| # Prediction Button | |
| if st.button("Predict Sales", type="primary", use_container_width=True): | |
| payload = { | |
| "Product_Weight": Product_Weight, | |
| "Product_Sugar_Content": Product_Sugar_Content, | |
| "Product_Allocated_Area": Product_Allocated_Area, | |
| "Product_MRP": Product_MRP, | |
| "Store_Size": Store_Size, | |
| "Store_Location_City_Type": Store_Location_City_Type, | |
| "Store_Type": Store_Type, | |
| "Product_Id_char": Product_Id_char, | |
| "Store_Age_Years": Store_Age_Years, | |
| "Product_Type_Category": Product_Type_Category | |
| } | |
| with st.spinner("Contacting prediction API -- please wait..."): | |
| try: | |
| response = requests.post( | |
| "https://ramzai9-superkartprediction.hf.space/v1/predict", | |
| json=payload, | |
| timeout=60 | |
| ) | |
| if response.status_code == 200: | |
| predicted_sales = response.json()["Sales"] | |
| st.success(f"Predicted Product Store Sales Total: INR {predicted_sales:,.2f}") | |
| st.balloons() | |
| elif response.status_code == 400: | |
| st.error(f"Validation Error: {response.json().get('error', 'Bad request')}") | |
| else: | |
| st.error(f"API Error (HTTP {response.status_code}): {response.text}") | |
| except requests.exceptions.Timeout: | |
| st.warning( | |
| "Request timed out. The backend space may be waking up -- " | |
| "please wait 30 seconds and try again." | |
| ) | |
| except requests.exceptions.ConnectionError: | |
| st.error( | |
| "Could not reach the prediction backend. " | |
| "Please verify the backend Hugging Face Space is running." | |
| ) | |
| except Exception as e: | |
| st.error(f"Unexpected error: {str(e)}") | |
| st.caption("SuperKart Sales Predictor | Powered by XGBoost & Streamlit | Deployed on Hugging Face Spaces") | |