Spaces:
Sleeping
Sleeping
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') | |
def main(): | |
return {'message': 'Hello, World'} | |
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() |