gigcode commited on
Commit
ea7ea44
·
verified ·
1 Parent(s): 3a770ab

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +48 -46
app.py CHANGED
@@ -2,60 +2,62 @@ import streamlit as st
2
  import requests
3
  from datetime import datetime
4
 
5
- st.title("SuperKart Sales Forecast")
6
-
7
- # ----- Input fields for product and store data -----
8
- Product_Weight = st.number_input("Product Weight", min_value=0.0, value=12.66)
9
- Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low", "Regular", "No Sugar"])
10
-
11
- # Numeric inputs
12
- Product_Allocated_Area = st.number_input("Product Allocated Area (0–1)", min_value=0.0, max_value=1.0, value=0.05, step=0.01, format="%.2f")
13
- Product_MRP = st.number_input("Product MRP", min_value=0.0, value=150.00, step=0.5)
14
-
15
- # Categorical inputs
16
- Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"])
17
- Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
18
- Store_Type = st.selectbox("Store Type", ["Grocery Store", "Supermarket Type1", "Supermarket Type2", "Supermarket Type3"])
19
-
20
- # IDs and engineered-style fields to match your template
21
- Product_Id_char = st.text_input("Product Id", "FD6114")
22
- Store_Age_Years = st.number_input("Store Age (Years)", min_value=0, value=10, step=1)
23
- Product_Type_Category = st.selectbox(
 
24
  "Product Type Category",
25
  [
26
- "Fruits and Vegetables","Snack Foods","Household","Frozen Foods","Dairy",
27
- "Baking Goods","Health and Hygiene","Hard Drinks","Soft Drinks","Meat",
28
- "Canned","Breads","Breakfast","Seafood","Others"
29
  ]
30
  )
31
 
32
- # Map UI fields to the backend model's expected feature names
33
  current_year = datetime.now().year
34
  payload = {
35
- "Product_Weight": Product_Weight,
36
- "Product_Sugar_Content": Product_Sugar_Content,
37
- "Product_Allocated_Area": Product_Allocated_Area,
38
- "Product_MRP": Product_MRP,
39
- "Store_Size": Store_Size,
40
- "Store_Location_City_Type": Store_Location_City_Type,
41
- "Store_Type": Store_Type,
42
- "Product_Id": Product_Id_char, # map Product_Id_char -> Product_Id
43
- "Store_Establishment_Year": int(current_year - Store_Age_Years), # derive year from age
44
- "Product_Type": Product_Type_Category # map Product_Type_Category -> Product_Type
45
- # (Optional) If your model expects Store_Id, add another text_input and include it here
46
  }
47
 
48
- # ---- Call your backend Space (replace with your actual username/space) ----
49
- API_URL = "https://<user_name>-<space_name>.hf.space/v1/sales" # <-- fill in
50
 
51
  if st.button("Predict", type="primary"):
52
- try:
53
- resp = requests.post(API_URL, json=payload, timeout=30)
54
- if resp.status_code == 200:
55
- result = resp.json()
56
- predicted_sales = result.get("Predicted Product_Store_Sales_Total") or next(iter(result.values()))
57
- st.success(f"Predicted Product Store Sales Total: ₹{float(predicted_sales):,.2f}")
58
- else:
59
- st.error(f"API error [{resp.status_code}]: {resp.text}")
60
- except Exception as e:
61
- st.error(f"Request failed: {e}")
 
 
 
 
2
  import requests
3
  from datetime import datetime
4
 
5
+ # App title and description
6
+ st.set_page_config(page_title="SuperKart Sales Forecasting", page_icon="🛒", layout="centered")
7
+ st.title("🛒 SuperKart Sales Forecasting App")
8
+ st.markdown("Enter product and store attributes to forecast monthly product sales revenue.")
9
+ st.markdown("*All sales are reported in ($) USD.*")
10
+
11
+
12
+ # --- Input fields for product and store data (matching screenshot) ---
13
+ # Corrected input widgets to align with the model's training data
14
+ product_weight = st.number_input("Product Weight (oz)", min_value=0.0, value=12.66, step=0.1)
15
+ product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
16
+ product_allocated_area = st.number_input("Product Allocated Area (linear in.)", min_value=0.0, value=100.00, step=1.0)
17
+ product_mrp = st.number_input("Maximum Retail Price (USD)", min_value=0.0, value=150.00, step=0.5)
18
+ store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
19
+ store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
20
+ store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"])
21
+ store_age_years = st.slider("Store Age (years)", min_value=0, max_value=30, value=10, step=1)
22
+
23
+ # Corrected Product Type categories to match the dataset
24
+ product_type_category = st.selectbox(
25
  "Product Type Category",
26
  [
27
+ "Fruits and Vegetables", "Snack Foods", "Household", "Frozen Foods", "Dairy",
28
+ "Baking Goods", "Health and Hygiene", "Hard Drinks", "Soft Drinks", "Meat",
29
+ "Canned", "Breads", "Breakfast", "Seafood", "Starchy Foods", "Others"
30
  ]
31
  )
32
 
33
+ # --- Prepare payload for the backend API ---
34
  current_year = datetime.now().year
35
  payload = {
36
+ "Product_Weight": product_weight,
37
+ "Product_Sugar_Content": product_sugar_content,
38
+ "Product_Allocated_Area": product_allocated_area / 1000, # Normalize based on EDA insights
39
+ "Product_MRP": product_mrp,
40
+ "Store_Size": store_size,
41
+ "Store_Location_City_Type": store_location_city_type,
42
+ "Store_Type": store_type,
43
+ "Store_Establishment_Year": int(current_year - store_age_years),
44
+ "Product_Type": product_type_category
 
 
45
  }
46
 
47
+ # ---- Call your backend Space (URL is now correct and hardcoded) ----
48
+ API_URL = "https://gigcode-superkartbackend.hf.space/v1/sales"
49
 
50
  if st.button("Predict", type="primary"):
51
+ with st.spinner('Forecasting...'):
52
+ try:
53
+ resp = requests.post(API_URL, json=payload, timeout=30)
54
+ if resp.status_code == 200:
55
+ result = resp.json()
56
+ predicted_sales = result.get("Predicted Product_Store_Sales_Total", 0)
57
+ st.success(f"Predicted Product Store Sales Total: ${float(predicted_sales):,.2f}")
58
+ else:
59
+ st.error(f"API error [{resp.status_code}]: {resp.text}")
60
+ except requests.exceptions.RequestException as e:
61
+ st.error(f"Request failed: Could not connect to the API. Please ensure the backend is running. Error: {e}")
62
+ except Exception as e:
63
+ st.error(f"An unexpected error occurred: {e}")