def _load_and_set_updated_model(agent: "Agent", model_directory: Text, fingerprint: Text): """Load the persisted model into memory and set the model on the agent.""" logger.debug( "Found new model with fingerprint {}. Loading...".format(fingerprint)) core_path, nlu_path = get_model_subdirectories(model_directory) if os.path.exists(nlu_path): from rasa.core.interpreter import RasaNLUInterpreter interpreter = RasaNLUInterpreter(model_directory=nlu_path) else: interpreter = (agent.interpreter if agent.interpreter is not None else RegexInterpreter()) domain = None if os.path.exists(core_path): domain_path = os.path.join(os.path.abspath(core_path), DEFAULT_DOMAIN_PATH) domain = Domain.load(domain_path) try: policy_ensemble = None if os.path.exists(core_path): policy_ensemble = PolicyEnsemble.load(core_path) agent.update_model(domain, policy_ensemble, fingerprint, interpreter, model_directory) logger.debug("Finished updating agent to new model.") except Exception: logger.exception("Failed to load policy and update agent. " "The previous model will stay loaded instead.")
def _load_and_set_updated_model(agent: "Agent", model_directory: Text, fingerprint: Text): """Load the persisted model into memory and set the model on the agent.""" logger.debug( "Found new model with fingerprint {}. Loading...".format(fingerprint)) stack_model_directory = _get_stack_model_directory(model_directory) if stack_model_directory: from rasa.core.interpreter import RasaNLUInterpreter nlu_model = os.path.join(stack_model_directory, "nlu") core_model = os.path.join(stack_model_directory, "core") interpreter = RasaNLUInterpreter(model_directory=nlu_model) else: interpreter = agent.interpreter core_model = model_directory domain_path = os.path.join(os.path.abspath(core_model), "domain.yml") domain = Domain.load(domain_path) # noinspection PyBroadException try: policy_ensemble = PolicyEnsemble.load(core_model) agent.update_model(domain, policy_ensemble, fingerprint, interpreter) logger.debug("Finished updating agent to new model.") except Exception: logger.exception("Failed to load policy and update agent. " "The previous model will stay loaded instead.")
def test_policy_loading_simple(tmp_path: Path): original_policy_ensemble = PolicyEnsemble([WorkingPolicy()]) original_policy_ensemble.train([], None, RegexInterpreter()) original_policy_ensemble.persist(str(tmp_path)) loaded_policy_ensemble = PolicyEnsemble.load(str(tmp_path)) assert original_policy_ensemble.policies == loaded_policy_ensemble.policies
def test_policy_loading_simple(tmpdir): original_policy_ensemble = PolicyEnsemble([WorkingPolicy()]) original_policy_ensemble.train([], None) original_policy_ensemble.persist(str(tmpdir)) loaded_policy_ensemble = PolicyEnsemble.load(str(tmpdir)) assert original_policy_ensemble.policies == loaded_policy_ensemble.policies
def load(cls, path: Text, interpreter: Optional[NaturalLanguageInterpreter] = None, generator: Union[EndpointConfig, 'NLG'] = None, tracker_store: Optional['TrackerStore'] = None, action_endpoint: Optional[EndpointConfig] = None) -> 'Agent': """Load a persisted model from the passed path.""" if not path: raise ValueError("You need to provide a valid directory where " "to load the agent from when calling " "`Agent.load`.") if os.path.isfile(path): raise ValueError("You are trying to load a MODEL from a file " "('{}'), which is not possible. \n" "The persisted path should be a directory " "containing the various model files. \n\n" "If you want to load training data instead of " "a model, use `agent.load_data(...)` " "instead.".format(path)) domain = Domain.load(os.path.join(path, "domain.yml")) ensemble = PolicyEnsemble.load(path) if path else None # ensures the domain hasn't changed between test and train domain.compare_with_specification(path) return cls(domain=domain, policies=ensemble, interpreter=interpreter, generator=generator, tracker_store=tracker_store, action_endpoint=action_endpoint)
def load( cls, model_path: Union[Text, Path], interpreter: Optional[NaturalLanguageInterpreter] = None, generator: Union[EndpointConfig, NaturalLanguageGenerator] = None, tracker_store: Optional[TrackerStore] = None, lock_store: Optional[LockStore] = None, action_endpoint: Optional[EndpointConfig] = None, model_server: Optional[EndpointConfig] = None, remote_storage: Optional[Text] = None, path_to_model_archive: Optional[Text] = None, ) -> "Agent": """Load a persisted model from the passed path.""" try: if not model_path: raise ModelNotFound("No path specified.") if not os.path.exists(model_path): raise ModelNotFound(f"No file or directory at '{model_path}'.") if os.path.isfile(model_path): model_path = get_model(str(model_path)) except ModelNotFound as e: raise ModelNotFound( f"You are trying to load a model from '{model_path}', " f"which is not possible. \n" f"The model path should be a 'tar.gz' file or a directory " f"containing the various model files in the sub-directories " f"'core' and 'nlu'. \n\n" f"If you want to load training data instead of a model, use " f"`agent.load_data(...)` instead. {e}" ) core_model, nlu_model = get_model_subdirectories(model_path) if not interpreter and nlu_model: interpreter = rasa.core.interpreter.create_interpreter(nlu_model) domain = None ensemble = None if core_model: domain = Domain.load(os.path.join(core_model, DEFAULT_DOMAIN_PATH)) ensemble = PolicyEnsemble.load(core_model) if core_model else None # ensures the domain hasn't changed between test and train domain.compare_with_specification(core_model) return cls( domain=domain, policies=ensemble, interpreter=interpreter, generator=generator, tracker_store=tracker_store, lock_store=lock_store, action_endpoint=action_endpoint, model_directory=model_path, model_server=model_server, remote_storage=remote_storage, path_to_model_archive=path_to_model_archive, )
def load( cls, model_path: Text, interpreter: Optional[NaturalLanguageInterpreter] = None, generator: Union[EndpointConfig, NaturalLanguageGenerator] = None, tracker_store: Optional[TrackerStore] = None, action_endpoint: Optional[EndpointConfig] = None, model_server: Optional[EndpointConfig] = None, remote_storage: Optional[Text] = None, ) -> "Agent": """Load a persisted model from the passed path.""" try: if not model_path: raise ModelNotFound("No path specified.") elif not os.path.exists(model_path): raise ModelNotFound( "No file or directory at '{}'.".format(model_path)) elif os.path.isfile(model_path): model_path = get_model(model_path) except ModelNotFound: raise ValueError( "You are trying to load a MODEL from '{}', which is not possible. \n" "The model path should be a 'tar.gz' file or a directory " "containing the various model files in the sub-directories 'core' " "and 'nlu'. \n\nIf you want to load training data instead of " "a model, use `agent.load_data(...)` instead.".format( model_path)) core_model, nlu_model = get_model_subdirectories(model_path) if not interpreter and os.path.exists(nlu_model): interpreter = NaturalLanguageInterpreter.create(nlu_model) domain = None ensemble = None if os.path.exists(core_model): domain = Domain.load(os.path.join(core_model, DEFAULT_DOMAIN_PATH)) ensemble = PolicyEnsemble.load(core_model) if core_model else None # ensures the domain hasn't changed between test and train domain.compare_with_specification(core_model) return cls( domain=domain, policies=ensemble, interpreter=interpreter, generator=generator, tracker_store=tracker_store, action_endpoint=action_endpoint, model_directory=model_path, model_server=model_server, remote_storage=remote_storage, )
def test_policy_loading_load_returns_none(tmp_path: Path, caplog: LogCaptureFixture): original_policy_ensemble = PolicyEnsemble([LoadReturnsNonePolicy()]) original_policy_ensemble.train([], None, RegexInterpreter()) original_policy_ensemble.persist(str(tmp_path)) with caplog.at_level(logging.WARNING): ensemble = PolicyEnsemble.load(str(tmp_path)) assert (caplog.records.pop().msg == "Failed to load policy tests.core.test_ensemble." "LoadReturnsNonePolicy: load returned None") assert len(ensemble.policies) == 0
def load( cls, unpacked_model_path: Text, interpreter: Optional[NaturalLanguageInterpreter] = None, generator: Union[EndpointConfig, "NLG"] = None, tracker_store: Optional["TrackerStore"] = None, action_endpoint: Optional[EndpointConfig] = None, model_server: Optional[EndpointConfig] = None, remote_storage: Optional[Text] = None, ) -> "Agent": """Load a persisted model from the passed path.""" if not os.path.exists(unpacked_model_path) or not os.path.isdir( unpacked_model_path ): raise ValueError( "You are trying to load a MODEL from " "('{}'), which is not possible. \n" "The persisted path should be a directory " "containing the various model files in the " "sub-directories 'core' and 'nlu'. \n\n" "If you want to load training data instead of " "a model, use `agent.load_data(...)` " "instead.".format(unpacked_model_path) ) core_model, nlu_model = get_model_subdirectories(unpacked_model_path) if not interpreter and os.path.exists(nlu_model): interpreter = NaturalLanguageInterpreter.create(nlu_model) domain = None ensemble = None if os.path.exists(core_model): domain = Domain.load(os.path.join(core_model, DEFAULT_DOMAIN_PATH)) ensemble = PolicyEnsemble.load(core_model) if core_model else None # ensures the domain hasn't changed between test and train domain.compare_with_specification(core_model) return cls( domain=domain, policies=ensemble, interpreter=interpreter, generator=generator, tracker_store=tracker_store, action_endpoint=action_endpoint, model_directory=unpacked_model_path, model_server=model_server, remote_storage=remote_storage, )
def _load_and_set_updated_model(agent: "Agent", model_directory: Text, fingerprint: Text): """Load the persisted model into memory and set the model on the agent.""" logger.debug( "Found new model with fingerprint {}. Loading...".format(fingerprint)) # bf mod core_path, nlu_models = get_model_subdirectories(model_directory) interpreters = {} # If NLU models exist then create interpreters from them if len(nlu_models): for lang, model_path in nlu_models.items(): interpreters[lang] = NaturalLanguageInterpreter.create( os.path.join(model_directory, model_path)) # If no NLU models exist, then associate a RegexInterpreter to the language code found in the fingerprints, # that should correspond to the default language in Botfront. This is to make sure an interpreter is available # when training stories without NLU else: from rasa.model import fingerprint_from_path, FINGERPRINT_CONFIG_NLU_KEY models_fingerprint = fingerprint_from_path(model_directory) if len(models_fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys()): interpreters = { list( models_fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys())[0]: RegexInterpreter() } # /bf mod domain = None if core_path: domain_path = os.path.join(os.path.abspath(core_path), DEFAULT_DOMAIN_PATH) domain = Domain.load(domain_path) try: policy_ensemble = None if core_path: policy_ensemble = PolicyEnsemble.load(core_path) agent.update_model(domain, policy_ensemble, fingerprint, interpreters, model_directory) logger.debug("Finished updating agent to new model.") except Exception: logger.exception("Failed to load policy and update agent. " "The previous model will stay loaded instead.")
def _load_domain_and_policy_ensemble( core_path: Optional[Text], ) -> Tuple[Optional[Domain], Optional[PolicyEnsemble]]: """Load the domain and policy ensemble from the model at `core_path`. Args: core_path: Core model path. Returns: An instance of `Domain` and `PolicyEnsemble` if `core_path` is not `None`. """ policy_ensemble = None domain = None if core_path: policy_ensemble = PolicyEnsemble.load(core_path) domain_path = os.path.join(os.path.abspath(core_path), DEFAULT_DOMAIN_PATH) domain = Domain.load(domain_path) return domain, policy_ensemble
def load( cls, model_path: Text, interpreter: Optional[Dict[Text, NaturalLanguageInterpreter]] = None, generator: Union[EndpointConfig, NaturalLanguageGenerator] = None, tracker_store: Optional[TrackerStore] = None, lock_store: Optional[LockStore] = None, action_endpoint: Optional[EndpointConfig] = None, model_server: Optional[EndpointConfig] = None, remote_storage: Optional[Text] = None, path_to_model_archive: Optional[Text] = None, ) -> "Agent": """Load a persisted model from the passed path.""" try: if not model_path: raise ModelNotFound("No path specified.") elif not os.path.exists(model_path): raise ModelNotFound(f"No file or directory at '{model_path}'.") elif os.path.isfile(model_path): model_path = get_model(model_path) except ModelNotFound: raise ValueError( "You are trying to load a MODEL from '{}', which is not possible. \n" "The model path should be a 'tar.gz' file or a directory " "containing the various model files in the sub-directories 'core' " "and 'nlu'. \n\nIf you want to load training data instead of " "a model, use `agent.load_data(...)` instead.".format(model_path) ) core_model, nlu_models = get_model_subdirectories(model_path) if nlu_models: if not interpreter: interpreter = {} for lang, model_path in nlu_models.items(): interpreter[lang] = NaturalLanguageInterpreter.create(os.path.join(model_path, model_path)) else: from rasa.model import fingerprint_from_path, FINGERPRINT_CONFIG_NLU_KEY fingerprint = fingerprint_from_path(model_path) if len(fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys()): interpreter = {list(fingerprint.get(FINGERPRINT_CONFIG_NLU_KEY).keys())[0]: RegexInterpreter()} domain = None ensemble = None if core_model: domain = Domain.load(os.path.join(core_model, DEFAULT_DOMAIN_PATH)) ensemble = PolicyEnsemble.load(core_model) if core_model else None # ensures the domain hasn't changed between test and train try: domain.compare_with_specification(core_model) except rasa.core.domain.InvalidDomain as e: logger.warning(e.message) domain = None return cls( domain=domain, policies=ensemble, interpreter=interpreter, generator=generator, tracker_store=tracker_store, lock_store=lock_store, action_endpoint=action_endpoint, model_directory=model_path, model_server=model_server, remote_storage=remote_storage, path_to_model_archive=path_to_model_archive, )