Backend / app.py
sgpai's picture
Upload folder using huggingface_hub
7deb946 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_model_v1_0.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/productstore')
def predict_sales():
# Get JSON data from the request
Prodstore_data = request.get_json()
# Extract relevant customer features from the input data
sample = {
'Product_Weight': Prodstore_data['Product_Weight'],
'Product_Allocated_Area': Prodstore_data['Product_Allocated_Area'],
'Product_MRP': Prodstore_data['Product_MRP'],
'Store_Establishment_Year': Prodstore_data['Store_Establishment_Year'],
'Product_Sugar_Content': Prodstore_data['Product_Sugar_Content'],
'Product_Type': Prodstore_data['Product_Type'],
'Store_Size': Prodstore_data['Store_Size'],
'Store_Location_City_Type': Prodstore_data['Store_Location_City_Type'],
'Store_Type': Prodstore_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]
# Return the prediction as a JSON response
return jsonify({'Prediction': prediction})
# Run the Flask app in debug mode
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True)