riyadhrazzaq commited on
Commit
a31c8a2
·
1 Parent(s): 067ac42

separate officer race and gender code

Browse files
Files changed (5) hide show
  1. app.py +2 -1
  2. block.py → block_officer_race.py +1 -11
  3. inference.py +19 -5
  4. model.py +1 -0
  5. preprocessor.py +46 -5
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
 
3
- from block import officerRaceDemo, officerGenderDemo
 
4
 
5
  demo = gr.TabbedInterface([officerRaceDemo, officerGenderDemo],
6
  ["Predict OfficerRace", "Predict Officer Gender"],
 
1
  import gradio as gr
2
 
3
+ from block_officer_race import officerRaceDemo
4
+ from block_officer_gender import officerGenderDemo
5
 
6
  demo = gr.TabbedInterface([officerRaceDemo, officerGenderDemo],
7
  ["Predict OfficerRace", "Predict Officer Gender"],
block.py → block_officer_race.py RENAMED
@@ -144,14 +144,4 @@ with gr.Blocks() as officerRaceDemo:
144
  impacted_race_dropdown,
145
  incident_precinct_dropdown]
146
  btn = gr.Button("Predict")
147
- btn.click(fn=infer, inputs=input_components, outputs=out)
148
-
149
- with gr.Blocks() as officerGenderDemo:
150
- def greet(name):
151
- return "Hello " + name + "!"
152
-
153
-
154
- name = gr.Textbox(label="Name")
155
- output = gr.Textbox(label="Output Box")
156
- greet_btn = gr.Button("Greet")
157
- greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
 
144
  impacted_race_dropdown,
145
  incident_precinct_dropdown]
146
  btn = gr.Button("Predict")
147
+ btn.click(fn=infer, inputs=input_components, outputs=out)
 
 
 
 
 
 
 
 
 
 
inference.py CHANGED
@@ -2,7 +2,7 @@ import numpy as np
2
 
3
  import config
4
  from model import load_models
5
- from preprocessor import preprocess
6
 
7
 
8
  def predict_officer_race(model_name, X):
@@ -21,10 +21,24 @@ def infer_officer_race(model_name, current_rank, incident_rank, previous_complai
21
  days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
22
  penalty_rec, penalty_cat, location_type, contact_outcome, impacted_gender, impacted_race,
23
  incident_precinct):
24
- input_array = preprocess(current_rank, incident_rank, previous_complaints, complaint_duration_days,
25
- days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
26
- penalty_rec, penalty_cat, location_type, contact_outcome, impacted_gender, impacted_race,
27
- incident_precinct)
28
  prediction = predict_officer_race(model_name, input_array)
29
  prediction = f"The officer is predicted to be {prediction}"
30
  return prediction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import config
4
  from model import load_models
5
+ from preprocessor import process_officer_race
6
 
7
 
8
  def predict_officer_race(model_name, X):
 
21
  days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
22
  penalty_rec, penalty_cat, location_type, contact_outcome, impacted_gender, impacted_race,
23
  incident_precinct):
24
+ input_array = process_officer_race(current_rank, incident_rank, previous_complaints, complaint_duration_days,
25
+ days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
26
+ penalty_rec, penalty_cat, location_type, contact_outcome, impacted_gender, impacted_race,
27
+ incident_precinct)
28
  prediction = predict_officer_race(model_name, input_array)
29
  prediction = f"The officer is predicted to be {prediction}"
30
  return prediction
31
+
32
+
33
+ def infer_officer_gender(model_name, current_rank, incident_rank, previous_complaints, complaint_duration_days,
34
+ days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
35
+ penalty_rec, penalty_cat, location_type, contact_outcome, impacted_gender, impacted_race,
36
+ incident_precinct):
37
+ input_array = process_officer_race(current_rank, incident_rank, previous_complaints, complaint_duration_days,
38
+ days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
39
+ penalty_rec, penalty_cat, location_type, contact_outcome, impacted_gender, impacted_race,
40
+ incident_precinct)
41
+ prediction = predict_officer_race(model_name, input_array)
42
+ prediction = f"The officer is predicted to be {prediction}"
43
+ return prediction
44
+
model.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import joblib
3
  from pathlib import Path
4
 
 
5
  def load_models(model_name, target_column):
6
  """Load models from disk"""
7
  model_root = Path("./models")
 
2
  import joblib
3
  from pathlib import Path
4
 
5
+
6
  def load_models(model_name, target_column):
7
  """Load models from disk"""
8
  model_root = Path("./models")
preprocessor.py CHANGED
@@ -77,11 +77,52 @@ def transform_to_ohe(column_name, value):
77
  return one_hot
78
 
79
 
80
- def preprocess(current_rank, incident_rank, previous_complaints, complaint_duration_days,
81
- days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
82
- penalty_rec, penalty_cat, location_type, contact_outcome,
83
- impacted_gender, impacted_race,
84
- incident_precinct):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  current_rank = transform_rank(current_rank)
86
  incident_rank = transform_rank(incident_rank)
87
  previous_complaints = transform_previous_complaints(previous_complaints)
 
77
  return one_hot
78
 
79
 
80
+ def process_officer_race(current_rank, incident_rank, previous_complaints, complaint_duration_days,
81
+ days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
82
+ penalty_rec, penalty_cat, location_type, contact_outcome,
83
+ impacted_gender, impacted_race,
84
+ incident_precinct):
85
+ current_rank = transform_rank(current_rank)
86
+ incident_rank = transform_rank(incident_rank)
87
+ previous_complaints = transform_previous_complaints(previous_complaints)
88
+ complaint_duration_days = transform_complaint_duration_days(complaint_duration_days)
89
+ days_on_force = transform_days_on_force(days_on_force)
90
+ officer_gender = transform_to_ohe('OfficerGender', officer_gender)
91
+ fado_type = transform_to_ohe('FADOType', fado_type)
92
+ allegation = transform_to_ohe('Allegation', allegation)
93
+ ccrb_disposition = transform_to_ohe('CCRBDisposition', ccrb_disposition)
94
+ penalty_rec = transform_to_ohe('PenaltyRec', penalty_rec)
95
+ penalty_cat = transform_to_ohe('PenaltyCat', penalty_cat)
96
+ location_type = transform_to_ohe('LocationType', location_type)
97
+ contact_outcome = transform_to_ohe('ContactOutcome', contact_outcome)
98
+ impacted_gender = transform_to_ohe("ImpactedGender", impacted_gender)
99
+ impacted_race = transform_to_ohe("ImpactedRace", impacted_race)
100
+ incident_precinct = transform_to_ohe("IncidentPrecinct", incident_precinct)
101
+
102
+ input_array = np.concatenate((current_rank,
103
+ incident_rank,
104
+ days_on_force,
105
+ previous_complaints,
106
+ complaint_duration_days,
107
+ officer_gender,
108
+ fado_type,
109
+ allegation,
110
+ ccrb_disposition,
111
+ penalty_rec,
112
+ penalty_cat,
113
+ location_type,
114
+ contact_outcome,
115
+ impacted_gender,
116
+ impacted_race,
117
+ incident_precinct), dtype=np.float32)
118
+ input_array = input_array.reshape(1, -1)
119
+ return input_array
120
+
121
+ def process_officer_gender(current_rank, incident_rank, previous_complaints, complaint_duration_days,
122
+ days_on_force, officer_gender, fado_type, allegation, ccrb_disposition,
123
+ penalty_rec, penalty_cat, location_type, contact_outcome,
124
+ impacted_gender, impacted_race,
125
+ incident_precinct):
126
  current_rank = transform_rank(current_rank)
127
  incident_rank = transform_rank(incident_rank)
128
  previous_complaints = transform_previous_complaints(previous_complaints)