Garg06 commited on
Commit
ce1d05c
·
verified ·
1 Parent(s): 3e72608

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -29,8 +29,8 @@ def predict_product_sales():
29
  # Get the JSON data from the request body
30
  product_data = request.get_json()
31
 
32
- # Extract relevant features from the JSON data
33
- sample = {
34
  'Product_Id': product_data['Product_Id'],
35
  'Product_Weight': product_data['Product_Weight'],
36
  'Product_Sugar_Content': product_data['Product_Sugar_Content'],
@@ -41,14 +41,22 @@ def predict_product_sales():
41
  'Store_Establishment_Year': product_data['Store_Establishment_Year'],
42
  'Store_Size': product_data['Store_Size'],
43
  'Store_Location_City_Type': product_data['Store_Location_City_Type'],
44
- 'Store_Type': product_data['Store_Type'],
45
- 'Product_Category_from_ID': product_data['Product_Category_from_ID'], # Include engineered features
46
- 'Store_Age': product_data['Store_Age'],
47
- 'Product_Category_Type': product_data['Product_Category_Type']
48
  }
49
 
50
- # Convert the extracted data into a Pandas DataFrame
51
- input_data = pd.DataFrame([sample])
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Make prediction and round to 2 decimal places
54
  prediction = round(model.predict(input_data).tolist()[0], 2)
 
29
  # Get the JSON data from the request body
30
  product_data = request.get_json()
31
 
32
+ # Extract original features from the JSON data
33
+ original_features = {
34
  'Product_Id': product_data['Product_Id'],
35
  'Product_Weight': product_data['Product_Weight'],
36
  'Product_Sugar_Content': product_data['Product_Sugar_Content'],
 
41
  'Store_Establishment_Year': product_data['Store_Establishment_Year'],
42
  'Store_Size': product_data['Store_Size'],
43
  'Store_Location_City_Type': product_data['Store_Location_City_Type'],
44
+ 'Store_Type': product_data['Store_Type']
 
 
 
45
  }
46
 
47
+ # Convert to DataFrame to easily calculate engineered features
48
+ input_data = pd.DataFrame([original_features])
49
+
50
+ # Calculate engineered features
51
+ current_year = pd.to_datetime('now').year
52
+ input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year']
53
+
54
+ perishables = ['Fruits and Vegetables', 'Dairy', 'Meat', 'Seafood', 'Breads', 'Breakfast']
55
+ input_data['Product_Category_Type'] = input_data['Product_Type'].apply(lambda x: 'Perishables' if x in perishables else 'Non Perishables')
56
+
57
+ # Assuming Product_Id is in the format 'XX####' and we need the first two characters
58
+ input_data['Product_Category_from_ID'] = input_data['Product_Id'].apply(lambda x: x[:2])
59
+
60
 
61
  # Make prediction and round to 2 decimal places
62
  prediction = round(model.predict(input_data).tolist()[0], 2)