|
|
|
|
|
import os |
|
|
import numpy as np |
|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
rental_price_predictor_api = Flask("Extraa Learn conversion Predictor") |
|
|
|
|
|
|
|
|
|
|
|
model_path = os.path.join(os.path.dirname(__file__), "conversion_prediction_model_v1_0.joblib") |
|
|
model = joblib.load(model_path) |
|
|
|
|
|
print("Model loaded successfully.") |
|
|
|
|
|
|
|
|
|
|
|
@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 "HI, Welcome to the Extraa Learn conversion Predictor API!" |
|
|
|
|
|
|
|
|
@rental_price_predictor_api.post('/v1/conversion') |
|
|
def predict_rental_price(): |
|
|
property_data = request.get_json() |
|
|
|
|
|
sample = { |
|
|
'age': property_data['age'], |
|
|
'website_visits': property_data['website_visits'], |
|
|
'time_spent_on_website': property_data['time_spent_on_website'], |
|
|
'page_views_per_visit': property_data['page_views_per_visit'], |
|
|
'current_occupation': property_data['current_occupation'], |
|
|
'first_interaction': property_data['first_interaction'], |
|
|
'profile_completed': property_data['profile_completed'], |
|
|
'last_activity': property_data['last_activity'], |
|
|
'print_media_type1': property_data['print_media_type1'], |
|
|
'print_media_type2': property_data['print_media_type2'], |
|
|
'digital_media': property_data['digital_media'], |
|
|
'educational_channels': property_data['educational_channels'], |
|
|
'referral': property_data['referral'] |
|
|
} |
|
|
|
|
|
input_data = pd.DataFrame([sample]) |
|
|
|
|
|
|
|
|
predicted_status = int(model.predict(input_data)[0]) |
|
|
|
|
|
return jsonify({'Predicted Status': predicted_status}) |
|
|
|
|
|
|
|
|
|
|
|
@rental_price_predictor_api.post('/v1/conversionbatch') |
|
|
def predict_rental_price_batch(): |
|
|
""" |
|
|
This function handles POST requests to the '/v1/conversionbatch' 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) |
|
|
|
|
|
|
|
|
status_log = model.predict(input_data).tolist() |
|
|
|
|
|
|
|
|
status = [round(float(np.exp(log_price)), 2) for log_price in status_log] |
|
|
|
|
|
|
|
|
property_ids = input_data['ID'].tolist() |
|
|
output_dict = dict(zip(property_ids, status)) |
|
|
|
|
|
|
|
|
return output_dict |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
rental_price_predictor_api.run(debug=True) |
|
|
|