RPeltier's picture
Upload folder using huggingface_hub
cdae9fb verified
raw
history blame
1.72 kB
# Import necessary libraries
import numpy as np
import joblib
import pandas as pd
from flask import Flask, request, jsonify
# Initialize Flask app with a name
app = Flask("Super Kart Product Pricing Predictor")
model = joblib.load("super_kart_product_pricing_model.joblib")
# Define a route for the home page, used to validate backend is functional and accessible
@app.get('/')
def home():
return "Welcome to the Super Kart Product Pricing Predictor API!"
# Define an endpoint to predict churn for a single customer
@app.post('/v1/predict')
def predict_sales():
# Get JSON data from the request
data = request.get_json()
# Extract relevant customer features from the input data. The order of the column names matters.
sample = {
'Product_Weight': data['Product_Weight'],
'Product_Sugar_Content': data['Product_Sugar_Content'],
'Product_Allocated_Area': data['Product_Allocated_Area'],
'Product_Type_Category': data['Product_Type_Category'],
'Product_MRP': data['Product_MRP'],
'Store_Size': data['Store_Size'],
'Store_Location_City_Type': data['Store_Location_City_Type'],
'Store_Type': data['Store_Type'],
'Product_Id_char': data['Product_Id_char'].astype(str),
'Store_Age_Years': data['Store_Age_Years'],
}
# Convert the extracted data into a DataFrame and preprocess it
input_data = pd.DataFrame([sample])
prediction = model.predict(input_data).tolist()[0]
# Return the prediction as a JSON response
return jsonify({'Predicted Product Price': prediction})
# Run the Flask app in debug mode
if __name__ == '__main__':
app.run(debug=True)