Skip to content

Commit

Permalink
Improve preset error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdangerw committed Dec 6, 2023
1 parent caec1fc commit 00d79d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 15 additions & 1 deletion keras_nlp/utils/preset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

def get_file(preset, path):
"""Download a preset file in necessary and return the local path."""
if not isinstance(preset, str):
raise ValueError(
f"A preset identifier must be a string. Received: preset={preset}"
)
if preset.startswith(KAGGLE_PREFIX):
if kagglehub is None:
raise ImportError(
Expand Down Expand Up @@ -64,9 +68,19 @@ def get_file(preset, path):
url,
cache_subdir=os.path.join("models", subdir),
)
else:
elif os.path.exists(preset):
# Assume a local filepath.
return os.path.join(preset, path)
else:
raise ValueError(
"Unknown preset identifier. A preset must be a one of:\n"
"1) a built in preset identifier like `'bert_base_en'`\n"
"2) a Kaggle Models handle like `'kaggle://keras/bert/bert_base_en'`\n"
"3) a path to a local preset directory like `'./bert_base_en`\n"
"Use `print(cls.presets.keys())` to view all built-in presets for "
"API symbol `cls`.\n"
f"Received: preset='{preset}'"
)


def get_tokenizer(layer):
Expand Down
7 changes: 7 additions & 0 deletions keras_nlp/utils/preset_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@ def test_preset_saving(self, cls, preset_name, tokenizer_type):
self.assertAllEqual(
model(model_input_data), restored_model(restored_model_input_data)
)

def test_preset_errors(self):
with self.assertRaisesRegex(ValueError, "must be a string"):
AlbertClassifier.from_preset(AlbertClassifier)

with self.assertRaisesRegex(ValueError, "Unknown preset identifier"):
AlbertClassifier.from_preset("snaggle://bort/bort/bort")

0 comments on commit 00d79d6

Please sign in to comment.