SandeepMM commited on
Commit
1e9e683
·
verified ·
1 Parent(s): 215a9c7

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +117 -107
app.py CHANGED
@@ -1,107 +1,117 @@
1
-
2
- import sys
3
- import joblib
4
- import pandas as pd
5
- import numpy as np
6
- from flask import Flask, request, jsonify
7
-
8
- from sklearn.pipeline import Pipeline
9
- from sklearn.compose import ColumnTransformer
10
- from sklearn.base import BaseEstimator, TransformerMixin
11
- from sklearn.preprocessing import OneHotEncoder, StandardScaler, LabelEncoder
12
- from sklearn.impute import SimpleImputer
13
- from sklearn.ensemble import RandomForestRegressor
14
- from xgboost import XGBRegressor # Included for compatibility if you switch models
15
-
16
- class FeatureEngineer(BaseEstimator, TransformerMixin):
17
- def __init__(self):
18
- self.le_prod = LabelEncoder()
19
- self.le_store = LabelEncoder()
20
-
21
- def fit(self, X, y=None):
22
- X_copy = X.copy()
23
- X_copy['Product_Id_Cd'] = X_copy['Product_Id'].apply(lambda x: x[:2])
24
- X_copy['Product_Sugar_Content_Corr'] = X_copy['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
25
- X_copy['Operation_Years'] = 2025 - X_copy['Store_Establishment_Year']
26
-
27
- self.le_prod.fit(X_copy['Product_Id_Cd'])
28
- le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
29
- for i in le_feat:
30
- self.le_prod.fit(X_copy[i])
31
-
32
- self.le_store.fit(X_copy['Store_Id'])
33
- return self
34
-
35
- def transform(self, X):
36
- X_copy = X.copy()
37
- X_copy['Product_Id_Cd'] = X_copy['Product_Id'].apply(lambda x: x[:2])
38
- X_copy['Product_Sugar_Content_Corr'] = X_copy['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
39
- X_copy['Operation_Years'] = 2013 - X_copy['Store_Establishment_Year']
40
-
41
- try:
42
- le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
43
- for i in le_feat:
44
- X_copy[i] = self.le_prod.transform(X_copy[i])
45
- except ValueError:
46
- X_copy['Product_Id_Cd'] = -1
47
-
48
- try:
49
- X_copy['Store'] = self.le_store.transform(X_copy['Store_Id'])
50
- except ValueError:
51
- X_copy['Store'] = -1
52
-
53
- rem_feat=['Product_Id','Store_Id','Product_Sugar_Content','Product_Type', 'Store_Establishment_Year']
54
- X_copy.drop(rem_feat, axis=1, inplace=True)
55
-
56
- return X_copy
57
-
58
- import sys
59
-
60
- # This allows joblib's pickle to find the class reference it saved during training.
61
- sys.modules['__main__'].FeatureEngineer = FeatureEngineer
62
-
63
- # Initialize Flask app with a name
64
- app = Flask("SuperKart Sales Predictor")
65
-
66
- # Load the trained churn prediction model
67
- model = joblib.load("XGBoostRegressor_BEST_Pipeline.joblib")
68
-
69
- # Define a route for the home page
70
- @app.get('/')
71
- def home():
72
- return "Welcome to the SuperKart Sales Prediction API"
73
-
74
- # Define an endpoint to predict churn for a single customer
75
- @app.post('/v1/product')
76
- def predict_churn():
77
- # Get JSON data from the request
78
- customer_data = request.get_json()
79
-
80
- # Extract relevant customer features from the input data
81
- sample = {
82
- 'Product_Id': customer_data['Product_Id'],
83
- 'Product_Weight': customer_data['Product_Weight'],
84
- 'Product_Sugar_Content': customer_data['Product_Sugar_Content'],
85
- 'Product_Allocated_Area': customer_data['Product_Allocated_Area'],
86
- 'Product_Type': customer_data['Product_Type'],
87
- 'Product_MRP': customer_data['Product_MRP'],
88
- 'Store_Id': customer_data['Store_Id'],
89
- 'Store_Establishment_Year': customer_data['Store_Establishment_Year'],
90
- 'Store_Size': customer_data['Store_Size'],
91
- 'Store_Location_City_Type': customer_data['Store_Location_City_Type'],
92
- 'Store_Type': customer_data['Store_Type']
93
- }
94
-
95
- # Convert the extracted data into a DataFrame
96
- input_data = pd.DataFrame([sample])
97
-
98
- # Make a Sales prediction using the trained model
99
- prediction = model.predict(input_data).tolist()[0]
100
-
101
- # Return the prediction as a JSON response
102
- return jsonify({'Prediction': prediction})
103
-
104
-
105
- # Run the Flask app in debug mode
106
- if __name__ == '__main__':
107
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import sys
3
+ import joblib
4
+ import pandas as pd
5
+ import numpy as np
6
+ from flask import Flask, request, jsonify
7
+
8
+ from sklearn.pipeline import Pipeline
9
+ from sklearn.compose import ColumnTransformer
10
+ from sklearn.base import BaseEstimator, TransformerMixin
11
+ from sklearn.preprocessing import OneHotEncoder, StandardScaler, LabelEncoder
12
+ from sklearn.impute import SimpleImputer
13
+ from sklearn.ensemble import RandomForestRegressor
14
+ from xgboost import XGBRegressor # Included for compatibility if you switch models
15
+
16
+ class FeatureEngineer(BaseEstimator, TransformerMixin):
17
+ def __init__(self):
18
+ self.le_prod = LabelEncoder()
19
+ self.le_store = LabelEncoder()
20
+
21
+ def fit(self, X, y=None):
22
+ X_copy = X.copy()
23
+ X_copy['Product_Id_Cd'] = X_copy['Product_Id'].apply(lambda x: x[:2])
24
+ X_copy['Product_Sugar_Content_Corr'] = X_copy['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
25
+ X_copy['Operation_Years'] = 2025 - X_copy['Store_Establishment_Year']
26
+
27
+ self.le_prod.fit(X_copy['Product_Id_Cd'])
28
+ le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
29
+ for i in le_feat:
30
+ self.le_prod.fit(X_copy[i])
31
+
32
+ self.le_store.fit(X_copy['Store_Id'])
33
+ return self
34
+
35
+ def transform(self, X):
36
+ X_copy = X.copy()
37
+ X_copy['Product_Id_Cd'] = X_copy['Product_Id'].apply(lambda x: x[:2])
38
+ X_copy['Product_Sugar_Content_Corr'] = X_copy['Product_Sugar_Content'].str.replace('reg', 'Regular', regex=True)
39
+ X_copy['Operation_Years'] = 2013 - X_copy['Store_Establishment_Year']
40
+
41
+ try:
42
+ le_feat=['Product_Sugar_Content_Corr','Store_Size','Store_Location_City_Type','Store_Type','Product_Id_Cd']
43
+ for i in le_feat:
44
+ X_copy[i] = self.le_prod.transform(X_copy[i])
45
+ except ValueError:
46
+ X_copy['Product_Id_Cd'] = -1
47
+
48
+ try:
49
+ X_copy['Store'] = self.le_store.transform(X_copy['Store_Id'])
50
+ except ValueError:
51
+ X_copy['Store'] = -1
52
+
53
+ rem_feat=['Product_Id','Store_Id','Product_Sugar_Content','Product_Type', 'Store_Establishment_Year']
54
+ X_copy.drop(rem_feat, axis=1, inplace=True)
55
+
56
+ return X_copy
57
+
58
+ # This allows joblib's pickle to find the class reference it saved during training.
59
+ sys.modules['__main__'].FeatureEngineer = FeatureEngineer
60
+
61
+ # Initialize Flask app with a name
62
+ app = Flask("SuperKart Sales Predictor")
63
+
64
+ # Load the trained churn prediction model
65
+ model = joblib.load("XGBoostRegressor_BEST_Pipeline.joblib")
66
+
67
+ # Define a route for the home page
68
+ @app.get('/')
69
+ def home():
70
+ return "Welcome to the SuperKart Sales Prediction API"
71
+
72
+ # Define an endpoint to predict churn for a single customer
73
+ @app.post('/v1/product')
74
+ def predict_sales():
75
+ # Get JSON data from the request
76
+ customer_data = request.get_json()
77
+
78
+ # Use .get() with a default value to avoid a KeyError
79
+ required_keys = ['Product_Id', 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area',
80
+ 'Product_Type', 'Product_MRP', 'Store_Id', 'Store_Establishment_Year',
81
+ 'Store_Size', 'Store_Location_City_Type', 'Store_Type']
82
+
83
+ sample = {}
84
+ for key in required_keys:
85
+ sample[key] = customer_data.get(key)
86
+
87
+ if sample[key] is None:
88
+ return jsonify({'error': f'Missing key: {key}'}), 400
89
+
90
+ # Extract relevant customer features from the input data
91
+ sample = {
92
+ 'Product_Id': customer_data['Product_Id'],
93
+ 'Product_Weight': customer_data['Product_Weight'],
94
+ 'Product_Sugar_Content': customer_data['Product_Sugar_Content'],
95
+ 'Product_Allocated_Area': customer_data['Product_Allocated_Area'],
96
+ 'Product_Type': customer_data['Product_Type'],
97
+ 'Product_MRP': customer_data['Product_MRP'],
98
+ 'Store_Id': customer_data['Store_Id'],
99
+ 'Store_Establishment_Year': customer_data['Store_Establishment_Year'],
100
+ 'Store_Size': customer_data['Store_Size'],
101
+ 'Store_Location_City_Type': customer_data['Store_Location_City_Type'],
102
+ 'Store_Type': customer_data['Store_Type']
103
+ }
104
+
105
+ # Convert the extracted data into a DataFrame
106
+ input_data = pd.DataFrame([sample])
107
+
108
+ # Make a Sales prediction using the trained model
109
+ prediction = model.predict(input_data).tolist()[0]
110
+
111
+ # Return the prediction as a JSON response
112
+ return jsonify({'Prediction': prediction})
113
+
114
+
115
+ # Run the Flask app in debug mode
116
+ if __name__ == '__main__':
117
+ app.run(debug=True)