| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| superkart_price_predictor_api = Flask("SuperKart Price Predictor") |
|
|
| |
| model = joblib.load("superkart_sales_price_prediction_model_v1_0.joblib") |
|
|
| |
| @superkart_price_predictor_api.get('/') |
| def home(): |
| """ |
| This function handles GET requests to the root URL ('/') of the API. |
| It returns a simple welcome message. |
| """ |
| return "Welcome to the SuperKart Price Prediction API!" |
|
|
| |
| @superkart_price_predictor_api.post('/v1/superkartPricePrediction') |
| def predict_rental_price(): |
| """ |
| This function handles POST requests to the '/v1/superkartPricePrediction' endpoint. |
| It expects a JSON payload containing property details and returns |
| the predicted rental price as a JSON response. |
| """ |
| |
| property_data = request.get_json() |
|
|
| |
| property_data["Product_Category"] = property_data["Product_Id"][:2] |
|
|
| |
| current_year = pd.Timestamp.now().year |
| store_Age = current_year - property_data["Store_Establishment_Year"] |
| bins = [0, 5, 10, 20, 30, 50, 100] |
| labels = ["0-5 yrs", "6-10 yrs", "11-20 yrs", "21-30 yrs", "31-50 yrs", "50+ yrs"] |
| property_data["Store_Age_Group"] = pd.cut([store_Age], bins=bins, labels=labels, right=True)[0] |
|
|
| |
| sample = { |
| 'Product_Weight': property_data['Product_Weight'], |
| 'Product_Allocated_Area': property_data['Product_Allocated_Area'], |
| 'Product_MRP': property_data['Product_MRP'], |
| 'Product_Sugar_Content': property_data['Product_Sugar_Content'], |
| 'Product_Type': property_data['Product_Type'], |
| 'Store_Size': property_data['Store_Size'], |
| 'Store_Location_City_Type': property_data['Store_Location_City_Type'], |
| 'Store_Type': property_data['Store_Type'], |
| 'Product_Category': property_data['Product_Category'], |
| 'Store_Age_Group': property_data['Store_Age_Group'] |
| } |
|
|
| |
| input_data = pd.DataFrame([sample]) |
|
|
| |
| predicted_price = model.predict(input_data)[0] |
|
|
| |
| predicted_price = round(float(predicted_price), 2) |
| |
| |
|
|
| |
| return jsonify({'Predicted Price': predicted_price}) |
|
|
| |
| if __name__ == '__main__': |
| superkart_price_predictor_api.run(debug=True) |
|
|