Lokiiparihar commited on
Commit
75e8027
Β·
verified Β·
1 Parent(s): 62c0ff4

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # 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,
@@ -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-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"βœ… Predicted Sales: {prediction .json()['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}")
 
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}")