swamu commited on
Commit
81e5b4f
·
verified ·
1 Parent(s): a7b7ee6

Upload folder using huggingface_hub

Browse files
Dockerfile CHANGED
@@ -9,6 +9,14 @@ COPY . .
9
  # Install dependencies from the requirements file without using cache to reduce image size
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
 
 
 
 
 
 
 
 
12
  # Define the command to start the application using Gunicorn with 4 worker processes
13
  # - `-w 4`: Uses 4 worker processes for handling requests
14
  # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
 
9
  # Install dependencies from the requirements file without using cache to reduce image size
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
12
+ # Create a directory for models
13
+ RUN mkdir -p /app/models
14
+
15
+ # Copy the XGBoost model files (these should be in the same directory as Dockerfile)
16
+ COPY best_xgboost_model_20250821_220910.pkl /app/
17
+ COPY xgboost_preprocessor_20250821_220910.pkl /app/
18
+ COPY xgboost_metadata_20250821_220910.pkl /app/
19
+
20
  # Define the command to start the application using Gunicorn with 4 worker processes
21
  # - `-w 4`: Uses 4 worker processes for handling requests
22
  # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
app.py CHANGED
@@ -5,66 +5,137 @@ from flask import Flask, request, jsonify
5
  # Initialize Flask app with a name
6
  app = Flask("SuperKart Sales Forecaster")
7
 
8
- # Load the trained sales forecasting model
9
- model = joblib.load("best_tuned_random_forest_model.pkl")
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # Define a route for the home page
12
  @app.get('/')
13
  def home():
14
- return "Welcome to the SuperKart Sales Forecasting API"
15
 
16
  # Define an endpoint to predict sales for a single product
17
  @app.post('/v1/sales')
18
  def predict_sales():
19
- # Get JSON data from the request
20
- product_data = request.get_json()
21
-
22
- # Extract relevant product features from the input data
23
- sample = {
24
- 'Product_Weight': product_data['Product_Weight'],
25
- 'Product_Allocated_Area': product_data['Product_Allocated_Area'],
26
- 'Product_MRP': product_data['Product_MRP'],
27
- 'Store_Size': product_data['Store_Size'],
28
- 'Store_Location_City_Type': product_data['Store_Location_City_Type'],
29
- 'Store_Type': product_data['Store_Type'],
30
- 'Product_Type': product_data['Product_Type'],
31
- 'Product_Sugar_Content': product_data['Product_Sugar_Content']
32
- }
33
-
34
- # Convert the extracted data into a DataFrame
35
- input_data = pd.DataFrame([sample])
36
-
37
- # Make a sales prediction using the trained model
38
- prediction = model.predict(input_data).tolist()[0]
39
-
40
- # Return the prediction as a JSON response
41
- return jsonify({'Predicted_Sales': f"₹{prediction:.2f}"})
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Define an endpoint to predict sales for a batch of products
44
  @app.post('/v1/salesbatch')
45
  def predict_sales_batch():
46
- # Get the uploaded CSV file from the request
47
- file = request.files['file']
48
-
49
- # Read the file into a DataFrame
50
- input_data = pd.read_csv(file)
51
-
52
- # Make predictions for the batch data
53
- predictions = model.predict(input_data).tolist()
54
-
55
- # Convert predictions to formatted strings
56
- formatted_predictions = [f"₹{pred:.2f}" for pred in predictions]
57
-
58
- # Create output dictionary with product IDs and predictions
59
- if 'Product_Id' in input_data.columns:
60
- product_ids = input_data['Product_Id'].values.tolist()
61
- else:
62
- product_ids = [f"Product_{i+1}" for i in range(len(predictions))]
63
-
64
- output_dict = dict(zip(product_ids, formatted_predictions))
65
-
66
- return output_dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # Run the Flask app in debug mode
69
  if __name__ == '__main__':
70
- app.run(debug=True)
 
 
 
 
 
5
  # Initialize Flask app with a name
6
  app = Flask("SuperKart Sales Forecaster")
7
 
8
+ # Load the trained XGBoost sales forecasting model
9
+ try:
10
+ model = joblib.load("best_xgboost_model_20250821_220910.pkl")
11
+ preprocessor = joblib.load("xgboost_preprocessor_20250821_220910.pkl")
12
+ metadata = joblib.load("xgboost_metadata_20250821_220910.pkl")
13
+ print("✅ XGBoost model loaded successfully!")
14
+ print(f"Model: {metadata.get('model_name', 'XGBoost')}")
15
+ print(f"Training Date: {metadata.get('training_date', 'Unknown')}")
16
+ except Exception as e:
17
+ print(f"❌ Error loading model: {e}")
18
+ model = None
19
+ preprocessor = None
20
+ metadata = None
21
 
22
  # Define a route for the home page
23
  @app.get('/')
24
  def home():
25
+ return "Welcome to the SuperKart Sales Forecasting API (XGBoost)"
26
 
27
  # Define an endpoint to predict sales for a single product
28
  @app.post('/v1/sales')
29
  def predict_sales():
30
+ if model is None:
31
+ return jsonify({'error': 'Model not loaded'}), 500
32
+
33
+ try:
34
+ # Get JSON data from the request
35
+ product_data = request.get_json()
36
+
37
+ # Extract relevant product features from the input data
38
+ sample = {
39
+ 'Product_Weight': product_data['Product_Weight'],
40
+ 'Product_Allocated_Area': product_data['Product_Allocated_Area'],
41
+ 'Product_MRP': product_data['Product_MRP'],
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_Type': product_data['Product_Type'],
46
+ 'Product_Sugar_Content': product_data['Product_Sugar_Content']
47
+ }
48
+
49
+ # Convert the extracted data into a DataFrame
50
+ input_data = pd.DataFrame([sample])
51
+
52
+ # Apply preprocessing if available
53
+ if preprocessor is not None:
54
+ processed_data = preprocessor.transform(input_data)
55
+ else:
56
+ processed_data = input_data
57
+
58
+ # Make a sales prediction using the trained XGBoost model
59
+ prediction = model.predict(processed_data).tolist()[0]
60
+
61
+ # Return the prediction as a JSON response
62
+ return jsonify({'Predicted_Sales': f"₹{prediction:.2f}"})
63
+
64
+ except Exception as e:
65
+ return jsonify({'error': f'Prediction failed: {str(e)}'}), 500
66
 
67
  # Define an endpoint to predict sales for a batch of products
68
  @app.post('/v1/salesbatch')
69
  def predict_sales_batch():
70
+ if model is None:
71
+ return jsonify({'error': 'Model not loaded'}), 500
72
+
73
+ try:
74
+ # Get the uploaded CSV file from the request
75
+ file = request.files['file']
76
+
77
+ # Read the file into a DataFrame
78
+ input_data = pd.read_csv(file)
79
+
80
+ # Apply preprocessing if available
81
+ if preprocessor is not None:
82
+ processed_data = preprocessor.transform(input_data)
83
+ else:
84
+ processed_data = input_data
85
+
86
+ # Make predictions for the batch data
87
+ predictions = model.predict(processed_data).tolist()
88
+
89
+ # Convert predictions to formatted strings
90
+ formatted_predictions = [f"₹{pred:.2f}" for pred in predictions]
91
+
92
+ # Create output dictionary with product IDs and predictions
93
+ if 'Product_Id' in input_data.columns:
94
+ product_ids = input_data['Product_Id'].values.tolist()
95
+ else:
96
+ product_ids = [f"Product_{i+1}" for i in range(len(predictions))]
97
+
98
+ output_dict = dict(zip(product_ids, formatted_predictions))
99
+
100
+ return output_dict
101
+
102
+ except Exception as e:
103
+ return jsonify({'error': f'Batch prediction failed: {str(e)}'}), 500
104
+
105
+ # Health check endpoint
106
+ @app.get('/health')
107
+ def health_check():
108
+ if model is None:
109
+ return jsonify({
110
+ 'status': 'unhealthy',
111
+ 'model_loaded': False,
112
+ 'model_type': 'XGBoost'
113
+ }), 500
114
+
115
+ return jsonify({
116
+ 'status': 'healthy',
117
+ 'model_loaded': True,
118
+ 'model_type': 'XGBoost',
119
+ 'metadata': metadata
120
+ })
121
+
122
+ # Model info endpoint
123
+ @app.get('/model-info')
124
+ def model_info():
125
+ if model is None:
126
+ return jsonify({'error': 'XGBoost model not loaded'}), 500
127
+
128
+ return jsonify({
129
+ 'model_type': 'XGBoost Regressor',
130
+ 'model_loaded': True,
131
+ 'preprocessor_loaded': preprocessor is not None,
132
+ 'metadata': metadata
133
+ })
134
 
135
  # Run the Flask app in debug mode
136
  if __name__ == '__main__':
137
+ if model is not None:
138
+ print("�� Starting SuperKart Sales Forecasting API with XGBoost...")
139
+ app.run(debug=True, host='0.0.0.0', port=5000)
140
+ else:
141
+ print("❌ Cannot start API: Model not loaded")
best_xgboost_model_20250821_220910.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8431281a8287cd895c36a1942e1525c589a44b596abb2b79eefd93c86c749cda
3
+ size 1416350
xgboost_metadata_20250821_220910.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ebe4a86554a6ec16fe2ad52eee8ac69418f6ea9cf3dbbe88fbb295f46d5d3f9c
3
+ size 1197
xgboost_preprocessor_20250821_220910.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:44de370800e33315d314c49a51096d1b76bfc248162eebe229c587585c534f8b
3
+ size 70173