File size: 3,201 Bytes
e7c622f
e62687d
 
75e8027
e62687d
 
 
 
 
 
 
 
 
 
0cbcad5
 
e62687d
 
 
 
 
 
 
 
 
75e8027
 
 
 
 
 
 
 
 
e62687d
75e8027
e62687d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
794ea8b
e62687d
 
 
c0232cb
 
1e6ae11
7d72275
1e6ae11
369beb7
c0232cb
 
1e6ae11
 
0cbcad5
c0232cb
 
369beb7
c0232cb
 
e62687d
75e8027
c0232cb
e62687d
75e8027
1e6ae11
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import streamlit as st
import requests

# --- Streamlit UI config ---
st.set_page_config(page_title="SuperKart Sales Prediction", layout="centered")

st.title("πŸ›’ SuperKart Sales Prediction")
st.write("Enter product and store features below to get a sales forecast.")

# --- INPUT FIELDS ---
product_weight = st.number_input("Product Weight (kg)", min_value=0.0, step=0.1, value=12.0)
product_sugar = st.selectbox("Product Sugar Content", [0, 1])
product_alloc_area = st.number_input("Allocated Display Area (sq. m)", min_value=0.0, step=0.01, value=0.05)
product_mrp = st.number_input("Product MRP", min_value=1.0, step=0.5, value=150.0)
store_size = st.selectbox("Store Size", [0, 1, 2])
store_city_type = st.selectbox("Store Location City Type", [0, 1, 2])
store_type = st.selectbox("Store Type", [0, 1, 2, 3])
store_age = st.slider("Store Age (Years)", 0, 30, 10)

product_type = st.selectbox("Product Category", [
    "Breads", "Breakfast", "Canned", "Dairy", "Frozen Foods", "Fruits and Vegetables",
    "Hard Drinks", "Health and Hygiene", "Household", "Meat", "Others", "Seafood",
    "Snack Foods", "Soft Drinks", "Starchy Foods"
])

# --- One-hot encode the product type ---
product_type_features = {
    f"Product_Type_{pt}": int(pt == product_type)
    for pt in [
        "Breads", "Breakfast", "Canned", "Dairy", "Frozen Foods", "Fruits and Vegetables",
        "Hard Drinks", "Health and Hygiene", "Household", "Meat", "Others", "Seafood",
        "Snack Foods", "Soft Drinks", "Starchy Foods"
    ]
}

# --- Create input JSON ---
input_data = {
    "Product_Weight": product_weight,
    "Product_Sugar_Content": product_sugar,
    "Product_Allocated_Area": product_alloc_area,
    "Product_MRP": product_mrp,
    "Store_Size": store_size,
    "Store_Location_City_Type": store_city_type,
    "Store_Type": store_type,
    "Store_Age": store_age,
    **product_type_features
}

if st.button("Predict Sales"):
    with st.spinner("Fetching prediction from backend..."):
        try:
            response = requests.post(
                "https://lokiiparihar-SuperkartBackendModalDeploy-XGBoost.hf.space/predict",
                json=input_data
            )
            if response.status_code == 200:
                try:
                    result = response.json()
                    st.subheader("πŸ” Raw Backend Response")
                    #st.json(result)  # SHOW FULL JSON RETURNED

                    prediction = result.get("Predicted_Sales", None)
                except ValueError:
                    prediction = response.text
                    st.warning("⚠ Backend did not return JSON, showing raw text:")
                    st.code(prediction)

                try:
                    prediction = float(prediction)
                    st.success(f"βœ… Predicted Sales: **{prediction:.2f} units**")
                except (ValueError, TypeError):
                    st.error(f"❌ Could not convert prediction to number: {prediction}")
            else:
                st.error(f"❌ API Error: Status code {response.status_code}")
                st.text(response.text)
        except Exception as e:
            st.error(f"❌ Request failed: {e}")