-
Notifications
You must be signed in to change notification settings - Fork 3
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
Implement save and load methods in DIET classifier #240
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,7 @@ dmypy.json | |
|
||
# Rasa traning folder | ||
.rasa/* | ||
tests/.rasa/* | ||
moviebot/* | ||
testing/* | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ | |
""" | ||
|
||
import copy | ||
import logging | ||
import os | ||
import pickle | ||
import tempfile | ||
from pathlib import Path | ||
from typing import Any, Dict, List, Optional, Text, Type | ||
|
@@ -73,7 +75,11 @@ def __init__( | |
else: | ||
raise TypeError("Provided 'training_data_path' is not a string!") | ||
|
||
self.init_pipeline() | ||
try: | ||
self.load_model() | ||
self._processes_utterances: Dict[str, Any] = {} | ||
except Exception: | ||
self.init_pipeline() | ||
|
||
def init_pipeline(self) -> None: | ||
"""Creates classifier and initialize. | ||
|
@@ -113,7 +119,7 @@ def init_pipeline(self) -> None: | |
), | ||
resource=self._def_resource, | ||
) | ||
self._processes_utterances: Dict[str, Any] = {} | ||
self._processes_utterances = {} | ||
|
||
def train_model( | ||
self, | ||
|
@@ -278,18 +284,18 @@ def process_message( | |
|
||
return message | ||
|
||
def save_model(self, file_path: str) -> None: | ||
"""Saves the trained model to a file. | ||
def save_model(self, file_path: str = None) -> None: | ||
"""Saves the trained model and preprocess pipeline. | ||
|
||
Args: | ||
file_path: File path. | ||
|
||
Raises: | ||
NotImplementedError: If not implemented in derived class. | ||
""" | ||
raise NotImplementedError("Rasa Diet") | ||
self._diet.persist() | ||
pipeline_path = os.path.join(self._model_path, "pipeline.pkl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could move "pipeline.pkl" to a global variable |
||
pickle.dump(self._component_pipeline, open(pipeline_path, "wb")) | ||
logging.info(f"Model saved to {self._model_path}.") | ||
|
||
def load_model(self, file_path: str) -> None: | ||
def load_model(self, file_path: str = None) -> None: | ||
"""Loads a model from a file. | ||
|
||
Args: | ||
|
@@ -298,4 +304,14 @@ def load_model(self, file_path: str) -> None: | |
Raises: | ||
NotImplementedError: If not implemented in derived class. | ||
""" | ||
raise NotImplementedError | ||
self._diet = DIETClassifier.load( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename it to diet_classifier? |
||
{**DIETClassifier.get_default_config()}, | ||
model_storage=self._def_model_storage, | ||
execution_context=ExecutionContext( | ||
GraphSchema({}), node_name="diet_1" | ||
), | ||
resource=self._def_resource, | ||
) | ||
pipeline_path = os.path.join(self._model_path, "pipeline.pkl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a global variable here |
||
self._component_pipeline = pickle.load(open(pipeline_path, "rb")) | ||
logging.info(f"Model loaded from {self._model_path}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this? Are these processed utterances? If yes, can we change the name?