|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
super_kart_api = Flask("Super Kart Product Sales Predictor") |
|
|
|
|
|
|
|
|
model = joblib.load("super_kart_model_v1_0.joblib") |
|
|
|
|
|
|
|
|
@super_kart_api.get('/') |
|
|
def home(): |
|
|
return "Welcome to the Super Kart Product Sales Prediction API!" |
|
|
|
|
|
|
|
|
@super_kart_api.post('/v1/product') |
|
|
def predict_product_sales_price(): |
|
|
|
|
|
product_data = request.get_json() |
|
|
current_year = datetime.now().year |
|
|
|
|
|
|
|
|
sample = { |
|
|
'Product_Weight': product_data['Product_Weight'], |
|
|
'Product_Sugar_Content': product_data['Product_Sugar_Content'], |
|
|
'Product_Type': product_data['Product_Type'], |
|
|
'Product_MRP': product_data['Product_MRP'], |
|
|
'Store_Id': product_data['Store_Id'], |
|
|
'Store_Size': product_data['Store_Size'], |
|
|
'Store_Location_City_Type': product_data['Store_Location_City_Type'], |
|
|
'Store_Type': product_data['Store_Type'], |
|
|
'Store_Age': int(current_year - product_data['Store_Establishment_Year']) |
|
|
} |
|
|
|
|
|
|
|
|
input_data = pd.DataFrame([sample]) |
|
|
|
|
|
|
|
|
prediction = model.predict(input_data).tolist()[0] |
|
|
|
|
|
|
|
|
return jsonify({'Predicted_Product_Sales': prediction}) |
|
|
|
|
|
|
|
|
@super_kart_api.post('/v1/productbatch') |
|
|
def predict_product_batch(): |
|
|
|
|
|
file = request.files['file'] |
|
|
|
|
|
|
|
|
data = pd.read_csv(file) |
|
|
|
|
|
current_year = datetime.now().year |
|
|
input_data = data.copy() |
|
|
input_data['Store_Establishment_Year'] = pd.to_numeric(input_data['Store_Establishment_Year'], errors='coerce') |
|
|
input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year'] |
|
|
input_data = input_data.drop(['Product_Id','Store_Establishment_Year','Product_Allocated_Area'],axis=1) |
|
|
|
|
|
|
|
|
input_data["Product_Sugar_Content"] = input_data["Product_Sugar_Content"].replace("reg", "Regular") |
|
|
|
|
|
|
|
|
predictions = model.predict(input_data).tolist() |
|
|
|
|
|
|
|
|
data['Predicted_Product_Sales'] = predictions |
|
|
|
|
|
|
|
|
result = data.to_dict(orient="records") |
|
|
|
|
|
return jsonify(result) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
super_kart_api.run(debug=True) |
|
|
|