Update app.py
Browse files
app.py
CHANGED
@@ -3,70 +3,44 @@ 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
|
9 |
-
def predict_price(host_id, neighbourhood_group,
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
'calculated_host_listings_count': calculated_host_listings_count,
|
15 |
-
'latitude': latitude,
|
16 |
-
'longitude': longitude,
|
17 |
-
# Set dummy values for categorical features
|
18 |
-
'neighbourhood_group_Brooklyn': 0,
|
19 |
-
'neighbourhood_group_Manhattan': 0,
|
20 |
-
'neighbourhood_group_Queens': 0,
|
21 |
-
'neighbourhood_group_Bronx': 0,
|
22 |
-
'neighbourhood_group_Staten Island': 0,
|
23 |
-
'room_type_Shared room': 0,
|
24 |
-
'room_type_Private room': 0,
|
25 |
-
'room_type_Entire home/apt': 0,
|
26 |
-
}
|
27 |
-
|
28 |
-
# Set appropriate values based on user input
|
29 |
-
if neighbourhood_group == 'Brooklyn':
|
30 |
-
data['neighbourhood_group_Brooklyn'] = 1
|
31 |
-
elif neighbourhood_group == 'Manhattan':
|
32 |
-
data['neighbourhood_group_Manhattan'] = 1
|
33 |
-
elif neighbourhood_group == 'Queens':
|
34 |
-
data['neighbourhood_group_Queens'] = 1
|
35 |
-
elif neighbourhood_group == 'Bronx':
|
36 |
-
data['neighbourhood_group_Bronx'] = 1
|
37 |
-
elif neighbourhood_group == 'Staten Island':
|
38 |
-
data['neighbourhood_group_Staten Island'] = 1
|
39 |
-
|
40 |
-
if room_type == 'Shared room':
|
41 |
-
data['room_type_Shared room'] = 1
|
42 |
-
elif room_type == 'Private room':
|
43 |
-
data['room_type_Private room'] = 1
|
44 |
-
elif room_type == 'Entire home/apt':
|
45 |
-
data['room_type_Entire home/apt'] = 1
|
46 |
|
47 |
-
#
|
48 |
-
custom_data =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# Make prediction
|
51 |
predicted_price = model.predict(custom_data)
|
52 |
-
return predicted_price[0]
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
|
71 |
-
|
72 |
-
iface.launch(share=True) # Set share=True to create a public link
|
|
|
3 |
import joblib
|
4 |
|
5 |
# Load the trained model
|
6 |
+
model = joblib.load('/content/random_forest_model.pkl') # replace with your model path
|
7 |
|
8 |
+
# Define the prediction function
|
9 |
+
def predict_price(host_id, neighbourhood_group, latitude, longitude, number_of_reviews, calculated_host_listings_count):
|
10 |
+
# Initialize custom input data
|
11 |
+
training_columns = model.feature_names_in_
|
12 |
+
custom_data = pd.DataFrame(0, index=[0], columns=training_columns)
|
13 |
+
custom_data = custom_data.astype({'latitude': 'float64', 'longitude': 'float64'}) # Ensure float types
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Specify values for the relevant columns in `custom_data`
|
16 |
+
custom_data.at[0, 'host_id'] = host_id
|
17 |
+
custom_data.at[0, 'latitude'] = latitude
|
18 |
+
custom_data.at[0, 'longitude'] = longitude
|
19 |
+
custom_data.at[0, 'number_of_reviews'] = number_of_reviews
|
20 |
+
custom_data.at[0, 'calculated_host_listings_count'] = calculated_host_listings_count
|
21 |
+
|
22 |
+
# Set neighbourhood group feature
|
23 |
+
custom_data.at[0, f'neighbourhood_group_{neighbourhood_group}'] = 1
|
24 |
|
25 |
# Make prediction
|
26 |
predicted_price = model.predict(custom_data)
|
|
|
27 |
|
28 |
+
# Display input data and predicted price
|
29 |
+
input_data_display = custom_data.iloc[0].to_dict()
|
30 |
+
input_data_display['Predicted Price'] = predicted_price[0]
|
31 |
+
|
32 |
+
return input_data_display
|
33 |
+
|
34 |
+
# Define Gradio interface
|
35 |
+
inputs = [
|
36 |
+
gr.Number(label="Host ID"),
|
37 |
+
gr.Dropdown(choices=["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"], label="Neighbourhood Group"),
|
38 |
+
gr.Number(label="Latitude"),
|
39 |
+
gr.Number(label="Longitude"),
|
40 |
+
gr.Number(label="Number of Reviews"),
|
41 |
+
gr.Number(label="Calculated Host Listings Count")
|
42 |
+
]
|
43 |
+
|
44 |
+
output = gr.JSON(label="Input Data and Predicted Price")
|
45 |
|
46 |
+
gr.Interface(fn=predict_price, inputs=inputs, outputs=output, title="Airbnb Price Prediction", description="Input data to predict Airbnb listing prices").launch()
|
|