File size: 2,808 Bytes
69b5b7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask,request,jsonify
from flask_cors import CORS
import joblib
import numpy as np
import pandas as pd
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

app = Flask(__name__)
#CORS(app)
CORS(app, resources={r"/predict":{"origins":"*"}})

try:
  model_gradbosot = joblib.load('gradboost_RSCV.joblib')
  model_rndmfrst = joblib.load('RndmFrstReg_RSCV.joblib')
  pipeline = joblib.load('pipeline.joblib')
  feature_names = joblib.load('feature_names.joblib')
except Exception as Ex:
  logging.error(f'Exception in loading joblib file: {Ex}')

required_features =['Product_Weight','Product_Sugar_Content','Product_Allocated_Area',
                    'Product_Type','Product_MRP',
                    'Store_Size','Store_Location_City_Type','Store_Type','Age_Of_Store'
                    ]

@app.get('/')
def home():
  logging.debug("Accessed endpoint of Home page")
  return "Welcome to Superkart Prediction system"

@app.route('/predict', methods=['POST'])
def predict():
  try:

    data = request.get_json()
    logging.debug(f"Input received:{data}")
    if not data:
      return jsonify({'Error':'No data provided for prediction'},400)

    if not all(feature in data for feature in required_features):
      feature_missing = [feature for feature in required_features if feature not in data]
      logging.error(f"Exception feature missing:{feature_missing}")
      return jsonify({'Exception':f'Feature missing {feature_missing}'},400)

    feature_for_prediction =pd.DataFrame([{
        'Product_Weight':float(data['Product_Weight']),
        'Product_Sugar_Content':data['Product_Sugar_Content'],
        'Product_Allocated_Area':float(data['Product_Allocated_Area']),
        'Product_Type': data['Product_Type'],
        'Product_MRP':float(data['Product_MRP']),
        'Store_Size':data['Store_Size'],
        'Store_Location_City_Type':data['Store_Location_City_Type'],
        'Store_Type':data['Store_Type'],
        'Age_Of_Store':float(data['Age_Of_Store'])
        }],columns=required_features)

    features_scaled = pipeline.transform(feature_for_prediction)
    logging.debug(f"Features scaled: {features_scaled}")

    prediction_gradboost = model_gradbosot.predict(features_scaled)[0]
    prediction_randFrst = model_rndmfrst.predict(features_scaled)[0]

    logging.debug(f"Prediction gradmodel: {prediction_gradboost}")
    logging.debug(f"Prediction RandmFrst: {prediction_randFrst}")

    return jsonify ({'gradientBoosting':float(prediction_gradboost),
              'randomForest':float(prediction_randFrst)})


  except Exception as ex:
    logging.error(f'Exception: {ex}')
    return jsonify({'Exception': str(ex) })



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