def load(meta, rasa_config): # type: (Metadata, RasaNLUConfig) -> Interpreter """Load a stored model and its components defined by the provided metadata.""" from rasa_nlu.registry import load_component_by_name context = {"model_dir": meta.model_dir} config = dict(rasa_config.items()) config.update(meta.metadata) pipeline = [] for component_name in meta.pipeline: try: component = load_component_by_name(component_name, context, config) except rasa_nlu.components.MissingArgumentError as e: raise Exception( "Failed to create/load component '{}'. {}".format( component_name, e.message)) try: rasa_nlu.components.init_component(component, context, config) pipeline.append(component) except rasa_nlu.components.MissingArgumentError as e: raise Exception( "Failed to initialize component '{}'. {}".format( component.name, e.message)) return Interpreter(pipeline, context, config, meta)
def load_component(self, component_name: Text, model_dir: Text, model_metadata: 'Metadata', **context: Any) -> Component: """Tries to retrieve a component from the cache, else calls ``load`` to create a new component. Args: component_name (str): the name of the component to load model_dir (str): the directory to read the model from model_metadata (Metadata): the model's :class:`rasa_nlu.models.Metadata` Returns: Component: the loaded component. """ from rasa_nlu import registry try: cached_component, cache_key = self.__get_cached_component( component_name, model_metadata) component = registry.load_component_by_name( component_name, model_dir, model_metadata, cached_component, **context) if not cached_component: # If the component wasn't in the cache, # let us add it if possible self.__add_to_cache(component, cache_key) return component except MissingArgumentError as e: # pragma: no cover raise Exception("Failed to load component '{}'. " "{}".format(component_name, e))
def load_component(self, component_name, model_dir, model_metadata, **context): # type: (Text, Text, Metadata, **Any) -> Component """Tries to retrieve a component from the cache, else calls ``load`` to create a new component. Args: component_name (str): the name of the component to load model_dir (str): the directory to read the model from model_metadata (Metadata): the model's :class:`rasa_nlu.models.Metadata` Returns: Component: the loaded component. """ from rasa_nlu import registry from rasa_nlu.model import Metadata try: cached_component, cache_key = self.__get_cached_component( component_name, model_metadata) component = registry.load_component_by_name( component_name, model_dir, model_metadata, cached_component, **context) if not cached_component: # If the component wasn't in the cache, # let us add it if possible self.__add_to_cache(component, cache_key) return component except MissingArgumentError as e: # pragma: no cover raise Exception("Failed to load component '{}'. " "{}".format(component_name, e))
def __get_component(self, component_name, meta, context, model_config): component_class = registry.get_component_class(component_name) cache_key = component_class.cache_key(meta) if cache_key is not None and self.use_cache and cache_key in self.component_cache: component = self.component_cache[cache_key] else: component = registry.load_component_by_name(component_name, context, model_config) if cache_key is not None and self.use_cache: self.component_cache[cache_key] = component return component
def load_component(self, component_name, context, model_config, meta): # type: (Text, Dict[Text, Any], Dict[Text, Any], Metadata) -> Component """Tries to retrieve a component from the cache, calls `load` to create a new component.""" from rasa_nlu import registry from rasa_nlu.model import Metadata try: component, cache_key = self.__get_cached_component(component_name, meta) if component is None: component = registry.load_component_by_name(component_name, context, model_config) self.__add_to_cache(component, cache_key) return component except MissingArgumentError as e: # pragma: no cover raise Exception("Failed to load component '{}'. {}".format(component_name, e))
def load_component(self, component_name, context, model_config, meta): # type: (Text, Dict[Text, Any], Dict[Text, Any], Metadata) -> Component """Tries to retrieve a component from the cache, calls `load` to create a new component.""" from rasa_nlu import registry from rasa_nlu.model import Metadata try: component, cache_key = self.__get_cached_component(component_name, meta) if component is None: component = registry.load_component_by_name(component_name, context, model_config) self.__add_to_cache(component, cache_key) return component except MissingArgumentError as e: # pragma: no cover raise Exception("Failed to load component '{}'. {}".format(component_name, e))
def load_component(self, component_name, model_dir, model_metadata, **context): # type: (Text, Text, Metadata, **Any) -> Component """Tries to retrieve a component from the cache, calls `load` to create a new component.""" from rasa_nlu import registry from rasa_nlu.model import Metadata try: cached_component, cache_key = self.__get_cached_component(component_name, model_metadata) component = registry.load_component_by_name(component_name, model_dir, model_metadata, cached_component, **context) if not cached_component: # If the component wasn't in the cache, let us add it if possible self.__add_to_cache(component, cache_key) return component except MissingArgumentError as e: # pragma: no cover raise Exception("Failed to load component '{}'. {}".format(component_name, e))
def load_component(self, component_name, model_dir, model_metadata, **context): # type: (Text, Text, Metadata, **Any) -> Component """Tries to retrieve a component from the cache, calls `load` to create a new component.""" from rasa_nlu import registry from rasa_nlu.model import Metadata try: cached_component, cache_key = self.__get_cached_component(component_name, model_metadata) component = registry.load_component_by_name(component_name, model_dir, model_metadata, cached_component, **context) if not cached_component: # If the component wasn't in the cache, let us add it if possible self.__add_to_cache(component, cache_key) return component except MissingArgumentError as e: # pragma: no cover raise Exception("Failed to load component '{}'. {}".format(component_name, e))