File size: 4,521 Bytes
077c9f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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()