zjpiazza commited on
Commit
00d2af5
·
1 Parent(s): 3084e65
Files changed (2) hide show
  1. handler.py +17 -55
  2. predictor.py +27 -19
handler.py CHANGED
@@ -50,76 +50,38 @@ class EndpointHandler:
50
  fighter1_name = None
51
  fighter2_name = None
52
 
53
- # try:
54
- # Get prediction using FightPredictor - it will handle random selection if names are None
55
- f1_prob, f2_prob, details = self.predictor.get_prediction(
56
- fighter1_name,
57
- fighter2_name,
58
- verbose=True
59
- )
60
-
61
- print(f"f1_prob: {f1_prob}, f2_prob: {f2_prob}, details: {details}")
62
-
63
- # Update fighter names if it was a random matchup
64
- if random_matchup:
65
- fighter1_name = details.get("fighter1_name")
66
- fighter2_name = details.get("fighter2_name")
67
- if not fighter1_name or not fighter2_name:
68
- return {"error": "Failed to get random fighters"}
69
 
70
- if f1_prob is None:
71
  return {"error": "Prediction failed"}
72
-
 
 
73
  # Get fighter images
74
- f1_image = get_fighter_image_url(fighter1_name)
75
- f2_image = get_fighter_image_url(fighter2_name)
76
 
77
  return {
78
  "fighter1": {
79
- "name": fighter1_name,
80
  "image_url": f1_image,
81
- "probability": f1_prob,
82
  "details": {
83
- "form_score": float(details.get(f"{fighter1_name}_form_score", 0)),
84
- "total_fights": int(details.get(f"{fighter1_name}_total_fights", 0))
85
  }
86
  },
87
  "fighter2": {
88
- "name": fighter2_name,
89
  "image_url": f2_image,
90
- "probability": f2_prob,
91
  "details": {
92
- "form_score": float(details.get(f"{fighter2_name}_form_score", 0)),
93
- "total_fights": int(details.get(f"{fighter2_name}_total_fights", 0))
94
  }
95
  },
96
  "details": {
97
- "age_difference": float(details.get("age_difference", 0))
98
  }
99
  }
100
-
101
- # except Exception as e:
102
- # print(f"Error: {str(e)}")
103
- # return {
104
- # "fighter1": {
105
- # "name": fighter1_name,
106
- # "image_url": "/default-fighter.png",
107
- # "probability": None,
108
- # "details": {
109
- # "form_score": 0,
110
- # "total_fights": 0
111
- # }
112
- # },
113
- # "fighter2": {
114
- # "name": fighter2_name,
115
- # "image_url": "/default-fighter.png",
116
- # "probability": None,
117
- # "details": {
118
- # "form_score": 0,
119
- # "total_fights": 0
120
- # }
121
- # },
122
- # "details": {
123
- # "age_difference": 0
124
- # }
125
- # }
 
50
  fighter1_name = None
51
  fighter2_name = None
52
 
53
+ # Get prediction using FightPredictor
54
+ result = self.predictor.get_prediction(fighter1_name, fighter2_name, verbose=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ if result is None:
57
  return {"error": "Prediction failed"}
58
+
59
+ f1_data, f2_data, details = result
60
+
61
  # Get fighter images
62
+ f1_image = get_fighter_image_url(f1_data['name'])
63
+ f2_image = get_fighter_image_url(f2_data['name'])
64
 
65
  return {
66
  "fighter1": {
67
+ "name": f1_data['name'],
68
  "image_url": f1_image,
69
+ "probability": f1_data['prob'],
70
  "details": {
71
+ "form_score": float(f1_data['form_score']),
72
+ "total_fights": f1_data['total_fights']
73
  }
74
  },
75
  "fighter2": {
76
+ "name": f2_data['name'],
77
  "image_url": f2_image,
78
+ "probability": f2_data['prob'],
79
  "details": {
80
+ "form_score": float(f2_data['form_score']),
81
+ "total_fights": f2_data['total_fights']
82
  }
83
  },
84
  "details": {
85
+ "age_difference": float(details['age_difference'])
86
  }
87
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
predictor.py CHANGED
@@ -111,7 +111,7 @@ class FightPredictor:
111
 
112
  return fighter1, fighter2
113
 
114
- def get_prediction(self, f1: str, f2: str, verbose: bool = False) -> Optional[Tuple[float, float, Dict]]:
115
  """
116
  Generate fight prediction between two fighters
117
 
@@ -121,32 +121,23 @@ class FightPredictor:
121
  verbose: Whether to return additional details
122
 
123
  Returns:
124
- Tuple of (fighter1_win_probability, fighter2_win_probability, details_dict)
125
  Returns None if prediction fails
126
  """
127
-
128
  try:
129
  # If both fighters are None, get random fighters
130
  if not f1 and not f2:
131
  f1, f2 = self.get_random_fighters()
132
- if verbose:
133
- details = {"fighter1_name": f1, "fighter2_name": f2} # Add names to details for random matchups
134
 
135
  # Validate fighters exist
136
  self._validate_fighters(f1, f2)
137
 
138
  # Get fighter stats and scale input
139
- vstup, details = self._get_fighter_stats(f1, f2, verbose)
140
-
141
- if verbose and not f1 and not f2:
142
- details.update({"fighter1_name": f1, "fighter2_name": f2}) # Ensure names are in details
143
-
144
  vstup_scaled = self._scale_input(vstup)
145
 
146
- # Reshape for prediction
147
  new_data = np.reshape(vstup_scaled, (1, 200, vstup_scaled.shape[1]))
148
-
149
- # Make predictions both ways and average
150
  pred_1 = self.model.predict(new_data, verbose=0)
151
 
152
  # Get reverse prediction
@@ -155,17 +146,34 @@ class FightPredictor:
155
  new_data_rev = np.reshape(vstup_rev_scaled, (1, 200, vstup_rev_scaled.shape[1]))
156
  pred_2 = self.model.predict(new_data_rev, verbose=0)
157
 
158
- # Calculate final probability (as decimal between 0 and 1)
159
  f1_prob = float(((1 - pred_1) + pred_2) / 2)
160
  f2_prob = round(1 - f1_prob, 4)
161
  f1_prob = round(f1_prob, 4)
162
 
163
- # Add probability percentages to details if verbose
164
- if verbose:
165
- details["fighter1_win_percentage"] = f"{f1_prob * 100:.2f}%"
166
- details["fighter2_win_percentage"] = f"{f2_prob * 100:.2f}%"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
- return f1_prob, f2_prob, details
169
  except Exception as e:
170
  print(f"Prediction failed: {e}")
171
  return None
 
111
 
112
  return fighter1, fighter2
113
 
114
+ def get_prediction(self, f1: str, f2: str, verbose: bool = False) -> Optional[Tuple[Dict, Dict, Dict]]:
115
  """
116
  Generate fight prediction between two fighters
117
 
 
121
  verbose: Whether to return additional details
122
 
123
  Returns:
124
+ Tuple of (fighter1_dict, fighter2_dict, details_dict)
125
  Returns None if prediction fails
126
  """
 
127
  try:
128
  # If both fighters are None, get random fighters
129
  if not f1 and not f2:
130
  f1, f2 = self.get_random_fighters()
 
 
131
 
132
  # Validate fighters exist
133
  self._validate_fighters(f1, f2)
134
 
135
  # Get fighter stats and scale input
136
+ vstup, raw_details = self._get_fighter_stats(f1, f2, verbose=True)
 
 
 
 
137
  vstup_scaled = self._scale_input(vstup)
138
 
139
+ # Make predictions
140
  new_data = np.reshape(vstup_scaled, (1, 200, vstup_scaled.shape[1]))
 
 
141
  pred_1 = self.model.predict(new_data, verbose=0)
142
 
143
  # Get reverse prediction
 
146
  new_data_rev = np.reshape(vstup_rev_scaled, (1, 200, vstup_rev_scaled.shape[1]))
147
  pred_2 = self.model.predict(new_data_rev, verbose=0)
148
 
149
+ # Calculate final probability
150
  f1_prob = float(((1 - pred_1) + pred_2) / 2)
151
  f2_prob = round(1 - f1_prob, 4)
152
  f1_prob = round(f1_prob, 4)
153
 
154
+ # Structure the response data
155
+ fighter1_data = {
156
+ 'name': f1,
157
+ 'form_score': raw_details.get(f"{f1}_form_score", "0.00"),
158
+ 'total_fights': int(raw_details.get(f"{f1}_total_fights", 0)),
159
+ 'win_percentage': f"{f1_prob * 100:.2f}%",
160
+ 'prob': f1_prob
161
+ }
162
+
163
+ fighter2_data = {
164
+ 'name': f2,
165
+ 'form_score': raw_details.get(f"{f2}_form_score", "0.00"),
166
+ 'total_fights': int(raw_details.get(f"{f2}_total_fights", 0)),
167
+ 'win_percentage': f"{f2_prob * 100:.2f}%",
168
+ 'prob': f2_prob
169
+ }
170
+
171
+ details = {
172
+ 'age_difference': raw_details.get("age_difference", "0.0"),
173
+ }
174
+
175
+ return fighter1_data, fighter2_data, details
176
 
 
177
  except Exception as e:
178
  print(f"Prediction failed: {e}")
179
  return None