Beispiel #1
0
    def create(model_metadata: Metadata,
               component_builder: Optional[ComponentBuilder] = None,
               skip_validation: bool = False) -> 'Interpreter':
        """Load stored model and components defined by the provided metadata."""

        context = {}

        if component_builder is None:
            # If no builder is passed, every interpreter creation will result
            # in a new builder. hence, no components are reused.
            component_builder = components.ComponentBuilder()

        pipeline = []

        # Before instantiating the component classes,
        # lets check if all required packages are available
        if not skip_validation:
            components.validate_requirements(model_metadata.component_classes)

        for i in range(model_metadata.number_of_components):
            component_meta = model_metadata.for_component(i)
            component = component_builder.load_component(
                component_meta, model_metadata.model_dir, model_metadata,
                **context)
            try:
                updates = component.provide_context()
                if updates:
                    context.update(updates)
                pipeline.append(component)
            except components.MissingArgumentError as e:
                raise Exception("Failed to initialize component '{}'. "
                                "{}".format(component.name, e))

        return Interpreter(pipeline, context, model_metadata)
Beispiel #2
0
    def __init__(
        self,
        cfg: RasaNLUModelConfig,
        component_builder: Optional[ComponentBuilder] = None,
        skip_validation: bool = False,
        model_to_finetune: Optional["Interpreter"] = None,
    ) -> None:

        self.config = cfg
        self.skip_validation = skip_validation
        self.training_data = None  # type: Optional[TrainingData]

        if component_builder is None:
            # If no builder is passed, every interpreter creation will result in
            # a new builder. hence, no components are reused.
            component_builder = components.ComponentBuilder()

        # Before instantiating the component classes, lets check if all
        # required packages are available
        if not self.skip_validation:
            components.validate_requirements(cfg.component_names)

        if model_to_finetune:
            self.pipeline = model_to_finetune.pipeline
        else:
            self.pipeline = self._build_pipeline(cfg, component_builder)
Beispiel #3
0
    def get(self,
            repository_version,
            repository_authorization,
            rasa_version,
            use_cache=True):
        update_request = backend().request_backend_parse_nlu(
            repository_version, repository_authorization)

        repository_name = (f"{update_request.get('version_id')}_"
                           f"{update_request.get('total_training_end')}_"
                           f"{update_request.get('language')}")

        interpreter = self.interpreters.get(repository_name)

        if interpreter and use_cache:
            return interpreter
        persistor = BothubPersistor(repository_version,
                                    repository_authorization, rasa_version)
        model_directory = mkdtemp()
        persistor.retrieve(str(update_request.get("repository_uuid")),
                           model_directory)
        self.interpreters[repository_name] = Interpreter(
            None, {
                "language": update_request.get("language")
            }).load(model_directory,
                    components.ComponentBuilder(use_cache=False))
        return self.get(repository_version, repository_authorization,
                        rasa_version)
Beispiel #4
0
def load_entity_extractor(data_file, config_file):
    training_data = load_data(data_file)
    configuration = config.load(config_file)
    comp_builder = components.ComponentBuilder()
    #component = comp_builder.create_component("ner_crf",configuration)
    #ee = EntityExtractor(components.Component(configuration))
    crf = CRFEntityExtractor()
    crf.train(training_data, configuration)
    model_directory = crf.persist('./models/default/')
    return model_directory
Beispiel #5
0
    def create(
        model_dir: Text,
        model_metadata: Metadata,
        component_builder: Optional[ComponentBuilder] = None,
        skip_validation: bool = False,
        should_finetune: bool = False,
    ) -> "Interpreter":
        """Create model and components defined by the provided metadata.

        Args:
            model_dir: The directory containing the model.
            model_metadata: The metadata describing each component.
            component_builder: The
                :class:`rasa.nlu.components.ComponentBuilder` to use.
            skip_validation: If set to `True`, does not check that all
                required packages for the components are installed
                before loading them.
            should_finetune: Indicates if the model components will be fine-tuned.

        Returns:
            An interpreter that uses the created model.
        """
        context: Dict[Text, Any] = {"should_finetune": should_finetune}

        if component_builder is None:
            # If no builder is passed, every interpreter creation will result
            # in a new builder. hence, no components are reused.
            component_builder = components.ComponentBuilder()

        pipeline = []

        # Before instantiating the component classes,
        # lets check if all required packages are available
        if not skip_validation:
            components.validate_requirements(model_metadata.component_classes)

        for i in range(model_metadata.number_of_components):
            component_meta = model_metadata.for_component(i)
            component = component_builder.load_component(
                component_meta, model_dir, model_metadata, **context
            )
            try:
                updates = component.provide_context()
                if updates:
                    context.update(updates)
                pipeline.append(component)
            except components.MissingArgumentError as e:
                raise Exception(
                    "Failed to initialize component '{}'. "
                    "{}".format(component.name, e)
                )

        return Interpreter(pipeline, context, model_metadata)
Beispiel #6
0
 def get(self, update, use_cache=True):
     interpreter = self.interpreters.get('teste')
     if interpreter and use_cache:
         return interpreter
     persistor = BothubPersistor(update)
     model_directory = mkdtemp()
     # persistor.retrieve(
     #     str(update.repository.uuid),
     #     str(update.id),
     #     model_directory)
     persistor.retrieve(str('teste'), model_directory)
     self.interpreters['teste'] = BothubInterpreter.load(
         model_directory, components.ComponentBuilder(use_cache=False))
     return self.get(update)
Beispiel #7
0
    def get(self, update, repository_authorization, use_cache=True):
        update_request = backend().request_backend_parse_nlu(
            update, repository_authorization
        )

        interpreter = self.interpreters.get(update_request.get("update_id"))
        if interpreter and use_cache:
            return interpreter
        persistor = BothubPersistor(update, repository_authorization)
        model_directory = mkdtemp()
        persistor.retrieve(str(update_request.get("repository_uuid")), model_directory)
        self.interpreters[update_request.get("update_id")] = BothubInterpreter.load(
            model_directory, components.ComponentBuilder(use_cache=False)
        )
        return self.get(update, repository_authorization)
    def get_interpreter(self,
                        repository_version,
                        repository_authorization,
                        rasa_version,
                        use_cache=True) -> BothubInterpreter:

        update_request = backend().request_backend_parse_nlu_persistor(
            repository_version,
            repository_authorization,
            rasa_version,
            no_bot_data=True)

        repository_name = (
            f"{update_request.get('version_id')}_{update_request.get('language')}"
        )
        last_training = f"{update_request.get('total_training_end')}"

        # tries to fetch cache
        retrieved_cache = self.cached_interpreters.get(repository_name)
        if retrieved_cache and use_cache:
            # retrieve cache only if it's the same training
            if retrieved_cache["last_training"] == last_training:
                retrieved_cache["last_request"] = datetime.now()
                return retrieved_cache["interpreter_data"]

        persistor = BothubPersistor(repository_version,
                                    repository_authorization, rasa_version)
        model_directory = mkdtemp()
        persistor.retrieve(str(update_request.get("repository_uuid")),
                           model_directory)

        interpreter = BothubInterpreter(
            None, {"language": update_request.get("language")})
        interpreter = interpreter.load(
            model_directory, components.ComponentBuilder(use_cache=False))

        if use_cache:  # update/creates cache
            self.cached_interpreters[repository_name] = {
                "last_training": last_training,
                "interpreter_data": interpreter,
                "last_request": datetime.now()
            }

        return interpreter
Beispiel #9
0
    model_path = get_model(model)
    if not model_path:
        print_error("No model found. Train a model before running the "
                    "server using `rasa train nlu`.")
        exit(1)

    _, nlu_model = get_model_subdirectories(model_path)

    if not os.path.exists(nlu_model):
        print_error("No NLU model found. Train a model before running the "
                    "server using `rasa train nlu`.")
        exit(1)

    # input shell
    # rasa.nlu.run.run_cmdline(nlu_model)
    print("model_path is {},nlu_model is {}".format(model_path, nlu_model))
    print("please input your text to parse")
    # message = input().strip()
    # message = "这款衣服有货吗"
    message = "身高170体重140"
    interpreter = Interpreter.load(nlu_model, component_builder)
    result = interpreter.parse(message)
    print(json.dumps(result, indent=2))


if __name__ == "__main__":

    component_builder = components.ComponentBuilder()
    test_train_nlu_core()
    test_parse()