File size: 3,082 Bytes
f0156c3
b6639b0
b1bd4cc
f0156c3
 
 
 
966c9c6
b6639b0
 
f0156c3
 
b6639b0
f0156c3
 
 
 
 
b1bd4cc
 
 
 
 
 
 
 
 
 
f0156c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1bd4cc
 
f0156c3
 
 
 
 
 
 
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

import os
import time
import streamlit as st
import pandas as pd
import requests

DEFAULT_BACKEND_URL = "https://12ongo-backend.hf.space"
# Try env var first; fall back to default
backend_url_default = os.environ.get("BACKEND_URL", DEFAULT_BACKEND_URL)
backend_url = st.text_input(
    "Backend URL",
    value=backend_url_default,
    help="Override if needed (e.g., local http://127.0.0.1:5000)",
)

st.title("SuperKart Sales Prediction")

with st.expander("Connection", expanded=False):
    if st.button("Test connection"):
        with st.spinner("Pinging backend..."):
            try:
                r = requests.get(f"{backend_url}/", timeout=30)
                st.write(f"Status: {r.status_code}")
                st.write(r.text[:200])
            except Exception as e:
                st.error(f"Ping failed: {e}")

st.subheader("Online Prediction")

# Input widgets
product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0, 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", min_value=0.0, value=0.15, step=0.01)
product_type = st.selectbox(
    "Product Type",
    [
        "meat",
        "snack foods",
        "hard drinks",
        "dairy",
        "canned",
        "soft drinks",
        "health and hygiene",
        "baking goods",
        "bread",
        "breakfast",
        "frozen foods",
        "fruits and vegetables",
        "household",
        "seafood",
        "starchy foods",
        "others",
    ],
)
product_mrp = st.number_input("Product MRP", min_value=0.0, value=99.0, step=0.5)
store_before_2005 = st.checkbox("Store Established Before 2005?", value=True)
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
store_city_type = st.selectbox("Store City Type", ["Tier 1", "Tier 2", "Tier 3"])
store_type = st.selectbox(
    "Store Type",
    ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"],
)

if st.button("Predict Sales"):
    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_Establishment_Year_Before_2005": bool(store_before_2005),
        "Store_Size": store_size,
        "Store_Location_City_Type": store_city_type,
        "Store_Type": store_type,
    }
    try:
        with st.spinner("Requesting prediction (this may take up to 2 minutes if the backend is sleeping)..."):
            resp = requests.post(f"{backend_url}/v1/sales", json=payload, timeout=120)
        if resp.status_code == 200:
            st.success(f"Predicted Sales: {resp.json().get('Predicted_Sales')}")
        else:
            st.error(f"Error {resp.status_code}: {resp.text}")
    except Exception as e:
        st.error(f"Request failed: {e}")