ZipeiZhang commited on
Commit
971243e
·
1 Parent(s): 904c0b4

add files

Browse files
Files changed (2) hide show
  1. app.py +230 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import requests
4
+ import hopsworks
5
+ import joblib
6
+ import pandas as pd
7
+
8
+ api = 'UtYWT9JBE4jbsOVW.dzfTExU7QMCzzR51EADTOZCXBzl0VmgB2y012yd8nFTG6v1VHgWazdx2a2SuJAY1'
9
+ project = hopsworks.login(api_key_value = api)
10
+
11
+
12
+ mr = project.get_model_registry()
13
+ model = mr.get_model("sf_traffic_model_1", version=1)
14
+ model_dir = model.download()
15
+ model = joblib.load(model_dir + "/sf_traffic_model_1.pkl")
16
+ print("Model downloaded")
17
+
18
+ # Mapping functions for each categorical feature
19
+ def map_time_cat(time_cat):
20
+ time_cat_mapping = {
21
+ '2:01 pm to 6:00 pm': 0, '10:01 am to 2:00 pm': 1, '6:01 pm to 10:00 pm': 2,
22
+ '6:01 am to 10:00 am': 3, '10:01 pm to 2:00 am': 4, '2:01 am to 6:00 am': 5
23
+ }
24
+ return time_cat_mapping.get(time_cat, -1) # Default to -1 for unknown categories
25
+
26
+ def map_party2_move_pre_acc(move):
27
+ party2_move_mapping = {
28
+ 'Proceeding Straight': 0, 'Stopped In Road': 1, 'Not Stated': 2, 'Making Left Turn': 3,
29
+ 'Parked': 4, 'Other': 5, 'Stopped': 6, 'Making Right Turn': 7, 'Slowing/Stopping': 8,
30
+ 'Entering Traffic': 9, 'Backing': 10, 'Changing Lanes': 11, 'Parking Maneuver': 12,
31
+ 'Passing Other Vehicle': 13, 'Making U Turn': 14, 'Merging': 15, 'Ran Off Road': 16,
32
+ 'Traveling Wrong Way': 17, 'Crossed Into Opposing Lane': 18,
33
+ 'Other Unsafe Turning': 19, 'Crossed Into Opposing Lane - Unplanned': 20
34
+ }
35
+ return party2_move_mapping.get(move, -1)
36
+
37
+ def map_party1_move_pre_acc(move):
38
+ party1_move_mapping = {
39
+ 'Proceeding Straight': 0, 'Making Left Turn': 1, 'Making Right Turn': 2,
40
+ 'Changing Lanes': 3, 'Other': 4, 'Entering Traffic': 5, 'Not Stated': 6, 'Backing': 7,
41
+ 'Stopped In Road': 8, 'Making U Turn': 9, 'Parked': 10, 'Slowing/Stopping': 11,
42
+ 'Passing Other Vehicle': 12, 'Traveling Wrong Way': 13, 'Stopped': 14, 'Ran Off Road': 15,
43
+ 'Other Unsafe Turning': 16, 'Parking Maneuver': 17, 'Crossed Into Opposing Lane': 18,
44
+ 'Merging': 19, 'Crossed Into Opposing Lane - Unplanned': 20
45
+ }
46
+ return party1_move_mapping.get(move, -1)
47
+
48
+ def map_ped_action(action):
49
+ ped_action_mapping = {
50
+ 'No Pedestrian Involved': 0, 'Crossing in Crosswalk at Intersection': 1,
51
+ 'Crossing Not in Crosswalk': 2, 'In Road, Including Shoulder': 3, 'Not in Road': 4,
52
+ 'Not Stated': 5, 'Crossing in Crosswalk Not at Intersection': 6,
53
+ 'Approaching/Leaving School Bus': 7
54
+ }
55
+ return ped_action_mapping.get(action, -1)
56
+
57
+ def map_party1_type(party_type):
58
+ party1_type_mapping = {
59
+ 'Driver': 0, 'Bicyclist': 1, 'Pedestrian': 2, 'Other': 3, 'Parked Vehicle': 4,
60
+ 'Not Stated': 5
61
+ }
62
+ return party1_type_mapping.get(party_type, -1)
63
+
64
+ def map_party2_type(party_type):
65
+ party2_type_mapping = {
66
+ 'Driver': 0, 'Pedestrian': 1, 'Bicyclist': 2, 'Parked Vehicle': 3, 'Other': 4,
67
+ 'Not Stated': 5
68
+ }
69
+ return party2_type_mapping.get(party_type, -1)
70
+
71
+ def map_mviw(mviw):
72
+ mviw_mapping = {
73
+ 'Other Motor Vehicle': 0, 'Pedestrian': 1, 'Bicycle': 2, 'Fixed Object': 3,
74
+ 'Parked Motor Vehicle': 4, 'Non-Collision': 5, 'Not Stated': 6, 'Other Object': 7,
75
+ 'Motor Vehicle on Other Roadway': 8, 'Train': 9, 'Animal': 10
76
+ }
77
+ return mviw_mapping.get(mviw, -1)
78
+
79
+
80
+ def map_intersection(intersection):
81
+ mapping = {
82
+ 'Intersection <= 20ft': 0,
83
+ 'Midblock > 20ft': 1,
84
+ 'Intersection Rear End <= 150ft': 2
85
+ }
86
+ return mapping.get(intersection, -1)
87
+
88
+ def map_road_cond_1(road_cond):
89
+ mapping = {
90
+ 'No Unusual Condition': 0,
91
+ 'Not Stated': 1,
92
+ 'Other': 2,
93
+ 'Construction or Repair Zone': 3,
94
+ 'Holes, Deep Ruts': 4,
95
+ 'Obstruction on Roadway': 5,
96
+ 'Loose Material on Roadway': 6,
97
+ 'Holes, Deep Rut': 7,
98
+ 'Reduced Roadway Width': 8,
99
+ 'Flooded': 9
100
+ }
101
+ return mapping.get(road_cond, -1)
102
+
103
+ def map_control_device(device):
104
+ mapping = {
105
+ 'Functioning': 0,
106
+ 'None': 1,
107
+ 'Not Stated': 2,
108
+ 'Not Functioning': 3,
109
+ 'Obscured': 4
110
+ }
111
+ return mapping.get(device, -1)
112
+
113
+ def map_lighting(lighting):
114
+ mapping = {
115
+ 'Daylight': 0,
116
+ 'Dark - Street Lights': 1,
117
+ 'Dusk - Dawn': 2,
118
+ 'Not Stated': 3,
119
+ 'Dark - No Street Lights': 4,
120
+ 'Dark - Street Lights Not Functioning': 5
121
+ }
122
+ return mapping.get(lighting, -1)
123
+
124
+ def map_road_surface(surface):
125
+ mapping = {
126
+ 'Dry': 0,
127
+ 'Wet': 1,
128
+ 'Not Stated': 2,
129
+ 'Slippery': 3,
130
+ 'Snowy or Icy': 4
131
+ }
132
+ return mapping.get(surface, -1)
133
+
134
+ def map_type_of_collision(collision):
135
+ mapping = {
136
+ 'Broadside': 0,
137
+ 'Vehicle/Pedestrian': 1,
138
+ 'Rear End': 2,
139
+ 'Sideswipe': 3,
140
+ 'Head-On': 4,
141
+ 'Other': 5,
142
+ 'Hit Object': 6,
143
+ 'Not Stated': 7,
144
+ 'Overturned': 8
145
+ }
146
+ return mapping.get(collision, -1)
147
+
148
+ def map_weather_condition(conditions):
149
+ # If 'conditions' is a list with one element, take the first element
150
+ if isinstance(conditions, list) and len(conditions) == 1:
151
+ condition = conditions[0]
152
+ else:
153
+ condition = conditions # Otherwise, assume it's a single value
154
+
155
+ weather_mapping = {
156
+ 'Clear': 0,
157
+ 'Cloudy': 1,
158
+ 'Raining': 2,
159
+ 'Fog': 3,
160
+ 'Other': 4,
161
+ 'Not Stated': 5,
162
+ 'Unknown': 6
163
+ }
164
+ return weather_mapping.get(condition, -1) # Default to -1 for unknown categories
165
+
166
+
167
+
168
+ # Add other mapping functions as necessary
169
+
170
+ def traffic_predict(weather, intersection, road_cond, control_device,
171
+ lighting, road_surface, collision_type, time_cat,
172
+ party2_move, party1_move, ped_action, party1_type,
173
+ party2_type, mviw):
174
+ # Apply mappings to each input
175
+ weather_mapped = map_weather_condition(weather)
176
+ intersection_mapped = map_intersection(intersection)
177
+ road_cond_mapped = map_road_cond_1(road_cond)
178
+ control_device_mapped = map_control_device(control_device)
179
+ lighting_mapped = map_lighting(lighting)
180
+ road_surface_mapped = map_road_surface(road_surface)
181
+ collision_type_mapped = map_type_of_collision(collision_type)
182
+ time_cat_mapped = map_time_cat(time_cat)
183
+ party2_move_mapped = map_party2_move_pre_acc(party2_move)
184
+ party1_move_mapped = map_party1_move_pre_acc(party1_move)
185
+ ped_action_mapped = map_ped_action(ped_action)
186
+ party1_type_mapped = map_party1_type(party1_type)
187
+ party2_type_mapped = map_party2_type(party2_type)
188
+ mviw_mapped = map_mviw(mviw)
189
+
190
+ # Prepare DataFrame for prediction
191
+ df = pd.DataFrame([[weather_mapped, intersection_mapped, road_cond_mapped, control_device_mapped,
192
+ lighting_mapped, road_surface_mapped, collision_type_mapped, time_cat_mapped,
193
+ party2_move_mapped, party1_move_mapped, ped_action_mapped, party1_type_mapped,
194
+ party2_type_mapped, mviw_mapped]],
195
+ columns=['weather_1_mapped', 'intersection_mapped', 'road_cond_1_mapped',
196
+ 'control_device_mapped', 'lighting_mapped', 'road_surface_mapped',
197
+ 'type_of_collision_mapped', 'time_cat_mapped', 'party2_move_pre_acc_mapped',
198
+ 'party1_move_pre_acc_mapped', 'ped_action_mapped', 'party1_type_mapped',
199
+ 'party2_type_mapped', 'mviw_mapped'])
200
+
201
+ # Predict with model
202
+ prediction = model.predict(df)
203
+ return prediction[0]
204
+
205
+ # Now, 'mapped_incident_df' contains only the mapped versions of your original categorical variables
206
+
207
+ demo = gr.Interface(
208
+ fn=traffic_predict,
209
+ inputs=[
210
+ gr.inputs.Dropdown(['Clear', 'Cloudy', 'Raining', 'Fog', 'Other', 'Not Stated', 'Unknown'], label="Weather Condition"),
211
+ gr.inputs.Dropdown(['Intersection <= 20ft', 'Midblock > 20ft', 'Intersection Rear End <= 150ft'], label="Intersection"),
212
+ gr.inputs.Dropdown(['No Unusual Condition', 'Not Stated', 'Other', 'Construction or Repair Zone', 'Holes, Deep Ruts', 'Obstruction on Roadway', 'Loose Material on Roadway', 'Holes, Deep Rut', 'Reduced Roadway Width', 'Flooded'], label="Road Condition 1"),
213
+ gr.inputs.Dropdown(['Functioning', 'None', 'Not Stated', 'Not Functioning', 'Obscured'], label="Control Device"),
214
+ gr.inputs.Dropdown(['Daylight', 'Dark - Street Lights', 'Dusk - Dawn', 'Not Stated', 'Dark - No Street Lights', 'Dark - Street Lights Not Functioning'], label="Lighting"),
215
+ gr.inputs.Dropdown(['Dry', 'Wet', 'Not Stated', 'Slippery', 'Snowy or Icy'], label="Road Surface"),
216
+ gr.inputs.Dropdown(['Broadside', 'Vehicle/Pedestrian', 'Rear End', 'Sideswipe', 'Head-On', 'Other', 'Hit Object', 'Not Stated', 'Overturned'], label="Type of Collision"),
217
+ gr.inputs.Dropdown(['2:01 pm to 6:00 pm', '10:01 am to 2:00 pm', '6:01 pm to 10:00 pm', '6:01 am to 10:00 am', '10:01 pm to 2:00 am', '2:01 am to 6:00 am'], label="Time Category"),
218
+ gr.inputs.Dropdown(['Proceeding Straight', 'Stopped In Road', 'Not Stated', 'Making Left Turn', 'Parked', 'Other', 'Stopped', 'Making Right Turn', 'Slowing/Stopping', 'Entering Traffic', 'Backing', 'Changing Lanes', 'Parking Maneuver', 'Passing Other Vehicle', 'Making U Turn', 'Merging', 'Ran Off Road', 'Traveling Wrong Way', 'Crossed Into Opposing Lane', 'Other Unsafe Turning', 'Crossed Into Opposing Lane - Unplanned'], label="Party 2 Movement Pre-Accident"),
219
+ gr.inputs.Dropdown(['Proceeding Straight', 'Making Left Turn', 'Making Right Turn', 'Changing Lanes', 'Other', 'Entering Traffic', 'Not Stated', 'Backing', 'Stopped In Road', 'Making U Turn', 'Parked', 'Slowing/Stopping', 'Passing Other Vehicle', 'Traveling Wrong Way', 'Stopped', 'Ran Off Road', 'Other Unsafe Turning', 'Parking Maneuver', 'Crossed Into Opposing Lane', 'Merging', 'Crossed Into Opposing Lane - Unplanned'], label="Party 1 Movement Pre-Accident"),
220
+ gr.inputs.Dropdown(['No Pedestrian Involved', 'Crossing in Crosswalk at Intersection', 'Crossing Not in Crosswalk', 'In Road, Including Shoulder', 'Not in Road', 'Not Stated', 'Crossing in Crosswalk Not at Intersection', 'Approaching/Leaving School Bus'], label="Pedestrian Action"),
221
+ gr.inputs.Dropdown(['Driver', 'Bicyclist', 'Pedestrian', 'Other', 'Parked Vehicle', 'Not Stated'], label="Party 1 Type"),
222
+ gr.inputs.Dropdown(['Driver', 'Pedestrian', 'Bicyclist', 'Parked Vehicle', 'Other', 'Not Stated'], label="Party 2 Type"),
223
+ gr.inputs.Dropdown(['Other Motor Vehicle', 'Pedestrian', 'Bicycle', 'Fixed Object', 'Parked Motor Vehicle', 'Non-Collision', 'Not Stated', 'Other Object', 'Motor Vehicle on Other Roadway', 'Train', 'Animal'], label="MVIW")
224
+ ],
225
+ outputs=gr.outputs.Textbox(label="Predicted Severity")
226
+ )
227
+
228
+ demo.launch()
229
+
230
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ hopsworks
2
+ joblib
3
+ scikit-learn==1.1.1
4
+ transformers
5
+ gradio