| 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) |
|
|
| |
| |
| model = joblib.load('accident_prediction_model_Final.m5') |
|
|
| |
| 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) |
|
|
| |
| prediction = model.predict(input_with_coords) |
|
|
| temp = False |
| if prediction[0] == 1: |
| temp = True |
| |
| return {'prediction': temp} |
|
|
|
|
| if __name__ == '__main__': |
| app.run() |