alidenewade commited on
Commit
b6e4981
·
verified ·
1 Parent(s): 4ae5b69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -118,13 +118,16 @@ def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_p
118
 
119
  # Create a DataFrame for the model from all features used during training
120
  # Ensure the column order matches the training data
121
- feature_names = ['crime_felony', 'crime_misd', 'crime_viol', 'sr311_total', 'dob_permits_total']
122
 
123
  # Check if the model has feature names attribute
124
  if hasattr(model, 'feature_name_'):
125
  feature_names = model.feature_name_
126
  elif hasattr(model, 'feature_names_in_'):
127
  feature_names = model.feature_names_in_
 
 
 
128
 
129
  input_data = pd.DataFrame({
130
  'crime_felony': [crime_felony],
@@ -135,8 +138,7 @@ def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_p
135
  })
136
 
137
  # Add any other columns from the panel_df that the model might expect, filling with 0
138
- all_cols = set(panel_df.columns) - {'GEOID', 'month', 'crime_total'}
139
- for col in all_cols:
140
  if col not in input_data.columns:
141
  input_data[col] = 0
142
 
@@ -151,7 +153,7 @@ def predict_crime_level(crime_felony, crime_misd, crime_viol, sr311_total, dob_p
151
  prediction = labels[np.argmax(probabilities)]
152
 
153
  # Create a confidence dictionary
154
- confidence = {label: prob for label, prob in zip(labels, probabilities)}
155
 
156
  return f"Predicted Crime Level: {prediction}", confidence
157
 
@@ -223,6 +225,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
223
  map_plot = gr.Plot()
224
  ts_plot = gr.Plot()
225
 
 
226
  update_button.click(
227
  fn=create_choropleth_map,
228
  inputs=[metric_selector, start_date_picker, end_date_picker],
@@ -233,6 +236,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
233
  inputs=[metric_selector, start_date_picker, end_date_picker],
234
  outputs=ts_plot
235
  )
 
 
 
 
 
 
 
 
 
 
 
 
236
 
237
  with gr.Tab("Predictive Analytics"):
238
  with gr.Tabs():
 
118
 
119
  # Create a DataFrame for the model from all features used during training
120
  # Ensure the column order matches the training data
121
+ feature_names = None
122
 
123
  # Check if the model has feature names attribute
124
  if hasattr(model, 'feature_name_'):
125
  feature_names = model.feature_name_
126
  elif hasattr(model, 'feature_names_in_'):
127
  feature_names = model.feature_names_in_
128
+
129
+ if feature_names is None:
130
+ return "Model is missing feature name information.", {}
131
 
132
  input_data = pd.DataFrame({
133
  'crime_felony': [crime_felony],
 
138
  })
139
 
140
  # Add any other columns from the panel_df that the model might expect, filling with 0
141
+ for col in feature_names:
 
142
  if col not in input_data.columns:
143
  input_data[col] = 0
144
 
 
153
  prediction = labels[np.argmax(probabilities)]
154
 
155
  # Create a confidence dictionary
156
+ confidence = {label: f"{prob:.2%}" for label, prob in zip(labels, probabilities)}
157
 
158
  return f"Predicted Crime Level: {prediction}", confidence
159
 
 
225
  map_plot = gr.Plot()
226
  ts_plot = gr.Plot()
227
 
228
+ # --- Event Listeners ---
229
  update_button.click(
230
  fn=create_choropleth_map,
231
  inputs=[metric_selector, start_date_picker, end_date_picker],
 
236
  inputs=[metric_selector, start_date_picker, end_date_picker],
237
  outputs=ts_plot
238
  )
239
+
240
+ # Trigger initial plot generation on app load
241
+ demo.load(
242
+ fn=create_choropleth_map,
243
+ inputs=[metric_selector, start_date_picker, end_date_picker],
244
+ outputs=map_plot
245
+ )
246
+ demo.load(
247
+ fn=create_time_series_plot,
248
+ inputs=[metric_selector, start_date_picker, end_date_picker],
249
+ outputs=ts_plot
250
+ )
251
 
252
  with gr.Tab("Predictive Analytics"):
253
  with gr.Tabs():