Spaces:
Sleeping
Sleeping
Jimin Park
commited on
Commit
·
0367bbe
1
Parent(s):
6af173b
added new structure
Browse files- util/app.py +61 -129
util/app.py
CHANGED
@@ -7,7 +7,8 @@ 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 |
import joblib
|
12 |
|
13 |
|
@@ -127,7 +128,7 @@ def show_stats(player_opgg_url):
|
|
127 |
except Exception as e:
|
128 |
return f"Error processing stats: {e}. ", None
|
129 |
|
130 |
-
def
|
131 |
"""Make prediction based on selected champions"""
|
132 |
if not player_opgg_url or None in champions:
|
133 |
return "Please fill in all fields"
|
@@ -146,56 +147,92 @@ def predict_champion(player_opgg_url, *champions):
|
|
146 |
return training_df
|
147 |
|
148 |
training_df = convert_df(training_df)
|
149 |
-
#print("type(training_df): ", type(training_df), "\n")
|
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 |
# Get feature columns
|
156 |
-
feature_columns = [col for col in training_df.columns
|
157 |
-
|
158 |
-
X = training_df[feature_columns]
|
159 |
|
160 |
# Handle categorical features
|
161 |
-
categorical_columns = X.select_dtypes(include=['category']).columns
|
162 |
-
X_processed = X.copy()
|
163 |
|
164 |
-
for col in categorical_columns:
|
165 |
-
|
166 |
|
167 |
-
X_processed = X_processed.astype('float32')
|
168 |
|
169 |
# Create DMatrix and predict
|
170 |
-
dtest = DMatrix(X_processed, enable_categorical=False)
|
171 |
-
predictions = model.predict(dtest)
|
|
|
172 |
|
173 |
# Get prediction indices
|
174 |
-
if len(predictions.shape) > 1:
|
175 |
-
|
176 |
-
else:
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
# First get the numeric ID from the original label encoder
|
180 |
-
decoded_numeric = label_encoder.inverse_transform(pred_indices)
|
181 |
|
182 |
# Map numeric ID to index in CHAMPIONS list
|
183 |
# Since your label encoder seems to use champion IDs, we need to map these to list indices
|
184 |
try:
|
185 |
# Get the first 3 prediction
|
186 |
-
champion_id = int(decoded_numeric[0])
|
187 |
|
188 |
# Print debug information
|
189 |
-
print(f"Champion ID from model: {champion_id}")
|
190 |
|
191 |
# Find the closest matching index
|
192 |
# Note: This assumes champion IDs roughly correspond to their position in the list
|
193 |
-
champion_index = min(max(champion_id - 1, 0), len(CHAMPIONS) - 1)
|
194 |
-
predicted_champion = CHAMPIONS[champion_index]
|
195 |
|
196 |
-
print(f"Mapped to champion: {predicted_champion}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
-
return
|
199 |
|
200 |
except Exception as e:
|
201 |
print(f"Error mapping champion ID: {e}")
|
@@ -206,111 +243,6 @@ def predict_champion(player_opgg_url, *champions):
|
|
206 |
print(f"Full error trace:\n{traceback.format_exc()}")
|
207 |
return f"Error making prediction: {e}"
|
208 |
|
209 |
-
''' current working function!!!!!!
|
210 |
-
def predict_champion(player_opgg_url, *champions):
|
211 |
-
"""Make prediction based on selected champions"""
|
212 |
-
|
213 |
-
print("==================== Inside: predict_champion() ===================== \n")
|
214 |
-
if not player_opgg_url or None in champions:
|
215 |
-
return "Please fill in all fields"
|
216 |
-
|
217 |
-
try:
|
218 |
-
if model is None:
|
219 |
-
return "Model not loaded properly"
|
220 |
-
|
221 |
-
if label_encoder is None:
|
222 |
-
return "Label encoder not loaded properly"
|
223 |
-
|
224 |
-
# Print label encoder information
|
225 |
-
print("\nLabel Encoder Information:")
|
226 |
-
print("Classes in encoder:", label_encoder.classes_)
|
227 |
-
print("Number of classes:", len(label_encoder.classes_))
|
228 |
-
|
229 |
-
# Get and process the data
|
230 |
-
training_df = get_user_training_df(player_opgg_url)
|
231 |
-
print("training_df retrieved: ", training_df, "\n")
|
232 |
-
|
233 |
-
if isinstance(training_df, str): # Error message
|
234 |
-
return training_df
|
235 |
-
|
236 |
-
# Apply necessary transformations
|
237 |
-
training_df = convert_df(training_df)
|
238 |
-
training_df = apply_feature_engineering(training_df)
|
239 |
-
print("training_df converted and feature engineered: ", training_df, "\n")
|
240 |
-
|
241 |
-
# Get feature columns (excluding champion and region)
|
242 |
-
feature_columns = [col for col in training_df.columns
|
243 |
-
if col not in ['champion', 'region', 'stratify_label']]
|
244 |
-
X = training_df[feature_columns]
|
245 |
-
print("Got feature columns X: ", X, "\n")
|
246 |
-
|
247 |
-
# Handle categorical features
|
248 |
-
categorical_columns = X.select_dtypes(include=['category']).columns
|
249 |
-
X_processed = X.copy()
|
250 |
-
print("Handled categorical features, X_processed = ", X_processed, "\n")
|
251 |
-
|
252 |
-
# Convert categorical columns to numeric
|
253 |
-
for col in categorical_columns:
|
254 |
-
X_processed[col] = X_processed[col].cat.codes
|
255 |
-
print("Converted categorical columns to numeric: ", categorical_columns, "\n")
|
256 |
-
|
257 |
-
# Convert to float32
|
258 |
-
X_processed = X_processed.astype('float32')
|
259 |
-
print("Converted X_processed to float32: ", X_processed, "\n")
|
260 |
-
|
261 |
-
# Create DMatrix with categorical feature support
|
262 |
-
dtest = DMatrix(X_processed, enable_categorical=True)
|
263 |
-
print("Converted to Dmatrix: ", dtest, "\n")
|
264 |
-
|
265 |
-
# Make prediction
|
266 |
-
print("Starting model prediction...\n")
|
267 |
-
predictions = model.predict(dtest)
|
268 |
-
print("Model prediction complete\n")
|
269 |
-
|
270 |
-
print("\nPrediction Information:")
|
271 |
-
print("Raw predictions shape:", predictions.shape)
|
272 |
-
print("Raw predictions:", predictions)
|
273 |
-
|
274 |
-
# Get the highest probability prediction
|
275 |
-
if len(predictions.shape) > 1:
|
276 |
-
pred_indices = predictions.argmax(axis=1)
|
277 |
-
else:
|
278 |
-
pred_indices = predictions.astype(int)
|
279 |
-
|
280 |
-
print("\nPrediction Indices:")
|
281 |
-
print("Indices shape:", pred_indices.shape)
|
282 |
-
print("Indices:", pred_indices)
|
283 |
-
|
284 |
-
# Check if indices are within valid range
|
285 |
-
print("\nValidation:")
|
286 |
-
print("Min index:", pred_indices.min())
|
287 |
-
print("Max index:", pred_indices.max())
|
288 |
-
print("Valid index range:", 0, len(label_encoder.classes_) - 1)
|
289 |
-
# Try to decode predictions
|
290 |
-
|
291 |
-
try:
|
292 |
-
decoded_preds = label_encoder.inverse_transform(pred_indices)
|
293 |
-
print("\nDecoded Predictions:")
|
294 |
-
print("Type:", type(decoded_preds))
|
295 |
-
print("Value:", decoded_preds)
|
296 |
-
print("==================== Exiting: predict_champion()===================\n")
|
297 |
-
return f"Predicted champion: {decoded_preds[0]}"
|
298 |
-
except Exception as e:
|
299 |
-
print(f"\nError during decoding: {e}")
|
300 |
-
# Fallback: try to directly index into classes
|
301 |
-
try:
|
302 |
-
champion = label_encoder.classes_[int(pred_indices[0])]
|
303 |
-
return f"Predicted champion: {champion}"
|
304 |
-
except Exception as e2:
|
305 |
-
print(f"Fallback error: {e2}")
|
306 |
-
return f"Error decoding prediction: {pred_indices[0]}"
|
307 |
-
|
308 |
-
except Exception as e:
|
309 |
-
import traceback
|
310 |
-
print(f"Full error trace:\n{traceback.format_exc()}")
|
311 |
-
return f"Error making prediction: {e}"
|
312 |
-
'''
|
313 |
-
|
314 |
# Define your interface
|
315 |
with gr.Blocks() as demo:
|
316 |
gr.Markdown("# League of Legends Champion Prediction")
|
|
|
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 |
|
|
|
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"
|
|
|
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 |
# 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(training_df)
|
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]
|
185 |
+
top_5_proba = np.take_along_axis(proba, top_5_idx, axis=1)
|
186 |
+
|
187 |
+
# Initialize results DataFrame
|
188 |
+
results = pd.DataFrame()
|
189 |
+
|
190 |
+
champion_converter = ChampionConverter()
|
191 |
|
192 |
+
# Add true champion - convert numeric label to champion name
|
193 |
+
true_numbers = label_column
|
194 |
+
results['True_Champion'] = [champion_converter.num_to_champion(int(num)) for num in true_numbers]
|
195 |
+
|
196 |
+
# Process each rank separately
|
197 |
+
for i in range(5):
|
198 |
+
# Convert indices to champion names using the champion converter
|
199 |
+
champions = [champion_converter.num_to_champion(int(label_encoder.classes_[idx])) for idx in top_5_idx[:, i]]
|
200 |
+
probabilities = top_5_proba[:, i]
|
201 |
+
|
202 |
+
# Add to results
|
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):
|
227 |
+
true_champ = row['True_Champion']
|
228 |
+
for i in range(1, 6):
|
229 |
+
if row[f'Rank_{i}_Champion'] == true_champ:
|
230 |
+
return f'Rank_{i}'
|
231 |
+
return 'Not in Top 5'
|
232 |
+
|
233 |
+
results['Prediction_Rank'] = results.apply(find_champion_rank, axis=1)
|
234 |
|
235 |
+
return results
|
236 |
|
237 |
except Exception as e:
|
238 |
print(f"Error mapping champion ID: {e}")
|
|
|
243 |
print(f"Full error trace:\n{traceback.format_exc()}")
|
244 |
return f"Error making prediction: {e}"
|
245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
# Define your interface
|
247 |
with gr.Blocks() as demo:
|
248 |
gr.Markdown("# League of Legends Champion Prediction")
|