| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| superkart_product_sales_predictor_api = Flask("SuperKart Product Sales Predictor") |
|
|
| |
| model = joblib.load("superkart_sales_prediction_model_v1_0.joblib") |
|
|
| |
| @superkart_product_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 Product Sales Prediction API!" |
|
|
| |
| @superkart_product_sales_predictor_api.post('/v1/products') |
| def predict_product_sale_price(): |
| """ |
| This function handles POST requests to the '/v1/products' endpoint. |
| It expects a JSON payload containing property details and returns |
| the predicted product sale price as a JSON response. |
| """ |
| |
| property_data = request.get_json() |
|
|
| |
| sample = { |
| 'Product_Weight': property_data['productWeight'], |
| 'Product_Allocated_Area': property_data['productAllocatedArea'], |
| 'Product_MRP': property_data['productMRP'], |
| 'Store_Establishment_Year': property_data['storeEstablishmentYear'], |
| 'Product_Sugar_Content': property_data['productSugarContent'], |
| 'Product_Type': property_data['prouctType'], |
| 'Store_Size': property_data['storeSize'], |
| 'Store_Location_City_Type': property_data['storeLocationCityType'], |
| 'Store_Type': property_data['storeType'] |
| } |
|
|
| |
| input_data = pd.DataFrame([sample]) |
|
|
| |
| predicted_sales = model.predict(input_data)[0] |
|
|
| predicted_price = round(float(predicted_sales), 2) |
|
|
|
|
| |
| return jsonify({'Predicted Price (in dollars)': predicted_price}) |
|
|
|
|
| |
| @superkart_product_sales_predictor_api.post('/v1/productsbatch') |
| def predict_product_sale_price_batch(): |
| """ |
| This function handles POST requests to the '/v1/productsbatch' endpoint. |
| It expects a CSV file containing property details for multiple properties |
| and returns the predicted product sale prices as a dictionary in the JSON response. |
| """ |
| |
| file = request.files['file'] |
|
|
| |
| input_data = pd.read_csv(file) |
|
|
| |
| predicted_sales = model.predict(input_data).tolist() |
|
|
| |
| predicted_prices = [round(float(sale_price), 2) for sale_price in predicted_sales] |
|
|
| |
| product_ids = input_data['Product_Id'].tolist() |
| output_dict = dict(zip(product_ids, predicted_prices)) |
|
|
| |
| return output_dict |
|
|
| |
| if __name__ == '__main__': |
| superkart_product_sales_predictor_api.run(debug=True) |
|
|