SuperKart1API / app.py
Srinivas1969's picture
Upload folder using huggingface_hub
0df774e verified
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app with a name
Sales_predictor_api = Flask("SuperKart Sales Predictor")
# Load the trained churn prediction model
model = joblib.load("SuperKart_Sales_Predictor.joblib")
# Define a route for the home page
@Sales_predictor_api.get('/')
def home():
return "Welcome to the SuperKart Sales Predictor API!"
# Define an endpoint to predict churn for a single customer
@Sales_predictor_api.post('/v1/sales')
def predict_sales():
# Get JSON data from the request
sales_data = request.get_json()
# Extract relevant sales features from the input data
sample = {
'Product_Weight': sales_data['Product_Weight'],
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
'Product_MRP': sales_data['Product_MRP'],
'Product_Type' : sales_data['Product_Type'],
'Product_MRP' : sales_data['Product_Type'] ,
'Store_Size' : sales_data['Store_Size'] ,
'Store_Location_City_Type' : sales_data['Store_Location_City_Type'] ,
'Store_Type' : sales_data['Store_Type'],
'Store_Age' : sales_data['Store_Age'] ,
'Avg_Sales_Per_Product' : sales_data['Avg_Sales_Per_Product'] ,
'Avg_Sales_Per_Store' : sales_data['Avg_Sales_Per_Store'] ,
'Product_Sales_Rank' : sales_data['Product_Sales_Rank'] ,
'Store_Product_Share' : sales_data['Store_Product_Share'] ,
'Product_MRP_Band' : sales_data['Product_MRP_Band']
}
# 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]
# Return the prediction as a JSON response
return jsonify({'Prediction': prediction})
# Run the Flask app in debug mode
if __name__ == '__main__':
app.run(debug=True)