Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
|
5 |
+
# Load your Random Forest model
|
6 |
+
loaded_model = joblib.load('random_forest_model.pkl')
|
7 |
+
|
8 |
+
# Function to make predictions
|
9 |
+
def predict(host_id, neighbourhood_group, neighbourhood, room_type, latitude, longitude, number_of_reviews, calculated_host_listings_count):
|
10 |
+
# Prepare input data as DataFrame
|
11 |
+
input_data = pd.DataFrame({
|
12 |
+
'host_id': [host_id],
|
13 |
+
'neighbourhood_group': [neighbourhood_group],
|
14 |
+
'neighbourhood': [neighbourhood],
|
15 |
+
'room_type': [room_type],
|
16 |
+
'latitude': [latitude],
|
17 |
+
'longitude': [longitude],
|
18 |
+
'number_of_reviews': [number_of_reviews],
|
19 |
+
'calculated_host_listings_count': [calculated_host_listings_count]
|
20 |
+
})
|
21 |
+
|
22 |
+
# One-hot encode the categorical features
|
23 |
+
input_data = pd.get_dummies(input_data, columns=['room_type', 'neighbourhood_group', 'neighbourhood'], drop_first=True)
|
24 |
+
|
25 |
+
# Ensure the input data has the same columns as the training data
|
26 |
+
input_data = input_data.reindex(columns=X.columns, fill_value=0)
|
27 |
+
|
28 |
+
# Make the prediction
|
29 |
+
predicted_price = loaded_model.predict(input_data)
|
30 |
+
return predicted_price[0]
|
31 |
+
|
32 |
+
# Create a Gradio interface
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=predict,
|
35 |
+
inputs=[
|
36 |
+
gr.Number(label="Host ID"),
|
37 |
+
gr.Dropdown(["Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island"], label="Neighbourhood Group"),
|
38 |
+
gr.Dropdown(["Upper East Side", "Chelsea", "Williamsburg"], label="Neighbourhood"),
|
39 |
+
gr.Dropdown(["Entire home/apt", "Private room", "Shared room"], label="Room Type"),
|
40 |
+
gr.Number(label="Latitude"),
|
41 |
+
gr.Number(label="Longitude"),
|
42 |
+
gr.Number(label="Number of Reviews"),
|
43 |
+
gr.Number(label="Calculated Host Listings Count")
|
44 |
+
],
|
45 |
+
outputs="number",
|
46 |
+
title="NYC Rental Price Prediction",
|
47 |
+
description="Predict the rental price of an Airbnb listing in NYC."
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch the interface
|
51 |
+
iface.launch()
|