VrundavGamit's picture
Upload folder using huggingface_hub
d31ac8b verified
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app with a name
app = Flask("SuperKart product sales revenue Prediction")
# Load the trained SuperKart sales revenue prediction model
model_superkart = joblib.load("superkart_revenue_prediction_model_v1_0.joblib")
# Define a route for the home page
@app.get('/')
def home():
return "Welcome to the SuperKart Revenue Prediction API, created by Vrundav Gamit"
# Define an endpoint to predict revenue for single data point
# POST endpoint
# v1 in the url is a industry practise for versioning of endpoint urls
# For example v1 urls can be used to testing, v2 urls can be used for validation testing
@app.post('/v1/predictrevenue')
# @app.route('/v1/predictrevenue', methods=['POST'])
def predict_revenue():
# Get JSON data from the request
sales_data = request.get_json()
# Extract relevant store and product features from the input data
payload = {
'Product_Weight': sales_data['Product_Weight'],
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
'Product_MRP': sales_data['Product_MRP'],
'Store_Size': sales_data['Store_Size'],
'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
'Store_Type': sales_data['Store_Type'],
'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
'Product_Type': sales_data['Product_Type']
}
# Convert the extracted data into a DataFrame
input_data = pd.DataFrame([payload])
# Make a sales revenue prediction using the trained model
prediction = model_superkart.predict(input_data).tolist()[0]
# Return the prediction as a JSON response
return jsonify({'Prediction': prediction})
# Define an endpoint to predict revenue for a batch of store and product data
@app.post('/v1/predictrevenuebatch')
def predict_revenue_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)
# Drop Product_Id from the input before performing a predict
# As Product_Id is not one of the input features of the model
predictions = model_superkart.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, predictions))
return output_dict
# Run the Flask app in debug mode
if __name__ == '__main__':
app.run(debug=True)