Skip to content

Commit

Permalink
🎉 ModelCache: add custom exceptions when failing to load model.
Browse files Browse the repository at this point in the history
  • Loading branch information
asaf-kali committed Jun 4, 2024
1 parent be3b17f commit c02ca9b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[tool.poetry]
name = "codenames-solvers"
version = "1.7.3"
version = "1.7.5"
description = "Solvers implementation for Codenames board game in python."
authors = ["Michael Kali <[email protected]>", "Asaf Kali <[email protected]>"]
readme = "README.md"
Expand Down
23 changes: 20 additions & 3 deletions solvers/models/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
log = logging.getLogger(__name__)


class ModelLoadError(Exception):
def __init__(self, model_identifier: ModelIdentifier, message: str):
self.model_identifier = model_identifier
super().__init__(f"Failed to load model {model_identifier}: {message}")


class ModelNotFoundError(ModelLoadError):
def __init__(self, model_identifier: ModelIdentifier):
super().__init__(model_identifier, "Model not found")


class ModelCache:
def __init__(self, language_data_folder: str = "~/.cache/language_data"):
self.language_data_folder = language_data_folder
Expand Down Expand Up @@ -49,9 +60,15 @@ def _load_model(self, model_identifier: ModelIdentifier) -> KeyedVectors:
model_name=model_identifier.model_name,
is_stemmed=model_identifier.is_stemmed,
)
except Exception as e:
log.warning(f"Failed to load model: {e}", exc_info=True)
return load_from_gensim(model_identifier)
except Exception as local_load_error:
log.warning(f"Failed to load local model: {local_load_error}")
try:
return load_from_gensim(model_identifier)
except Exception as gensim_load_error:
log.warning(f"Failed to load model from gensim: {gensim_load_error}")
if isinstance(local_load_error, FileNotFoundError):
raise ModelNotFoundError(model_identifier) from gensim_load_error
raise ModelLoadError(model_identifier, str(local_load_error)) from gensim_load_error


def load_kv_format(language_base_folder: str, model_name: str, is_stemmed: bool = False) -> KeyedVectors:
Expand Down
2 changes: 1 addition & 1 deletion solvers/models/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def __hash__(self):
return hash(f"{self.language}-{self.model_name}-{self.is_stemmed}")

def __str__(self) -> str:
return f"{self.language}-{self.model_name}"
return f"{self.language}/{self.model_name}"

0 comments on commit c02ca9b

Please sign in to comment.