File size: 4,186 Bytes
e7404b9
 
 
 
 
 
d76bedd
e7404b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
import streamlit as st
import requests
import pandas as pd

st.set_page_config(page_title="SuperKart Sales Forecaster", page_icon="๐Ÿ›’", layout="wide")

# Backend API URL pointing to deployed Flask backend
BACKEND_URL = "https://codedfortamara-superkart-backend.hf.space/predict"

st.title("๐Ÿ›’ SuperKart Sales Revenue Forecaster")
st.markdown("Predict sales revenue for any product-store combination using our trained ML model.")
st.divider()

col1, col2 = st.columns(2)

with col1:
    st.subheader("๐Ÿ“ฆ Product Details")
    product_weight = st.number_input("Product Weight (kg)", min_value=1.0, max_value=25.0, value=12.0, step=0.1)
    product_sugar_content = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
    product_allocated_area = st.slider("Allocated Display Area (ratio)", min_value=0.0, max_value=0.30, value=0.05, step=0.005, format="%.3f")
    product_type = st.selectbox("Product Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods", "Dairy",
        "Household", "Baking Goods", "Canned", "Health and Hygiene", "Meat", "Soft Drinks",
        "Breads", "Hard Drinks", "Others", "Starchy Foods", "Breakfast", "Seafood"])
    product_mrp = st.number_input("Product MRP", min_value=30.0, max_value=270.0, value=140.0, step=1.0)
    product_category = st.selectbox("Product Category", ["FD", "NC", "DR"],
        format_func=lambda x: {"FD": "FD โ€“ Food", "NC": "NC โ€“ Non-Consumable", "DR": "DR โ€“ Drinks"}[x])

with col2:
    st.subheader("๐Ÿฌ Store Details")
    store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
    store_location = st.selectbox("City Tier", ["Tier 1", "Tier 2", "Tier 3"])
    store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"])
    store_age = st.number_input("Store Age (years)", min_value=1, max_value=50, value=20, step=1)

st.divider()

if st.button("๐Ÿ”ฎ Predict Sales Revenue", type="primary", use_container_width=True):
    payload = {
        "Product_Weight": product_weight,
        "Product_Sugar_Content": product_sugar_content,
        "Product_Allocated_Area": product_allocated_area,
        "Product_Type": product_type,
        "Product_MRP": product_mrp,
        "Store_Size": store_size,
        "Store_Location_City_Type": store_location,
        "Store_Type": store_type,
        "Store_Age": store_age,
        "Product_Category": product_category,
    }
    try:
        response = requests.post(BACKEND_URL, json=payload, timeout=30)
        if response.status_code == 200:
            prediction = response.json()["predictions"][0]
            st.success(f"### ๐Ÿ’ฐ Predicted Sales Revenue: โ‚น{prediction:,.2f}")
            st.markdown("#### Input Summary")
            st.dataframe(pd.DataFrame([payload]), use_container_width=True)
        else:
            st.error(f"API Error: {response.json().get('error', 'Unknown error')}")
    except requests.exceptions.ConnectionError:
        st.error("Could not connect to the backend. Check if the backend space is running.")
    except Exception as e:
        st.error(f"Error: {str(e)}")

st.divider()
st.subheader("๐Ÿ“Š Batch Prediction")
uploaded_file = st.file_uploader("Upload CSV for batch predictions", type=["csv"])

if uploaded_file is not None:
    batch_df = pd.read_csv(uploaded_file)
    st.dataframe(batch_df.head(), use_container_width=True)
    if st.button("Run Batch Prediction", use_container_width=True):
        try:
            response = requests.post(BACKEND_URL, json=batch_df.to_dict(orient='records'), timeout=60)
            if response.status_code == 200:
                batch_df["Predicted_Sales"] = response.json()["predictions"]
                st.success(f"Predictions generated for {len(batch_df)} records!")
                st.dataframe(batch_df, use_container_width=True)
                st.download_button("Download Predictions", batch_df.to_csv(index=False),
                                   "superkart_predictions.csv", "text/csv")
            else:
                st.error(f"API Error: {response.json().get('error', 'Unknown error')}")
        except Exception as e:
            st.error(f"Error: {str(e)}")