# Import necessary libraries import numpy as np from custom_preprocessor import CustomPreprocessor # for the serialized model to call import joblib # For loading the serialized model import pandas as pd # For data manipulation from flask import Flask, request, jsonify # For creating the Flask API # Initialize the Flask application SuperKart_predictor_api = Flask("SuperKart Sales Forecast API") # Load the trained machine learning model model = joblib.load("SuperKart_model_v1_0.joblib") # Define a route for the home page (GET request) @SuperKart_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 SuperKart Sales Forecast API!" # Define an endpoint for single product prediction (POST request) @SuperKart_predictor_api.post('/v1/product') def predict_SuperKart(): # This function handles POST requests to the '/v1/product' endpoint. # It expects a JSON payload containing property details and returns # the predicted product sales as a JSON response. # Get the JSON data from the request body product_data = request.get_json() # Extract relevant features from the JSON data sample = { 'Product_Weight': product_data['Product_Weight'], 'Product_Allocated_Area': product_data['Product_Allocated_Area'], 'Product_MRP': product_data['Product_MRP'], 'Product_Sugar_Content': product_data['Product_Sugar_Content'], 'Product_Type': product_data['Product_Type'], 'Store_Size': product_data['Store_Size'], 'Store_Location_City_Type': product_data['Store_Location_City_Type'], 'Store_Type': product_data['Store_Type'] } # Convert the extracted data into a Pandas DataFrame input_data = pd.DataFrame([sample]) # Make prediction (get sales) predicted_sales = model.predict(input_data)[0] # Convert predicted_sales to Python float predicted_sales = round(float(predicted_sales), 2) # Return the actual price return jsonify({'Predicted sales (in dollars)': predicted_sales}) # Define an endpoint for batch prediction (POST request) @SuperKart_predictor_api.post('/v1/productbatch') def predict_SuperKart_batch(): # This function handles POST requests to the '/v1/productbatch' endpoint. # It expects a CSV file containing product details for multiple products # and returns the predicted product sales as a dictionary in the JSON response. # debug log method: # print("I got a request",flush=True) # Get the uploaded CSV file from the request file = request.files['file'] # Read the CSV file into a Pandas DataFrame input_data = pd.read_csv(file) # Make predictions for all products in the DataFrame (get log_sales) predicted_sales_list = model.predict(input_data).tolist() predicted_sales = [round(float(pred), 2) for pred in predicted_sales_list] # Create a dictionary of predictions with product IDs as keys product_ids = input_data['Product_Id'].tolist() # Assuming 'Product_Id' is the product ID column output_dict = dict(zip(product_ids, predicted_sales)) # Use actual sales # Return the predictions dictionary as a JSON response return output_dict # Run the Flask application in debug mode if this script is executed directly if __name__ == '__main__': SuperKart_predictor_api.run(debug=True)