Spaces:
Runtime error
Runtime error
import gradio as gr | |
import joblib | |
import pandas as pd | |
# Load the model | |
model = joblib.load('accident_prediction_model_Final.m5') | |
# Load the encoder | |
encoder = joblib.load('encoder.pkl') | |
# Define classes for accident prediction | |
classes = ["No", "Yes"] | |
# Create the inputs list with dropdown menus and sliders | |
inputs = [ | |
gr.Dropdown( | |
choices=['Sunny/Clear', 'Rainy', 'Hail/Sleet', 'Foggy/Misty', 'Others'], | |
label="Weather Conditions" | |
), | |
gr.Dropdown( | |
choices=['Pedestrian', 'Bicycles', 'Two Wheelers', 'Auto Rickshaws', 'Cars, Taxis, Vans & LMV', 'Trucks, Lorries', 'Buses', 'Non-motorized Vehicles', 'Others'], | |
label="Impact Type" | |
), | |
gr.Dropdown( | |
choices=['Speeding', 'Jumping Red Light', 'Distracted Driving', 'Drunk Driving', 'Other'], | |
label="Traffic Violations" | |
), | |
gr.Dropdown( | |
choices=['Straight Road', 'Curved Road', 'Bridge', 'Culvert', 'Pot Holes', 'Steep Grade', 'Ongoing Road Works/Under Construction', 'Others'], | |
label="Road Features" | |
), | |
gr.Dropdown( | |
choices=['T-Junction', 'Y-Junction', 'Four arm Junction', 'Staggered Junction', 'Round about Junction', 'Others'], | |
label="Junction Types" | |
), | |
gr.Dropdown( | |
choices=['Traffic Light Signal', 'Police Controlled', 'Stop Sign', 'Flashing Signal/Blinker', 'Uncontrolled', 'Others'], | |
label="Traffic Controls" | |
), | |
gr.Dropdown( | |
choices=['morning', 'afternoon', 'evening', 'night'], | |
label="Time of Day" | |
), | |
gr.Dropdown( | |
choices=['13-17', '18-25', '26-40', '41-60', '60-80', '80 above'], | |
label="Age Group" | |
), | |
gr.Dropdown( | |
choices=['Killed', 'Grievously Injured', 'Minor Injury'], | |
label="Injury Type" | |
), | |
gr.Dropdown( | |
choices=['Yes', 'No'], | |
label="Safety Features" | |
), | |
gr.Slider(minimum=-90, maximum=90, label="Latitude"), | |
gr.Slider(minimum=-180, maximum=180, label="Longitude"), | |
gr.Slider(minimum=1, maximum=10, step= 1, label="Person Count"), | |
] | |
# Define output label | |
output_label = gr.Label(num_top_classes=4) | |
# Create a function to make predictions | |
def predict_accident( | |
weather_conditions, | |
impact_type, | |
traffic_violations, | |
road_features, | |
junction_types, | |
traffic_controls, | |
time_day, | |
age_group, | |
injury, | |
safety_features, | |
Latitude, | |
Longitude, | |
person_count | |
): | |
data = { | |
'selectedWeatherCondition': weather_conditions, | |
'selectedImpactType': impact_type, | |
'selectedTrafficViolationType': traffic_violations, | |
'selectedRoadFeaturesType': road_features, | |
'selectedRoadJunctionType': junction_types, | |
'selectedTrafficControl': traffic_controls, | |
'selectedTimeOfDay': time_day, | |
'selectedAge': age_group, | |
'selectedInjuryType': injury, | |
'selectedSafetyFeature': safety_features, | |
'Latitude': Latitude, | |
'Longitude': Longitude, | |
'personCount': person_count | |
} | |
num_input = {'Latitude': data['Latitude'], 'Longitude': data['Longitude'], 'person_count': data['personCount']} | |
cat_input = {'weather_conditions': data['selectedWeatherCondition'], 'impact_type': data['selectedImpactType'], | |
'traffic_voilations': data['selectedTrafficViolationType'], | |
'road_features': data['selectedRoadFeaturesType'], | |
'junction_types': data['selectedRoadJunctionType'], | |
'traffic_controls': data['selectedTrafficControl'], 'time_day': data['selectedTimeOfDay'], | |
'age_group': data['selectedAge'], 'safety_features': data['selectedSafetyFeature'], | |
'injury': data['selectedInjuryType']} | |
input_df = pd.DataFrame([cat_input]) | |
encoded_input = encoder['encoder'].transform(input_df) | |
encoded_input_df = pd.DataFrame(encoded_input, columns=encoder['encoded_columns']) | |
num_df = pd.DataFrame([num_input]) | |
input_with_coords = pd.concat([num_df, encoded_input_df], axis=1) | |
# Make a prediction using the trained model | |
prediction = model.predict(input_with_coords) | |
label = f"Accident Prediction: {classes[int(prediction[0])]}" | |
return label | |
# Create the Gradio interface | |
title = "Accident Prediction" | |
description = "Predict the severity of an accident based on input features." | |
output_label = [gr.Label(num_top_classes=4)] | |
gr.Interface( | |
fn=predict_accident, | |
inputs=inputs, | |
outputs=output_label, | |
title=title, | |
description=description, | |
).launch() |