|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
superkart_product_sales_prediction_api = Flask("SuperKart Sales Forecast API") |
|
|
|
|
|
|
|
|
model = joblib.load("superkart_product_sales_prediction_model_v1_0.joblib") |
|
|
|
|
|
|
|
|
@superkart_product_sales_prediction_api.get('/') |
|
|
def home(): |
|
|
return "Welcome to the SuperKart Sales Forecast API" |
|
|
|
|
|
|
|
|
@superkart_product_sales_prediction_api.post('/v1/forecast') |
|
|
def predict_sales_forecast(): |
|
|
|
|
|
""" |
|
|
This function handles POST requests to the '/v1/forecast' endpoint. |
|
|
It expects a JSON payload containing property details and returns |
|
|
the predicted rental price as a JSON response. |
|
|
""" |
|
|
|
|
|
|
|
|
product_data = request.get_json() |
|
|
|
|
|
|
|
|
sample = { |
|
|
'Product_Weight': product_data.get('Product_Weight'), |
|
|
'Product_Sugar_Content': product_data.get('Product_Sugar_Content'), |
|
|
'Product_Allocated_Area': product_data.get('Product_Allocated_Area'), |
|
|
'Product_Type': product_data.get('Product_Type'), |
|
|
'Product_MRP': product_data.get('Product_MRP'), |
|
|
'Store_Id': product_data.get('Store_Id'), |
|
|
'Store_Establishment_Year': product_data.get('Store_Establishment_Year'), |
|
|
'Store_Size': product_data.get('Store_Size'), |
|
|
'Store_Location_City_Type': product_data.get('Store_Location_City_Type'), |
|
|
'Store_Type': product_data.get('Store_Type') |
|
|
} |
|
|
|
|
|
|
|
|
input_data = pd.DataFrame([sample]) |
|
|
|
|
|
|
|
|
predicted_sales = model.predict(input_data).tolist()[0] |
|
|
|
|
|
|
|
|
predicted_sales = round(float(predicted_sales), 2) |
|
|
|
|
|
|
|
|
|
|
|
return jsonify({'Prediction': predicted_sales}) |
|
|
|
|
|
@superkart_product_sales_prediction_api.post('/v1/forecastbatch') |
|
|
def predict_sales_forecast_batch(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/forecastbatch' endpoint. |
|
|
It expects a CSV file containing product and store details for multiple products |
|
|
and returns the predicted sales forecast 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_sales = [round(float(price), 2) for price in predicted_sales] |
|
|
|
|
|
|
|
|
|
|
|
product_ids = input_data['Product_Id'].tolist() |
|
|
output_dict = dict(zip(product_ids, predicted_sales)) |
|
|
|
|
|
|
|
|
return output_dict |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
superkart_product_sales_prediction_api.run(debug=True) |
|
|
|