Spaces:
Sleeping
Sleeping
Jimin Park
commited on
Commit
·
299f9c4
1
Parent(s):
0605202
kermitting soon
Browse files- util/app.py +69 -1
util/app.py
CHANGED
@@ -44,6 +44,13 @@ except Exception as e:
|
|
44 |
print(f"Error loading model: {e}")
|
45 |
model = None
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Functions
|
48 |
def get_user_training_df(player_opgg_url):
|
49 |
try:
|
@@ -67,7 +74,7 @@ def get_user_training_df(player_opgg_url):
|
|
67 |
#return f"Error getting training data: {e}"
|
68 |
|
69 |
def prepare_training_df(df, target_column='champion', stratify_columns=['champion', 'region'],
|
70 |
-
min_samples_per_class=
|
71 |
df = df.copy()
|
72 |
original_dtypes = df.dtypes.to_dict()
|
73 |
|
@@ -200,6 +207,67 @@ def show_stats(player_opgg_url):
|
|
200 |
|
201 |
def predict_champion(player_opgg_url, *champions):
|
202 |
"""Make prediction based on selected champions"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
print("============= Inside predict_champion()=================\n")
|
205 |
|
|
|
44 |
print(f"Error loading model: {e}")
|
45 |
model = None
|
46 |
|
47 |
+
try:
|
48 |
+
label_encoder = joblib.load('label_encoder.joblib')
|
49 |
+
print("Label encoder loaded successfully")
|
50 |
+
except Exception as e:
|
51 |
+
print(f"Error loading label encoder: {e}")
|
52 |
+
label_encoder = None
|
53 |
+
|
54 |
# Functions
|
55 |
def get_user_training_df(player_opgg_url):
|
56 |
try:
|
|
|
74 |
#return f"Error getting training data: {e}"
|
75 |
|
76 |
def prepare_training_df(df, target_column='champion', stratify_columns=['champion', 'region'],
|
77 |
+
min_samples_per_class=2, train_size=0.6, val_size=0.2, random_state=42):
|
78 |
df = df.copy()
|
79 |
original_dtypes = df.dtypes.to_dict()
|
80 |
|
|
|
207 |
|
208 |
def predict_champion(player_opgg_url, *champions):
|
209 |
"""Make prediction based on selected champions"""
|
210 |
+
if not player_opgg_url or None in champions:
|
211 |
+
return "Please fill in all fields"
|
212 |
+
|
213 |
+
try:
|
214 |
+
if model is None:
|
215 |
+
return "Model not loaded properly"
|
216 |
+
|
217 |
+
if label_encoder is None:
|
218 |
+
return "Label encoder not loaded properly"
|
219 |
+
|
220 |
+
# Get and process the data
|
221 |
+
training_df = get_user_training_df(player_opgg_url)
|
222 |
+
|
223 |
+
if isinstance(training_df, str): # Error message
|
224 |
+
return training_df
|
225 |
+
|
226 |
+
# Apply necessary transformations
|
227 |
+
training_df = convert_df(training_df)
|
228 |
+
training_df = apply_feature_engineering(training_df)
|
229 |
+
|
230 |
+
# Get feature columns (excluding champion and region)
|
231 |
+
feature_columns = [col for col in training_df.columns
|
232 |
+
if col not in ['champion', 'region', 'stratify_label']]
|
233 |
+
X = training_df[feature_columns]
|
234 |
+
|
235 |
+
# Handle categorical features
|
236 |
+
categorical_columns = X.select_dtypes(include=['category']).columns
|
237 |
+
X_processed = X.copy()
|
238 |
+
|
239 |
+
# Convert categorical columns to numeric
|
240 |
+
for col in categorical_columns:
|
241 |
+
X_processed[col] = X_processed[col].cat.codes
|
242 |
+
|
243 |
+
# Convert to float32
|
244 |
+
X_processed = X_processed.astype('float32')
|
245 |
+
|
246 |
+
# Create DMatrix with categorical feature support
|
247 |
+
dtest = DMatrix(X_processed, enable_categorical=True)
|
248 |
+
|
249 |
+
# Make prediction
|
250 |
+
predictions = model.predict(dtest)
|
251 |
+
|
252 |
+
# Get the highest probability prediction
|
253 |
+
if len(predictions.shape) > 1:
|
254 |
+
pred_indices = predictions.argmax(axis=1)
|
255 |
+
else:
|
256 |
+
pred_indices = predictions.astype(int)
|
257 |
+
|
258 |
+
# Decode predictions using loaded label encoder
|
259 |
+
decoded_preds = label_encoder.inverse_transform(pred_indices)
|
260 |
+
|
261 |
+
# Return the first prediction
|
262 |
+
return f"Predicted champion: {decoded_preds[0]}"
|
263 |
+
|
264 |
+
except Exception as e:
|
265 |
+
import traceback
|
266 |
+
print(f"Full error trace:\n{traceback.format_exc()}")
|
267 |
+
return f"Error making prediction: {e}"
|
268 |
+
|
269 |
+
def predict_champion_NOT_IN_USE(player_opgg_url, *champions):
|
270 |
+
"""Make prediction based on selected champions"""
|
271 |
|
272 |
print("============= Inside predict_champion()=================\n")
|
273 |
|