Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +52 -0
- requirements.txt +11 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
# Set the working directory inside the container
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy all files from the current directory to the container's working directory
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
# Install dependencies from the requirements file without using cache to reduce image size
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 13 |
+
# - -w 4: Uses 4 worker processes for handling requests
|
| 14 |
+
# - -b 0.0.0.0:7860: Binds the server to port 7860 on all network interfaces
|
| 15 |
+
# - pp:app: Runs the Flask app (assuming pp.py contains the Flask instance named pp)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:rental_price_predictor_api"]
|
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)
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
xgboost==2.1.4
|
| 5 |
+
joblib==1.4.2
|
| 6 |
+
Werkzeug==2.2.2
|
| 7 |
+
flask==2.2.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.28.1
|
| 10 |
+
uvicorn[standard]
|
| 11 |
+
streamlit==1.43.2
|