Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,129 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import requests # Import requests for API calls
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
| 8 |
PREDICT_ENDPOINT = f"{BACKEND_API_URL}/v1/sales"
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
| 11 |
st.title("SuperKart Sales Prediction App")
|
| 12 |
-
st.write(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
input_data = {
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
| 43 |
if st.button("Predict Sales"):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
|
|
|
| 3 |
|
| 4 |
+
# ----------------------------------
|
| 5 |
+
# Backend API Configuration
|
| 6 |
+
# ----------------------------------
|
| 7 |
+
BACKEND_API_URL = "https://lokiiparihar-superkart-api-t.hf.space"
|
| 8 |
PREDICT_ENDPOINT = f"{BACKEND_API_URL}/v1/sales"
|
| 9 |
|
| 10 |
+
# ----------------------------------
|
| 11 |
+
# Streamlit UI
|
| 12 |
+
# ----------------------------------
|
| 13 |
st.title("SuperKart Sales Prediction App")
|
| 14 |
+
st.write(
|
| 15 |
+
"This tool predicts the sales revenue for a specific product in a SuperKart store "
|
| 16 |
+
"using a trained machine learning model."
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
st.subheader("Enter the product and store details")
|
| 20 |
+
|
| 21 |
+
# ----------------------------------
|
| 22 |
+
# User Inputs
|
| 23 |
+
# ----------------------------------
|
| 24 |
+
product_id = st.selectbox("Product ID Prefix", ["FD", "NC", "DR"])
|
| 25 |
+
|
| 26 |
+
product_weight = st.number_input(
|
| 27 |
+
"Product Weight",
|
| 28 |
+
min_value=0.0,
|
| 29 |
+
value=12.0
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
product_sugar_content = st.selectbox(
|
| 33 |
+
"Product Sugar Content",
|
| 34 |
+
["Low Sugar", "Regular", "No Sugar"]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
product_allocated_area = st.number_input(
|
| 38 |
+
"Product Allocated Area",
|
| 39 |
+
min_value=0.0,
|
| 40 |
+
value=0.05
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
product_type = st.selectbox(
|
| 44 |
+
"Product Type",
|
| 45 |
+
["Perishables", "Non Perishables"]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
product_mrp = st.number_input(
|
| 49 |
+
"Product MRP",
|
| 50 |
+
min_value=0.0,
|
| 51 |
+
value=150.0
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
store_id = st.selectbox(
|
| 55 |
+
"Store ID",
|
| 56 |
+
["OUT004", "OUT003", "OUT001", "OUT002"]
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
store_size = st.selectbox(
|
| 60 |
+
"Store Size",
|
| 61 |
+
["Small", "Medium", "High"]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
store_location_city_type = st.selectbox(
|
| 65 |
+
"Store Location City Type",
|
| 66 |
+
["Tier 1", "Tier 2", "Tier 3"]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
store_type = st.selectbox(
|
| 70 |
+
"Store Type",
|
| 71 |
+
[
|
| 72 |
+
"Grocery Store",
|
| 73 |
+
"Supermarket Type1",
|
| 74 |
+
"Supermarket Type2",
|
| 75 |
+
"Supermarket Type3"
|
| 76 |
+
]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
store_current_age = st.number_input(
|
| 80 |
+
"Store Current Age (Years)",
|
| 81 |
+
min_value=0,
|
| 82 |
+
value=15
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# ----------------------------------
|
| 86 |
+
# Payload for API
|
| 87 |
+
# ----------------------------------
|
| 88 |
input_data = {
|
| 89 |
+
"Product_Id": product_id,
|
| 90 |
+
"Product_Weight": product_weight,
|
| 91 |
+
"Product_Sugar_Content": product_sugar_content,
|
| 92 |
+
"Product_Allocated_Area": product_allocated_area,
|
| 93 |
+
"Product_Type": product_type,
|
| 94 |
+
"Product_MRP": product_mrp,
|
| 95 |
+
"Store_Id": store_id,
|
| 96 |
+
"Store_Size": store_size,
|
| 97 |
+
"Store_Location_City_Type": store_location_city_type,
|
| 98 |
+
"Store_Type": store_type, # ✅ REQUIRED
|
| 99 |
+
"Store_Current_Age": store_current_age
|
| 100 |
}
|
| 101 |
|
| 102 |
+
# ----------------------------------
|
| 103 |
+
# Predict Button
|
| 104 |
+
# ----------------------------------
|
| 105 |
if st.button("Predict Sales"):
|
| 106 |
+
with st.spinner("Predicting sales..."):
|
| 107 |
+
try:
|
| 108 |
+
response = requests.post(
|
| 109 |
+
PREDICT_ENDPOINT,
|
| 110 |
+
json=input_data,
|
| 111 |
+
timeout=10
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
if response.status_code == 200:
|
| 115 |
+
result = response.json()
|
| 116 |
+
st.success(
|
| 117 |
+
f"Predicted Sales Revenue: ${result['Predicted Sales']:.2f}"
|
| 118 |
+
)
|
| 119 |
+
else:
|
| 120 |
+
st.error("Prediction failed")
|
| 121 |
+
st.json(response.json())
|
| 122 |
+
|
| 123 |
+
except requests.exceptions.ConnectionError:
|
| 124 |
+
st.error("Could not connect to backend API.")
|
| 125 |
+
except requests.exceptions.Timeout:
|
| 126 |
+
st.error("Request timed out. Please try again.")
|
| 127 |
+
except Exception as e:
|
| 128 |
+
st.error(f"Unexpected error: {e}")
|
| 129 |
+
|