File size: 3,180 Bytes
e670d86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests

API_URL = "https://lokiiparihar-superkart-api.hf.space/predict"  # use the working API

st.set_page_config(page_title="Superkart Sales Prediction", layout="centered")

st.title("Sales Prediction App")
st.write("This tool predicts Superkart sales. Enter the required information below.")

# Model Choice
model_choice = st.selectbox(
    "Select Model",
    options=["dt", "xgb"],
    format_func=lambda x: {
        "dt": "Decision Tree",
        "xgb": "XGBoost",
        "rf": "Random Forest",
        "lr": "Linear Regression",
    }.get(x, x),
)

# Inputs (set defaults so the API call has valid values)
col1, col2 = st.columns(2)

with col1:
    product_weight = st.number_input("Product Weight", min_value=0.0, value=12.5, step=0.1)
    sugar = st.selectbox("Sugar Content", [0, 1, 2], index=0)
    area = st.number_input("Allocated Area", min_value=0.0, value=0.08, step=0.01)
    product_type = st.number_input("Product Type Code", min_value=0, value=0, step=1)

with col2:
    mrp = st.number_input("Product MRP", min_value=0.0, value=249.99, step=1.0)
    store_size = st.selectbox("Store Size Code", [0, 1, 2], index=1)
    city = st.selectbox("City Type Code", [0, 1, 2], index=0)
    store_type = st.number_input("Store Type Code", min_value=0, value=1, step=1)
    store_age = st.number_input("Store Age", min_value=0, value=15, step=1)

# Build payload EXACTLY as your working notebook request expects
sample = {
    "Product_Weight": float(product_weight),
    "Product_Sugar_Content": float(sugar),
    "Product_Allocated_Area": float(area),
    "Product_Type": int(product_type),
    "Product_MRP": float(mrp),
    "Store_Size": int(store_size),
    "Store_Location_City_Type": int(city),
    "Store_Type": int(store_type),
    "Store_Age": int(store_age),
    "model": model_choice,
}

st.subheader("Payload being sent")
st.json(sample)

if st.button("Predict", type="primary"):
    try:
        headers = {"Content-Type": "application/json"}
        with st.spinner("Calling prediction API..."):
            response = requests.post(API_URL, json=sample, headers=headers, timeout=30)

        st.write("Status Code:", response.status_code)

        # Show raw response for debugging
        st.write("Raw Response:")
        st.code(response.text)

        if response.headers.get("content-type", "").startswith("application/json"):
            result = response.json()
            st.write("Parsed JSON:")
            st.json(result)

            # Try common keys (your API might return a different one)
            pred_key = next((k for k in ["Prediction", "prediction", "pred", "result", "output"] if k in result), None)
            if pred_key:
                st.success(f"Prediction: {result[pred_key]}")
            else:
                st.info("Prediction key not found. See JSON above.")
        else:
            st.error("API did not return JSON. See raw response above.")

    except requests.exceptions.RequestException as e:
        st.error(f"Request failed: {e}")

# IMPORTANT:
# Streamlit apps do NOT use app.run(). Remove any Flask-related code.
# if __name__ == '__main__':
#     app.run(debug=True)