# import numpy as np # import joblib # import pandas as pd # from flask import Flask, request, jsonify # # Initialize the Flask app with a custom name # super_kart_predictor_api = Flask("Super Kart Sales Predictor") # # Load the trained model from the specified path # # Make sure model_path variable is defined or replace with the actual path string # model = joblib.load(model_path) # # Define a route for the home page (GET request) # @super_kart_predictor_api.get('/') # def home(): # """ # This function handles GET requests to the root URL ('/') of the API. # It returns a simple welcome message. # """ # return "Welcome to the Super Kart Sales Predictor API!" # # Define a route for predictions (POST request) # @super_kart_predictor_api.post("/v1/sales") # def predict_sales(): # """ # This function handles POST requests to the /v1/sales endpoint. # It expects a JSON payload containing commodity sales details and returns # the predicted sales as a JSON response # """ # # Get JSON data from the POST request # superkart_data = request.get_json # print(superkart_data) # # Extract relevant features from the JSON payload into a dictionary # sample = { # 'Product_Weight': superkart_data['Product_Weight'], # 'Product_Allocated_Area': superkart_data['Product_Allocated_Area'], # 'Product_MRP': superkart_data['Product_MRP'], # 'Store_Tenure': superkart_data['Store_Tenure'], # 'Product_Category': superkart_data['Product_Category'], # 'Product_Sugar_Content': superkart_data['Product_Sugar_Content'], # 'Product_Type': superkart_data['Product_Type'], # 'Store_Id': superkart_data['Store_Id'], # 'Store_Size': superkart_data['Store_Size'], # 'Store_Location_City_Type': superkart_data['Store_Location_City_Type'], # 'Store_Type': superkart_data['Store_Type'], # 'Perishability': superkart_data['Perishability'] # } # # Create a DataFrame from the input dictionary for model compatibility # input_data = pd.DataFrame([sample]) # # Predict sales price using the loaded model # predicted_sales_price = model.predict(input_data)[0] # # Convert predicted sales price back from log scale using exponential # predicted_sales = np.exp(predicted_sales_price) # # Convert the prediction to a float type with rounding to 2 decimal places # predicted_sales = float(predicted_sales, 2) # # Return the prediction as a JSON response # return jsonify({"predicted_sales_price": predicted_sales}) import numpy as np import joblib import pandas as pd from flask import Flask, request, jsonify # Initialize the Flask app with a custom name super_kart_predictor_api = Flask("Super Kart Sales Predictor") # Load the trained model from the specified path # Make sure model_path variable is defined or replace with the actual path string model_path = "super_kart_prediction_gbr_tuned_model_v1_0.joblib" model = joblib.load(model_path) # Define a route for the home page (GET request) @super_kart_predictor_api.get('/') def home(): """ This function handles GET requests to the root URL ('/') of the API. It returns a simple welcome message. """ return "Welcome to the Super Kart Sales Predictor API!" # Define a route for predictions (POST request) @super_kart_predictor_api.post("/v1/sales") def predict_sales(): """ This function handles POST requests to the /v1/sales endpoint. It expects a JSON payload containing commodity sales details and returns the predicted sales as a JSON response """ # Get JSON data from the POST request superkart_data = request.get_json() print(f"\nIncoming request data: \n{superkart_data}\n") # Extract relevant features from the JSON payload into a dictionary sample = { 'Product_Weight': superkart_data['Product_Weight'], 'Product_Allocated_Area': superkart_data['Product_Allocated_Area'], 'Product_MRP': superkart_data['Product_MRP'], 'Product_Sugar_Content': superkart_data['Product_Sugar_Content'], 'Product_Type': superkart_data['Product_Type'], 'Product_Category': superkart_data['Product_Category'], 'Store_Id': superkart_data['Store_Id'], 'Store_Establishment_Year': superkart_data['Store_Establishment_Year'], 'Store_Size': superkart_data['Store_Size'], 'Store_Location_City_Type': superkart_data['Store_Location_City_Type'], 'Store_Type': superkart_data['Store_Type'], 'Store_Tenure': superkart_data['Store_Tenure'], 'Perishability': superkart_data['Perishability'], } # Create a DataFrame from the input dictionary for model compatibility input_data = pd.DataFrame([sample]) # Predict sales price using the loaded model predicted_sales_price = model.predict(input_data)[0] # Convert the prediction to a float type with rounding to 3 decimal places predicted_sales = round(predicted_sales_price, 3) print(f"\nPredicted Sales Price: {predicted_sales}\n") # Return the prediction as a JSON response return jsonify({"predicted_sales_price": predicted_sales}) # if __name__ == '__main__': # super_kart_predictor_api.run()