|
|
|
|
|
import numpy as np |
|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
superkart_sales_forecast_api = Flask("SuperKart Sales Forecast Backend") |
|
|
|
|
|
|
|
|
model = joblib.load("SuperKart_Sales_Forecast_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') |
|
|
def predict_sales(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/sales' endpoint. |
|
|
It expects a JSON payload containing property details and returns |
|
|
the predicted rental price as a JSON response. |
|
|
""" |
|
|
|
|
|
property_data = request.get_json() |
|
|
|
|
|
|
|
|
sample = { |
|
|
'Product_Weight': property_data['product_weight'], |
|
|
'Product_Sugar_Content': property_data['product_sugar_content'], |
|
|
'Product_Allocated_Area': property_data['product_allocated_area'], |
|
|
'Product_Type': property_data['product_type'], |
|
|
'Product_MRP': property_data['product_mrp'], |
|
|
'Store_Establishment_Year': property_data['store_establishment_year'], |
|
|
'Store_Size': property_data['store_size'], |
|
|
'Store_Location_City_Type': property_data['store_location_city_type'], |
|
|
'Store_Type': property_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 Sales Forecast (in dollars)': predicted_sales}) |
|
|
|
|
|
|
|
|
|
|
|
@superkart_sales_forecast_api.post('/v1/salesbatch') |
|
|
def predict_sales_batch(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/salesbatch' 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_sales = model.predict(input_data).tolist() |
|
|
|
|
|
output_dict = {f"Row_{i}": round(float(pred), 2) for i, pred in enumerate(predicted_sales)} |
|
|
|
|
|
return jsonify(output_dict) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
superkart_sales_forecast_api.run(debug=True) |
|
|
|