|
|
|
|
|
|
|
|
import numpy as np |
|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
product_store_sales_predictor_api = Flask("SuperKart Product Store Sales Predictor") |
|
|
|
|
|
|
|
|
model = joblib.load("superkart_sales_prediction_model_v1_0.joblib") |
|
|
|
|
|
|
|
|
@product_store_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 SuperKart Product Store Sales Predictor API!" |
|
|
|
|
|
|
|
|
@product_store_sales_predictor_api.post('/v1/productsales') |
|
|
def predict_product_sales(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/productsales' endpoint. |
|
|
It expects a JSON payload containing Product and store details and returns |
|
|
the predicted sales price as a JSON response. |
|
|
""" |
|
|
|
|
|
product_data = request.get_json() |
|
|
|
|
|
|
|
|
sample = { |
|
|
'Product_Weight': product_data['Product_Weight'], |
|
|
'Product_Allocated_Area': product_data['Product_Allocated_Area'], |
|
|
'Product_MRP': product_data['Product_MRP'], |
|
|
'Store_Age': product_data['Store_Age'], |
|
|
'Product_Identifier': product_data['Product_Identifier'], |
|
|
'Product_Sugar_Content_No Sugar': product_data['Product_Sugar_Content_No Sugar'], |
|
|
'Product_Sugar_Content_Regular': product_data['Product_Sugar_Content_Regular'], |
|
|
'Product_Sugar_Content_reg': product_data['Product_Sugar_Content_reg'], |
|
|
'Product_Type_Breads': product_data['Product_Type_Breads'], |
|
|
'Product_Type_Breakfast': product_data['Product_Type_Breakfast'], |
|
|
'Product_Type_Canned': product_data['Product_Type_Canned'], |
|
|
'Product_Type_Dairy': product_data['Product_Type_Dairy'], |
|
|
'Product_Type_Frozen Foods': product_data['Product_Type_Frozen Foods'], |
|
|
'Product_Type_Fruits and Vegetables': product_data['Product_Type_Fruits and Vegetables'], |
|
|
'Product_Type_Hard Drinks': product_data['Product_Type_Hard Drinks'], |
|
|
'Product_Type_Health and Hygiene': product_data['Product_Type_Health and Hygiene'], |
|
|
'Product_Type_Household': product_data['Product_Type_Household'], |
|
|
'Product_Type_Meat': product_data['Product_Type_Meat'], |
|
|
'Product_Type_Others': product_data['Product_Type_Others'], |
|
|
'Product_Type_Seafood': product_data['Product_Type_Seafood'], |
|
|
'Product_Type_Snack Foods': product_data['Product_Type_Snack Foods'], |
|
|
'Product_Type_Soft Drinks': product_data['Product_Type_Soft Drinks'], |
|
|
'Product_Type_Starchy Foods': product_data['Product_Type_Starchy Foods'], |
|
|
'Store_Id_OUT002': product_data['Store_Id_OUT002'], |
|
|
'Store_Id_OUT003': product_data['Store_Id_OUT003'], |
|
|
'Store_Id_OUT004': product_data['Store_Id_OUT004'], |
|
|
'Store_Size_Medium': product_data['Store_Size_Medium'], |
|
|
'Store_Size_Small': product_data['Store_Size_Small'], |
|
|
'Store_Location_City_Type_Tier 2': product_data['Store_Location_City_Type_Tier 2'], |
|
|
'Store_Location_City_Type_Tier 3': product_data['Store_Location_City_Type_Tier 3'], |
|
|
'Store_Type_Food Mart': product_data['Store_Type_Food Mart'], |
|
|
'Store_Type_Supermarket Type1': product_data['Store_Type_Supermarket Type1'], |
|
|
'Store_Type_Supermarket Type2': product_data['Store_Type_Supermarket Type2'] |
|
|
} |
|
|
|
|
|
|
|
|
input_data = pd.DataFrame([sample]) |
|
|
|
|
|
|
|
|
predicted_sales = model.predict(input_data)[0] |
|
|
|
|
|
|
|
|
predicted_sales = round(float(predicted_sales), 2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify({'Predicted Product Store Sales Total': predicted_sales}) |
|
|
|
|
|
|
|
|
|
|
|
@product_store_sales_predictor_api.post('/v1/productsalesbatch') |
|
|
def predict_product_sales_batch(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/productsalesbatch' endpoint. |
|
|
It expects a CSV file containing product and store details for multiple entries |
|
|
and returns the predicted sales as a dictionary in the JSON response. |
|
|
""" |
|
|
|
|
|
file = request.files['file'] |
|
|
|
|
|
|
|
|
input_data = pd.read_csv(file) |
|
|
|
|
|
|
|
|
predicted_sales = model.predict(input_data).tolist() |
|
|
|
|
|
|
|
|
product_ids = input_data['Product_Id'].tolist() |
|
|
output_dict = dict(zip(product_ids, predicted_sales)) |
|
|
|
|
|
|
|
|
return output_dict |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
product_store_sales_predictor_api.run(debug=True) |
|
|
|