|
import random |
|
import gradio as gr |
|
from joblib import load |
|
|
|
|
|
rf_model_url = 'random_forest_model.joblib' |
|
|
|
|
|
|
|
def load_model(url): |
|
return load(rf_model_url) |
|
|
|
|
|
def bool_value(val): |
|
if not val: |
|
return 0 |
|
else: |
|
return 1 |
|
|
|
|
|
def gender(val): |
|
if val == 'Male': |
|
return 1 |
|
elif val == 'Female': |
|
return 0 |
|
else: |
|
return 2 |
|
|
|
|
|
def race(val): |
|
if val == 'Hispanic': |
|
return 2 |
|
elif val == 'Black': |
|
return 1 |
|
elif val == 'White': |
|
return 5 |
|
elif val == 'Other': |
|
return 4 |
|
elif val == 'Asian': |
|
return 0 |
|
else: |
|
return 3 |
|
|
|
|
|
def search_outcome(val, end_range): |
|
if not val: |
|
return 2 |
|
else: |
|
return random.randrange(0, end_range) |
|
|
|
|
|
def search_reason(val, end_range): |
|
if not val: |
|
return 726 |
|
else: |
|
return random.randrange(0, end_range) |
|
|
|
|
|
|
|
def make_predication(year_stopped, offences, search, doc, car_year, alcohol, safety, genders, speeding, |
|
races, accident, actual_accident, damage, road_signs, injury, belt, disobedience, bad_driving, |
|
phone): |
|
input_features = { |
|
'encoded_Search Outcome': search_outcome(search, 6), |
|
'encoded_Search Reason For Stop': search_reason(search, 727), |
|
'Number Of Offences': offences, |
|
'Search Conducted': bool_value(search), |
|
'Year Stopped': year_stopped, |
|
'encoded_SubAgency': random.randrange(0, 7), |
|
'encoded_Arrest Type': random.randrange(0, 18), |
|
'Invalid Documentation': bool_value(doc), |
|
'Year': car_year, |
|
'encoded_Driver City': random.randrange(0, 8114), |
|
'encoded_Make': random.randrange(0, 57), |
|
'Alcohol': bool_value(alcohol), |
|
'encoded_Color': random.randrange(0, 25), |
|
'Vehicle Safety And Standards': bool_value(safety), |
|
'Speeding': bool_value(speeding), |
|
'encoded_Race': race(races), |
|
'Contributed To Accident': bool_value(accident), |
|
'Accident': bool_value(actual_accident), |
|
'encoded_VehicleType': random.randrange(0, 31), |
|
'Property Damage': bool_value(damage), |
|
'Road Signs And Markings': bool_value(road_signs), |
|
'Personal Injury': bool_value(injury), |
|
'encoded_Gender': gender(genders), |
|
'Belts': bool_value(belt), |
|
'Disobedience': bool_value(disobedience), |
|
'encoded_DL State': random.randrange(0, 70), |
|
'encoded_State': random.randrange(0, 68), |
|
'Negligent Driving': bool_value(bad_driving), |
|
'encoded_Driver State': random.randrange(0, 67), |
|
'Mobile Phone': bool_value(phone) |
|
} |
|
|
|
x_input_feature = [[input_features[feature] for feature in sorted(input_features)]] |
|
rfc_model = load_model(rf_model_url) |
|
prd = rfc_model.predict(x_input_feature) |
|
|
|
if prd == 0: |
|
return 'Citation' |
|
elif prd == 1: |
|
return 'SERO' |
|
else: |
|
return 'Warning' |
|
|
|
|
|
iface = gr.Interface(fn=make_predication, |
|
inputs=[gr.components.Slider(minimum=2010, maximum=2023, step=1, label='Citation Year'), |
|
gr.components.Slider(minimum=1, step=1, label='Number of offences found for stop'), |
|
gr.components.Checkbox(label='Was a search conducted'), |
|
gr.components.Checkbox(label='Any invalid documents'), |
|
gr.components.Slider(minimum=1990, maximum=2023, step=1, label='Make Year'), |
|
gr.components.Checkbox(label='Was alcohol involved'), |
|
gr.components.Checkbox(label='Safety Standard issues in Vehicle'), |
|
gr.components.Dropdown(label='Gender', choices=['Female', |
|
'Male', |
|
'Unknown']), |
|
gr.components.Checkbox(label='Speeding'), |
|
gr.components.Dropdown(label='Race', choices=['Asian', |
|
'Black', |
|
'Hispanic', |
|
'Native American', |
|
'Other', |
|
'White']), |
|
gr.components.Checkbox(label='Irregularities which could contribute to accident'), |
|
gr.components.Checkbox(label='Was the stop actual for an accident'), |
|
gr.components.Checkbox(label='Any Property damage'), |
|
gr.components.Checkbox(label='Road sign violation involvement'), |
|
gr.components.Checkbox(label='Any injuries'), |
|
gr.components.Checkbox(label='Seat Belts irregulation'), |
|
gr.components.Checkbox(label='Any disobedience'), |
|
gr.components.Checkbox(label='Bad driving'), |
|
gr.components.Checkbox(label='Mobile phone')], |
|
outputs=["text"]) |
|
|
|
iface.launch(debug=True) |
|
|