Upload backend_files/app.py with huggingface_hub
Browse files- backend_files/app.py +52 -0
backend_files/app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib # For loading the serialized model
|
| 4 |
+
import pandas as pd # For data manipulation
|
| 5 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 6 |
+
|
| 7 |
+
# Initialize the Flask application
|
| 8 |
+
rental_price_predictor_api = Flask('Airbnb Rental Price Predictor')
|
| 9 |
+
|
| 10 |
+
# Load the trained machine learning model
|
| 11 |
+
model = joblib.load('rental_price_prediction_model_v1_0.joblib')
|
| 12 |
+
|
| 13 |
+
# Define a route for the home page (GET request)
|
| 14 |
+
@rental_price_predictor_api.get('/')
|
| 15 |
+
def home():
|
| 16 |
+
return 'Welcome to the Airbnb Rental Price Prediction API!'
|
| 17 |
+
|
| 18 |
+
# Define an endpoint for single property prediction (POST request)
|
| 19 |
+
@rental_price_predictor_api.post('/v1/rental')
|
| 20 |
+
def predict_rental_price():
|
| 21 |
+
property_data = request.get_json()
|
| 22 |
+
sample = {
|
| 23 |
+
'room_type': property_data['room_type'],
|
| 24 |
+
'accommodates': property_data['accommodates'],
|
| 25 |
+
'bathrooms': property_data['bathrooms'],
|
| 26 |
+
'cancellation_policy': property_data['cancellation_policy'],
|
| 27 |
+
'cleaning_fee': property_data['cleaning_fee'],
|
| 28 |
+
'instant_bookable': property_data['instant_bookable'],
|
| 29 |
+
'review_scores_rating': property_data['review_scores_rating'],
|
| 30 |
+
'bedrooms': property_data['bedrooms'],
|
| 31 |
+
'beds': property_data['beds']
|
| 32 |
+
}
|
| 33 |
+
input_data = pd.DataFrame([sample])
|
| 34 |
+
predicted_log_price = model.predict(input_data)[0]
|
| 35 |
+
predicted_price = np.exp(predicted_log_price)
|
| 36 |
+
predicted_price = round(float(predicted_price), 2)
|
| 37 |
+
return jsonify({'Predicted Price (in dollars)': predicted_price})
|
| 38 |
+
|
| 39 |
+
# Define an endpoint for batch prediction (POST request)
|
| 40 |
+
@rental_price_predictor_api.post('/v1/rentalbatch')
|
| 41 |
+
def predict_rental_price_batch():
|
| 42 |
+
file = request.files['file']
|
| 43 |
+
input_data = pd.read_csv(file)
|
| 44 |
+
predicted_log_prices = model.predict(input_data).tolist()
|
| 45 |
+
predicted_prices = [round(float(np.exp(log_price)), 2) for log_price in predicted_log_prices]
|
| 46 |
+
property_ids = input_data['id'].tolist()
|
| 47 |
+
output_dict = dict(zip(property_ids, predicted_prices))
|
| 48 |
+
return output_dict
|
| 49 |
+
|
| 50 |
+
# Run the Flask application
|
| 51 |
+
if __name__ == '__main__':
|
| 52 |
+
rental_price_predictor_api.run(debug=True)
|