from datetime import datetime 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"/*":{"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','Store_Establishment_Year' ] model_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) present_year = datetime.now().year 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'], 'Store_Establishment_Year':float(data['Store_Establishment_Year']) }],columns=required_features) feature_for_prediction['Age_Of_Store'] = present_year - feature_for_prediction['Store_Establishment_Year'] feature_for_prediction = feature_for_prediction[model_features] prediction_gradboost = model_gradbosot.predict(feature_for_prediction)[0] prediction_randFrst = model_rndmfrst.predict(feature_for_prediction)[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) }) @app.route('/Predict_BatchProcess', methods=['POST']) def Predict_BatchProcess(): logging.debug(f"Request headers : {request.headers}") if 'file' not in request.files: return jsonify({'Exception': 'No files provided for batch processing'}),400 file = request.files['file'] if not file.filename.endswith('csv'): logging.error("Invalid file, Application process only .csv files") return jsonify({'Exception': 'Invalid file, Application process only .csv files'}) ip_data = pd.read_csv(file) logging.debug(f'File loaded for batch processing total rows and columns: {ip_data.shape}') if not all(feature in ip_data.columns for feature in required_features): feature_missing = [feature for feature in required_features if feature not in ip_data.columns] logging.error(f"feature missing in csv {feature_missing}") return jsonify({'Exception':f"feature missing in csv {feature_missing}"}),400 ip_data = ip_data[required_features] present_year = datetime.now().year ip_data['Age_Of_Store'] = present_year - ip_data['Store_Establishment_Year'] ip_data = ip_data[model_features] prediction_gradboost = model_gradbosot.predict(ip_data) prediction_randFrst = model_rndmfrst.predict(ip_data) logging.debug(f"Prediction gradmodel: {prediction_gradboost}") logging.debug(f"Prediction RandmFrst: {prediction_randFrst}") df_prediction = ip_data.copy() df_prediction['GradeientModel_Prediction'] = prediction_gradboost df_prediction['RandomForest_Prediction'] = prediction_randFrst return jsonify (df_prediction.to_dict(orient='records')) if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=False)