# Import necessary libraries import numpy as np import joblib import pandas as pd from flask import Flask, request, jsonify # Import logging import logging import sys # Initialize the Flask application superkart_sales_predictor_api = Flask("SuperKart_Sales_Predictor") # Load the trained machine learning model model = joblib.load("superkart_sales_prediction_model_v1_0.joblib") # Define a route for the home page (GET request) @superkart_sales_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 Prediction API!" @superkart_sales_predictor_api.route("/health", methods=["GET"]) def health(): return {"status": "ok"} # Define an endpoint for single property prediction (POST request) @superkart_sales_predictor_api.post('/v1/superkart') def predict_superkart_sales(): """ This function handles POST requests to the '/v1/superkart' endpoint. It expects a JSON payload containing property details and returns the predicted superkart sales as a JSON response. """ try: # Get the JSON data from the request body property_data = request.get_json() # Extract relevant features from the JSON data sample = { 'Product_Weight': property_data['Product_Weight'], 'Product_Sugar_Content': property_data['Product_Sugar_Content'], 'Product_Allocated_Area': property_data['Product_Allocated_Area'], 'Product_MRP': property_data['Product_MRP'], 'Store_Size': property_data['Store_Size'], 'Store_Location_City_Type': property_data['Store_Location_City_Type'], 'Store_Type': property_data['Store_Type'], 'Store_Age': property_data['Store_Age'], 'Product_Category': property_data['Product_Category'], 'Product_Category_Type': property_data['Product_Category_Type'] } # Convert the extracted data into a Pandas DataFrame input_data = pd.DataFrame([sample]) # Make prediction (get log_price) prediction = model.predict(input_data)[0] # Return the actual price return jsonify({'Sales': prediction}) except Exception as e: return jsonify({'error': str(e)}), 500 # Run the Flask application in debug mode if this script is executed directly if __name__ == '__main__': superkart_sales_predictor_api.run(debug=True)