fahad1995 commited on
Commit
654e814
1 Parent(s): 0168a9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -60
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 to make predictions
9
- def predict_price(host_id, neighbourhood_group, room_type, number_of_reviews, calculated_host_listings_count, latitude, longitude):
10
- # Create a dictionary with default values
11
- data = {
12
- 'host_id': host_id,
13
- 'number_of_reviews': number_of_reviews,
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
- # Create DataFrame from the dictionary
48
- custom_data = pd.DataFrame([data])
 
 
 
 
 
 
 
49
 
50
  # Make prediction
51
  predicted_price = model.predict(custom_data)
52
- return predicted_price[0]
53
 
54
- # Create the Gradio interface
55
- iface = gr.Interface(
56
- fn=predict_price,
57
- inputs=[
58
- gr.Number(label="Host ID"),
59
- gr.Dropdown(label="Neighbourhood Group", choices=["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"]),
60
- gr.Dropdown(label="Room Type", choices=["Shared room", "Private room", "Entire home/apt"]),
61
- gr.Number(label="Number of Reviews"),
62
- gr.Number(label="Calculated Host Listings Count"),
63
- gr.Number(label="Latitude"),
64
- gr.Number(label="Longitude")
65
- ],
66
- outputs=gr.Number(label="Predicted Price"),
67
- title="Airbnb Price Prediction",
68
- description="Enter the details to predict the price of an Airbnb listing."
69
- )
 
70
 
71
- # Launch the interface
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()