|
|
|
|
|
import numpy as np |
|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
sales_forecast_api = Flask("Sales Forecast Prediction API ") |
|
|
|
|
|
|
|
|
model = joblib.load("sales_forecast_model_v1_0.joblib") |
|
|
|
|
|
|
|
|
@sales_forecast_api.get('/') |
|
|
def home(): |
|
|
return "Welcome to the Sales Forecast Prediction API!" |
|
|
|
|
|
|
|
|
@sales_forecast_api.post('/v1/product') |
|
|
def predict_sales_forecast(): |
|
|
|
|
|
product_data = request.get_json() |
|
|
|
|
|
|
|
|
sample = { |
|
|
'Product_Weight': product_data['Product_Weight'], |
|
|
'Product_Sugar_Content': product_data['Product_Sugar_Content'], |
|
|
'Product_Allocated_Area': product_data['Product_Allocated_Area'], |
|
|
'Product_Type': product_data['Product_Type'], |
|
|
'Product_MRP': product_data['Product_MRP'], |
|
|
'Store_Id': product_data['Store_Id'], |
|
|
'Store_Establishment_Year': product_data['Store_Establishment_Year'], |
|
|
'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_price = model.predict(input_data)[0] |
|
|
|
|
|
|
|
|
predicted_price = round(float(predicted_price), 2) |
|
|
|
|
|
|
|
|
return jsonify({'Sales Forecast (in dollars)': predicted_price}) |
|
|
|
|
|
|
|
|
@sales_forecast_api.post('/v1/productbatch') |
|
|
def predict_sales_forecast_batch(): |
|
|
|
|
|
file = request.files['file'] |
|
|
|
|
|
|
|
|
input_data = pd.read_csv(file) |
|
|
|
|
|
|
|
|
predicted_sales = model.predict(input_data).tolist() |
|
|
|
|
|
|
|
|
predicted_sales = [round(float(Product_Store_Sales_Total), 2) for Product_Store_Sales_Total in predicted_sales] |
|
|
|
|
|
|
|
|
product_ids = input_data['Product_Id'].tolist() |
|
|
output_dict = dict(zip(product_ids, predicted_sales)) |
|
|
|
|
|
|
|
|
return output_dict |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
sales_forecast_api.run(debug=True) |
|
|
|