Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +59 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,60 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="SuperKart Sales Prediction", layout="centered")
|
| 6 |
+
|
| 7 |
+
st.title("🛒 SuperKart Sales Prediction")
|
| 8 |
+
st.write("Enter product and store features below to get a sales forecast.")
|
| 9 |
+
|
| 10 |
+
# --- INPUT FIELDS ---
|
| 11 |
+
product_weight = st.number_input("Product Weight (kg)", min_value=0.0, step=0.1, value=12.0)
|
| 12 |
+
product_sugar = st.selectbox("Product Sugar Content", [0, 1])
|
| 13 |
+
product_alloc_area = st.number_input("Allocated Display Area (sq. m)", min_value=0.0, step=0.01, value=0.05)
|
| 14 |
+
product_mrp = st.number_input("Product MRP", min_value=1.0, step=0.5, value=150.0)
|
| 15 |
+
store_size = st.selectbox("Store Size", [0, 1, 2]) # small/medium/high
|
| 16 |
+
store_city_type = st.selectbox("Store Location City Type", [0, 1, 2]) # a/b/c
|
| 17 |
+
store_type = st.selectbox("Store Type", [0, 1, 2, 3])
|
| 18 |
+
store_age = st.slider("Store Age (Years)", 0, 30, 10)
|
| 19 |
+
|
| 20 |
+
product_type = st.selectbox("Product Category", [
|
| 21 |
+
"Breads", "Breakfast", "Canned", "Dairy", "Frozen Foods", "Fruits and Vegetables",
|
| 22 |
+
"Hard Drinks", "Health and Hygiene", "Household", "Meat", "Others", "Seafood",
|
| 23 |
+
"Snack Foods", "Soft Drinks", "Starchy Foods"
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
# Create one-hot encoded product types
|
| 27 |
+
product_type_features = {f"Product_Type_{pt}": int(pt == product_type) for pt in [
|
| 28 |
+
"Breads", "Breakfast", "Canned", "Dairy", "Frozen Foods", "Fruits and Vegetables",
|
| 29 |
+
"Hard Drinks", "Health and Hygiene", "Household", "Meat", "Others", "Seafood",
|
| 30 |
+
"Snack Foods", "Soft Drinks", "Starchy Foods"
|
| 31 |
+
]}
|
| 32 |
+
|
| 33 |
+
# --- PAYLOAD CREATION ---
|
| 34 |
+
input_data = {
|
| 35 |
+
"Product_Weight": product_weight,
|
| 36 |
+
"Product_Sugar_Content": product_sugar,
|
| 37 |
+
"Product_Allocated_Area": product_alloc_area,
|
| 38 |
+
"Product_MRP": product_mrp,
|
| 39 |
+
"Store_Size": store_size,
|
| 40 |
+
"Store_Location_City_Type": store_city_type,
|
| 41 |
+
"Store_Type": store_type,
|
| 42 |
+
"Store_Age": store_age,
|
| 43 |
+
**product_type_features
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# --- API CALL ---
|
| 47 |
+
if st.button("Predict Sales"):
|
| 48 |
+
with st.spinner("Fetching prediction from backend..."):
|
| 49 |
+
try:
|
| 50 |
+
response = requests.post(
|
| 51 |
+
"https://lokiiparihar-superkartbackendmodaldeploy-xgboost.hf.space/predict",
|
| 52 |
+
json=input_data
|
| 53 |
+
)
|
| 54 |
+
if response.status_code == 200:
|
| 55 |
+
prediction = response.json().get("prediction", "N/A")
|
| 56 |
+
st.success(f"🧮 Estimated Sales: **{prediction:.2f} units**")
|
| 57 |
+
else:
|
| 58 |
+
st.error(f"❌ API Error: {response.status_code}")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"Request failed: {e}")
|