AlvaroMros commited on
Commit
0e63702
·
1 Parent(s): e604a94

Add model caching and new event config paths

Browse files

Introduced a global MODEL_CACHE in app.py to avoid reloading models from disk on each prediction, improving performance. Also added UPCOMING_EVENTS_JSON_PATH and EVENTS_JSON_PATH to src/config.py for future event data handling.

Files changed (2) hide show
  1. app.py +13 -5
  2. src/config.py +3 -0
app.py CHANGED
@@ -23,6 +23,10 @@ from src.predict.models import (
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)
@@ -40,13 +44,17 @@ def predict_fight(model_name, fighter1_name, fighter2_name):
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,
 
23
  # Import the configuration variable for the models directory for consistency.
24
  from src.config import MODELS_DIR
25
 
26
+ # --- Model Cache ---
27
+ # This global dictionary will store loaded models to avoid reloading them from disk.
28
+ MODEL_CACHE = {}
29
+
30
  # --- Gradio App Setup ---
31
  if not os.path.exists(MODELS_DIR):
32
  os.makedirs(MODELS_DIR)
 
44
  Loads the selected model and predicts the winner of a fight.
45
  """
46
  if model_name == "No models found" or not fighter1_name or not fighter2_name:
47
+ return "Please select a model and enter both fighter names.", ""
48
 
 
 
49
  try:
50
+ # Load model from cache or from disk if it's the first time
51
+ if model_name not in MODEL_CACHE:
52
+ print(f"Loading and caching model: {model_name}...")
53
+ model_path = os.path.join(MODELS_DIR, model_name)
54
+ MODEL_CACHE[model_name] = joblib.load(model_path)
55
+ print("...model cached.")
56
+
57
+ model = MODEL_CACHE[model_name]
58
 
59
  fight = {
60
  'fighter_1': fighter1_name,
src/config.py CHANGED
@@ -5,3 +5,6 @@ MODELS_DIR = os.path.join(OUTPUT_DIR, 'models')
5
  MODEL_RESULTS_PATH = os.path.join(OUTPUT_DIR, 'model_results.json')
6
  FIGHTS_CSV_PATH = os.path.join(OUTPUT_DIR, 'ufc_fights.csv')
7
  FIGHTERS_CSV_PATH = os.path.join(OUTPUT_DIR, 'ufc_fighters.csv')
 
 
 
 
5
  MODEL_RESULTS_PATH = os.path.join(OUTPUT_DIR, 'model_results.json')
6
  FIGHTS_CSV_PATH = os.path.join(OUTPUT_DIR, 'ufc_fights.csv')
7
  FIGHTERS_CSV_PATH = os.path.join(OUTPUT_DIR, 'ufc_fighters.csv')
8
+ UPCOMING_EVENTS_JSON_PATH = os.path.join(OUTPUT_DIR, 'upcoming_events.json')
9
+ EVENTS_JSON_PATH = os.path.join(OUTPUT_DIR, 'events.json')
10
+