lindritdev commited on
Commit
d43c1ed
·
verified ·
1 Parent(s): e729ea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -1,22 +1,27 @@
 
1
  import gradio as gr
2
- import tensorflow as tf
3
  import numpy as np
4
  import pandas as pd
5
-
6
  import pickle
7
 
8
- # Define the filename of your pickle model
 
9
  model_filename = "random_forest_regression_luxurious.pkl"
10
 
11
- # Open the file in read-binary mode and load the model
12
  with open(model_filename, 'rb') as f:
13
- model = pickle.load(f)
14
 
15
- print("Pickle model loaded successfully!")
 
 
16
 
 
17
  df_bfs_data = pd.read_csv('bfs_municipality_and_tax_data.csv', sep=',', encoding='utf-8')
18
  df_bfs_data['tax_income'] = df_bfs_data['tax_income'].str.replace("'", "").astype(float)
19
 
 
20
  locations = {
21
  "Zürich": 261,
22
  "Kloten": 62,
@@ -124,18 +129,21 @@ locations = {
124
  "Horgen": 295
125
  }
126
 
127
- # Update the prediction function to include the "luxurious" input
 
128
  def predict_apartment(rooms, area, town, luxurious):
129
  bfs_number = locations[town]
130
- df = df_bfs_data[df_bfs_data['bfs_number'] == bfs_number]
131
-
 
 
132
  if len(df) != 1: # if not exactly one record, return -1
133
  return -1
134
-
135
  # Convert the luxurious input (a boolean from the checkbox) to an integer (1 if True, 0 if False)
136
  luxurious_value = 1 if luxurious else 0
137
 
138
- # Create the input vector (ensure the order of features matches your model training)
139
  input_features = np.array([
140
  rooms,
141
  area,
@@ -146,11 +154,14 @@ def predict_apartment(rooms, area, town, luxurious):
146
  df['tax_income'].iloc[0],
147
  luxurious_value
148
  ])
149
-
150
- input_features = input_features.reshape(1, 8) # Now 8 features are expected
151
- prediction = model.predict(input_features)
152
- return np.round(prediction[0][0], 0)
 
 
153
 
 
154
  # Create the Gradio interface with an extra input for luxurious (yes/no)
155
  iface = gr.Interface(
156
  fn=predict_apartment,
@@ -160,7 +171,13 @@ iface = gr.Interface(
160
  gr.Dropdown(choices=list(locations.keys()), label="Town", type="value"),
161
  gr.Checkbox(label="Luxurious?")
162
  ],
163
- outputs=gr.Number()
 
 
 
 
164
  )
165
 
166
- iface.launch()
 
 
 
1
+ # %%
2
  import gradio as gr
3
+ from sklearn.ensemble import RandomForestRegressor
4
  import numpy as np
5
  import pandas as pd
 
6
  import pickle
7
 
8
+ # %%
9
+ # TODO change the file to your own model.
10
  model_filename = "random_forest_regression_luxurious.pkl"
11
 
12
+ random_forest_model = RandomForestRegressor()
13
  with open(model_filename, 'rb') as f:
14
+ random_forest_model = pickle.load(f)
15
 
16
+ print('Number of features: ', random_forest_model.n_features_in_)
17
+ print('Features are (see week 1): ', ['rooms', 'area', 'pop', 'pop_dens', 'frg_pct', 'emp', 'tax_income', 'luxurious'])
18
+ random_forest_model
19
 
20
+ # %%
21
  df_bfs_data = pd.read_csv('bfs_municipality_and_tax_data.csv', sep=',', encoding='utf-8')
22
  df_bfs_data['tax_income'] = df_bfs_data['tax_income'].str.replace("'", "").astype(float)
23
 
24
+ # %%
25
  locations = {
26
  "Zürich": 261,
27
  "Kloten": 62,
 
129
  "Horgen": 295
130
  }
131
 
132
+ # %%
133
+ # Define the core prediction function including the "luxurious" input
134
  def predict_apartment(rooms, area, town, luxurious):
135
  bfs_number = locations[town]
136
+ df = df_bfs_data[df_bfs_data['bfs_number'] == bfs_number].copy()
137
+ df.reset_index(inplace=True)
138
+ df.loc[0, 'rooms'] = rooms
139
+ df.loc[0, 'area'] = area
140
  if len(df) != 1: # if not exactly one record, return -1
141
  return -1
142
+
143
  # Convert the luxurious input (a boolean from the checkbox) to an integer (1 if True, 0 if False)
144
  luxurious_value = 1 if luxurious else 0
145
 
146
+ # Create the input vector with the new "luxurious" attribute as the last feature
147
  input_features = np.array([
148
  rooms,
149
  area,
 
154
  df['tax_income'].iloc[0],
155
  luxurious_value
156
  ])
157
+ input_features = input_features.reshape(1, 8)
158
+ prediction = random_forest_model.predict(input_features)
159
+ return np.round(prediction[0], 0)
160
+
161
+ # %%
162
+ print(predict_apartment(3, 100, 'Zürich', True))
163
 
164
+ # %%
165
  # Create the Gradio interface with an extra input for luxurious (yes/no)
166
  iface = gr.Interface(
167
  fn=predict_apartment,
 
171
  gr.Dropdown(choices=list(locations.keys()), label="Town", type="value"),
172
  gr.Checkbox(label="Luxurious?")
173
  ],
174
+ outputs=gr.Number(),
175
+ examples=[
176
+ [4.5, 120, "Dietikon", True],
177
+ [3.5, 60, "Winterthur", False]
178
+ ]
179
  )
180
 
181
+ iface.launch()
182
+
183
+