Spaces:
Sleeping
Sleeping
Commit
·
60e1d24
1
Parent(s):
db06c26
New iteration
Browse files
app.py
CHANGED
|
@@ -24,15 +24,24 @@ EXPECTED_COLUMNS.extend([f'games_{i}' for i in games_ids])
|
|
| 24 |
EXPECTED_COLUMNS.extend([f'winrate_{i}' for i in games_ids])
|
| 25 |
|
| 26 |
def load_reference_data(player_id):
|
| 27 |
-
"""Load reference prediction data
|
| 28 |
try:
|
|
|
|
| 29 |
ref_df = pd.read_csv(f"{player_id}.csv", encoding='utf-8')
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
print(f"Could not load reference data: {e}")
|
| 35 |
-
return None
|
| 36 |
|
| 37 |
def prepare_single_player_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
| 38 |
"""Creates a DataFrame in the expected format for the model"""
|
|
@@ -128,11 +137,19 @@ def predict_cost(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
|
| 128 |
try:
|
| 129 |
# Extract player_id for reference data
|
| 130 |
player_id = user_id.split("/")[-1] if "/" in user_id else user_id
|
| 131 |
-
|
| 132 |
|
| 133 |
# Prepare the player data
|
| 134 |
processed_data = prepare_single_player_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5)
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# Load and use the model
|
| 137 |
model_path = Path("model/rd2l_forest.onnx")
|
| 138 |
if not model_path.exists():
|
|
@@ -140,10 +157,6 @@ def predict_cost(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
|
| 140 |
|
| 141 |
session = ort.InferenceSession(str(model_path))
|
| 142 |
|
| 143 |
-
# Debug information
|
| 144 |
-
print("\nModel Input Information:")
|
| 145 |
-
print("Processed data shape:", processed_data.shape)
|
| 146 |
-
|
| 147 |
# Make prediction
|
| 148 |
input_name = session.get_inputs()[0].name
|
| 149 |
prediction = session.run(None, {input_name: processed_data.values.astype(np.float32)})[0]
|
|
@@ -156,11 +169,11 @@ def predict_cost(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
|
| 156 |
total_winrate = hero_stats.get('total_winrate', 'N/A')
|
| 157 |
|
| 158 |
comparison = ""
|
| 159 |
-
if
|
| 160 |
-
diff = abs(predicted_cost -
|
| 161 |
comparison = f"""
|
| 162 |
-
Reference Cost: {
|
| 163 |
-
Difference: {diff:.2f} ({(diff/
|
| 164 |
|
| 165 |
return f"""Predicted Cost: {predicted_cost}{comparison}
|
| 166 |
|
|
|
|
| 24 |
EXPECTED_COLUMNS.extend([f'winrate_{i}' for i in games_ids])
|
| 25 |
|
| 26 |
def load_reference_data(player_id):
|
| 27 |
+
"""Load reference prediction data and input features"""
|
| 28 |
try:
|
| 29 |
+
# Read the full CSV to get both input features and prediction
|
| 30 |
ref_df = pd.read_csv(f"{player_id}.csv", encoding='utf-8')
|
| 31 |
+
|
| 32 |
+
# Extract the features
|
| 33 |
+
features = ref_df[ref_df.index != 'Predicted_Cost']
|
| 34 |
+
print("\nReference features:")
|
| 35 |
+
print(features)
|
| 36 |
+
|
| 37 |
+
# Get the prediction from the last row
|
| 38 |
+
prediction = ref_df.iloc[-1][f"{player_id}_S34"]
|
| 39 |
+
print(f"Reference prediction: {prediction}")
|
| 40 |
+
|
| 41 |
+
return features, prediction
|
| 42 |
except Exception as e:
|
| 43 |
print(f"Could not load reference data: {e}")
|
| 44 |
+
return None, None
|
| 45 |
|
| 46 |
def prepare_single_player_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
| 47 |
"""Creates a DataFrame in the expected format for the model"""
|
|
|
|
| 137 |
try:
|
| 138 |
# Extract player_id for reference data
|
| 139 |
player_id = user_id.split("/")[-1] if "/" in user_id else user_id
|
| 140 |
+
reference_features, reference_prediction = load_reference_data(player_id)
|
| 141 |
|
| 142 |
# Prepare the player data
|
| 143 |
processed_data = prepare_single_player_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5)
|
| 144 |
|
| 145 |
+
print("\nComparing processed data with reference:")
|
| 146 |
+
if reference_features is not None:
|
| 147 |
+
for col in processed_data.columns:
|
| 148 |
+
if col in reference_features.index:
|
| 149 |
+
print(f"{col}:")
|
| 150 |
+
print(f" Our value: {processed_data[col].iloc[0]}")
|
| 151 |
+
print(f" Ref value: {reference_features.loc[col, player_id + '_S34']}")
|
| 152 |
+
|
| 153 |
# Load and use the model
|
| 154 |
model_path = Path("model/rd2l_forest.onnx")
|
| 155 |
if not model_path.exists():
|
|
|
|
| 157 |
|
| 158 |
session = ort.InferenceSession(str(model_path))
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
# Make prediction
|
| 161 |
input_name = session.get_inputs()[0].name
|
| 162 |
prediction = session.run(None, {input_name: processed_data.values.astype(np.float32)})[0]
|
|
|
|
| 169 |
total_winrate = hero_stats.get('total_winrate', 'N/A')
|
| 170 |
|
| 171 |
comparison = ""
|
| 172 |
+
if reference_prediction is not None:
|
| 173 |
+
diff = abs(predicted_cost - reference_prediction)
|
| 174 |
comparison = f"""
|
| 175 |
+
Reference Cost: {reference_prediction}
|
| 176 |
+
Difference: {diff:.2f} ({(diff/reference_prediction*100):.1f}% {'higher' if predicted_cost > reference_prediction else 'lower'})"""
|
| 177 |
|
| 178 |
return f"""Predicted Cost: {predicted_cost}{comparison}
|
| 179 |
|