fahad1995 commited on
Commit
58ab919
1 Parent(s): 7ba6cbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -21
app.py CHANGED
@@ -5,42 +5,66 @@ import joblib
5
  # Load the trained model
6
  model = joblib.load('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()
 
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, latitude, longitude, number_of_reviews, calculated_host_listings_count,
10
+ room_type_Private_room, room_type_Shared_room,
11
+ neighbourhood_group_Brooklyn, neighbourhood_group_Manhattan,
12
+ neighbourhood_group_Queens, neighbourhood_group_Staten_Island,
13
+ neighbourhood_Arden_Heights, neighbourhood_Arrochar, neighbourhood_Arverne):
14
+
15
+ # Prepare input data
16
+ custom_data = pd.DataFrame(0, index=[0], columns=model.feature_names_in_)
17
  custom_data.at[0, 'host_id'] = host_id
18
  custom_data.at[0, 'latitude'] = latitude
19
  custom_data.at[0, 'longitude'] = longitude
20
  custom_data.at[0, 'number_of_reviews'] = number_of_reviews
21
  custom_data.at[0, 'calculated_host_listings_count'] = calculated_host_listings_count
22
+ custom_data.at[0, 'room_type_Private room'] = room_type_Private_room
23
+ custom_data.at[0, 'room_type_Shared room'] = room_type_Shared_room
24
+ custom_data.at[0, 'neighbourhood_group_Brooklyn'] = neighbourhood_group_Brooklyn
25
+ custom_data.at[0, 'neighbourhood_group_Manhattan'] = neighbourhood_group_Manhattan
26
+ custom_data.at[0, 'neighbourhood_group_Queens'] = neighbourhood_group_Queens
27
+ custom_data.at[0, 'neighbourhood_group_Staten Island'] = neighbourhood_group_Staten_Island
28
+ custom_data.at[0, 'neighbourhood_Arden Heights'] = neighbourhood_Arden_Heights
29
+ custom_data.at[0, 'neighbourhood_Arrochar'] = neighbourhood_Arrochar
30
+ custom_data.at[0, 'neighbourhood_Arverne'] = neighbourhood_Arverne
31
 
32
  # Make prediction
33
  predicted_price = model.predict(custom_data)
34
+ return f"The predicted house price is: ${predicted_price[0]:.2f}"
35
 
36
+ # Set up the Gradio interface
37
+ title = "House Price Predictor"
38
+ description = """
39
+ This application predicts the price of a house based on several features.
40
+ Please fill in the following details to get a prediction:
41
+ - **Latitude**: Geographic coordinate.
42
+ - **Longitude**: Geographic coordinate.
43
+ - **Number of Reviews**: Total reviews received by the listing.
44
+ - **Calculated Host Listings Count**: Total number of listings by the host.
45
+ - **Room Type**: Select whether the room is a private or shared room.
46
+ - **Neighbourhood Groups**: Select the corresponding neighbourhood group.
47
 
48
+ After entering the information, click on the **'Submit'** button to see the predicted price.
49
+ """
50
 
 
51
  inputs = [
52
+
 
53
  gr.Number(label="Latitude"),
54
  gr.Number(label="Longitude"),
55
  gr.Number(label="Number of Reviews"),
56
+ gr.Number(label="Calculated Host Listings Count"),
57
+ gr.Radio(label="Room Type - Private Room", choices=[0, 1]),
58
+ gr.Radio(label="Room Type - Shared Room", choices=[0, 1]),
59
+ gr.Radio(label="Neighbourhood Group - Brooklyn", choices=[0, 1]),
60
+ gr.Radio(label="Neighbourhood Group - Manhattan", choices=[0, 1]),
61
+ gr.Radio(label="Neighbourhood Group - Queens", choices=[0, 1]),
62
+ gr.Radio(label="Neighbourhood Group - Staten Island", choices=[0, 1]),
63
+ gr.Radio(label="Neighbourhood - Arden Heights", choices=[0, 1]),
64
+ gr.Radio(label="Neighbourhood - Arrochar", choices=[0, 1]),
65
+ gr.Radio(label="Neighbourhood - Arverne", choices=[0, 1]),
66
  ]
67
 
68
+ output = gr.Textbox(label="Predicted Price", placeholder="The predicted price will appear here.", lines=2)
69
 
70
+ gr.Interface(fn=predict_price, inputs=inputs, outputs=output, title=title, description=description).launch()