superkart_backend / bk_app.py
Abhinavbhatnagar's picture
Upload folder using huggingface_hub
d5d4a3c verified
#import joblib
#import pandas as pd
#from flask import Flask, request, jsonify
# Initialize Flask app with a name
app_run = Flask("Superkart Store Sale Predictor")
# Load the trained churn prediction model
model = joblib.load("D:\\AI_ML\\Model_Deployment\\Model_deployment_project\\deployment_files\\superkart_model_deployement.joblib")
# Define a route for the home page
@app_run.get('/')
def home():
return "Welcome to the Superkart Store Sale Predictor API"
# Define an endpoint to predict churn for a single customer
@app_run.post('/v1/store_sale')
def predict_sales():
# Get JSON data from the request
customer_data = request.get_json()
# Extract relevant customer features from the input data
sample = {
'Product_Weight': customer_data['Product_Weight'],
'Product_Sugar_Content': customer_data['Product_Sugar_Content'],
'Product_Allocated_Area': customer_data['Product_Allocated_Area'],
'Product_Type': customer_data['Product_Type'],
'Product_MRP': customer_data['Product_MRP'],
'Store_Id': customer_data['Store_Id'],
'Store_Establishment_Year': customer_data['Store_Establishment_Year'],
'Store_Size': customer_data['Store_Size'],
'Store_Location_City_Type': customer_data['Store_Location_City_Type'],
'Store_Type': customer_data['Store_Type']
}
# Convert the extracted data into a DataFrame
input_data = pd.DataFrame([sample])
# Make a churn prediction using the trained model
prediction = model.predict(input_data).tolist()[0]
# Map prediction result to a human-readable label
#prediction_label = "churn" if prediction == 1 else "not churn"
# Predict button
# Return the prediction as a JSON response
return jsonify({'Prediction': prediction})
# Define an endpoint to predict churn for a batch of customers
@app_run.post('/v1/customerbatch')
def predict_churn_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)
# Make predictions for the batch data and convert raw predictions into a readable format
#predictions = [
# 'Churn' if x == 1
# else "Not Churn"
# for x in model.predict(input_data.drop("customerID",axis=1)).tolist()
#]
predictions = [
for x 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, predictions))
return output_dict
# Run the Flask app in debug mode
if __name__ == '__main__':
app_run.run(debug=True)