Add custom handler for UFC predictor
Browse files- handler.py +44 -0
- requirements.txt +2 -0
handler.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import numpy as np
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class CustomLSTM(tf.keras.layers.LSTM):
|
| 7 |
+
def __init__(self, *args, **kwargs):
|
| 8 |
+
kwargs.pop('time_major', None)
|
| 9 |
+
super().__init__(*args, **kwargs)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class EndpointHandler:
|
| 13 |
+
def __init__(self, path=""):
|
| 14 |
+
# Load your Keras model
|
| 15 |
+
tf.keras.utils.get_custom_objects()['LSTM'] = CustomLSTM
|
| 16 |
+
self.model = tf.keras.models.load_model(f"{path}/tf_model.h5")
|
| 17 |
+
|
| 18 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 19 |
+
"""
|
| 20 |
+
Custom inference handler for UFC predictions.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
data: A dictionary containing the input data.
|
| 24 |
+
Expected keys: "fighter1" and "fighter2".
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
A dictionary with prediction results.
|
| 28 |
+
"""
|
| 29 |
+
fighter1 = data.get("fighter1")
|
| 30 |
+
fighter2 = data.get("fighter2")
|
| 31 |
+
|
| 32 |
+
# Validate inputs
|
| 33 |
+
if not fighter1 or not fighter2:
|
| 34 |
+
return {"error": "Both 'fighter1' and 'fighter2' must be provided."}
|
| 35 |
+
|
| 36 |
+
# Prepare input for the model (replace with your logic)
|
| 37 |
+
input_data = np.random.rand(1, 200, 89) # Replace with real pre-processing logic
|
| 38 |
+
|
| 39 |
+
# Get predictions
|
| 40 |
+
prediction = self.model.predict(input_data)
|
| 41 |
+
return {
|
| 42 |
+
"fighter1_win_probability": float(prediction[0][0]),
|
| 43 |
+
"fighter2_win_probability": float(1 - prediction[0][0]),
|
| 44 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
numpy
|