| | |
| | import numpy as np |
| | import joblib |
| | import pandas as pd |
| | from flask import Flask, request, jsonify |
| |
|
| | |
| | superkart_sales_forecast_api = Flask("Superkart Sales Forecast") |
| |
|
| | |
| | model = joblib.load("superkart_model_v1_0.joblib") |
| |
|
| | |
| | @superkart_sales_forecast_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 Sales Forecast API!" |
| |
|
| | |
| | @superkart_sales_forecast_api.post('/v1/sales_forecast') |
| | def predict_sales_forecast(): |
| | """ |
| | This function handles POST requests to the '/v1/sales_forecast' endpoint. |
| | It expects a JSON payload containing property details and returns |
| | the predicted rental price as a JSON response. |
| | """ |
| | |
| | superkart_data = request.get_json() |
| |
|
| | |
| | sample = { |
| | 'Product_Weight': superkart_data['product_weight'], |
| | 'Product_Sugar_Content': superkart_data['product_sugar_content'], |
| | 'Product_Allocated_Area': superkart_data['product_allocated_area'], |
| | 'Product_Type': superkart_data['product_type'], |
| | 'Product_MRP': superkart_data['product_mrp'], |
| | 'Store_Id': superkart_data['store_id'], |
| | 'Store_Establishment_Year': superkart_data['store_establishment_year'], |
| | 'Store_Size': superkart_data['store_size'], |
| | 'Store_Location_City_Type': superkart_data['store_location_city_type'], |
| | 'Store_Type' : superkart_data['store_type'] |
| | } |
| |
|
| | |
| | input_data = pd.DataFrame([sample]) |
| |
|
| | |
| | predicted_sales_price = model.predict(input_data)[0] |
| |
|
| | |
| | |
| |
|
| | |
| | predicted_price = round(float(predicted_sales_price), 2) |
| | |
| | |
| |
|
| | |
| | return jsonify({'Predicted Price (in dollars)': predicted_price}) |
| |
|
| |
|
| | |
| | @superkart_sales_forecast_api.post('/v1/sales_forecast_batch') |
| | def predict_sales_forecast_batch(): |
| | """ |
| | This function handles POST requests to the '/v1/sales_forecast_batch' 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_price = model.predict(input_data).tolist() |
| |
|
| | |
| | |
| |
|
| | |
| | product_ids = input_data['product_id'].tolist() |
| | output_dict = dict(zip(product_ids, predicted_price)) |
| |
|
| | |
| | return output_dict |
| |
|
| | |
| | if __name__ == '__main__': |
| | superkart_sales_forecast_api.run(debug=True) |
| |
|