Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +8 -0
- app.py +119 -48
- best_xgboost_model_20250821_220910.pkl +3 -0
- xgboost_metadata_20250821_220910.pkl +3 -0
- xgboost_preprocessor_20250821_220910.pkl +3 -0
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Define an endpoint to predict sales for a batch of products
|
| 44 |
@app.post('/v1/salesbatch')
|
| 45 |
def predict_sales_batch():
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Run the Flask app in debug mode
|
| 69 |
if __name__ == '__main__':
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|