File size: 6,523 Bytes
a9fdd98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51f1c5f
 
 
 
a9fdd98
 
 
 
 
 
 
 
 
51f1c5f
 
a9fdd98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

from flask import Flask, request, jsonify
import pandas as pd
import joblib
import numpy as np
import logging
import json

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize Flask app
app = Flask(__name__)

# Load the trained model
try:
    model = joblib.load("best_sales_forecasting_model.pkl")
    logger.info("Model loaded successfully")
except Exception as e:
    logger.error(f"Error loading model: {str(e)}")
    model = None

@app.route('/', methods=['GET'])
def home():
    """Health check endpoint"""
    return jsonify({
        "message": "SuperKart Sales Forecasting API is running!",
        "status": "healthy",
        "model_loaded": model is not None
    })

@app.route('/predict', methods=['POST'])
def predict():
    """Predict sales revenue endpoint"""
    try:
        # Get JSON data from request
        data = request.get_json()
        
        if not data:
            return jsonify({"error": "No data provided"}), 400
        
        if model is None:
            return jsonify({"error": "Model not loaded"}), 500
        
        # Validate required fields
        required_fields = [
            'Product_Weight',
            'Product_Sugar_Content',
            'Product_Allocated_Area',
            'Product_Type',
            'Product_MRP',
            'Store_Establishment_Year',
            'Store_Size',
            'Store_Location_City_Type',
            'Store_Type'
        ]
        
        missing_fields = [field for field in required_fields if field not in data]
        if missing_fields:
            return jsonify({
                "error": "Missing required fields",
                "missing_fields": missing_fields
            }), 400
        
        # Create DataFrame for prediction
        input_data = pd.DataFrame([{
            'Product_Weight': float(data['Product_Weight']),
            'Product_Sugar_Content': str(data['Product_Sugar_Content']),
            'Product_Allocated_Area': float(data['Product_Allocated_Area']),
            'Product_Type': str(data['Product_Type']),
            'Product_MRP': float(data['Product_MRP']),
            'Store_Establishment_Year': int(data['Store_Establishment_Year']),
            'Store_Size': str(data['Store_Size']),
            'Store_Location_City_Type': str(data['Store_Location_City_Type']),
            'Store_Type': str(data['Store_Type'])
        }])
        
        # Make prediction
        prediction = model.predict(input_data)[0]
        
        # Prepare response
        response = {
            "prediction": float(prediction),
            "formatted_prediction": f"₹ {prediction:,.2f}",
            "input_data": data,
            "status": "success"
        }
        
        logger.info(f"Prediction made: {prediction}")
        return jsonify(response)
        
    except ValueError as e:
        logger.error(f"Validation error: {str(e)}")
        return jsonify({"error": f"Invalid data format: {str(e)}"}), 400
        
    except Exception as e:
        logger.error(f"Prediction error: {str(e)}")
        return jsonify({"error": f"Prediction failed: {str(e)}"}), 500

@app.route('/model-info', methods=['GET'])
def model_info():
    """Get model information"""
    try:
        if model is None:
            return jsonify({"error": "Model not loaded"}), 500
            
        # Get model type
        model_type = str(type(model)).split('.')[-1].replace("'>", "")
        
        return jsonify({
            "model_type": model_type,
            "model_loaded": True,
            "supported_features": [
                'Product_Weight',
                'Product_Sugar_Content',
                'Product_Allocated_Area', 
                'Product_Type',
                'Product_MRP',
                'Store_Establishment_Year',
                'Store_Size',
                'Store_Location_City_Type',
                'Store_Type'
            ],
            "prediction_type": "Sales Revenue Forecasting"
        })
        
    except Exception as e:
        logger.error(f"Model info error: {str(e)}")
        return jsonify({"error": str(e)}), 500

@app.route('/batch-predict', methods=['POST'])
def batch_predict():
    """Batch prediction endpoint"""
    try:
        data = request.get_json()
        
        if not data or 'predictions' not in data:
            return jsonify({"error": "No batch data provided"}), 400
        
        if model is None:
            return jsonify({"error": "Model not loaded"}), 500
        
        predictions = []
        errors = []
        
        for i, item in enumerate(data['predictions']):
            try:
                # Calculate derived features
                current_year = 2025
                price_efficiency = float(item['Product_MRP']) * 0.1
                
                # Create DataFrame for prediction
                input_data = pd.DataFrame([{
                    'Product_Weight': float(item['Product_Weight']),
                    'Product_Sugar_Content': str(item['Product_Sugar_Content']),
                    'Product_Allocated_Area': float(item['Product_Allocated_Area']),
                    'Product_Type': str(item['Product_Type']),
                    'Product_MRP': float(item['Product_MRP']),
                    'Store_Size': str(item['Store_Size']),
                    'Store_Location_City_Type': str(item['Store_Location_City_Type']),
                    'Store_Type': str(item['Store_Type']),
                    'Price_Efficiency': price_efficiency
                }])
                
                prediction = model.predict(input_data)[0]
                predictions.append({
                    "index": i,
                    "prediction": float(prediction),
                    "formatted_prediction": f"₹ {prediction:,.2f}"
                })
                
            except Exception as e:
                errors.append({
                    "index": i,
                    "error": str(e)
                })
        
        return jsonify({
            "predictions": predictions,
            "errors": errors,
            "total_processed": len(data['predictions']),
            "successful_predictions": len(predictions),
            "failed_predictions": len(errors)
        })
        
    except Exception as e:
        logger.error(f"Batch prediction error: {str(e)}")
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=False)