|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
sales_forecast_api = Flask("Sales Forecasting") |
|
|
|
|
|
|
|
|
model = joblib.load("sales_forecast_model.joblib") |
|
|
|
|
|
|
|
|
@sales_forecast_api.get('/') |
|
|
def home(): |
|
|
return "Welcome to the Sales Forecast API!" |
|
|
|
|
|
|
|
|
@sales_forecast_api.post('/v1/customer') |
|
|
def predict_sales(): |
|
|
|
|
|
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]) |
|
|
|
|
|
|
|
|
prediction = model.predict(input_data).tolist()[0] |
|
|
|
|
|
|
|
|
return jsonify({'Prediction': prediction}) |
|
|
|
|
|
|
|
|
@sales_forecast_api.post('/v1/customerbatch') |
|
|
def predict_sales_batch(): |
|
|
|
|
|
file = request.files['file'] |
|
|
|
|
|
|
|
|
input_data = pd.read_csv(file) |
|
|
|
|
|
|
|
|
predictions = model.predict(input_data.drop("Product_Id",axis=1)).tolist() |
|
|
|
|
|
prod_id_list = input_data.Product_Id.values.tolist() |
|
|
output_dict = dict(zip(prod_id_list, predictions)) |
|
|
|
|
|
return jsonify(output_dict) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
sales_forecast_api.run(debug=True, host="0.0.0.0", port=7860) |
|
|
|