# Import necessary libraries import numpy as np import joblib # For loading the serialized model import pandas as pd # For data manipulation from flask import Flask, request, jsonify # For creating the Flask API # Initialize the Flask application rental_price_predictor_api = Flask("Airbnb Rental Price Predictor") # Load the trained machine learning model model = joblib.load("rental_price_prediction_model_v1_0.joblib") # Define a route for the home page (GET request) @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 Airbnb Rental Price Prediction API!" # Define an endpoint for single property prediction (POST request) @rental_price_predictor_api.post('/v1/rental') def predict_rental_price(): """ This function handles POST requests to the '/v1/rental' endpoint. It expects a JSON payload containing property details and returns the predicted rental price as a JSON response. """ # Get the JSON data from the request body property_data = request.get_json() # Extract relevant features from the JSON data sample = { 'room_type': property_data['room_type'], 'accommodates': property_data['accommodates'], 'bathrooms': property_data['bathrooms'], 'cancellation_policy': property_data['cancellation_policy'], 'cleaning_fee': property_data['cleaning_fee'], 'instant_bookable': property_data['instant_bookable'], 'review_scores_rating': property_data['review_scores_rating'], 'bedrooms': property_data['bedrooms'], 'beds': property_data['beds'] } # Convert the extracted data into a Pandas DataFrame input_data = pd.DataFrame([sample]) # Make prediction (get log_price) predicted_log_price = model.predict(input_data)[0] # Calculate actual price predicted_price = np.exp(predicted_log_price) # Convert predicted_price to Python float predicted_price = round(float(predicted_price), 2) # The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values. # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error # Return the actual price return jsonify({'Predicted Price (in dollars)': predicted_price}) # Define an endpoint for batch prediction (POST request) @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. """ # Get the uploaded CSV file from the request file = request.files['file'] # Read the CSV file into a Pandas DataFrame input_data = pd.read_csv(file) # Make predictions for all properties in the DataFrame (get log_prices) predicted_log_prices = model.predict(input_data).tolist() # Calculate actual prices predicted_prices = [round(float(np.exp(log_price)), 2) for log_price in predicted_log_prices] # Create a dictionary of predictions with property IDs as keys property_ids = input_data['id'].tolist() # Assuming 'id' is the property ID column output_dict = dict(zip(property_ids, predicted_prices)) # Use actual prices # Return the predictions dictionary as a JSON response return output_dict # Run the Flask application in debug mode if this script is executed directly if __name__ == '__main__': rental_price_predictor_api.run(debug=True)