ThorkildFregi commited on
Commit
02b70cf
1 Parent(s): 7a08f6a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +109 -109
main.py CHANGED
@@ -1,109 +1,109 @@
1
- from flask import Flask, render_template, redirect, url_for, request
2
- from vct_data import create_data
3
- from model import create_model
4
- import pandas as pd
5
- import pickle
6
- import os
7
-
8
- app = Flask(__name__)
9
-
10
- @app.route('/')
11
- def home():
12
- if "data.csv" not in os.listdir():
13
- return redirect(url_for("loading_update_data"))
14
- elif "model.sav" not in os.listdir():
15
- return redirect(url_for("loading_train_model"))
16
- else:
17
- df = pd.read_csv("data.csv")
18
-
19
- tournaments_df = df["Tournament"]
20
- tournaments = list(pd.unique(tournaments_df))
21
-
22
- maps_df = df["Map"]
23
- maps = list(pd.unique(maps_df))
24
-
25
- team_A_df = df["Team A"]
26
- team_A = list(pd.unique(team_A_df))
27
-
28
- team_B_df = df["Team B"]
29
- team_B = list(pd.unique(team_B_df))
30
-
31
- agents = ["Astra", "Breach", "Brimstone", "Chamber", "Clove", "Cypher", "Deadlock", "Fade", "Gekko", "Harbor", "Iso", "Jett", "Kayo", "Killjoy", "Neon", "Omen", "Phoenix", "Raze", "Reyna", "Sage", "Skye", "Sova", "Viper", "Yoru"]
32
-
33
- return render_template("home.html", tournaments=tournaments, maps=maps, team_A=team_A, team_B=team_B, agents=agents)
34
-
35
- @app.route('/update-data')
36
- def update_data():
37
- create_data()
38
- return redirect(url_for("loading_train_model"))
39
-
40
- @app.route('/loading-update-data')
41
- def loading_update_data():
42
- return render_template("loading.html", next_page="update_data", loading_mission="Updating data...")
43
-
44
- @app.route('/train-model')
45
- def train_model():
46
- create_model()
47
- return redirect(url_for("home"))
48
-
49
- @app.route('/loading-train-model')
50
- def loading_train_model():
51
- return render_template("loading.html", next_page="train_model", loading_mission="Training model...")
52
-
53
- @app.route('/prediction', methods=["post", "get"])
54
- def prediction():
55
- if request.method == "POST":
56
- tournament = request.form["tournament"]
57
- map = request.form["map"]
58
- team_a = request.form["team_a"]
59
- team_b = request.form["team_b"]
60
- ta_agents_list = sorted(request.form["ta_agents"].split())
61
- tb_agents_list = sorted(request.form["tb_agents"].split())
62
-
63
- i = 0
64
- ta_agents = "["
65
- for agents in ta_agents_list:
66
- if i == 4:
67
- ta_agents += agents + "]"
68
- else:
69
- ta_agents += agents + ", "
70
-
71
- i = 0
72
- tb_agents = "["
73
- for agents in tb_agents_list:
74
- if i == 4:
75
- tb_agents += agents + "]"
76
- else:
77
- tb_agents += agents + ", "
78
-
79
- train_data = pd.read_csv("data.csv")
80
- model = pickle.load(open("model.sav", 'rb'))
81
-
82
- features = ["Tournament", "Map", "Team A", "Team B", "TA Agents", "TB Agents"]
83
-
84
- x = pd.get_dummies(train_data[features])
85
-
86
- data_test = {"Tournament": [tournament], "Map": [map], "Team A": [team_a], "Team B": [team_b], "TA Agents": [ta_agents], "TB Agents": [tb_agents]}
87
- test_data = pd.DataFrame(data=data_test)
88
- x_test = pd.get_dummies(test_data[features])
89
-
90
- col_x_test = []
91
- for column in x_test:
92
- col_x_test.append(column)
93
-
94
- for column in x:
95
- if column in col_x_test:
96
- pass
97
- else:
98
- x_test.insert(0, column, [False])
99
-
100
- x_test = x_test.reindex(x.columns, axis=1)
101
-
102
- prediction = model.predict(x_test)[0]
103
-
104
- return render_template("result.html", prediction=prediction)
105
- else:
106
- return redirect(url_for("home"))
107
-
108
- if __name__ == "__main__":
109
- app.run(debug=True)
 
1
+ from flask import Flask, render_template, redirect, url_for, request
2
+ from vct_data import create_data
3
+ from model import create_model
4
+ import pandas as pd
5
+ import pickle
6
+ import os
7
+
8
+ app = Flask(__name__)
9
+
10
+ @app.route('/')
11
+ def home():
12
+ if "data.csv" not in os.listdir():
13
+ return redirect(url_for("loading_update_data"))
14
+ elif "model.sav" not in os.listdir():
15
+ return redirect(url_for("loading_train_model"))
16
+ else:
17
+ df = pd.read_csv("data.csv")
18
+
19
+ tournaments_df = df["Tournament"]
20
+ tournaments = list(pd.unique(tournaments_df))
21
+
22
+ maps_df = df["Map"]
23
+ maps = list(pd.unique(maps_df))
24
+
25
+ team_A_df = df["Team A"]
26
+ team_A = list(pd.unique(team_A_df))
27
+
28
+ team_B_df = df["Team B"]
29
+ team_B = list(pd.unique(team_B_df))
30
+
31
+ agents = ["Astra", "Breach", "Brimstone", "Chamber", "Clove", "Cypher", "Deadlock", "Fade", "Gekko", "Harbor", "Iso", "Jett", "Kayo", "Killjoy", "Neon", "Omen", "Phoenix", "Raze", "Reyna", "Sage", "Skye", "Sova", "Viper", "Yoru"]
32
+
33
+ return render_template("home.html", tournaments=tournaments, maps=maps, team_A=team_A, team_B=team_B, agents=agents)
34
+
35
+ @app.route('/update-data')
36
+ def update_data():
37
+ create_data()
38
+ return redirect(url_for("loading_train_model"))
39
+
40
+ @app.route('/loading-update-data')
41
+ def loading_update_data():
42
+ return render_template("loading.html", next_page="update_data", loading_mission="Updating data...")
43
+
44
+ @app.route('/train-model')
45
+ def train_model():
46
+ create_model()
47
+ return redirect(url_for("home"))
48
+
49
+ @app.route('/loading-train-model')
50
+ def loading_train_model():
51
+ return render_template("loading.html", next_page="train_model", loading_mission="Training model...")
52
+
53
+ @app.route('/prediction', methods=["post", "get"])
54
+ def prediction():
55
+ if request.method == "POST":
56
+ tournament = request.form["tournament"]
57
+ map = request.form["map"]
58
+ team_a = request.form["team_a"]
59
+ team_b = request.form["team_b"]
60
+ ta_agents_list = sorted(request.form["ta_agents"].split())
61
+ tb_agents_list = sorted(request.form["tb_agents"].split())
62
+
63
+ i = 0
64
+ ta_agents = "["
65
+ for agents in ta_agents_list:
66
+ if i == 4:
67
+ ta_agents += agents + "]"
68
+ else:
69
+ ta_agents += agents + ", "
70
+
71
+ i = 0
72
+ tb_agents = "["
73
+ for agents in tb_agents_list:
74
+ if i == 4:
75
+ tb_agents += agents + "]"
76
+ else:
77
+ tb_agents += agents + ", "
78
+
79
+ train_data = pd.read_csv("data.csv")
80
+ model = pickle.load(open("model.sav", 'rb'))
81
+
82
+ features = ["Tournament", "Map", "Team A", "Team B", "TA Agents", "TB Agents"]
83
+
84
+ x = pd.get_dummies(train_data[features])
85
+
86
+ data_test = {"Tournament": [tournament], "Map": [map], "Team A": [team_a], "Team B": [team_b], "TA Agents": [ta_agents], "TB Agents": [tb_agents]}
87
+ test_data = pd.DataFrame(data=data_test)
88
+ x_test = pd.get_dummies(test_data[features])
89
+
90
+ col_x_test = []
91
+ for column in x_test:
92
+ col_x_test.append(column)
93
+
94
+ for column in x:
95
+ if column in col_x_test:
96
+ pass
97
+ else:
98
+ x_test.insert(0, column, [False])
99
+
100
+ x_test = x_test.reindex(x.columns, axis=1)
101
+
102
+ prediction = model.predict(x_test)[0]
103
+
104
+ return render_template("result.html", prediction=prediction)
105
+ else:
106
+ return redirect(url_for("home"))
107
+
108
+ if __name__ == "__main__":
109
+ app.run(host="0.0.0.0", port=7860, debug=True)