nick-leland commited on
Commit
ddcf7cb
·
1 Parent(s): 472609c
Files changed (1) hide show
  1. app.py +39 -90
app.py CHANGED
@@ -10,10 +10,9 @@ from feature_engineering import heroes, hero_information
10
 
11
  # Global variables for model and feature columns
12
  MODEL = None
13
- FEATURE_COLUMNS = None
14
 
15
  def load_model():
16
- """Load the ONNX model and get input features"""
17
  global MODEL
18
  try:
19
  model_path = Path("model/rd2l_forest.onnx")
@@ -26,93 +25,53 @@ def load_model():
26
  return f"Error loading model: {str(e)}"
27
 
28
  def process_player_data(player_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
29
- """Process player data similar to training pipeline"""
30
  try:
31
  # Clean player ID from URL if needed
32
  if "/" in player_id:
33
  player_id = player_id.split("/")[-1]
34
 
35
- # Create initial player series
36
- player_data = {
37
- "player_id": player_id,
38
- "mmr": float(mmr),
39
- "p1": int(comf_1),
40
- "p2": int(comf_2),
41
- "p3": int(comf_3),
42
- "p4": int(comf_4),
43
- "p5": int(comf_5)
 
 
 
 
 
 
 
44
  }
45
 
46
- # Read the example row from prediction_data_prepped.csv to get the expected structure
47
- try:
48
- pred_data = pd.read_csv("prediction_data_prepped.csv")
49
- if not pred_data.empty:
50
- # Get column structure from the first row
51
- for col in pred_data.columns:
52
- if col not in player_data:
53
- player_data[col] = 0
54
- except Exception as e:
55
- print(f"Warning - Error reading prediction data template: {str(e)}")
56
-
57
- # Get hero statistics using OpenDota API
58
  try:
59
  hero_stats = hero_information(player_id)
60
- player_data.update(hero_stats.to_dict())
61
-
62
- # Add season identifier to match training data format
63
- player_season = f"{player_id}_S34" # Assuming current season is 34
64
- temp_dict = {}
65
- temp_dict[player_season] = 1.0 # Set current season flag to 1.0
66
- player_data.update(temp_dict)
67
 
 
 
 
 
68
  except Exception as e:
69
  print(f"Warning - Error fetching hero data: {str(e)}")
70
- # If hero stats fail, add placeholder values
71
- player_data.update({
72
- "total_games_played": 0,
73
- "total_winrate": 0.0
74
- })
75
-
76
- # Convert to DataFrame for consistency with training
77
- df = pd.DataFrame([player_data])
78
 
79
- # Load reference data structure if available
80
- try:
81
- ref_data = pd.read_csv("result_prediction_data_prepped.csv")
82
- if not ref_data.empty:
83
- # Get all columns from reference data
84
- for col in ref_data.columns:
85
- if col not in df.columns:
86
- df[col] = 0
87
- # Reorder columns to match reference data
88
- df = df[ref_data.columns]
89
- except Exception as e:
90
- print(f"Warning - Error matching reference data structure: {str(e)}")
91
-
92
- # Load the expected columns from your prediction data
93
- pred_data = pd.read_csv("prediction_data_prepped.csv")
94
- expected_columns = pred_data.columns.tolist()
95
 
96
- # Debug print
97
- print(f"\nNumber of expected columns: {len(expected_columns)}")
98
- print(f"Number of current columns: {len(df.columns)}")
99
-
100
- # Find missing columns
101
- missing_columns = [col for col in expected_columns if col not in df.columns]
102
- extra_columns = [col for col in df.columns if col not in expected_columns]
103
-
104
- print(f"\nMissing columns: {missing_columns}")
105
- print(f"Extra columns: {extra_columns}")
106
-
107
- # Ensure all expected columns exist
108
- for col in expected_columns:
109
- if col not in df.columns:
110
- df[col] = 0
111
-
112
- # Remove any extra columns
113
- df = df[expected_columns]
114
-
115
- print(f"\nFinal number of columns: {len(df.columns)}")
116
  print(f"First few columns: {list(df.columns)[:5]}")
117
 
118
  return df
@@ -133,11 +92,7 @@ def predict_cost(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
133
 
134
  if isinstance(processed_data, str): # Error occurred
135
  return processed_data
136
-
137
- # Print debug information
138
- print("Processed data shape:", processed_data.shape)
139
- print("Processed data columns:", processed_data.columns.tolist())
140
-
141
  # Make prediction
142
  try:
143
  input_name = MODEL.get_inputs()[0].name
@@ -188,16 +143,10 @@ demo = gr.Interface(
188
  - The predictor uses machine learning trained on historical RD2L draft data
189
  - Player statistics are fetched from OpenDota API
190
  - Position comfort levels range from 1 (least comfortable) to 5 (most comfortable)
191
- - Predictions are based on both current stats and historical performance
192
-
193
- ### Notes
194
- - MMR should be the player's current solo MMR
195
- - Position comfort should reflect actual role experience
196
- - Predictions are estimates and may vary from actual draft results"""
197
  )
198
 
199
- # Load model on startup
200
- print(load_model())
201
-
202
  if __name__ == "__main__":
203
- demo.launch()
 
 
 
10
 
11
  # Global variables for model and feature columns
12
  MODEL = None
 
13
 
14
  def load_model():
15
+ """Load the ONNX model"""
16
  global MODEL
17
  try:
18
  model_path = Path("model/rd2l_forest.onnx")
 
25
  return f"Error loading model: {str(e)}"
26
 
27
  def process_player_data(player_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
28
+ """Process player data with correct feature structure"""
29
  try:
30
  # Clean player ID from URL if needed
31
  if "/" in player_id:
32
  player_id = player_id.split("/")[-1]
33
 
34
+ # Create initial data structure with basic features
35
+ data = {
36
+ 'mmr': float(mmr),
37
+ 'p1': int(comf_1),
38
+ 'p2': int(comf_2),
39
+ 'p3': int(comf_3),
40
+ 'p4': int(comf_4),
41
+ 'p5': int(comf_5),
42
+ 'count': 0,
43
+ 'mean': 0,
44
+ 'std': 0,
45
+ 'min': 0,
46
+ 'max': 0,
47
+ 'sum': 0,
48
+ 'total_games_played': 0,
49
+ 'total_winrate': 0
50
  }
51
 
52
+ # Add hero-specific features
53
+ for i in range(1, 139): # Add all possible hero IDs
54
+ data[f'games_{i}'] = 0
55
+ data[f'winrate_{i}'] = 0
56
+
57
+ # Get hero statistics from OpenDota
 
 
 
 
 
 
58
  try:
59
  hero_stats = hero_information(player_id)
60
+ data['total_games_played'] = hero_stats['total_games_played']
61
+ data['total_winrate'] = hero_stats['total_winrate']
 
 
 
 
 
62
 
63
+ # Update hero-specific stats
64
+ for key, value in hero_stats.items():
65
+ if key in data:
66
+ data[key] = value
67
  except Exception as e:
68
  print(f"Warning - Error fetching hero data: {str(e)}")
 
 
 
 
 
 
 
 
69
 
70
+ # Convert to DataFrame
71
+ df = pd.DataFrame([data])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ print(f"Processed data shape: {df.shape}")
74
+ print(f"Number of features: {len(df.columns)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  print(f"First few columns: {list(df.columns)[:5]}")
76
 
77
  return df
 
92
 
93
  if isinstance(processed_data, str): # Error occurred
94
  return processed_data
95
+
 
 
 
 
96
  # Make prediction
97
  try:
98
  input_name = MODEL.get_inputs()[0].name
 
143
  - The predictor uses machine learning trained on historical RD2L draft data
144
  - Player statistics are fetched from OpenDota API
145
  - Position comfort levels range from 1 (least comfortable) to 5 (most comfortable)
146
+ - Predictions are based on both current stats and historical performance"""
 
 
 
 
 
147
  )
148
 
 
 
 
149
  if __name__ == "__main__":
150
+ print("===== Application Startup =====")
151
+ print(load_model())
152
+ demo.launch(server_name="0.0.0.0")