Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import joblib
|
4 |
|
5 |
-
# Load
|
6 |
model = joblib.load('random_forest_model.pkl') # replace with your model path
|
7 |
|
8 |
-
#
|
9 |
def predict_price(host_id, neighbourhood_group, room_type, number_of_reviews, calculated_host_listings_count, latitude, longitude):
|
10 |
-
#
|
11 |
-
custom_data = pd.DataFrame(
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Make prediction
|
28 |
predicted_price = model.predict(custom_data)
|
29 |
return predicted_price[0]
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
fn=predict_price,
|
34 |
inputs=[
|
35 |
gr.Number(label="Host ID"),
|
36 |
-
gr.Dropdown(["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"]
|
37 |
-
gr.Dropdown(["Shared room", "Private room", "Entire home/apt"]
|
38 |
gr.Number(label="Number of Reviews"),
|
39 |
gr.Number(label="Calculated Host Listings Count"),
|
40 |
gr.Number(label="Latitude"),
|
41 |
gr.Number(label="Longitude")
|
42 |
],
|
43 |
-
outputs="
|
44 |
title="Airbnb Price Prediction",
|
45 |
description="Enter the details to predict the price of an Airbnb listing."
|
46 |
)
|
47 |
|
48 |
# Launch the interface
|
49 |
-
|
50 |
-
|
51 |
-
print("Custom Data Columns:", custom_data.columns.tolist())
|
52 |
-
print("Model Training Features:", model.feature_names_in_)
|
53 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import joblib
|
4 |
|
5 |
+
# Load the trained model
|
6 |
model = joblib.load('random_forest_model.pkl') # replace with your model path
|
7 |
|
8 |
+
# Define the function to make predictions
|
9 |
def predict_price(host_id, neighbourhood_group, room_type, number_of_reviews, calculated_host_listings_count, latitude, longitude):
|
10 |
+
# Initialize custom input data with columns matching the training data
|
11 |
+
custom_data = pd.DataFrame(0, index=[0], columns=model.feature_names_in_)
|
12 |
+
custom_data = custom_data.astype({'latitude': 'float64', 'longitude': 'float64'}) # Ensure latitude and longitude are floats
|
13 |
+
|
14 |
+
# Set values for the relevant columns
|
15 |
+
custom_data.at[0, 'host_id'] = host_id
|
16 |
+
custom_data.at[0, 'number_of_reviews'] = number_of_reviews
|
17 |
+
custom_data.at[0, 'calculated_host_listings_count'] = calculated_host_listings_count
|
18 |
+
custom_data.at[0, 'latitude'] = latitude
|
19 |
+
custom_data.at[0, 'longitude'] = longitude
|
20 |
+
|
21 |
+
# Set neighbourhood group features
|
22 |
+
custom_data['neighbourhood_group_Brooklyn'] = 1 if neighbourhood_group == 'Brooklyn' else 0
|
23 |
+
custom_data['neighbourhood_group_Manhattan'] = 1 if neighbourhood_group == 'Manhattan' else 0
|
24 |
+
custom_data['neighbourhood_group_Queens'] = 1 if neighbourhood_group == 'Queens' else 0
|
25 |
+
custom_data['neighbourhood_group_Bronx'] = 1 if neighbourhood_group == 'Bronx' else 0
|
26 |
+
custom_data['neighbourhood_group_Staten Island'] = 1 if neighbourhood_group == 'Staten Island' else 0
|
27 |
+
|
28 |
+
# Set room type features
|
29 |
+
custom_data['room_type_Shared room'] = 1 if room_type == 'Shared room' else 0
|
30 |
+
custom_data['room_type_Private room'] = 1 if room_type == 'Private room' else 0
|
31 |
+
custom_data['room_type_Entire home/apt'] = 1 if room_type == 'Entire home/apt' else 0
|
32 |
|
33 |
# Make prediction
|
34 |
predicted_price = model.predict(custom_data)
|
35 |
return predicted_price[0]
|
36 |
|
37 |
+
# Create the Gradio interface
|
38 |
+
iface = gr.Interface(
|
39 |
fn=predict_price,
|
40 |
inputs=[
|
41 |
gr.Number(label="Host ID"),
|
42 |
+
gr.Dropdown(label="Neighbourhood Group", choices=["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"]),
|
43 |
+
gr.Dropdown(label="Room Type", choices=["Shared room", "Private room", "Entire home/apt"]),
|
44 |
gr.Number(label="Number of Reviews"),
|
45 |
gr.Number(label="Calculated Host Listings Count"),
|
46 |
gr.Number(label="Latitude"),
|
47 |
gr.Number(label="Longitude")
|
48 |
],
|
49 |
+
outputs=gr.Number(label="Predicted Price"),
|
50 |
title="Airbnb Price Prediction",
|
51 |
description="Enter the details to predict the price of an Airbnb listing."
|
52 |
)
|
53 |
|
54 |
# Launch the interface
|
55 |
+
iface.launch()
|
|
|
|
|
|
|
|