Upload 2 files
Browse files- app.txt +129 -0
- requirements.txt +4 -0
    	
        app.txt
    ADDED
    
    | @@ -0,0 +1,129 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import joblib
         | 
| 3 | 
            +
            import pandas as pd
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # Load the model
         | 
| 6 | 
            +
            model = joblib.load('accident_prediction_model_Final.m5')
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            # Load the encoder
         | 
| 9 | 
            +
            encoder = joblib.load('encoder.pkl')
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            # Define classes for accident prediction
         | 
| 12 | 
            +
            classes = ["No", "Yes"]
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            # Create the inputs list with dropdown menus and sliders
         | 
| 15 | 
            +
            inputs = [
         | 
| 16 | 
            +
                gr.Dropdown(
         | 
| 17 | 
            +
                    choices=['Sunny/Clear', 'Rainy', 'Hail/Sleet', 'Foggy/Misty', 'Others'],
         | 
| 18 | 
            +
                    label="Weather Conditions"
         | 
| 19 | 
            +
                ),
         | 
| 20 | 
            +
                gr.Dropdown(
         | 
| 21 | 
            +
                    choices=['Pedestrian', 'Bicycles', 'Two Wheelers', 'Auto Rickshaws', 'Cars, Taxis, Vans & LMV', 'Trucks, Lorries', 'Buses', 'Non-motorized Vehicles', 'Others'],
         | 
| 22 | 
            +
                    label="Impact Type"
         | 
| 23 | 
            +
                ),
         | 
| 24 | 
            +
                gr.Dropdown(
         | 
| 25 | 
            +
                    choices=['Speeding', 'Jumping Red Light', 'Distracted Driving', 'Drunk Driving', 'Other'],
         | 
| 26 | 
            +
                    label="Traffic Violations"
         | 
| 27 | 
            +
                ),
         | 
| 28 | 
            +
                gr.Dropdown(
         | 
| 29 | 
            +
                    choices=['Straight Road', 'Curved Road', 'Bridge', 'Culvert', 'Pot Holes', 'Steep Grade', 'Ongoing Road Works/Under Construction', 'Others'],
         | 
| 30 | 
            +
                    label="Road Features"
         | 
| 31 | 
            +
                ),
         | 
| 32 | 
            +
                gr.Dropdown(
         | 
| 33 | 
            +
                    choices=['T-Junction', 'Y-Junction', 'Four arm Junction', 'Staggered Junction', 'Round about Junction', 'Others'],
         | 
| 34 | 
            +
                    label="Junction Types"
         | 
| 35 | 
            +
                ),
         | 
| 36 | 
            +
                gr.Dropdown(
         | 
| 37 | 
            +
                    choices=['Traffic Light Signal', 'Police Controlled', 'Stop Sign', 'Flashing Signal/Blinker', 'Uncontrolled', 'Others'],
         | 
| 38 | 
            +
                    label="Traffic Controls"
         | 
| 39 | 
            +
                ),
         | 
| 40 | 
            +
                gr.Dropdown(
         | 
| 41 | 
            +
                    choices=['morning', 'afternoon', 'evening', 'night'],
         | 
| 42 | 
            +
                    label="Time of Day"
         | 
| 43 | 
            +
                ),
         | 
| 44 | 
            +
                gr.Dropdown(
         | 
| 45 | 
            +
                    choices=['13-17', '18-25', '26-40', '41-60', '60-80', '80 above'],
         | 
| 46 | 
            +
                    label="Age Group"
         | 
| 47 | 
            +
                ),
         | 
| 48 | 
            +
                gr.Dropdown(
         | 
| 49 | 
            +
                    choices=['Killed', 'Grievously Injured', 'Minor Injury'],
         | 
| 50 | 
            +
                    label="Injury Type"
         | 
| 51 | 
            +
                ),
         | 
| 52 | 
            +
                gr.Dropdown(
         | 
| 53 | 
            +
                    choices=['Yes', 'No'],
         | 
| 54 | 
            +
                    label="Safety Features"
         | 
| 55 | 
            +
                ),
         | 
| 56 | 
            +
                gr.Slider(minimum=-90, maximum=90, label="Latitude"),
         | 
| 57 | 
            +
                gr.Slider(minimum=-180, maximum=180, label="Longitude"),
         | 
| 58 | 
            +
                gr.Slider(minimum=1, maximum=10, step= 1, label="Person Count"),
         | 
| 59 | 
            +
            ]
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            # Define output label
         | 
| 62 | 
            +
            output_label = gr.Label(num_top_classes=4)
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            # Create a function to make predictions
         | 
| 65 | 
            +
            def predict_accident(
         | 
| 66 | 
            +
                weather_conditions,
         | 
| 67 | 
            +
                impact_type,
         | 
| 68 | 
            +
                traffic_violations,
         | 
| 69 | 
            +
                road_features,
         | 
| 70 | 
            +
                junction_types,
         | 
| 71 | 
            +
                traffic_controls,
         | 
| 72 | 
            +
                time_day,
         | 
| 73 | 
            +
                age_group,
         | 
| 74 | 
            +
                injury,
         | 
| 75 | 
            +
                safety_features,
         | 
| 76 | 
            +
                Latitude,
         | 
| 77 | 
            +
                Longitude,
         | 
| 78 | 
            +
                person_count
         | 
| 79 | 
            +
            ):
         | 
| 80 | 
            +
                data = {
         | 
| 81 | 
            +
                    'selectedWeatherCondition': weather_conditions,
         | 
| 82 | 
            +
                    'selectedImpactType': impact_type,
         | 
| 83 | 
            +
                    'selectedTrafficViolationType': traffic_violations,
         | 
| 84 | 
            +
                    'selectedRoadFeaturesType': road_features,
         | 
| 85 | 
            +
                    'selectedRoadJunctionType': junction_types,
         | 
| 86 | 
            +
                    'selectedTrafficControl': traffic_controls,
         | 
| 87 | 
            +
                    'selectedTimeOfDay': time_day,
         | 
| 88 | 
            +
                    'selectedAge': age_group,
         | 
| 89 | 
            +
                    'selectedInjuryType': injury,
         | 
| 90 | 
            +
                    'selectedSafetyFeature': safety_features,
         | 
| 91 | 
            +
                    'Latitude': Latitude,
         | 
| 92 | 
            +
                    'Longitude': Longitude,
         | 
| 93 | 
            +
                    'personCount': person_count
         | 
| 94 | 
            +
                }
         | 
| 95 | 
            +
                
         | 
| 96 | 
            +
                num_input = {'Latitude': data['Latitude'], 'Longitude': data['Longitude'], 'person_count': data['personCount']}
         | 
| 97 | 
            +
                cat_input = {'weather_conditions': data['selectedWeatherCondition'], 'impact_type': data['selectedImpactType'],
         | 
| 98 | 
            +
                             'traffic_voilations': data['selectedTrafficViolationType'],
         | 
| 99 | 
            +
                             'road_features': data['selectedRoadFeaturesType'],
         | 
| 100 | 
            +
                             'junction_types': data['selectedRoadJunctionType'],
         | 
| 101 | 
            +
                             'traffic_controls': data['selectedTrafficControl'], 'time_day': data['selectedTimeOfDay'],
         | 
| 102 | 
            +
                             'age_group': data['selectedAge'], 'safety_features': data['selectedSafetyFeature'],
         | 
| 103 | 
            +
                             'injury': data['selectedInjuryType']}
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                input_df = pd.DataFrame([cat_input])
         | 
| 106 | 
            +
             | 
| 107 | 
            +
                encoded_input = encoder['encoder'].transform(input_df)
         | 
| 108 | 
            +
                encoded_input_df = pd.DataFrame(encoded_input, columns=encoder['encoded_columns'])
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                num_df = pd.DataFrame([num_input])
         | 
| 111 | 
            +
                input_with_coords = pd.concat([num_df, encoded_input_df], axis=1)
         | 
| 112 | 
            +
             | 
| 113 | 
            +
                # Make a prediction using the trained model
         | 
| 114 | 
            +
                prediction = model.predict(input_with_coords)
         | 
| 115 | 
            +
             | 
| 116 | 
            +
                label = f"Accident Prediction: {classes[int(prediction[0])]}"
         | 
| 117 | 
            +
                return label
         | 
| 118 | 
            +
             | 
| 119 | 
            +
            # Create the Gradio interface
         | 
| 120 | 
            +
            title = "Accident Prediction"
         | 
| 121 | 
            +
            description = "Predict the severity of an accident based on input features."
         | 
| 122 | 
            +
            output_label = [gr.Label(num_top_classes=4)]
         | 
| 123 | 
            +
            gr.Interface(
         | 
| 124 | 
            +
                fn=predict_accident,
         | 
| 125 | 
            +
                inputs=inputs,
         | 
| 126 | 
            +
                outputs=output_label,
         | 
| 127 | 
            +
                title=title,
         | 
| 128 | 
            +
                description=description,
         | 
| 129 | 
            +
            ).launch()
         | 
    	
        requirements.txt
    ADDED
    
    | @@ -0,0 +1,4 @@ | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            gradio
         | 
| 2 | 
            +
            pandas
         | 
| 3 | 
            +
            joblib
         | 
| 4 | 
            +
            scikit-learn
         | 
