Spaces:
Sleeping
Sleeping
Commit
·
ca842a9
1
Parent(s):
be1532b
plps
Browse files
app.py
CHANGED
@@ -5,68 +5,103 @@ import onnxruntime as ort
|
|
5 |
import sys
|
6 |
from pathlib import Path
|
7 |
sys.path.append("rd2l_pred")
|
8 |
-
from training_data_prep import list_format, modification, league_money, df_gen
|
9 |
from feature_engineering import heroes, hero_information
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
'Comfort (Pos 3)': [comf_3],
|
21 |
-
'Comfort (Pos 4)': [comf_4],
|
22 |
-
'Comfort (Pos 5)': [comf_5],
|
23 |
-
'Player statement': ['N/A'] # Placeholder for player statement
|
24 |
-
}
|
25 |
-
|
26 |
-
return pd.DataFrame(player_data)
|
27 |
|
28 |
-
def
|
29 |
-
"""Creates a
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def predict_cost(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
41 |
"""Main prediction function for Gradio interface"""
|
42 |
try:
|
43 |
-
#
|
44 |
-
|
45 |
-
captains_df = prepare_mock_captains()
|
46 |
-
|
47 |
-
# Use the original pipeline functions
|
48 |
-
money = league_money(captains_df, 'prediction')
|
49 |
-
prepped_data = df_gen(draft_df, money, 'prediction')
|
50 |
|
51 |
# Load and use the model
|
52 |
model_path = Path("model/rd2l_forest.onnx")
|
|
|
|
|
|
|
53 |
session = ort.InferenceSession(str(model_path))
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
hero_stats = hero_information(player_id)
|
59 |
-
# Add hero stats to prepped data
|
60 |
-
for col, value in hero_stats.items():
|
61 |
-
prepped_data[col] = value
|
62 |
-
except Exception as e:
|
63 |
-
print(f"Warning - Error fetching hero data: {str(e)}")
|
64 |
|
65 |
# Make prediction
|
66 |
input_name = session.get_inputs()[0].name
|
67 |
-
prediction = session.run(None, {input_name:
|
68 |
predicted_cost = round(float(prediction[0]), 2)
|
69 |
|
|
|
|
|
|
|
|
|
70 |
return f"""Predicted Cost: {predicted_cost}
|
71 |
|
72 |
Player Details:
|
@@ -78,6 +113,10 @@ Player Details:
|
|
78 |
* Pos 4: {comf_4}
|
79 |
* Pos 5: {comf_5}
|
80 |
|
|
|
|
|
|
|
|
|
81 |
Note: This prediction is based on historical data and player statistics from OpenDota."""
|
82 |
|
83 |
except Exception as e:
|
|
|
5 |
import sys
|
6 |
from pathlib import Path
|
7 |
sys.path.append("rd2l_pred")
|
|
|
8 |
from feature_engineering import heroes, hero_information
|
9 |
|
10 |
+
# Define expected columns
|
11 |
+
EXPECTED_COLUMNS = ['mmr', 'p1', 'p2', 'p3', 'p4', 'p5', 'count', 'mean', 'std', 'min', 'max', 'sum',
|
12 |
+
'total_games_played', 'total_winrate'] + \
|
13 |
+
[f'games_{i}' for i in range(1, 146) if i != 24 and i != 122 and i != 124 and i != 125 and
|
14 |
+
i != 127 and i != 130 and i != 132 and i != 133 and i != 134 and i != 139 and
|
15 |
+
i != 140 and i != 141 and i != 142 and i != 143 and i != 144] + \
|
16 |
+
[f'winrate_{i}' for i in range(1, 146) if i != 24 and i != 122 and i != 124 and i != 125 and
|
17 |
+
i != 127 and i != 130 and i != 132 and i != 133 and i != 134 and i != 139 and
|
18 |
+
i != 140 and i != 141 and i != 142 and i != 143 and i != 144]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def prepare_single_player_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
21 |
+
"""Creates a DataFrame in the expected format for the model"""
|
22 |
+
try:
|
23 |
+
# Extract player_id from URL if needed
|
24 |
+
player_id = user_id.split("/")[-1] if "/" in user_id else user_id
|
25 |
+
|
26 |
+
# Get hero statistics using OpenDota API
|
27 |
+
hero_stats = hero_information(player_id)
|
28 |
+
|
29 |
+
# Create initial data dictionary with zeros for all columns
|
30 |
+
data = {col: 0 for col in EXPECTED_COLUMNS}
|
31 |
+
|
32 |
+
# Fill in the basic features
|
33 |
+
data.update({
|
34 |
+
'mmr': float(mmr),
|
35 |
+
'p1': int(comf_1),
|
36 |
+
'p2': int(comf_2),
|
37 |
+
'p3': int(comf_3),
|
38 |
+
'p4': int(comf_4),
|
39 |
+
'p5': int(comf_5),
|
40 |
+
})
|
41 |
+
|
42 |
+
# Add hero statistics
|
43 |
+
if hero_stats is not None:
|
44 |
+
data['total_games_played'] = hero_stats.get('total_games_played', 0)
|
45 |
+
data['total_winrate'] = hero_stats.get('total_winrate', 0)
|
46 |
+
|
47 |
+
# Fill in the games and winrate columns from hero_stats
|
48 |
+
for key, value in hero_stats.items():
|
49 |
+
if key in EXPECTED_COLUMNS:
|
50 |
+
data[key] = value
|
51 |
+
|
52 |
+
# Add mock statistics for money-related columns
|
53 |
+
# These would normally come from league_money function
|
54 |
+
stats = {
|
55 |
+
'count': 1,
|
56 |
+
'mean': mmr / 200, # rough approximation
|
57 |
+
'std': mmr / 400,
|
58 |
+
'min': mmr / 250,
|
59 |
+
'max': mmr / 150,
|
60 |
+
'sum': mmr / 200
|
61 |
+
}
|
62 |
+
data.update(stats)
|
63 |
+
|
64 |
+
# Convert to DataFrame
|
65 |
+
df = pd.DataFrame([data])
|
66 |
+
|
67 |
+
# Ensure columns are in correct order
|
68 |
+
df = df[EXPECTED_COLUMNS]
|
69 |
+
|
70 |
+
print(f"DataFrame shape: {df.shape}")
|
71 |
+
print("Missing columns:", set(EXPECTED_COLUMNS) - set(df.columns))
|
72 |
+
|
73 |
+
return df
|
74 |
+
|
75 |
+
except Exception as e:
|
76 |
+
print(f"Error in data preparation: {e}")
|
77 |
+
raise e
|
78 |
|
79 |
def predict_cost(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
80 |
"""Main prediction function for Gradio interface"""
|
81 |
try:
|
82 |
+
# Prepare the player data
|
83 |
+
processed_data = prepare_single_player_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5)
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# Load and use the model
|
86 |
model_path = Path("model/rd2l_forest.onnx")
|
87 |
+
if not model_path.exists():
|
88 |
+
return f"Model file not found at: {model_path}"
|
89 |
+
|
90 |
session = ort.InferenceSession(str(model_path))
|
91 |
|
92 |
+
# Debug information
|
93 |
+
print("Processed data shape:", processed_data.shape)
|
94 |
+
print("Processed data columns:", processed_data.columns.tolist())
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
# Make prediction
|
97 |
input_name = session.get_inputs()[0].name
|
98 |
+
prediction = session.run(None, {input_name: processed_data.values.astype(np.float32)})[0]
|
99 |
predicted_cost = round(float(prediction[0]), 2)
|
100 |
|
101 |
+
hero_stats = processed_data.iloc[0]
|
102 |
+
total_games = hero_stats.get('total_games_played', 'N/A')
|
103 |
+
total_winrate = hero_stats.get('total_winrate', 'N/A')
|
104 |
+
|
105 |
return f"""Predicted Cost: {predicted_cost}
|
106 |
|
107 |
Player Details:
|
|
|
113 |
* Pos 4: {comf_4}
|
114 |
* Pos 5: {comf_5}
|
115 |
|
116 |
+
Player Statistics:
|
117 |
+
- Total Games: {total_games}
|
118 |
+
- Overall Winrate: {total_winrate:.1%} if isinstance(total_winrate, float) else 'N/A'
|
119 |
+
|
120 |
Note: This prediction is based on historical data and player statistics from OpenDota."""
|
121 |
|
122 |
except Exception as e:
|