File size: 3,013 Bytes
3a770ab
 
 
 
ea7ea44
ea264ae
 
ea7ea44
 
 
 
ea264ae
 
ea7ea44
 
 
 
 
 
 
 
 
ea264ae
ea7ea44
3a770ab
 
ea7ea44
 
 
3a770ab
 
 
ea7ea44
3a770ab
 
ea7ea44
 
 
 
 
 
 
 
 
3a770ab
 
ea264ae
ea7ea44
3a770ab
 
ea7ea44
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import requests
from datetime import datetime

# App title and description
st.set_page_config(page_title="SuperKart Sales Forecasting", layout="centered")
st.title("SuperKart Sales Forecasting App")
st.markdown("Enter product and store attributes to forecast monthly product sales revenue.")
st.markdown("*All sales are reported in ($) USD.*")


# --- Input fields for product and store data ---

product_weight = st.number_input("Product Weight (oz)", min_value=0.0, value=12.66, step=0.1)
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
product_allocated_area = st.number_input("Product Allocated Area (linear in.)", min_value=0.0, value=100.00, step=1.0)
product_mrp = st.number_input("Maximum Retail Price (USD)", min_value=0.0, value=150.00, step=0.5)
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"])
store_age_years = st.slider("Store Age (years)", min_value=0, max_value=30, value=10, step=1)

# Product Type categories to match the dataset
product_type_category = st.selectbox(
    "Product Type Category",
    [
        "Fruits and Vegetables", "Snack Foods", "Household", "Frozen Foods", "Dairy",
        "Baking Goods", "Health and Hygiene", "Hard Drinks", "Soft Drinks", "Meat",
        "Canned", "Breads", "Breakfast", "Seafood", "Starchy Foods", "Others"
    ]
)

# --- Prepare payload for the backend API ---
current_year = datetime.now().year
payload = {
    "Product_Weight": product_weight,
    "Product_Sugar_Content": product_sugar_content,
    "Product_Allocated_Area": product_allocated_area / 1000, # Normalize based on EDA insights
    "Product_MRP": product_mrp,
    "Store_Size": store_size,
    "Store_Location_City_Type": store_location_city_type,
    "Store_Type": store_type,
    "Store_Establishment_Year": int(current_year - store_age_years),
    "Product_Type": product_type_category
}

# ---- Call backend Space  ----
API_URL = "https://gigcode-superkartbackend.hf.space/v1/sales"

if st.button("Predict", type="primary"):
    with st.spinner('Forecasting...'):
        try:
            resp = requests.post(API_URL, json=payload, timeout=30)
            if resp.status_code == 200:
                result = resp.json()
                predicted_sales = result.get("Predicted Product_Store_Sales_Total", 0)
                st.success(f"Predicted Product Store Sales Total: ${float(predicted_sales):,.2f}")
            else:
                st.error(f"API error [{resp.status_code}]: {resp.text}")
        except requests.exceptions.RequestException as e:
            st.error(f"Request failed: Could not connect to the API. Please ensure the backend is running. Error: {e}")
        except Exception as e:
            st.error(f"An unexpected error occurred: {e}")