File size: 2,137 Bytes
316153b
 
 
 
 
 
 
930b6ee
 
 
316153b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import joblib
from flask import Flask, request, jsonify
import os # Import os for debugging

superkart_api = Flask(__name__)

# Define MODEL_FILENAME inside the app.py string
MODEL_FILENAME = "tuned_gradient_boosting_regressor_model.joblib" # <-- ADD THIS LINE

print("--- APP STARTUP: Flask app instance created ---")

# Load the trained model
try:
    print(f"--- APP STARTUP: Attempting to load model from {os.path.join(os.getcwd(), MODEL_FILENAME)} ---")
    model = joblib.load("tuned_gradient_boosting_regressor_model.joblib")
    print("--- APP STARTUP: Model loaded successfully ---")
except Exception as e:
    print(f"--- APP STARTUP: ERROR loading model: {e} ---")
    raise # Re-raise to crash early if model loading fails

print("--- APP STARTUP: Model variable assigned ---")

@superkart_api.get('/')
def home():
    print("--- REQUEST: Home endpoint accessed ---")
    return "Welcome to the Superkart Sales Prediction API!"

@superkart_api.post('/v1/predict')
def predict_sales():
    print("--- REQUEST: Predict endpoint accessed ---")
    data = request.get_json()
    # ... (rest of your predict_sales logic) ...
    sample = {
        'Product_Weight': data['Product_Weight'],
        'Product_Sugar_Content': data['Product_Sugar_Content'],
        'Product_Allocated_Area': data['Product_Allocated_Area'],
        'Product_MRP': data['Product_MRP'],
        'Store_Size': data['Store_Size'],
        'Store_Location_City_Type': data['Store_Location_City_Type'],
        'Store_Type': data['Store_Type'],
        'Product_Id_char': data['Product_Id_char'],
        'Store_Age_Years': data['Store_Age_Years'],
        'Product_Type_Category': data['Product_Type_Category']
    }
    input_df = pd.DataFrame([sample])
    prediction = model.predict(input_df).tolist()[0]
    print(f"--- REQUEST: Prediction made: {prediction} ---")
    return jsonify({'Sales': prediction})

if __name__ == '__main__':
    print("--- APP STARTUP: Running in __main__ block ---")
    # import os # Redundant import removed
    port = int(os.environ.get("PORT", 7860))
    superkart_api.run(host="0.0.0.0", port=port)