|
|
|
|
|
import numpy as np |
|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
rental_price_predictor_api = Flask("SuperKart Revenue Predictor") |
|
|
|
|
|
|
|
|
model = joblib.load("superKart_price_prediction_model_v1_0.joblib") |
|
|
|
|
|
|
|
|
@rental_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 Revenue Prediction API!" |
|
|
|
|
|
|
|
|
|
|
|
@rental_price_predictor_api.post('/v1/revenue') |
|
|
def predict_rental_price(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/revenue' endpoint. |
|
|
It expects a JSON payload containing input details and returns |
|
|
the predicted revenue 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_Id': property_data['store_id'], |
|
|
'Store_Age': property_data['store_age'], |
|
|
'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_price = model.predict(input_data)[0] |
|
|
|
|
|
|
|
|
predicted_price = round(float(predicted_price), 2) |
|
|
return jsonify({'Predicted Revenue (in dollars)': predicted_price}) |
|
|
|
|
|
|
|
|
|
|
|
@rental_price_predictor_api.post('/v1/rentalbatch') |
|
|
def predict_rental_price_batch(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/rentalbatch' 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_prices = model.predict(input_data).tolist() |
|
|
|
|
|
|
|
|
predicted_prices = [round(float(price), 2) for price in predicted_prices] |
|
|
|
|
|
|
|
|
|
|
|
if 'Product_Id' in input_data.columns: |
|
|
product_ids = input_data['Product_Id'].tolist() |
|
|
output_dict = dict(zip(product_ids, predicted_prices)) |
|
|
else: |
|
|
|
|
|
output_dict = {'predictions': predicted_prices} |
|
|
|
|
|
|
|
|
|
|
|
return jsonify(output_dict) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
rental_price_predictor_api.run(debug=True) |
|
|
|