ParthCodes commited on
Commit
b4d716b
1 Parent(s): 7515e94

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +69 -37
main.py CHANGED
@@ -1,58 +1,90 @@
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__':
 
1
  from flask import Flask, request
2
  import joblib
3
+ import numpy as np
4
  from flask_cors import CORS
5
 
 
6
  app = Flask(__name__)
 
 
7
 
8
+ app.secret_key = "cricktech-abhi-2023"
9
 
10
  CORS(app)
11
 
 
12
  # Load the model
13
+ model = joblib.load('dataset/world/cricket_world_cup_score_prediction_model.pkl')
14
 
 
 
15
 
16
  @app.route('/', methods=['GET'])
17
  def main():
18
+ return {'message': 'Welcome to CrickTech!!'}
19
 
20
 
21
  @app.route('/prediction', methods=['POST'])
22
  def prediction():
23
+ if request.method == 'POST':
24
+ data = request.get_json()
25
+ batting_team = data.get('batting_team')
26
+ bowling_team = data.get('bowling_team')
27
+ score = score_predict(batting_team, bowling_team, runs=data.get('total_runs'), wickets=data.get('total_wickets'), overs=data.get('overs'), runs_last_5_overs=data.get('runs_last_5_overs'), wickets_last_5_overs=data.get('wickets_last_5_overs'))
28
+ return score
29
+
30
+
31
+ def score_predict(batting_team, bowling_team, runs, wickets, overs, runs_last_5_overs, wickets_last_5_overs):
32
+ prediction_array = []
33
+ # Batting Team
34
+ if batting_team == 'Afghanistan':
35
+ prediction_array = prediction_array + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
36
+ elif batting_team == 'Australia':
37
+ prediction_array = prediction_array + [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
38
+ elif batting_team == 'Bangladesh':
39
+ prediction_array = prediction_array + [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
40
+ elif batting_team == 'England':
41
+ prediction_array = prediction_array + [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
42
+ elif batting_team == 'India':
43
+ prediction_array = prediction_array + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
44
+ elif batting_team == 'Ireland':
45
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
46
+ elif batting_team == 'New Zealand':
47
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
48
+ elif batting_team == 'Pakistan':
49
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
50
+ elif batting_team == 'South Africa':
51
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
52
+ elif batting_team == 'Sri Lanka':
53
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
54
+ elif batting_team == 'West Indies':
55
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
56
+ elif batting_team == 'Zimbabwe':
57
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
58
+ # Bowling Team
59
+ if bowling_team == 'Afghanistan':
60
+ prediction_array = prediction_array + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
61
+ elif bowling_team == 'Australia':
62
+ prediction_array = prediction_array + [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
63
+ elif bowling_team == 'Bangladesh':
64
+ prediction_array = prediction_array + [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
65
+ elif bowling_team == 'England':
66
+ prediction_array = prediction_array + [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
67
+ elif bowling_team == 'India':
68
+ prediction_array = prediction_array + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
69
+ elif bowling_team == 'Ireland':
70
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
71
+ elif bowling_team == 'New Zealand':
72
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
73
+ elif bowling_team == 'Pakistan':
74
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
75
+ elif bowling_team == 'South Africa':
76
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
77
+ elif bowling_team == 'Sri Lanka':
78
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
79
+ elif bowling_team == 'West Indies':
80
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
81
+ elif bowling_team == 'Zimbabwe':
82
+ prediction_array = prediction_array + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
83
+
84
+ prediction_array = prediction_array + [overs, runs, wickets, runs_last_5_overs, wickets_last_5_overs]
85
+ prediction_array = np.array([prediction_array])
86
+ pred = model.predict(prediction_array)
87
+ return int(round(pred[0]))
88
 
89
 
90
  if __name__ == '__main__':