Spaces:
Sleeping
Sleeping
| # 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 | |
| def home(): | |
| return "Welcome to the Super Kart Product Pricing Predictor API!" | |
| # Define an endpoint to predict churn for a single customer | |
| 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_Allocated_Area': data['Product_Allocated_Area'], | |
| 'Product_MRP': data['Product_MRP'], | |
| 'Store_Establishment_Year': data['Store_Age_Years'], | |
| 'Product_Sugar_Content_Mapping': data['Product_Sugar_Content'], | |
| 'Store_Size_Mapping': data['Store_Size'], | |
| 'Store_Location_City_Type_Mapping': data['Store_Location_City_Type'], | |
| 'Product_Type_Mapping': data['Product_Type'], | |
| 'Store_Id_Mapping': data['Store_Id'], | |
| 'Store_Type_Mapping': data['Store_Type'], | |
| } | |
| # 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({'PredictedPrice': prediction}) | |
| # Run the Flask app in debug mode | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |