| | |
| | import numpy as np |
| | import joblib |
| | import pandas as pd |
| | from flask import Flask, request, jsonify |
| |
|
| | |
| | total_sales_predictor_api = Flask("Super Kart Product Sales Predictor") |
| |
|
| | |
| | model = joblib.load("super_kart_prediction_model_v1_0.joblib") |
| |
|
| | |
| | @total_sales_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 Super Kart Product Sales Prediction API!" |
| |
|
| | |
| | @total_sales_predictor_api.post('/v1/predict') |
| | def predict_product_sales(): |
| | """ |
| | This function handles POST requests to the '/v1/predict' endpoint. |
| | It expects a JSON payload containing property details and returns |
| | the predicted rental price as a JSON response. |
| | """ |
| | |
| | product_data = request.get_json() |
| |
|
| | |
| | sample = { |
| | 'Product_Id': product_data['Product_Id'], |
| | 'Product_Weight': product_data['Product_Weight'], |
| | 'Product_Sugar_Content': product_data['Product_Sugar_Content'], |
| | 'Product_Allocated_Area': product_data['Product_Allocated_Area'], |
| | 'Product_Type': product_data['Product_Type'], |
| | 'Product_MRP': product_data['Product_MRP'], |
| | 'Store_Establishment_Year': product_data['Store_Establishment_Year'], |
| | 'Store_Size': product_data['Store_Size'], |
| | 'Store_Location_City_Type': product_data['Store_Location_City_Type'], |
| | 'Store_Type': product_data['Store_Type'] |
| | } |
| |
|
| | |
| | input_data = pd.DataFrame([sample]) |
| |
|
| | |
| | predicted_sales = model.predict(input_data)[0] |
| |
|
| | |
| | predicted_sales = round(float(predicted_sales), 2) |
| | |
| | |
| |
|
| | |
| | return jsonify({'Predicted Total sales for the Product (in dollars)': predicted_sales}) |
| |
|
| |
|
| | |
| | @total_sales_predictor_api.post('/v1/predictbatch') |
| | def predict_product_sales_batch(): |
| | """ |
| | This function handles POST requests to the '/v1/predictbatch' endpoint. |
| | It expects a CSV file containing property details for multiple properties |
| | and returns the predicted rental prices as a dictionary in the JSON response. |
| | """ |
| | |
| | file = request.files['file'] |
| |
|
| | |
| | input_data = pd.read_csv(file) |
| |
|
| | |
| | predicted_total_sales = model.predict(input_data).tolist() |
| |
|
| | |
| | predicted_total_sales = [round(float(predicted_total_sales), 2) for log_price in predicted_total_sales] |
| |
|
| | |
| | product_ids = input_data['Product_Id'].tolist() |
| | output_dict = dict(zip(product_ids, predicted_total_sales)) |
| |
|
| | |
| | return output_dict |
| |
|
| | |
| | if __name__ == '__main__': |
| | total_sales_predictor_api.run(debug=True) |
| |
|