PrecisionBallistics/SuperKart-Backend
1
1 2# Import necessary libraries3import numpy as np4import joblib # For loading the serialized model5import pandas as pd # For data manipulation6from flask import Flask, request, jsonify # For creating the Flask API7 8# Initialize Flask app with a name9superkart_api = Flask(__name__) #Complete the code to define the name of the app10 11# Load the trained model12model = joblib.load("rf_tuned.joblib") #Complete the code to define the location of the serialized model13 14# Define a route for the home page15@superkart_api.get('/')16def home():17 return "Welcome to the SuperKart Sales Prediction API!" #Complete the code to define a welcome message18 19# Define an endpoint to predict sales20@superkart_api.post('/v1/predict')21def predict_sales():22 # Get JSON data from the request23 data = request.get_json()24 25 # Extract relevant customer features from the input data. The order of the column names matters.26 sample = {27 'Product_Weight': data['Product_Weight'],28 'Product_Sugar_Content': data['Product_Sugar_Content'],29 'Product_Allocated_Area': data['Product_Allocated_Area'],30 'Product_MRP': data['Product_MRP'],31 'Store_Size': data['Store_Size'],32 'Store_Location_City_Type': data['Store_Location_City_Type'],33 'Store_Type': data['Store_Type'],34 'Product_Id_char': data['Product_Id_char'],35 'Store_Age_Years': data['Store_Age_Years'],36 'Product_Type_Category': data['Product_Type_Category']37 }38 39 # Convert the extracted data into a DataFrame40 input_data = pd.DataFrame([sample])41 42 # Make a prediction using the trained model43 prediction = model.predict(input_data).tolist()[0]44 45 # Return the prediction as a JSON response46 return jsonify({'Sales': prediction})47 48 49# Run the Flask app in debug mode50if __name__ == '__main__':51 superkart_api.run(debug=True)52 