fahad1995 commited on
Commit
21e0e83
·
verified ·
1 Parent(s): e1b08fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -40
app.py CHANGED
@@ -1,57 +1,47 @@
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
- # Example: Define the training DataFrame X (replace with your actual training data)
9
- # This should be a DataFrame with the same structure as your training data
10
- X = pd.DataFrame(columns=['host_id', 'neighbourhood_group', 'neighbourhood', 'room_type',
11
- 'latitude', 'longitude', 'number_of_reviews',
12
- 'calculated_host_listings_count']) # Add one-hot encoded columns as well
13
-
14
- # Function to make predictions
15
- def predict(host_id, neighbourhood_group, neighbourhood, room_type, latitude, longitude, number_of_reviews, calculated_host_listings_count):
16
- # Prepare input data as DataFrame
17
- input_data = pd.DataFrame({
18
  'host_id': [host_id],
19
- 'neighbourhood_group': [neighbourhood_group],
20
- 'neighbourhood': [neighbourhood],
21
- 'room_type': [room_type],
 
 
 
 
 
 
 
 
22
  'latitude': [latitude],
23
- 'longitude': [longitude],
24
- 'number_of_reviews': [number_of_reviews],
25
- 'calculated_host_listings_count': [calculated_host_listings_count]
26
  })
27
 
28
- # One-hot encode the categorical features
29
- input_data = pd.get_dummies(input_data, columns=['room_type', 'neighbourhood_group', 'neighbourhood'], drop_first=True)
30
-
31
- # Ensure the input data has the same columns as the training data
32
- input_data = input_data.reindex(columns=X.columns, fill_value=0)
33
-
34
- # Make the prediction
35
- predicted_price = loaded_model.predict(input_data)
36
  return predicted_price[0]
37
 
38
- # Create a Gradio interface
39
- iface = gr.Interface(
40
- fn=predict,
41
  inputs=[
42
  gr.Number(label="Host ID"),
43
- gr.Dropdown(label="Neighbourhood Group", choices=["Manhattan", "Brooklyn", "Queens", "Staten Island", "Bronx"]),
44
- gr.Dropdown(label="Neighbourhood", choices=["Chelsea", "East Village", "Midtown", "SoHo", "Williamsburg"]), # Add actual neighbourhood options
45
- gr.Dropdown(label="Room Type", choices=["Entire home/apt", "Private room", "Shared room"]),
46
- gr.Number(label="Latitude"),
47
- gr.Number(label="Longitude"),
48
  gr.Number(label="Number of Reviews"),
49
- gr.Number(label="Calculated Host Listings Count")
 
 
50
  ],
51
- outputs=gr.Number(label="Predicted Price"),
52
  title="Airbnb Price Prediction",
53
- description="Enter the details of the Airbnb listing to predict the price."
54
  )
55
 
56
  # Launch the interface
57
- iface.launch()
 
1
  import gradio as gr
2
  import pandas as pd
 
3
 
4
+ # Function to predict price
5
+ def predict_price(host_id, neighbourhood_group, room_type, price, reviews, calculated_host_listings_count, latitude, longitude):
6
+ # Create a DataFrame for the input data
7
+ custom_data = pd.DataFrame({
 
 
 
 
 
 
 
 
 
8
  'host_id': [host_id],
9
+ 'neighbourhood_group_Brooklyn': [1 if neighbourhood_group == 'Brooklyn' else 0],
10
+ 'neighbourhood_group_Manhattan': [1 if neighbourhood_group == 'Manhattan' else 0],
11
+ 'neighbourhood_group_Queens': [1 if neighbourhood_group == 'Queens' else 0],
12
+ 'neighbourhood_group_Bronx': [1 if neighbourhood_group == 'Bronx' else 0],
13
+ 'neighbourhood_group_Staten Island': [1 if neighbourhood_group == 'Staten Island' else 0],
14
+ 'room_type_Shared room': [1 if room_type == 'Shared room' else 0],
15
+ 'room_type_Private room': [1 if room_type == 'Private room' else 0],
16
+ 'room_type_Entire home/apt': [1 if room_type == 'Entire home/apt' else 0],
17
+ 'price': [price],
18
+ 'number_of_reviews': [reviews],
19
+ 'calculated_host_listings_count': [calculated_host_listings_count],
20
  'latitude': [latitude],
21
+ 'longitude': [longitude]
 
 
22
  })
23
 
24
+ # Make prediction
25
+ predicted_price = model.predict(custom_data)
 
 
 
 
 
 
26
  return predicted_price[0]
27
 
28
+ # Define Gradio interface
29
+ interface = gr.Interface(
30
+ fn=predict_price,
31
  inputs=[
32
  gr.Number(label="Host ID"),
33
+ gr.Dropdown(["Brooklyn", "Manhattan", "Queens", "Bronx", "Staten Island"], label="Neighbourhood Group"),
34
+ gr.Dropdown(["Shared room", "Private room", "Entire home/apt"], label="Room Type"),
35
+ gr.Number(label="Price"),
 
 
36
  gr.Number(label="Number of Reviews"),
37
+ gr.Number(label="Calculated Host Listings Count"),
38
+ gr.Number(label="Latitude"),
39
+ gr.Number(label="Longitude")
40
  ],
41
+ outputs="number",
42
  title="Airbnb Price Prediction",
43
+ description="Enter the details to predict the price of an Airbnb listing."
44
  )
45
 
46
  # Launch the interface
47
+ interface.launch()