コード例 #1
0
ファイル: model.py プロジェクト: wscholar/rasa_nlu
    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)
コード例 #2
0
ファイル: components.py プロジェクト: sony2/rasa_nlu
    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))
コード例 #3
0
ファイル: components.py プロジェクト: shiva16/rasa_nlu
    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))
コード例 #4
0
 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
コード例 #5
0
    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))
コード例 #6
0
ファイル: components.py プロジェクト: dhpollack/rasa_nlu
    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))
コード例 #7
0
    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))
コード例 #8
0
ファイル: components.py プロジェクト: DominicBreuker/rasa_nlu
    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))