harikbab02's picture
Upload folder using huggingface_hub
69b5b7d verified
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)