Lokiiparihar commited on
Commit
7fd62e3
·
verified ·
1 Parent(s): 943b0f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -155
app.py CHANGED
@@ -1,166 +1,68 @@
1
- import re
2
  import streamlit as st
3
  import pandas as pd
4
  import joblib
5
 
6
- # ================================
7
  # Load trained model
8
- # ================================
9
- MODEL_PATH = "superkart_sales_prediction_model_v1_0.joblib"
10
- model = joblib.load(MODEL_PATH)
11
-
12
- # ================================
13
- # Training feature columns (must match training dataset)
14
- # ================================
15
- FEATURE_COLUMNS = [
16
- "Product_Weight","Product_Allocated_Area","Product_MRP","Store_Age",
17
- "Product_Sugar_Content_no sugar","Product_Sugar_Content_regular",
18
- "Product_Type_breads","Product_Type_breakfast","Product_Type_canned",
19
- "Product_Type_dairy","Product_Type_frozen foods",
20
- "Product_Type_fruits and vegetables","Product_Type_hard drinks",
21
- "Product_Type_health and hygiene","Product_Type_household",
22
- "Product_Type_meat","Product_Type_others","Product_Type_seafood",
23
- "Product_Type_snack foods","Product_Type_soft drinks",
24
- "Product_Type_starchy foods",
25
- "Store_Size_medium","Store_Size_small",
26
- "Store_Location_City_Type_tier 2","Store_Location_City_Type_tier 3",
27
- "Store_Id_out002","Store_Id_out003","Store_Id_out004",
28
- "Store_Type_food mart","Store_Type_supermarket type1","Store_Type_supermarket type2",
29
- "Product_Group_Code_fd","Product_Group_Code_nc"
30
- ]
31
-
32
- # ================================
33
- # Store metadata
34
- # ================================
35
- valid_store_ids = ["OUT001", "OUT002", "OUT003", "OUT004"]
36
- store_meta = {
37
- "OUT001": {"year": 1987, "size": "High", "city_type": "Tier 2", "store_type": "Supermarket Type1"},
38
- "OUT002": {"year": 1998, "size": "Small", "city_type": "Tier 3", "store_type": "Food Mart"},
39
- "OUT003": {"year": 1999, "size": "Medium", "city_type": "Tier 1", "store_type": "Departmental Store"},
40
- "OUT004": {"year": 2009, "size": "Medium", "city_type": "Tier 2", "store_type": "Supermarket Type2"},
41
- }
42
-
43
- # ================================
44
- # Preprocessing function
45
- # ================================
46
- def preprocess_input(user_inputs):
47
- """Convert raw form inputs into one-hot encoded feature vector."""
48
- row = dict.fromkeys(FEATURE_COLUMNS, 0)
49
-
50
- # Numeric
51
- row["Product_Weight"] = user_inputs["Product_Weight"]
52
- row["Product_Allocated_Area"] = user_inputs["Product_Allocated_Area"]
53
- row["Product_MRP"] = user_inputs["Product_MRP"]
54
- row["Store_Age"] = 2025 - user_inputs["Store_Establishment_Year"]
55
-
56
- # Sugar content
57
- if user_inputs["Product_Sugar_Content"].lower() == "no sugar":
58
- row["Product_Sugar_Content_no sugar"] = 1
59
- elif user_inputs["Product_Sugar_Content"].lower() == "regular":
60
- row["Product_Sugar_Content_regular"] = 1
61
- # (low sugar is baseline → all zeros)
62
-
63
- # Product type
64
- col_name = f"Product_Type_{user_inputs['Product_Type'].lower()}"
65
- if col_name in row:
66
- row[col_name] = 1
67
-
68
- # Store size
69
- if user_inputs["Store_Size"].lower() == "medium":
70
- row["Store_Size_medium"] = 1
71
- elif user_inputs["Store_Size"].lower() == "small":
72
- row["Store_Size_small"] = 1
73
- # (high = baseline)
74
-
75
- # Store location
76
- if user_inputs["Store_Location_City_Type"].lower() == "tier 2":
77
- row["Store_Location_City_Type_tier 2"] = 1
78
- elif user_inputs["Store_Location_City_Type"].lower() == "tier 3":
79
- row["Store_Location_City_Type_tier 3"] = 1
80
- # (tier 1 = baseline)
81
-
82
- # Store ID
83
- if user_inputs["Store_Id"].lower() == "out002":
84
- row["Store_Id_out002"] = 1
85
- elif user_inputs["Store_Id"].lower() == "out003":
86
- row["Store_Id_out003"] = 1
87
- elif user_inputs["Store_Id"].lower() == "out004":
88
- row["Store_Id_out004"] = 1
89
- # (out001 = baseline)
90
-
91
- # Store Type
92
- if user_inputs["Store_Type"].lower() == "food mart":
93
- row["Store_Type_food mart"] = 1
94
- elif user_inputs["Store_Type"].lower() == "supermarket type1":
95
- row["Store_Type_supermarket type1"] = 1
96
- elif user_inputs["Store_Type"].lower() == "supermarket type2":
97
- row["Store_Type_supermarket type2"] = 1
98
- # (departmental store = baseline)
99
-
100
- # Product group code (dummy rule: FD → food, NC → non-consumable)
101
- if user_inputs["Product_Id"].startswith("FD"):
102
- row["Product_Group_Code_fd"] = 1
103
- else:
104
- row["Product_Group_Code_nc"] = 1
105
-
106
- return pd.DataFrame([row], columns=FEATURE_COLUMNS)
107
-
108
- # ================================
109
- # Streamlit UI
110
- # ================================
111
- st.title("SmartKart: Product Sales Prediction")
112
- st.subheader("Online Prediction")
113
-
114
- # Product ID validation
115
- product_id = st.text_input("Product ID (2 uppercase letters + 4 digits, e.g., FD6114)", max_chars=6)
116
- product_id_valid = bool(re.fullmatch(r"[A-Z]{2}\d{4}", product_id))
117
- if product_id and not product_id_valid:
118
- st.error("Invalid Product ID format; it must be 2 uppercase letters followed by 4 digits.")
119
- elif product_id_valid:
120
- st.success("Product ID format is valid.")
121
-
122
- # Store selection
123
- store_id = st.selectbox("Store ID", options=valid_store_ids)
124
- meta = store_meta[store_id]
125
- st.text(f"Store Establishment Year: {meta['year']}")
126
- st.text(f"Store Size: {meta['size']}")
127
- st.text(f"Store Location City Type: {meta['city_type']}")
128
- st.text(f"Store Type: {meta['store_type']}")
129
-
130
- # Product features
131
- product_weight = st.number_input("Product Weight", min_value=0.0, value=12.0, format="%.2f")
132
- product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
133
- product_allocated_area = st.slider("Product Allocated Area Ratio", min_value=0.0, max_value=1.0, step=0.001, value=0.05)
134
- product_type = st.selectbox("Product Type", [
135
- "Meat", "Snack Foods", "Hard Drinks", "Dairy", "Canned", "Soft Drinks",
136
- "Health and Hygiene", "Baking Goods", "Bread", "Breakfast", "Frozen Foods",
137
- "Fruits and Vegetables", "Household", "Seafood", "Starchy Foods", "Others"
138
- ])
139
- product_mrp = st.number_input("Product MRP", min_value=0.0, value=150.0, format="%.2f")
140
-
141
- # Collect inputs
142
- user_inputs = {
143
- "Product_Id": product_id,
144
  "Product_Weight": product_weight,
145
- "Product_Sugar_Content": product_sugar_content,
146
  "Product_Allocated_Area": product_allocated_area,
147
- "Product_Type": product_type,
148
  "Product_MRP": product_mrp,
149
- "Store_Id": store_id,
150
- "Store_Establishment_Year": meta['year'],
151
- "Store_Size": meta['size'],
152
- "Store_Location_City_Type": meta['city_type'],
153
- "Store_Type": meta['store_type'],
 
 
 
154
  }
155
 
156
- # Predict button
 
 
 
 
 
 
157
  if st.button("Predict"):
158
- if not product_id_valid:
159
- st.error("Please fix the Product ID before proceeding.")
160
- else:
161
- try:
162
- processed = preprocess_input(user_inputs)
163
- prediction = model.predict(processed)[0]
164
- st.success(f"Predicted Product Sales: {prediction:.2f}")
165
- except Exception as e:
166
- st.error(f"Prediction failed: {e}")
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import joblib
4
 
 
5
  # Load trained model
6
+ @st.cache_resource
7
+ def load_model():
8
+ return joblib.load("superkart_sales_prediction_model_v1_0.joblib")
9
+
10
+ model = load_model()
11
+
12
+ st.set_page_config(page_title="SuperKart Sales Predictor", layout="wide")
13
+ st.title("🛒 SuperKart Sales Prediction App")
14
+
15
+ st.write("Fill in the product and store details below to predict sales.")
16
+
17
+ # Input fields
18
+ col1, col2 = st.columns(2)
19
+
20
+ with col1:
21
+ product_weight = st.number_input("Product Weight", min_value=0.0, step=0.01)
22
+ product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, step=0.001)
23
+ product_mrp = st.number_input("Product MRP", min_value=0.0, step=0.01)
24
+ product_store_sales_total = st.number_input("Product Store Sales Total", min_value=0.0, step=0.01)
25
+ store_age = st.number_input("Store Age (Years)", min_value=0, step=1)
26
+
27
+ with col2:
28
+ product_sugar_content = st.selectbox("Sugar Content", ["no sugar", "regular"])
29
+ product_type = st.selectbox(
30
+ "Product Type",
31
+ ["breads", "breakfast", "canned", "dairy", "frozen foods",
32
+ "fruits and vegetables", "hard drinks", "health and hygiene",
33
+ "household", "meat", "others", "seafood", "snack foods",
34
+ "soft drinks", "starchy foods"]
35
+ )
36
+ store_size = st.selectbox("Store Size", ["small", "medium"])
37
+ store_location_city_type = st.selectbox("Store Location City Type", ["tier 2", "tier 3"])
38
+ store_type = st.selectbox("Store Type", ["food mart", "supermarket type1", "supermarket type2"])
39
+ product_group_code = st.selectbox("Product Group Code", ["fd", "nc"])
40
+
41
+ # Convert inputs to DataFrame
42
+ input_data = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  "Product_Weight": product_weight,
 
44
  "Product_Allocated_Area": product_allocated_area,
 
45
  "Product_MRP": product_mrp,
46
+ "Product_Store_Sales_Total": product_store_sales_total,
47
+ "Store_Age": store_age,
48
+ f"Product_Sugar_Content_{product_sugar_content}": True,
49
+ f"Product_Type_{product_type}": True,
50
+ f"Store_Size_{store_size}": True,
51
+ f"Store_Location_City_Type_{store_location_city_type}": True,
52
+ f"Store_Type_{store_type}": True,
53
+ f"Product_Group_Code_{product_group_code}": True
54
  }
55
 
56
+ input_df = pd.DataFrame([input_data]).astype(object)
57
+
58
+ # Align with model features
59
+ if hasattr(model, "feature_names_in_"):
60
+ input_df = input_df.reindex(columns=model.feature_names_in_, fill_value=0)
61
+
62
+ # Prediction
63
  if st.button("Predict"):
64
+ try:
65
+ prediction = model.predict(input_df)[0]
66
+ st.success(f"Predicted Product Sales: {prediction:.2f}")
67
+ except Exception as e:
68
+ st.error(f"Prediction failed: {e}")