MainiSandeep1987's picture
Upload folder using huggingface_hub
a9a69f4 verified
import joblib
import pandas as pd ## To Read around the CSV Files.
from flask import Flask, request, jsonify # Flask to create the backwend API using FLASK, request to use and cater the influx requests,
# and jsonify to convert the python object to JSON String,as Python object CANNOT be transmitted over
# HTTP CALLS.
# Initialize Flask app with a name
app = Flask("Product Store Sales Revenue Predictor")
# Load the trained churn prediction model
model = joblib.load("product_store_prediction_model_v1_0.joblib")
# Define a route for the home page
@app.get('/')
def home():
return "Welcome to the Product Store Sales Revenue Predictor API.!"
# Single Prediction
# Define an endpoint to predict Sales revenue for a given Product in a store.
@app.post('/v1/salesTotal')
def predict_sales():
# Get JSON data from the request
product_store_data = request.get_json()
# Extract relevant product and store features from the input http request 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_Category' : product_store_data['Product_Category'],
'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']
}
# Convert the extracted data into a DataFrame
input_data = pd.DataFrame([sample])
# Make a Sales prediction using the trained model
salesPrediction = model.predict(input_data).tolist()[0]
# round off the value to 2 decimal places.
predicted_revenue = round(float(salesPrediction), 2)
# Return the predicted_revenue
return jsonify({'Predicted Sales Revenue Price': predicted_revenue})
# Batch Prediction
# Define an endpoint to predict Sales revenue for a given batch of Product among the given stores.
@app.post('/v1/salesTotalBatch')
def predict_sales_batch():
# Get the uploaded CSV file from the request
file = request.files['file']
# Read the file into a DataFrame
input_data = pd.read_csv(file)
# Adding Product_Category as part of Feature Engineering which was missing from exisiting csv file. It is derived using first 2 chars of
# Product_Id before dropping Product_id field in itself as being Unique in nature.
input_data["Product_Category"] = input_data["Product_Id"].str[:2]
# Make predictions for the batch data and convert raw predictions into a readable format.
salesPredictions = [round(pred, 2) for pred in model.predict(input_data.drop("Product_Id", axis=1)).tolist()]
product_id_list = input_data.Product_Id.values.tolist()
output_dict = dict(zip(product_id_list, salesPredictions))
return output_dict
# Run the Flask app in debug mode
if __name__ == '__main__':
app.run(debug=True)