KavinPrasathK's picture
Upload folder using huggingface_hub
ce17b17 verified
# Libraries to help with reading and manipulating data
import numpy as np
import pandas as pd
# For loading the serialized model
import joblib
# For creating the Flask API
from flask import Flask, request, jsonify
# Initializing the Flask application
product_store_sales_predictor_api = Flask("Product Store Sales Predictor")
# Loading the serialised ML model (XGBRegressor Tuned)
model = joblib.load("product_store_sales_prediction_model_v1_0.joblib")
# Route for the home page (GET request)
@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!"
# Endpoint for Sales prediction for a single product in a given store (POST request)
@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.
"""
# Retrieving the JSON data from the request body
product_store_data = request.get_json()
# Extracting the required from the JSON data
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']
}
# Converting the extracted data into a Pandas DataFrame
input_data = pd.DataFrame([sample])
# Predicting the sales amount
predicted_sales = model.predict(input_data)[0]
# Convert predicted_sales to Python float
predicted_sales = round(float(predicted_sales), 2)
# Return the predicted sales amount
return jsonify({'Predicted Sales Amount': predicted_sales})
if __name__ == '__main__':
product_store_sales_predictor_api.run(debug=True)