Files changed (1) hide show
  1. app.py +134 -4
app.py CHANGED
@@ -1,7 +1,137 @@
 
1
  import gradio as gr
 
2
 
3
- def hello_world(name):
4
- return f"Hello, {name}!"
5
 
6
- iface = gr.Interface(fn=hello_world, inputs="text", outputs="text")
7
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
  import gradio as gr
3
+ from joblib import load
4
 
5
+ # Model URL for each
6
+ rf_model_url = 'random_forest_model.joblib'
7
 
8
+
9
+ # Load Model method
10
+ def load_model(url):
11
+ return load(rf_model_url)
12
+
13
+
14
+ def bool_value(val):
15
+ if not val:
16
+ return 0
17
+ else:
18
+ return 1
19
+
20
+
21
+ def gender(val):
22
+ if val == 'Male':
23
+ return 1
24
+ elif val == 'Female':
25
+ return 0
26
+ else:
27
+ return 2
28
+
29
+
30
+ def race(val):
31
+ if val == 'Hispanic':
32
+ return 2
33
+ elif val == 'Black':
34
+ return 1
35
+ elif val == 'White':
36
+ return 5
37
+ elif val == 'Other':
38
+ return 4
39
+ elif val == 'Asian':
40
+ return 0
41
+ else:
42
+ return 3
43
+
44
+
45
+ def search_outcome(val, end_range):
46
+ if not val:
47
+ return 2 # Which mean no search was conducted
48
+ else:
49
+ return random.randrange(0, end_range)
50
+
51
+
52
+ def search_reason(val, end_range):
53
+ if not val:
54
+ return 726 # Which mean no search was conducted
55
+ else:
56
+ return random.randrange(0, end_range)
57
+
58
+
59
+ # Make Prediction Model but would also like to add Gender and Race
60
+ def make_predication(year_stopped, offences, search, doc, car_year, alcohol, safety, genders, speeding,
61
+ races, accident, actual_accident, damage, road_signs, injury, belt, disobedience, bad_driving,
62
+ phone):
63
+ input_features = {
64
+ 'encoded_Search Outcome': search_outcome(search, 6),
65
+ 'encoded_Search Reason For Stop': search_reason(search, 727),
66
+ 'Number Of Offences': offences,
67
+ 'Search Conducted': bool_value(search),
68
+ 'Year Stopped': year_stopped,
69
+ 'encoded_SubAgency': random.randrange(0, 7),
70
+ 'encoded_Arrest Type': random.randrange(0, 18),
71
+ 'Invalid Documentation': bool_value(doc),
72
+ 'Year': car_year, # int
73
+ 'encoded_Driver City': random.randrange(0, 8114), # int
74
+ 'encoded_Make': random.randrange(0, 57), # int
75
+ 'Alcohol': bool_value(alcohol), # bool
76
+ 'encoded_Color': random.randrange(0, 25), # int
77
+ 'Vehicle Safety And Standards': bool_value(safety), # bool
78
+ 'Speeding': bool_value(speeding),
79
+ 'encoded_Race': race(races),
80
+ 'Contributed To Accident': bool_value(accident),
81
+ 'Accident': bool_value(actual_accident),
82
+ 'encoded_VehicleType': random.randrange(0, 31),
83
+ 'Property Damage': bool_value(damage),
84
+ 'Road Signs And Markings': bool_value(road_signs),
85
+ 'Personal Injury': bool_value(injury),
86
+ 'encoded_Gender': gender(genders),
87
+ 'Belts': bool_value(belt),
88
+ 'Disobedience': bool_value(disobedience),
89
+ 'encoded_DL State': random.randrange(0, 70),
90
+ 'encoded_State': random.randrange(0, 68),
91
+ 'Negligent Driving': bool_value(bad_driving),
92
+ 'encoded_Driver State': random.randrange(0, 67),
93
+ 'Mobile Phone': bool_value(phone)
94
+ }
95
+
96
+ x_input_feature = [[input_features[feature] for feature in sorted(input_features)]]
97
+ rfc_model = load_model(rf_model_url)
98
+ prd = rfc_model.predict(x_input_feature)
99
+
100
+ if prd == 0:
101
+ return 'Citation'
102
+ elif prd == 1:
103
+ return 'SERO'
104
+ else:
105
+ return 'Warning'
106
+
107
+
108
+ iface = gr.Interface(fn=make_predication,
109
+ inputs=[gr.components.Slider(minimum=2010, maximum=2023, step=1, label='Citation Year'),
110
+ gr.components.Slider(minimum=1, step=1, label='Number of offences found for stop'),
111
+ gr.components.Checkbox(label='Was a search conducted'),
112
+ gr.components.Checkbox(label='Any invalid documents'),
113
+ gr.components.Slider(minimum=1990, maximum=2023, step=1, label='Make Year'),
114
+ gr.components.Checkbox(label='Was alcohol involved'),
115
+ gr.components.Checkbox(label='Safety Standard issues in Vehicle'),
116
+ gr.components.Dropdown(label='Gender', choices=['Female',
117
+ 'Male',
118
+ 'Unknown']),
119
+ gr.components.Checkbox(label='Speeding'),
120
+ gr.components.Dropdown(label='Race', choices=['Asian',
121
+ 'Black',
122
+ 'Hispanic',
123
+ 'Native American',
124
+ 'Other',
125
+ 'White']),
126
+ gr.components.Checkbox(label='Irregularities which could contribute to accident'),
127
+ gr.components.Checkbox(label='Was the stop actual for an accident'),
128
+ gr.components.Checkbox(label='Any Property damage'),
129
+ gr.components.Checkbox(label='Road sign violation involvement'),
130
+ gr.components.Checkbox(label='Any injuries'),
131
+ gr.components.Checkbox(label='Seat Belts irregulation'),
132
+ gr.components.Checkbox(label='Any disobedience'),
133
+ gr.components.Checkbox(label='Bad driving'),
134
+ gr.components.Checkbox(label='Mobile phone')],
135
+ outputs=["text"])
136
+
137
+ iface.launch(debug=True)