Spaces:
Sleeping
Sleeping
Commit
·
0da03da
1
Parent(s):
b33ffc9
Update main.py
Browse files
main.py
CHANGED
@@ -1,59 +1,16 @@
|
|
1 |
from flask import Flask, request
|
2 |
-
import joblib
|
3 |
-
import pandas as pd
|
4 |
from flask_cors import CORS
|
5 |
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
-
app.static_folder = 'static'
|
9 |
-
app.static_url_path = '/static'
|
10 |
-
|
11 |
-
app.secret_key = "roadsense-abhi-2023"
|
12 |
|
13 |
CORS(app)
|
14 |
|
15 |
-
|
16 |
-
# Load the model
|
17 |
-
model = joblib.load('accident_prediction_model_Final.m5')
|
18 |
|
19 |
-
# Load the encoder
|
20 |
-
encoder = joblib.load('encoder.pkl')
|
21 |
|
22 |
@app.route('/', methods=['GET'])
|
23 |
def main():
|
24 |
return {'message': 'Hello, World'}
|
25 |
|
26 |
-
|
27 |
-
@app.route('/prediction', methods=['POST'])
|
28 |
-
def prediction():
|
29 |
-
data = request.get_json()
|
30 |
-
|
31 |
-
num_input = {'Latitude': data['Latitude'], 'Longitude': data['Longitude'], 'person_count': data['personCount']}
|
32 |
-
cat_input = {'weather_conditions': data['selectedWeatherCondition'], 'impact_type': data['selectedImpactType'],
|
33 |
-
'traffic_voilations': data['selectedTrafficViolationType'],
|
34 |
-
'road_features': data['selectedRoadFeaturesType'],
|
35 |
-
'junction_types': data['selectedRoadJunctionType'],
|
36 |
-
'traffic_controls': data['selectedTrafficControl'], 'time_day': data['selectedTimeOfDay'],
|
37 |
-
'age_group': data['selectedAge'], 'safety_features': data['selectedSafetyFeature'],
|
38 |
-
'injury': data['selectedInjuryType']}
|
39 |
-
|
40 |
-
input_df = pd.DataFrame([cat_input])
|
41 |
-
|
42 |
-
encoded_input = encoder['encoder'].transform(input_df)
|
43 |
-
encoded_input_df = pd.DataFrame(encoded_input, columns=encoder['encoded_columns'])
|
44 |
-
|
45 |
-
num_df = pd.DataFrame([num_input])
|
46 |
-
input_with_coords = pd.concat([num_df, encoded_input_df], axis=1)
|
47 |
-
|
48 |
-
# Make a prediction using the trained model
|
49 |
-
prediction = model.predict(input_with_coords)
|
50 |
-
|
51 |
-
temp = False
|
52 |
-
if prediction[0] == 1:
|
53 |
-
temp = True
|
54 |
-
|
55 |
-
return {'prediction': temp}
|
56 |
-
|
57 |
-
|
58 |
if __name__ == '__main__':
|
59 |
app.run()
|
|
|
1 |
from flask import Flask, request
|
|
|
|
|
2 |
from flask_cors import CORS
|
3 |
|
4 |
|
5 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
6 |
|
7 |
CORS(app)
|
8 |
|
|
|
|
|
|
|
9 |
|
|
|
|
|
10 |
|
11 |
@app.route('/', methods=['GET'])
|
12 |
def main():
|
13 |
return {'message': 'Hello, World'}
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
if __name__ == '__main__':
|
16 |
app.run()
|