Spaces:
Running
Running
Commit
·
338c9b0
1
Parent(s):
1e04613
Add Gradio app and project configuration
Browse filesIntroduces a Gradio-based UFC fight prediction app in app.py, which loads models and predicts fight outcomes based on user input. Adds pyproject.toml with project metadata and dependencies for reproducible builds and packaging.
- app.py +95 -0
- pyproject.toml +29 -0
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
# --- Path and Module Setup ---
|
| 8 |
+
# Add the 'src' directory to the system path so we can import our custom modules.
|
| 9 |
+
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
|
| 10 |
+
|
| 11 |
+
# Although these models are not called directly, they MUST be imported here.
|
| 12 |
+
# joblib.load() needs these class definitions in scope to deserialize the model files correctly.
|
| 13 |
+
from src.predict.models import (
|
| 14 |
+
BaseMLModel,
|
| 15 |
+
EloBaselineModel,
|
| 16 |
+
LogisticRegressionModel,
|
| 17 |
+
XGBoostModel,
|
| 18 |
+
SVCModel,
|
| 19 |
+
RandomForestModel,
|
| 20 |
+
BernoulliNBModel,
|
| 21 |
+
LGBMModel
|
| 22 |
+
)
|
| 23 |
+
# Import the configuration variable for the models directory for consistency.
|
| 24 |
+
from src.config import MODELS_DIR
|
| 25 |
+
|
| 26 |
+
# --- Gradio App Setup ---
|
| 27 |
+
if not os.path.exists(MODELS_DIR):
|
| 28 |
+
os.makedirs(MODELS_DIR)
|
| 29 |
+
print(f"Warning: Models directory not found. Created a dummy directory at '{MODELS_DIR}'.")
|
| 30 |
+
|
| 31 |
+
# Get a list of available models
|
| 32 |
+
available_models = [f for f in os.listdir(MODELS_DIR) if f.endswith(".joblib")]
|
| 33 |
+
if not available_models:
|
| 34 |
+
print(f"Warning: No models found in '{MODELS_DIR}'. The dropdown will be empty.")
|
| 35 |
+
available_models.append("No models found")
|
| 36 |
+
|
| 37 |
+
# --- Prediction Function ---
|
| 38 |
+
def predict_fight(model_name, fighter1_name, fighter2_name):
|
| 39 |
+
"""
|
| 40 |
+
Loads the selected model and predicts the winner of a fight.
|
| 41 |
+
"""
|
| 42 |
+
if model_name == "No models found" or not fighter1_name or not fighter2_name:
|
| 43 |
+
return "Please select a model and enter both fighter names."
|
| 44 |
+
|
| 45 |
+
model_path = os.path.join(MODELS_DIR, model_name)
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
print(f"Loading model: {model_name}")
|
| 49 |
+
model = joblib.load(model_path)
|
| 50 |
+
|
| 51 |
+
fight = {
|
| 52 |
+
'fighter_1': fighter1_name,
|
| 53 |
+
'fighter_2': fighter2_name,
|
| 54 |
+
'event_date': datetime.now().strftime('%B %d, %Y')
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
predicted_winner = model.predict(fight)
|
| 58 |
+
|
| 59 |
+
if predicted_winner:
|
| 60 |
+
return f"Predicted Winner: {predicted_winner}"
|
| 61 |
+
else:
|
| 62 |
+
return "Could not make a prediction. Is one of the fighters new or not in the dataset?"
|
| 63 |
+
|
| 64 |
+
except FileNotFoundError:
|
| 65 |
+
return f"Error: Model file '{model_name}' not found."
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"An error occurred during prediction: {e}")
|
| 68 |
+
return f"An error occurred: {e}"
|
| 69 |
+
|
| 70 |
+
# --- Gradio Interface ---
|
| 71 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 72 |
+
gr.Markdown("# UFC Fight Predictor")
|
| 73 |
+
gr.Markdown("Select a prediction model and enter two fighter names to predict the outcome.")
|
| 74 |
+
|
| 75 |
+
with gr.Column():
|
| 76 |
+
model_dropdown = gr.Dropdown(
|
| 77 |
+
label="Select Model",
|
| 78 |
+
choices=available_models,
|
| 79 |
+
value=available_models[0] if available_models else None
|
| 80 |
+
)
|
| 81 |
+
with gr.Row():
|
| 82 |
+
fighter1_input = gr.Textbox(label="Fighter 1", placeholder="e.g., Jon Jones")
|
| 83 |
+
fighter2_input = gr.Textbox(label="Fighter 2", placeholder="e.g., Stipe Miocic")
|
| 84 |
+
|
| 85 |
+
predict_button = gr.Button("Predict Winner")
|
| 86 |
+
output_text = gr.Textbox(label="Prediction Result", interactive=False)
|
| 87 |
+
|
| 88 |
+
predict_button.click(
|
| 89 |
+
fn=predict_fight,
|
| 90 |
+
inputs=[model_dropdown, fighter1_input, fighter2_input],
|
| 91 |
+
outputs=output_text
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# --- Launch the App ---
|
| 95 |
+
demo.launch()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[build-system]
|
| 2 |
+
requires = ["setuptools>=61.0"]
|
| 3 |
+
build-backend = "setuptools.build_meta"
|
| 4 |
+
|
| 5 |
+
[project]
|
| 6 |
+
name = "ufc_predictor"
|
| 7 |
+
version = "0.1.0"
|
| 8 |
+
authors = [
|
| 9 |
+
{ name="Álvaro Menéndez Ros", email="[email protected]" },
|
| 10 |
+
]
|
| 11 |
+
description = "A model for predicting UFC fight outcomes."
|
| 12 |
+
requires-python = ">=3.8"
|
| 13 |
+
classifiers = [
|
| 14 |
+
"Programming Language :: Python :: 3",
|
| 15 |
+
"License :: OSI Approved :: MIT License",
|
| 16 |
+
"Operating System :: OS Independent",
|
| 17 |
+
]
|
| 18 |
+
dependencies = [
|
| 19 |
+
"gradio==4.28.3",
|
| 20 |
+
"scikit-learn==1.4.2",
|
| 21 |
+
"pandas==2.2.2",
|
| 22 |
+
"xgboost==2.0.3",
|
| 23 |
+
"lightgbm==4.3.0",
|
| 24 |
+
"joblib==1.4.2",
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
[tool.setuptools]
|
| 28 |
+
package-dir = {"" = "src"}
|
| 29 |
+
packages = ["find:"]
|