def load_from_remote_storage( remote_storage: Text, model_name: Text, 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, ) -> Optional["Agent"]: from rasa.nlu.persistor import get_persistor persistor = get_persistor(remote_storage) if persistor is not None: target_path = tempfile.mkdtemp() persistor.retrieve(model_name, target_path) return Agent.load( target_path, interpreter=interpreter, generator=generator, tracker_store=tracker_store, lock_store=lock_store, action_endpoint=action_endpoint, model_server=model_server, remote_storage=remote_storage, ) return None
def create_persistor(persistor: Optional[Text]): """Create a remote persistor to store the model if configured.""" if persistor is not None: from rasa.nlu.persistor import get_persistor return get_persistor(persistor) else: return None
def _load_model_from_cloud(self, model_name, target_path): try: from rasa.nlu.persistor import get_persistor p = get_persistor(self.remote_storage) if p is not None: p.retrieve(model_name, self._project, target_path) else: raise RuntimeError("Unable to initialize persistor") except Exception as e: logger.warning("Using default interpreter, couldn't fetch " "model: {}".format(e)) raise # re-raise this exception because nothing we can do now
def _list_models_in_cloud(self) -> List[Text]: try: from rasa.nlu.persistor import get_persistor p = get_persistor(self.remote_storage) if p is not None: return p.list_models(self._project) else: return [] except Exception as e: logger.warning("Failed to list models of project {}. " "{}".format(self._project, e)) return []
def _list_projects_in_cloud(self) -> List[Text]: # noinspection PyBroadException try: from rasa.nlu.persistor import get_persistor p = get_persistor(self.remote_storage) if p is not None: return p.list_projects() else: return [] except Exception: logger.exception("Failed to list projects. Make sure you have " "correctly configured your cloud storage " "settings.") return []
def load_from_remote_storage( remote_storage: Text, component_builder: ComponentBuilder, model_name: Text ) -> "NLUModel": from rasa.nlu.persistor import get_persistor p = get_persistor(remote_storage) if p is not None: target_path = tempfile.mkdtemp() p.retrieve(model_name, target_path) interpreter = interpreter_for_model(component_builder, target_path) return NLUModel(model_name, interpreter, target_path) else: raise RuntimeError("Unable to initialize persistor")
def load_model_from_remote_storage(self, model_name: Text) -> None: """Loads an Agent from remote storage.""" from rasa.nlu.persistor import get_persistor persistor = get_persistor(self.remote_storage) if persistor is not None: with tempfile.TemporaryDirectory() as temporary_directory: persistor.retrieve(model_name, temporary_directory) self.load_model(temporary_directory) else: raise RasaException( f"Persistor not found for remote storage: '{self.remote_storage}'." )
def test_raise_exception_in_get_external_persistor(): with pytest.raises(ImportError): _ = persistor.get_persistor("unknown.persistor")
def test_get_external_persistor(): p = persistor.get_persistor("tests.nlu.test_persistor.TestPersistor") assert isinstance(p, TestPersistor)