|
|
|
|
|
|
|
|
import numpy as np |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
import joblib |
|
|
|
|
|
|
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
product_store_sales_predictor_api = Flask("Product Store Sales Predictor") |
|
|
|
|
|
|
|
|
model = joblib.load("product_store_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 welcome message. |
|
|
""" |
|
|
return "Welcome to the Product Store Sales Prediction API!" |
|
|
|
|
|
|
|
|
@product_store_sales_predictor_api.post('/v1/sales') |
|
|
def predict_sales(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/sales' endpoint. |
|
|
It expects a JSON payload containing Product and Store details and returns |
|
|
the predicted sales amount as a JSON response. |
|
|
""" |
|
|
|
|
|
product_store_data = request.get_json() |
|
|
|
|
|
|
|
|
sample = { |
|
|
'Product_Weight': product_store_data['Product_Weight'], |
|
|
'Product_Allocated_Area': product_store_data['Product_Allocated_Area'], |
|
|
'Product_MRP': product_store_data['Product_MRP'], |
|
|
'Store_Establishment_Year': product_store_data['Store_Establishment_Year'], |
|
|
'Product_Sugar_Content': product_store_data['Product_Sugar_Content'], |
|
|
'Product_Type': product_store_data['Product_Type'], |
|
|
'Store_Id': product_store_data['Store_Id'], |
|
|
'Store_Size': product_store_data['Store_Size'], |
|
|
'Store_Location_City_Type': product_store_data['Store_Location_City_Type'], |
|
|
'Store_Type': product_store_data['Store_Type'] |
|
|
} |
|
|
|
|
|
|
|
|
input_data = pd.DataFrame([sample]) |
|
|
|
|
|
|
|
|
predicted_sales = model.predict(input_data)[0] |
|
|
|
|
|
|
|
|
predicted_sales = round(float(predicted_sales), 2) |
|
|
|
|
|
|
|
|
return jsonify({'Predicted Sales Amount': predicted_sales}) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
product_store_sales_predictor_api.run(debug=True) |
|
|
|