Jimin Park commited on
Commit
f9acbdd
·
1 Parent(s): dcaaa10

added new structure

Browse files
Files changed (2) hide show
  1. util/app.py +1 -40
  2. util/app_copy.py +248 -0
util/app.py CHANGED
@@ -155,30 +155,7 @@ def predict_top_5_champion_w_confidence(player_opgg_url, *champions):
155
  label_column = training_df['champion']
156
  predict_column = training_df.drop(columns=['champion', 'region'])
157
 
158
- # Get feature columns
159
- # feature_columns = [col for col in training_df.columns
160
- # if col not in ['champion', 'region', 'stratify_label']]
161
- # X = training_df[feature_columns]
162
-
163
- # Handle categorical features
164
- # categorical_columns = X.select_dtypes(include=['category']).columns
165
- # X_processed = X.copy()
166
-
167
- # for col in categorical_columns:
168
- # X_processed[col] = X_processed[col].cat.codes
169
-
170
- # X_processed = X_processed.astype('float32')
171
-
172
- # Create DMatrix and predict
173
- # dtest = DMatrix(X_processed, enable_categorical=False)
174
- # predictions = model.predict(dtest)
175
  proba = model.predict_proba(predict_column)
176
-
177
- # Get prediction indices
178
- # if len(predictions.shape) > 1:
179
- # pred_indices = predictions.argmax(axis=1)
180
- # else:
181
- # pred_indices = predictions.astype(int)
182
 
183
  # Get top 5 indices and probabilities
184
  top_5_idx = np.argsort(proba, axis=1)[:, -5:][:, ::-1]
@@ -203,24 +180,7 @@ def predict_top_5_champion_w_confidence(player_opgg_url, *champions):
203
  results[f'Rank_{i+1}_Champion'] = champions
204
  results[f'Rank_{i+1}_Confidence'] = probabilities.round(4)
205
 
206
- # First get the numeric ID from the original label encoder
207
- # decoded_numeric = label_encoder.inverse_transform(pred_indices)
208
-
209
- # Map numeric ID to index in CHAMPIONS list
210
- # Since your label encoder seems to use champion IDs, we need to map these to list indices
211
  try:
212
- # Get the first 3 prediction
213
- # champion_id = int(decoded_numeric[0])
214
-
215
- # Print debug information
216
- # print(f"Champion ID from model: {champion_id}")
217
-
218
- # Find the closest matching index
219
- # Note: This assumes champion IDs roughly correspond to their position in the list
220
- # champion_index = min(max(champion_id - 1, 0), len(CHAMPIONS) - 1)
221
- # predicted_champion = CHAMPIONS[champion_index]
222
-
223
- # print(f"Mapped to champion: {predicted_champion}")
224
 
225
 
226
  def find_champion_rank(row):
@@ -260,6 +220,7 @@ with gr.Blocks() as demo:
260
  gr.Dropdown(choices=CHAMPIONS, label=f"Champion {i+1}")
261
  for i in range(9)
262
  ]
 
263
 
264
  with gr.Row():
265
  predict_button = gr.Button("Predict")
 
155
  label_column = training_df['champion']
156
  predict_column = training_df.drop(columns=['champion', 'region'])
157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  proba = model.predict_proba(predict_column)
 
 
 
 
 
 
159
 
160
  # Get top 5 indices and probabilities
161
  top_5_idx = np.argsort(proba, axis=1)[:, -5:][:, ::-1]
 
180
  results[f'Rank_{i+1}_Champion'] = champions
181
  results[f'Rank_{i+1}_Confidence'] = probabilities.round(4)
182
 
 
 
 
 
 
183
  try:
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
 
186
  def find_champion_rank(row):
 
220
  gr.Dropdown(choices=CHAMPIONS, label=f"Champion {i+1}")
221
  for i in range(9)
222
  ]
223
+ print(champion_dropdowns)
224
 
225
  with gr.Row():
226
  predict_button = gr.Button("Predict")
util/app_copy.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ import xgboost as xgb
4
+ from xgboost import DMatrix
5
+ from huggingface_hub import hf_hub_download
6
+ from app_training_df_getter import create_app_user_training_df
7
+ import pandas as pd
8
+ from sklearn.model_selection import train_test_split
9
+ from sklearn.preprocessing import LabelEncoder
10
+ from helper import *
11
+ from helper import ChampionConverter
12
+ import joblib
13
+
14
+
15
+ # Define champion list for dropdowns
16
+ CHAMPIONS = [
17
+ "Aatrox", "Ahri", "Akali", "Akshan", "Alistar", "Amumu", "Anivia", "Annie", "Aphelios", "Ashe",
18
+ "Aurelion Sol", "Azir", "Bard", "Bel'Veth", "Blitzcrank", "Brand", "Braum", "Caitlyn", "Camille",
19
+ "Cassiopeia", "Cho'Gath", "Corki", "Darius", "Diana", "Dr. Mundo", "Draven", "Ekko", "Elise",
20
+ "Evelynn", "Ezreal", "Fiddlesticks", "Fiora", "Fizz", "Galio", "Gangplank", "Garen", "Gnar",
21
+ "Gragas", "Graves", "Gwen", "Hecarim", "Heimerdinger", "Illaoi", "Irelia", "Ivern", "Janna",
22
+ "Jarvan IV", "Jax", "Jayce", "Jhin", "Jinx", "Kai'Sa", "Kalista", "Karma", "Karthus", "Kassadin",
23
+ "Katarina", "Kayle", "Kayn", "Kennen", "Kha'Zix", "Kindred", "Kled", "Kog'Maw", "KSante", "LeBlanc",
24
+ "Lee Sin", "Leona", "Lillia", "Lissandra", "Lucian", "Lulu", "Lux", "Malphite", "Malzahar", "Maokai",
25
+ "Master Yi", "Milio", "Miss Fortune", "Mordekaiser", "Morgana", "Naafiri", "Nami", "Nasus", "Nautilus",
26
+ "Neeko", "Nidalee", "Nilah", "Nocturne", "Nunu & Willump", "Olaf", "Orianna", "Ornn", "Pantheon",
27
+ "Poppy", "Pyke", "Qiyana", "Quinn", "Rakan", "Rammus", "Rek'Sai", "Rell", "Renata Glasc", "Renekton",
28
+ "Rengar", "Riven", "Rumble", "Ryze", "Samira", "Sejuani", "Senna", "Seraphine", "Sett", "Shaco",
29
+ "Shen", "Shyvana", "Singed", "Sion", "Sivir", "Skarner", "Sona", "Soraka", "Swain", "Sylas",
30
+ "Syndra", "Tahm Kench", "Taliyah", "Talon", "Taric", "Teemo", "Thresh", "Tristana", "Trundle",
31
+ "Tryndamere", "Twisted Fate", "Twitch", "Udyr", "Urgot", "Varus", "Vayne", "Veigar", "Vel'Koz",
32
+ "Vex", "Vi", "Viego", "Viktor", "Vladimir", "Volibear", "Warwick", "Wukong", "Xayah", "Xerath",
33
+ "Xin Zhao", "Yasuo", "Yone", "Yorick", "Yuumi", "Zac", "Zed", "Zeri", "Ziggs", "Zilean", "Zoe", "Zyra"
34
+ ]
35
+
36
+ try:
37
+ label_encoder = joblib.load('util/label_encoder.joblib')
38
+ n_classes = len(label_encoder.classes_)
39
+ print("Label encoder loaded successfully")
40
+ except Exception as e:
41
+ print(f"Error loading label encoder: {e}")
42
+ label_encoder = None
43
+
44
+ # Load model
45
+ try:
46
+ model_path = hf_hub_download(
47
+ repo_id="ivwhy/champion-predictor-model",
48
+ filename="champion_predictor.json"
49
+ )
50
+
51
+ # Initialize the model with proper parameters
52
+ model = xgb.XGBClassifier(
53
+ use_label_encoder=False,
54
+ objective='multi:softprob',
55
+ num_class=n_classes
56
+ )
57
+
58
+ # Load the model
59
+ model._Booster = xgb.Booster()
60
+ model._Booster.load_model(model_path)
61
+
62
+ # Set only n_classes_
63
+ model.n_classes_ = n_classes
64
+ except Exception as e:
65
+ print(f"Error loading model: {e}")
66
+ model = None
67
+
68
+ # Initialize champion name encoder
69
+ # champion_encoder = LabelEncoder()
70
+ # champion_encoder.fit(CHAMPIONS)
71
+
72
+
73
+ #==================================== Functions =================================================
74
+ def get_user_training_df(player_opgg_url):
75
+ try:
76
+ print("========= Inside get_user_training_df(player_opgg_url) ============= \n")
77
+ #print("player_opgg_url: ", player_opgg_url, "\n type(player_opgg_url): ", type(player_opgg_url), "\n")
78
+
79
+ # Add input validation
80
+ if not player_opgg_url or not isinstance(player_opgg_url, str):
81
+ return "Invalid URL provided"
82
+
83
+ training_df = create_app_user_training_df(player_opgg_url)
84
+ return training_df
85
+ except Exception as e:
86
+
87
+ # Add more detailed error information
88
+ import traceback
89
+ error_trace = traceback.format_exc()
90
+ print(f"Full error trace:\n{error_trace}")
91
+ return f"Error getting training data: {str(e)}"
92
+
93
+ #return f"Error getting training data: {e}"
94
+
95
+ def show_stats(player_opgg_url):
96
+ """Display player statistics and recent matches"""
97
+ if not player_opgg_url:
98
+ return "Please enter a player link to OPGG", None
99
+
100
+ try:
101
+ training_features = get_user_training_df(player_opgg_url)
102
+
103
+ print("training_features: ", training_features, "\n")
104
+
105
+ if isinstance(training_features, str): # Error message
106
+ return training_features, None
107
+
108
+ wins = training_features['result'].sum()
109
+ losses = len(training_features) - wins
110
+ winrate = f"{(wins / len(training_features)) * 100:.0f}%"
111
+ favorite_champions = (
112
+ training_features['champion']
113
+ .value_counts()
114
+ .head(3)
115
+ .index.tolist()
116
+ )
117
+
118
+ stats_html = f"""
119
+ <div style='padding: 20px; background: #f5f5f5; border-radius: 10px;'>
120
+ <h3>Player's Recent Stats</h3>
121
+ <p>Wins: {wins} | Losses: {losses}</p>
122
+ <p>Winrate: {winrate}</p>
123
+ <p>Favorite Champions: {', '.join(favorite_champions)}</p>
124
+ </div>
125
+ """
126
+
127
+ return stats_html, None
128
+ except Exception as e:
129
+ return f"Error processing stats: {e}. ", None
130
+
131
+ def predict_top_5_champion_w_confidence(player_opgg_url, *champions):
132
+ """Make prediction based on selected champions"""
133
+ if not player_opgg_url or None in champions:
134
+ return "Please fill in all fields"
135
+
136
+ try:
137
+ if model is None:
138
+ return "Model not loaded properly"
139
+
140
+ if label_encoder is None:
141
+ return "Label encoder not loaded properly"
142
+
143
+ # Get and process the data
144
+ training_df = get_user_training_df(player_opgg_url)
145
+
146
+ if isinstance(training_df, str):
147
+ return training_df
148
+
149
+ training_df = convert_df(training_df)
150
+ print("check_datatypes(training_df) BEFORE feature eng: \n", check_datatypes(training_df), "\n")
151
+
152
+ training_df = apply_feature_engineering(training_df)
153
+ print("check_datatypes(training_df) AFTER feature eng: \n", check_datatypes(training_df), "\n")
154
+
155
+ label_column = training_df['champion']
156
+ predict_column = training_df.drop(columns=['champion', 'region'])
157
+
158
+ proba = model.predict_proba(predict_column)
159
+
160
+ # Get top 5 indices and probabilities
161
+ top_5_idx = np.argsort(proba, axis=1)[:, -5:][:, ::-1]
162
+ top_5_proba = np.take_along_axis(proba, top_5_idx, axis=1)
163
+
164
+ # Initialize results DataFrame
165
+ results = pd.DataFrame()
166
+
167
+ champion_converter = ChampionConverter()
168
+
169
+ # Add true champion - convert numeric label to champion name
170
+ true_numbers = label_column
171
+ results['True_Champion'] = [champion_converter.num_to_champion(int(num)) for num in true_numbers]
172
+
173
+ # Process each rank separately
174
+ for i in range(5):
175
+ # Convert indices to champion names using the champion converter
176
+ champions = [champion_converter.num_to_champion(int(label_encoder.classes_[idx])) for idx in top_5_idx[:, i]]
177
+ probabilities = top_5_proba[:, i]
178
+
179
+ # Add to results
180
+ results[f'Rank_{i+1}_Champion'] = champions
181
+ results[f'Rank_{i+1}_Confidence'] = probabilities.round(4)
182
+
183
+ try:
184
+
185
+
186
+ def find_champion_rank(row):
187
+ true_champ = row['True_Champion']
188
+ for i in range(1, 6):
189
+ if row[f'Rank_{i}_Champion'] == true_champ:
190
+ return f'Rank_{i}'
191
+ return 'Not in Top 5'
192
+
193
+ results['Prediction_Rank'] = results.apply(find_champion_rank, axis=1)
194
+
195
+ return results
196
+
197
+ except Exception as e:
198
+ print(f"Error mapping champion ID: {e}")
199
+ # return f"Error: Could not map champion ID {decoded_numeric[0]}"
200
+
201
+ except Exception as e:
202
+ import traceback
203
+ print(f"Full error trace:\n{traceback.format_exc()}")
204
+ return f"Error making prediction: {e}"
205
+
206
+ # Define your interface
207
+ with gr.Blocks() as demo:
208
+ gr.Markdown("# League of Legends Champion Prediction")
209
+
210
+ with gr.Row():
211
+ player_opgg_url = gr.Textbox(label="OPGG Player URL")
212
+ show_button = gr.Button("Show Player Stats")
213
+
214
+ with gr.Row():
215
+ stats_output = gr.HTML(label="Player Statistics")
216
+ recent_matches = gr.HTML(label="Recent Matches")
217
+
218
+ with gr.Row():
219
+ champion_dropdowns = [
220
+ gr.Dropdown(choices=CHAMPIONS, label=f"Champion {i+1}")
221
+ for i in range(9)
222
+ ]
223
+
224
+ with gr.Row():
225
+ predict_button = gr.Button("Predict")
226
+ prediction_output = gr.Text(label="Prediction")
227
+
228
+ # Set up event handlers
229
+ show_button.click(
230
+ fn=show_stats,
231
+ inputs=[player_opgg_url],
232
+ outputs=[stats_output, recent_matches]
233
+ )
234
+
235
+ predict_button.click(
236
+ fn=predict_top_5_champion_w_confidence,
237
+ inputs=[player_opgg_url] + champion_dropdowns,
238
+ outputs=prediction_output
239
+ )
240
+
241
+ # Optional: Save the champion encoder for future use
242
+ #joblib.dump(champion_encoder, 'champion_encoder.joblib')
243
+ # Enable queuing
244
+ demo.launch(debug=True)
245
+
246
+ # For local testing
247
+ if __name__ == "__main__":
248
+ demo.launch()