File size: 1,805 Bytes
252fac8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request
import joblib
import pandas as pd
from flask_cors import CORS


app = Flask(__name__)
app.static_folder = 'static'
app.static_url_path = '/static'

app.secret_key = "roadsense-abhi-2023"

CORS(app)

    
# Load the model
model = joblib.load('accident_prediction_model_Final.m5')

# Load the encoder
encoder = joblib.load('encoder.pkl')

@app.route('/', methods=['GET'])
def main():
    return {'message': 'Hello, World'}


@app.route('/prediction', methods=['POST'])
def prediction():
    data = request.get_json()
    
    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)

    temp = False
    if prediction[0] == 1:
        temp = True
    
    return {'prediction': temp}


if __name__ == '__main__':
    app.run()