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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -105
app.py CHANGED
@@ -1,105 +1,107 @@
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_churn():
75
- # Get JSON data from the request
76
- customer_data = request.get_json()
77
-
78
- # Extract relevant customer features from the input data
79
- sample = {
80
- 'Product_Id': customer_data['Product_Id'],
81
- 'Product_Weight': customer_data['Product_Weight'],
82
- 'Product_Sugar_Content': customer_data['Product_Sugar_Content'],
83
- 'Product_Allocated_Area': customer_data['Product_Allocated_Area'],
84
- 'Product_Type': customer_data['Product_Type'],
85
- 'Product_MRP': customer_data['Product_MRP'],
86
- 'Store_Id': customer_data['Store_Id'],
87
- 'Store_Establishment_Year': customer_data['Store_Establishment_Year'],
88
- 'Store_Size': customer_data['Store_Size'],
89
- 'Store_Location_City_Type': customer_data['Store_Location_City_Type'],
90
- 'Store_Type': customer_data['Store_Type']
91
- }
92
-
93
- # Convert the extracted data into a DataFrame
94
- input_data = pd.DataFrame([sample])
95
-
96
- # Make a Sales prediction using the trained model
97
- prediction = model.predict(input_data).tolist()[0]
98
-
99
- # Return the prediction as a JSON response
100
- return jsonify({'Prediction': prediction})
101
-
102
-
103
- # Run the Flask app in debug mode
104
- if __name__ == '__main__':
105
- 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
+ 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)