Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the "loading model" message which was logged twice #8278

Merged
merged 5 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7260.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the 'loading model' message which was logged twice when using `rasa run`.
Binary file added other
Binary file not shown.
4 changes: 2 additions & 2 deletions rasa/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def run(args: argparse.Namespace):
# make sure either a model server, a remote storage, or a local model is
# configured

from rasa.model import get_model
import rasa.model
from rasa.core.utils import AvailableEndpoints

# start server if remote storage is configured
Expand All @@ -113,7 +113,7 @@ def run(args: argparse.Namespace):
args.model = _validate_model_path(args.model, "model", DEFAULT_MODELS_PATH)
local_model_set = True
try:
get_model(args.model)
rasa.model.get_local_model(args.model)
except ModelNotFound:
local_model_set = False

Expand Down
31 changes: 27 additions & 4 deletions rasa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,19 @@ def should_retrain_nlu(self) -> bool:
return self.force_training or self.nlu


def get_model(model_path: Text = DEFAULT_MODELS_PATH) -> TempDirectoryPath:
"""Get a model and unpack it. Raises a `ModelNotFound` exception if
no model could be found at the provided path.
def get_local_model(model_path: Text = DEFAULT_MODELS_PATH) -> Text:
"""Verifies that a model path exists.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to adapt that one, too. How about:

Suggested change
"""Verifies that a model path exists.
"""Returns verified path to local model archive.


Args:
model_path: Path to the zipped model. If it's a directory, the latest
trained model is returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also document the Returns part (pydocstyle unfortunately has a bug which doesn't flag missing Returns 😬 )


Returns:
Path to the unpacked model.
model_path: Path to the zipped model. If it's a directory, the latest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
model_path: Path to the zipped model. If it's a directory, the latest
Path to the zipped model. If it's a directory, the latest

trained model is returned.

Raises:
ModelNotFound Exception: When no model could be found at the provided path.

"""
if not model_path:
Expand All @@ -154,10 +157,30 @@ def get_model(model_path: Text = DEFAULT_MODELS_PATH) -> TempDirectoryPath:
elif not model_path.endswith(".tar.gz"):
raise ModelNotFound(f"Path '{model_path}' does not point to a Rasa model file.")

return model_path


def get_model(model_path: Text = DEFAULT_MODELS_PATH) -> TempDirectoryPath:
"""Get a model and unpack it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Get a model and unpack it.
"""Gets a model and unpack it.


Args:
model_path: Path to the zipped model. If it's a directory, the latest
trained model is returned.

Returns:
Path to the unpacked model.

Raises:
ModelNotFound Exception: When no model could be found at the provided path.

"""
model_path = get_local_model(model_path)

try:
model_relative_path = os.path.relpath(model_path)
except ValueError:
model_relative_path = model_path

logger.info(f"Loading model {model_relative_path}...")

return unpack_model(model_path)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
can_finetune,
create_package_rasa,
get_latest_model,
get_local_model,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor thing: Could we please also import the things by their module here instead of importing the functions directly?

get_model,
get_model_subdirectories,
model_fingerprint,
Expand Down Expand Up @@ -78,6 +79,16 @@ def test_get_model_context_manager(trained_rasa_model: str):
assert not os.path.exists(unpacked)


def test_get_local_model(trained_rasa_model: str):
assert get_local_model(trained_rasa_model) == trained_rasa_model


@pytest.mark.parametrize("model_path", ["foobar", "rasa", "README.md", None])
def test_get_local_model_exception(model_path: Optional[Text]):
with pytest.raises(ModelNotFound):
get_local_model(model_path)


@pytest.mark.parametrize("model_path", ["foobar", "rasa", "README.md", None])
def test_get_model_exception(model_path: Optional[Text]):
with pytest.raises(ModelNotFound):
Expand Down