| | |
| | import numpy as np |
| | import joblib |
| | import pandas as pd |
| | from flask import Flask, request, jsonify |
| |
|
| | |
| | store_total_sales_predictor_api = Flask("Store Total Sales Predictor") |
| |
|
| | |
| | model = joblib.load("store_total_sales_prediction_model_v1_0.joblib") |
| |
|
| | |
| | @store_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 Store Total Sales Prediction API!" |
| |
|
| | |
| | @store_total_sales_predictor_api.post('/v1/storeSales') |
| | def predict_store_total_sales(): |
| | """ |
| | This function handles POST requests to the '/v1/storeSales' endpoint. |
| | It expects a JSON payload containing store details and returns |
| | the predicted total sales as a JSON response. |
| | """ |
| | |
| | store_data = request.get_json() |
| |
|
| | |
| | sample = { |
| | 'Product_Weight': store_data['product_weight'], |
| | 'Product_Sugar_Content': store_data['product_sugar_content'], |
| | 'Product_Allocated_Area': store_data['product_allocated_area'], |
| | 'Product_Type': store_data['product_type'], |
| | 'Product_MRP': store_data['product_mrp'], |
| | 'Store_Id': store_data['store_id'], |
| | 'Store_Establishment_Year': store_data['store_establishment_year'], |
| | 'Store_Size': store_data['store_size'], |
| | 'Store_Location_City_Type': store_data['store_location_city_type'], |
| | 'Store_Type': store_data['store_type'] |
| | } |
| |
|
| | |
| | input_data = pd.DataFrame([sample]) |
| |
|
| | |
| | |
| | predicted_log_total_sales = model.predict(input_data).tolist()[0] |
| |
|
| | |
| | |
| |
|
| | predicted_total_sales = predicted_log_total_sales |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | return jsonify({'Predicted_Store_Total_Sales': predicted_total_sales}) |
| |
|
| |
|
| | |
| | @store_total_sales_predictor_api.post('/v1/storeSalesbatch') |
| | def predict_store_total_sales_batch(): |
| | """ |
| | This function handles POST requests to the '/v1/storeSalesbatch' 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_log_total_sales = model.predict(input_data).tolist() |
| |
|
| | |
| | |
| | predicted_store_total_sales = predicted_log_total_sales |
| | |
| | product_ids = input_data['Product_Id'].tolist() |
| | output_dict = dict(zip(product_ids, predicted_store_total_sales)) |
| |
|
| | |
| | return output_dict |
| |
|
| | |
| | if __name__ == '__main__': |
| | store_total_sales_predictor_api.run(debug=True) |
| |
|