DeepFieldML/DF_E3_Football_Formation_Predictor
1
1import gradio as gr2import onnxruntime as ort3import numpy as np4import pickle5import re6 7# Load the ONNX model8onnx_model_path = "formation_predictor.onnx"9ort_session = ort.InferenceSession(onnx_model_path)10 11# Function to convert input data to one-hot encoding12def to_one_hot(indices, num_classes):13 indices = np.array(indices, dtype=int)14 return np.eye(num_classes)[indices]15 16# Load the label encoder17def load_label_encoder():18 with open("label_encoder.pkl", "rb") as f:19 le = pickle.load(f)20 return le21 22le = load_label_encoder()23num_classes = len(le.classes_)24 25# Function to prepare input data26def prepare_input(opponent_formation, le, num_classes):27 opponent_formation = opponent_formation.strip().strip("'\"[]") # Ensure no leading/trailing spaces, quotes, or brackets28 opp_idx = le.transform([opponent_formation])[0] if isinstance(opponent_formation, str) else opponent_formation29 opp_one_hot = to_one_hot([opp_idx], num_classes)30 return opp_one_hot31 32# Function to recommend formation using ONNX model33def recommend_formation_onnx(opponent_formation, ort_session, le, num_classes):34 opp_one_hot = prepare_input(opponent_formation, le, num_classes)35 36 best_formation, best_score = None, -float("inf")37 evaluated_formations = []38 for our_idx in range(num_classes):39 our_one_hot = to_one_hot([our_idx], num_classes)40 input_vector = np.concatenate([opp_one_hot, our_one_hot], axis=1).astype(np.float32)41 42 # Run the ONNX model43 ort_inputs = {ort_session.get_inputs()[0].name: input_vector}44 ort_outs = ort_session.run(None, ort_inputs)45 score = ort_outs[0][0, 0]46 47 formation = le.inverse_transform([our_idx])[0]48 evaluated_formations.append((formation, score))49 50 if score > best_score:51 best_score = score52 best_formation = formation53 54 evaluated_formations.sort(key=lambda x: x[1], reverse=True)55 return best_formation, evaluated_formations56 57# Function to handle the recommend button click58def recommend(opponent_formation):59 opponent_formation = opponent_formation.strip().strip("'\"[]") # Ensure no leading/trailing spaces, quotes, or brackets60 61 # Validate the format of the opponent formation62 if not re.match(r'^\d+(-\d+)+$', opponent_formation):63 return f"Error: Formation '{opponent_formation}' is not in the correct format (e.g., '3-4-2-1').", []64 65 if opponent_formation not in le.classes_:66 return f"Error: Formation '{opponent_formation}' not recognized.", []67 68 best_formation, evaluated_formations = recommend_formation_onnx(opponent_formation, ort_session, le, num_classes)69 return f"Recommended formation: {best_formation}", evaluated_formations70 71# Create the Gradio interface72iface = gr.Interface(73 fn=recommend,74 inputs=gr.Textbox(lines=1, placeholder="Enter opponent formation (e.g., '3-4-2-1')"),75 outputs=[76 gr.Textbox(label="Recommended Formation"),77 gr.Dataframe(headers=["Formation", "Score"], label="Evaluated Formations")78 ],79 title="Deepfield Proyecto Maradona E3 Football Formation Recommender",80 description="Enter the opponent formation to get the recommended formation and a list of evaluated formations with their scores."81)82 83# Launch the Gradio interface84iface.launch(share=True)