fahad1995 commited on
Commit
0168a9b
1 Parent(s): 2a2e057

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -21
app.py CHANGED
@@ -7,28 +7,45 @@ 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)
@@ -52,4 +69,4 @@ iface = gr.Interface(
52
  )
53
 
54
  # Launch the interface
55
- iface.launch()
 
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)
 
69
  )
70
 
71
  # Launch the interface
72
+ iface.launch(share=True) # Set share=True to create a public link