File size: 2,662 Bytes
3afe213
ff98d58
3afe213
4d412f1
 
8954400
 
 
86359e6
 
 
 
 
3ca4c9f
 
 
86359e6
3ca4c9f
0de55e6
d5e5341
4cd5505
 
8954400
3ca4c9f
 
 
 
 
 
 
8954400
86359e6
cb2ff2c
4cd5505
cb2ff2c
93ebbdb
cb2ff2c
ab25d36
302e6bd
365f0ea
 
 
 
817458e
71c50d4
365f0ea
 
4cd5505
365f0ea
3153b96
 
 
 
 
 
 
 
 
 
365f0ea
4cd5505
365f0ea
 
4cd5505
365f0ea
 
4cd5505
365f0ea
 
4cd5505
365f0ea
 
4cd5505
3afe213
8954400
ff98d58
b566529
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

from flask import Flask, request, jsonify
import pandas as pd
import joblib # Using pickle for model loading
from sklearn.compose import ColumnTransformer
import traceback
import numpy as np
import os
from typing import Iterable, Optional, Any
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
import joblib 
# 💡 CRITICAL: Import your custom class before loading the model
from custom_transformers import ManualProductTypeMapper 

# --- Global Model Loading ---
MODEL_PATH = "final_xgboost_pipeline.joblib" 

# Initialize the Flask application
superKart_sales_predictor_api = Flask("SuperKart Sales Predictor")

# --- Global Model Loading ---
try:
  model = joblib.load(MODEL_PATH)
  print("Model loaded successfully.")
except Exception as e:
  model = None
  print(f"Error loading model: {e}") 


# Define a route for the home page
@superKart_sales_predictor_api.get('/')
def home():
    print("Home route accessed.") # Add logging
    return "Welcome to the SuperKart Store Product Sales Prediction API."


@superKart_sales_predictor_api.post("/predict") # The simple, unversioned route
def predict_sales():
    """
    Receives product and store features, makes a sales prediction, and returns the result.
    """

    # Get the JSON data from the request body
    input_data = request.get_json()

    sample = {
        'Product_Weight': input_data['Product_Weight'],
        'Product_Sugar_Content': input_data['Product_Sugar_Content'],
        'Product_Allocated_Area': input_data['Product_Allocated_Area'],
        'Product_Type': input_data['Product_Type'],
        'Product_Quantity': input_data[ 'Product_Quantity'],
        'Product_MRP': input_data['Product_MRP'],
        'Store_Establishment_Year': input_data['Store_Establishment_Year'],
        'Store_Size': input_data['Store_Size'],
        'Store_Location_City_Type': input_data['Store_Location_City_Type'],
        'Store_Type': input_data['Store_Type']
    }

    # Convert the extracted data into a Pandas DataFrame
    input_data = pd.DataFrame([sample])

    # Make prediction (get log_price)
    predicted_sales = model.predict(input_data)[0]

    # Convert predicted_price to Python float
    predicted_sales = round(float(predicted_sales), 2)

    # Return the actual price
    return jsonify({'Predicted Sales (in dollars)': predicted_sales})
    

# --- Local Runner (Optional: Comment out for production WSGI) ---
if __name__ == '__main__':
    superKart_sales_predictor_api.run(debug=True) # Commented out to prevent blocking in notebook