ParthCodes commited on
Commit
252fac8
1 Parent(s): 0a0964a

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +17 -0
  2. main.py +59 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Menggunakan base image Python 3.9
2
+ FROM python:3.9
3
+
4
+ # Mengatur direktori kerja ke /code
5
+ WORKDIR /code
6
+
7
+ # Menyalin requirements.txt ke /code
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ # Menginstal dependensi dari requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
12
+
13
+ # Menyalin seluruh konten proyek Anda ke /code
14
+ COPY . /code
15
+
16
+ # CMD untuk menjalankan Gunicorn
17
+ CMD ["gunicorn", "main:app", "-b", "0.0.0.0:7860"]
main.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ flask
2
+ flask-cors
3
+ gunicorn
4
+ Jinja2
5
+ pandas
6
+ scikit-learn