| | |
| | import numpy as np |
| | import joblib |
| | import pandas as pd |
| | import logging |
| | from flask import Flask, request, jsonify |
| |
|
| |
|
| | |
| | product_sales_predictor_api = Flask("SuperKart Product Sales Predictor") |
| |
|
| | |
| | model = joblib.load("SuperKart_Sales_prediction_model_v1_0.joblib") |
| |
|
| | |
| | @product_sales_predictor_api.get('/') |
| | def home(): |
| | """ |
| | This function handles GET requests to the root URL ('/') of the API. |
| | The function displays simple welcome message. |
| | """ |
| |
|
| | |
| | |
| | handler = logging.FileHandler('app.log') |
| | product_sales_predictor_api.logger.addHandler(handler) |
| | product_sales_predictor_api.logger.setLevel(logging.INFO) |
| | product_sales_predictor_api.logger.info('GET INVOKED') |
| |
|
| | return "Welcome to the SuperKart Product Sales Prediction API!" |
| |
|
| |
|
| | |
| | @product_sales_predictor_api.post('/v1/ProductSale') |
| | def predict_Product_Sales(): |
| | """ |
| | This function handles POST requests to the '/v1/ProductSale' endpoint. |
| | It expects a JSON payload containing Proeduct details and returns |
| | the predicted sales price as a JSON response. |
| | """ |
| |
|
| | |
| | |
| | product_sales_predictor_api.logger.info('single prediction entered') |
| | |
| | print(">>> product endpoint invoked!", flush=True) |
| | |
| | product_data = request.get_json() |
| |
|
| | |
| | sample = { |
| | 'Product_Type': product_data['Product_Type'], |
| | 'Product_MRP': product_data['Product_MRP'], |
| | 'Product_Weight': product_data['Product_Weight'], |
| | 'Product_Sugar_Content': product_data['Product_Sugar_Content'], |
| | 'Product_Allocated_Area': product_data['Product_Allocated_Area'], |
| | 'Store_Size': product_data['Store_Size'], |
| | 'Store_Location_City_Type': product_data['Store_Location_City_Type'], |
| | 'Store_Type': product_data['Store_Type'] |
| | } |
| |
|
| | |
| | input_data = pd.DataFrame([sample]) |
| |
|
| | |
| | predicted_sales = model.predict(input_data)[0] |
| |
|
| | |
| | return jsonify({'Predicted Sales': predicted_sales}) |
| |
|
| |
|
| | |
| | @product_sales_predictor_api.post('/v1/batchsales') |
| | def predict_sales_batch(): |
| | print(">>> Batch endpoint invoked!", flush=True) |
| | try: |
| | file = request.files.get('file') |
| | print(">>> File received:", file is not None, flush=True) |
| |
|
| | |
| | input_data = pd.read_csv(file) |
| | print(">>> CSV loaded. Columns:", list(input_data.columns), flush=True) |
| |
|
| | |
| | drop_cols = [ |
| | 'Product_Id', |
| | 'Store_Id', |
| | 'Store_Establishment_Year', |
| | 'Product_Store_Sales_Total' |
| | ] |
| | input_data = input_data.drop(columns=[c for c in drop_cols if c in input_data.columns]) |
| | print(">>> After column drop:", list(input_data.columns), flush=True) |
| |
|
| | predictions = model.predict(input_data) |
| | predictions = [float(p) for p in predictions] |
| |
|
| | print(">>> Predictions completed", flush=True) |
| |
|
| | return jsonify({"predictions": predictions}) |
| |
|
| | except Exception as e: |
| | print(">>> ERROR:", str(e), flush=True) |
| | return jsonify({"error": str(e)}), 500 |
| |
|