File size: 3,807 Bytes
0d77d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
import streamlit as st
import requests
import pandas as pd

st.set_page_config(page_title="SuperKart Sales Prediction", page_icon="πŸ›’", layout="centered")
st.title("πŸ›’ SuperKart Sales Prediction")

# --- API base (set your HF Space URL here) ---
# api_url = st.text_input(
#     "API Base URL",
#     value="https://vinodcwanted-SuperKart.hf.space",
#     help="Your Flask API base (no trailing slash). Example: https://thiresh-rentalpricepredictionbackend.hf.space",
# )

api_url ="https://vinodcwanted-SuperKart.hf.space"

st.markdown("### Product Details")
col1, col2 = st.columns(2)
with col1:
    product_weight = st.number_input("Product_Weight", min_value=0.0, value=12.5, step=0.1)
    product_alloc_area = st.number_input("Product_Allocated_Area", min_value=0.0, value=0.05, step=0.001, format="%.3f")
    product_mrp = st.number_input("Product_MRP", min_value=0.0, value=150.0, step=0.1)
    product_id = st.text_input("Product_Id (optional)", value="", help="If provided, API derives product_categories from its prefix (FD/NC/DR).")
with col2:
    product_sugar_content = st.selectbox("Product_Sugar_Content", ["Low Sugar", "Regular", "No Sugar"])
    product_type = st.selectbox(
        "Product_Type",
        [
            "Frozen Foods","Dairy","Canned","Baking Goods","Health and Hygiene","Snack Foods","Meat",
            "Household","Hard Drinks","Fruits and Vegetables","Breads","Soft Drinks","Breakfast",
            "Others","Starchy Foods","Seafood"
        ],
    )
    product_categories = st.selectbox(
        "product_categories",
        ["FD","NC","DR"],
        help="'FD':'Food_and_Vegetables','NC':'Non_Consumable','DR':'Drinks'"
    )

st.markdown("### Store Details")
col3, col4 = st.columns(2)
with col3:
    store_size = st.selectbox("Store_Size", ["Small", "Medium", "High"])
    store_type = st.selectbox("Store_Type", ["Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"])
with col4:
    store_city_type = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"])
    store_est_year = st.number_input("Store_Establishment_Year", min_value=1900, max_value=2025, value=2005, step=1,
                                     help="API will compute Establishment_age = 2025 - this year.")

# Build payload (single JSON)
payload = {
    "Product_Weight": product_weight,
    "Product_Sugar_Content": product_sugar_content,
    "Product_Allocated_Area": product_alloc_area,
    "Product_Type": product_type,
    "Product_MRP": product_mrp,
    "Store_Establishment_Year": int(store_est_year),
    "Store_Size": store_size,
    "Store_Location_City_Type": store_city_type,
    "Store_Type": store_type,
}

# Optional fields
if product_id.strip():
    payload["Product_Id"] = product_id.strip()
if product_categories != "(leave blank)":
    payload["product_categories"] = product_categories

st.markdown("---")
if st.button("Predict"):
    if not api_url.strip():
        st.error("Please enter your API Base URL.")
    else:
        try:
            endpoint = api_url.rstrip("/") + "/v1/sale"
            with st.spinner("Contacting API..."):
                resp = requests.post(endpoint, json=payload, timeout=30)
            if resp.status_code == 200:
                data = resp.json()
                if "prediction" in data:
                    st.success(f"Predicted Sales: **{data['prediction']}**")
                else:
                    st.warning(f"API responded without 'prediction' key:\n{data}")
            else:
                st.error(f"API error {resp.status_code}: {resp.text}")
        except Exception as e:
            st.error(f"Request failed: {e}")

with st.expander("Show request payload"):
    st.json(payload)