Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +21 -13
src/streamlit_app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 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")
|
|
@@ -23,14 +23,17 @@ product_type = st.selectbox("Product Category", [
|
|
| 23 |
"Snack Foods", "Soft Drinks", "Starchy Foods"
|
| 24 |
])
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
product_type_features = {
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# ---
|
| 34 |
input_data = {
|
| 35 |
"Product_Weight": product_weight,
|
| 36 |
"Product_Sugar_Content": product_sugar,
|
|
@@ -48,13 +51,18 @@ if st.button("Predict Sales"):
|
|
| 48 |
with st.spinner("Fetching prediction from backend..."):
|
| 49 |
try:
|
| 50 |
response = requests.post(
|
| 51 |
-
"https://lokiiparihar-
|
| 52 |
json=input_data
|
| 53 |
)
|
| 54 |
if response.status_code == 200:
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
-
st.error(f"β API Error: {response.status_code}")
|
| 59 |
except Exception as e:
|
| 60 |
-
st.error(f"Request failed: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
+
# --- Streamlit UI config ---
|
| 5 |
st.set_page_config(page_title="SuperKart Sales Prediction", layout="centered")
|
| 6 |
|
| 7 |
st.title("π SuperKart Sales Prediction")
|
|
|
|
| 23 |
"Snack Foods", "Soft Drinks", "Starchy Foods"
|
| 24 |
])
|
| 25 |
|
| 26 |
+
# --- One-hot encode the product type ---
|
| 27 |
+
product_type_features = {
|
| 28 |
+
f"Product_Type_{pt}": int(pt == product_type)
|
| 29 |
+
for pt in [
|
| 30 |
+
"Breads", "Breakfast", "Canned", "Dairy", "Frozen Foods", "Fruits and Vegetables",
|
| 31 |
+
"Hard Drinks", "Health and Hygiene", "Household", "Meat", "Others", "Seafood",
|
| 32 |
+
"Snack Foods", "Soft Drinks", "Starchy Foods"
|
| 33 |
+
]
|
| 34 |
+
}
|
| 35 |
|
| 36 |
+
# --- Create input JSON ---
|
| 37 |
input_data = {
|
| 38 |
"Product_Weight": product_weight,
|
| 39 |
"Product_Sugar_Content": product_sugar,
|
|
|
|
| 51 |
with st.spinner("Fetching prediction from backend..."):
|
| 52 |
try:
|
| 53 |
response = requests.post(
|
| 54 |
+
"https://lokiiparihar-SuperkartBackendModalDeploy-XGBoost.hf.space/predict",
|
| 55 |
json=input_data
|
| 56 |
)
|
| 57 |
if response.status_code == 200:
|
| 58 |
+
result = response.json()
|
| 59 |
+
prediction = result.get("prediction", "N/A")
|
| 60 |
+
try:
|
| 61 |
+
prediction = float(prediction)
|
| 62 |
+
st.success(f"β
Predicted Sales: **{prediction:.2f} units**")
|
| 63 |
+
except ValueError:
|
| 64 |
+
st.error(f"β Prediction format error: {prediction}")
|
| 65 |
else:
|
| 66 |
+
st.error(f"β API Error: Status code {response.status_code}")
|
| 67 |
except Exception as e:
|
| 68 |
+
st.error(f"β Request failed: {e}")
|