Ejemplo n.º 1
0
def copy_models_to_compare(models: List[str]) -> Text:
    models_dir = tempfile.mkdtemp()

    for i, model in enumerate(models):
        if os.path.exists(model) and os.path.isfile(model):
            path = os.path.join(models_dir, "model_" + str(i))
            unpack_model(model, path)
        else:
            logger.warning("Ignore '{}' as it is not a valid model file.".format(model))

    logger.debug("Unpacked models to compare to '{}'".format(models_dir))

    return models_dir
Ejemplo n.º 2
0
def test_train_core_success(rasa_app, default_stack_config,
                            default_stories_file, default_domain_path):
    domain_file = open(default_domain_path)
    config_file = open(default_stack_config)
    core_file = open(default_stories_file)

    payload = dict(domain=domain_file.read(),
                   config=config_file.read(),
                   nlu=core_file.read())

    config_file.close()
    core_file.close()

    _, response = rasa_app.post("/model/train", json=payload)
    assert response.status == 200

    # save model to temporary file
    tempdir = tempfile.mkdtemp()
    model_path = os.path.join(tempdir, "model.tar.gz")
    with open(model_path, "wb") as f:
        f.write(response.body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, "fingerprint.json"))
Ejemplo n.º 3
0
def test_package_model(trained_rasa_model, parameters):
    output_path = tempfile.mkdtemp()
    train_path = unpack_model(trained_rasa_model)

    model_path = _package_model(
        _fingerprint(),
        output_path,
        train_path,
        parameters["model_name"],
        parameters["prefix"],
        parameters["uncompress"],
    )

    assert os.path.exists(model_path)

    file_name = os.path.basename(model_path)

    if parameters["model_name"]:
        assert parameters["model_name"] in file_name

    if parameters["prefix"]:
        assert parameters["prefix"] in file_name

    if parameters["uncompress"]:
        assert os.path.isdir(model_path)
        assert not file_name.endswith(".tar.gz")
    else:
        assert file_name.endswith(".tar.gz")
Ejemplo n.º 4
0
def perform_interactive_learning(
    args: argparse.Namespace, zipped_model: Text, file_importer: TrainingDataImporter
) -> None:
    """Performs interactive learning.

    Args:
        args: Namespace arguments.
        zipped_model: Path to zipped model.
        file_importer: File importer which provides the training data and model config.
    """
    from rasa.core.train import do_interactive_learning

    args.model = zipped_model

    with model.unpack_model(zipped_model) as model_path:
        args.core, args.nlu = model.get_model_subdirectories(model_path)
        if args.core is None:
            rasa.shared.utils.cli.print_error_and_exit(
                "Can not run interactive learning on an NLU-only model."
            )

        args.endpoints = rasa.cli.utils.get_validated_path(
            args.endpoints, "endpoints", DEFAULT_ENDPOINTS_PATH, True
        )

        do_interactive_learning(args, file_importer)
Ejemplo n.º 5
0
    def __init__(self, **kwargs):
        model_path = kwargs.get("model", None)
        try:
            if model_path is not None:
                logger.info("Loading model " + model_path)
                if os.path.isdir(model_path):
                    self.interpreter = Interpreter.load(model_path)
                elif model_path.endswith(".tar.gz"):
                    model = unpack_model(model_path)
                    if os.path.isdir(model + "/nlu"):
                        self.interpreter = Interpreter.load(model + "/nlu")
                    else:
                        self.interpreter = Interpreter.load(model)
                self.interpreter.parse("ok")

                """Cleanup tmp files and directories"""
                try :
                    if model is not None:
                        remove_file_or_dir(model)
                    tmp_dir = "/tmp/"
                    for root, dirs, files in os.walk(tmp_dir):
                        for file in files:
                            if file.startswith("tmp") and file.endswith(".py"):
                                remove_file_or_dir(tmp_dir+file)
                except Exception as e:
                    logger.error("Exception when cleanup tmp files and directories.", exc_info=True)
            else:
                self.interpreter = None
        except Exception as e:
            logger.error("Error when loading model {0}, exception {1}".format(model_path, e), exc_info=True)
            remove_file_or_dir(model_path)
            raise
Ejemplo n.º 6
0
async def _core_model_for_finetuning(
    model_to_finetune: Text,
    file_importer: TrainingDataImporter,
    finetuning_epoch_fraction: float = 1.0,
) -> Optional[Agent]:
    path_to_archive = model.get_model_for_finetuning(model_to_finetune)
    if not path_to_archive:
        return None

    rasa.shared.utils.cli.print_info(
        f"Loading Core model from {path_to_archive} for finetuning...", )

    with model.unpack_model(path_to_archive) as unpacked:
        new_fingerprint = await model.model_fingerprint(file_importer)
        old_fingerprint = model.fingerprint_from_path(unpacked)
        if not model.can_finetune(old_fingerprint, new_fingerprint, core=True):
            rasa.shared.utils.cli.print_error_and_exit(
                "Core model can not be finetuned.")

        config = await file_importer.get_config()
        agent = Agent.load(
            unpacked,
            new_config=config,
            finetuning_epoch_fraction=finetuning_epoch_fraction,
        )
        # Agent might be empty if no underlying Core model was found.
        if agent.domain is not None and agent.policy_ensemble is not None:
            return agent

        return None
Ejemplo n.º 7
0
    def load_local_model(
        model_path: 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,
        remote_storage: Optional[Text] = None,
    ) -> "Agent":
        if os.path.isfile(model_path):
            model_archive = model_path
        else:
            model_archive = get_latest_model(model_path)

        if model_archive is None:
            rasa.shared.utils.io.raise_warning(
                f"Could not load local model in '{model_path}'.")
            return Agent()

        working_directory = tempfile.mkdtemp()
        unpacked_model = unpack_model(model_archive, working_directory)

        return Agent.load(
            unpacked_model,
            interpreter=interpreter,
            generator=generator,
            tracker_store=tracker_store,
            lock_store=lock_store,
            action_endpoint=action_endpoint,
            model_server=model_server,
            remote_storage=remote_storage,
            path_to_model_archive=model_archive,
        )
Ejemplo n.º 8
0
def test_stack_training(app,
                        default_domain_path,
                        default_stories_file,
                        default_stack_config,
                        default_nlu_data):
    domain_file = open(default_domain_path)
    config_file = open(default_stack_config)
    stories_file = open(default_stories_file)
    nlu_file = open(default_nlu_data)

    payload = dict(
        domain=domain_file.read(),
        config=config_file.read(),
        stories=stories_file.read(),
        nlu=nlu_file.read()
    )

    domain_file.close()
    config_file.close()
    stories_file.close()
    nlu_file.close()

    _, response = app.post('/jobs', json=payload)
    assert response.status == 200

    # save model to temporary file
    tempdir = tempfile.mkdtemp()
    model_path = os.path.join(tempdir, 'model.tar.gz')
    with open(model_path, 'wb') as f:
        f.write(response.body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, 'fingerprint.json'))
Ejemplo n.º 9
0
def test_train_no_domain_exists(
        run_in_simple_project: Callable[..., RunResult]) -> None:

    os.remove("domain.yml")
    run_in_simple_project(
        "train",
        "-c",
        "config.yml",
        "--data",
        "data",
        "--out",
        "train_models_no_domain",
        "--fixed-model-name",
        "nlu-model-only",
    )

    assert os.path.exists("train_models_no_domain")
    files = rasa.shared.utils.io.list_files("train_models_no_domain")
    assert len(files) == 1

    trained_model_path = "train_models_no_domain/nlu-model-only.tar.gz"
    unpacked = model.unpack_model(trained_model_path)

    metadata_path = os.path.join(unpacked, "nlu", "metadata.json")
    assert os.path.exists(metadata_path)
Ejemplo n.º 10
0
async def _nlu_model_for_finetuning(
    model_to_finetune: Text,
    file_importer: TrainingDataImporter,
    finetuning_epoch_fraction: float = 1.0,
    called_from_combined_training: bool = False,
) -> Optional[Interpreter]:

    path_to_archive = model.get_model_for_finetuning(model_to_finetune)
    if not path_to_archive:
        return None

    rasa.shared.utils.cli.print_info(
        f"Loading NLU model from {path_to_archive} for finetuning...", )
    with model.unpack_model(path_to_archive) as unpacked:
        _, old_nlu = model.get_model_subdirectories(unpacked)
        new_fingerprint = await model.model_fingerprint(file_importer)
        old_fingerprint = model.fingerprint_from_path(unpacked)
        if not model.can_finetune(
                old_fingerprint,
                new_fingerprint,
                nlu=True,
                core=called_from_combined_training,
        ):
            rasa.shared.utils.cli.print_error_and_exit(
                "NLU model can not be finetuned.")

        config = await file_importer.get_config()
        model_to_finetune = Interpreter.load(
            old_nlu,
            new_config=config,
            finetuning_epoch_fraction=finetuning_epoch_fraction,
        )
        if not model_to_finetune:
            return None
    return model_to_finetune
Ejemplo n.º 11
0
async def test_train_nlu_success(
    rasa_app: SanicASGITestClient,
    default_stack_config: Text,
    default_nlu_data: Text,
    default_domain_path: Text,
    tmp_path: Path,
):
    domain_data = rasa.shared.utils.io.read_yaml_file(default_domain_path)
    config_data = rasa.shared.utils.io.read_yaml_file(default_stack_config)
    nlu_data = rasa.shared.utils.io.read_yaml_file(default_nlu_data)

    # combine all data into our payload
    payload = {
        key: val for d in [domain_data, config_data, nlu_data] for key, val in d.items()
    }

    data = StringIO()
    rasa.shared.utils.io.write_yaml(payload, data)

    _, response = await rasa_app.post(
        "/model/train",
        data=data.getvalue(),
        headers={"Content-type": rasa.server.YAML_CONTENT_TYPE},
    )
    assert response.status == 200

    # save model to temporary file
    model_path = str(tmp_path / "model.tar.gz")
    with open(model_path, "wb") as f:
        f.write(response.body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, "fingerprint.json"))
Ejemplo n.º 12
0
def test_train_stack_success(
    rasa_app,
    default_domain_path,
    default_stories_file,
    default_stack_config,
    default_nlu_data,
):
    with ExitStack() as stack:
        domain_file = stack.enter_context(open(default_domain_path))
        config_file = stack.enter_context(open(default_stack_config))
        stories_file = stack.enter_context(open(default_stories_file))
        nlu_file = stack.enter_context(open(default_nlu_data))

        payload = dict(
            domain=domain_file.read(),
            config=config_file.read(),
            stories=stories_file.read(),
            nlu=nlu_file.read(),
        )

    _, response = rasa_app.post("/model/train", json=payload)
    assert response.status == 200

    assert response.headers["filename"] is not None

    # save model to temporary file
    tempdir = tempfile.mkdtemp()
    model_path = os.path.join(tempdir, "model.tar.gz")
    with open(model_path, "wb") as f:
        f.write(response.body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, "fingerprint.json"))
Ejemplo n.º 13
0
    def load_local_model(
        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":
        if os.path.isfile(model_path):
            model_archive = model_path
        else:
            model_archive = get_latest_model(model_path)

        if model_archive is None:
            logger.warning(
                "Could not load local model in '{}'".format(model_path))
            return Agent()

        working_directory = tempfile.mkdtemp()
        unpacked_model = unpack_model(model_archive, working_directory)

        return Agent.load(
            unpacked_model,
            interpreter=interpreter,
            generator=generator,
            tracker_store=tracker_store,
            action_endpoint=action_endpoint,
            model_server=model_server,
            remote_storage=remote_storage,
        )
Ejemplo n.º 14
0
def test_train_with_retrieval_events_success(rasa_app, default_stack_config):
    with ExitStack() as stack:
        domain_file = stack.enter_context(
            open("data/test_domains/default_retrieval_intents.yml"))
        config_file = stack.enter_context(open(default_stack_config))
        core_file = stack.enter_context(
            open("data/test_stories/stories_retrieval_intents.md"))
        responses_file = stack.enter_context(
            open("data/test_responses/default.md"))
        nlu_file = stack.enter_context(
            open("data/test_nlu/default_retrieval_intents.md"))

        payload = dict(
            domain=domain_file.read(),
            config=config_file.read(),
            stories=core_file.read(),
            responses=responses_file.read(),
            nlu=nlu_file.read(),
        )

    _, response = rasa_app.post("/model/train", json=payload)
    assert response.status == 200

    # save model to temporary file
    tempdir = tempfile.mkdtemp()
    model_path = os.path.join(tempdir, "model.tar.gz")
    with open(model_path, "wb") as f:
        f.write(response.body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, "fingerprint.json"))
Ejemplo n.º 15
0
def test_train_core_success(
    rasa_app: SanicTestClient,
    default_stack_config: Text,
    default_stories_file: Text,
    default_domain_path: Text,
    tmp_path: Path,
):
    with ExitStack() as stack:
        domain_file = stack.enter_context(open(default_domain_path))
        config_file = stack.enter_context(open(default_stack_config))
        core_file = stack.enter_context(open(default_stories_file))

        payload = dict(
            domain=domain_file.read(),
            config=config_file.read(),
            stories=core_file.read(),
        )

    _, response = rasa_app.post("/model/train", json=payload)
    assert response.status == 200

    # save model to temporary file
    model_path = str(tmp_path / "model.tar.gz")
    with open(model_path, "wb") as f:
        f.write(response.body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, "fingerprint.json"))
Ejemplo n.º 16
0
 def _load_model(self, model_name):
     model_path = os.path.join("models", model_name)
     tempdir = tempfile.mkdtemp()
     unpacked_model = unpack_model(model_path, tempdir)
     _, nlu_model = get_model_subdirectories(unpacked_model)
     with self.lock:
         interpreter = Interpreter.load(nlu_model, self.component_builder)
     
     return interpreter
Ejemplo n.º 17
0
def _interpreter_from_previous_model(
    old_model_zip_path: Optional[Text],
) -> Optional[NaturalLanguageInterpreter]:
    if not old_model_zip_path:
        return None

    with model.unpack_model(old_model_zip_path) as unpacked:
        _, old_nlu = model.get_model_subdirectories(unpacked)
        return rasa.core.interpreter.create_interpreter(old_nlu)
Ejemplo n.º 18
0
def assert_trained_model(response_body: bytes, tmp_path: Path) -> None:
    # save model to temporary file
    model_path = str(tmp_path / "model.tar.gz")
    with open(model_path, "wb") as f:
        f.write(response_body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, "fingerprint.json"))
Ejemplo n.º 19
0
def assert_trained_model(response_body: bytes) -> None:
    # save model to temporary file
    tempdir = tempfile.mkdtemp()
    model_path = os.path.join(tempdir, "model.tar.gz")
    with open(model_path, "wb") as f:
        f.write(response_body)

    # unpack model and ensure fingerprint is present
    model_path = unpack_model(model_path)
    assert os.path.exists(os.path.join(model_path, "fingerprint.json"))
Ejemplo n.º 20
0
def perform_interactive_learning(args, zipped_model):
    from rasa.core.train import do_interactive_learning

    if zipped_model:
        model_path = model.unpack_model(zipped_model)
        args.core, args.nlu = model.get_model_subdirectories(model_path)
        stories_directory = data.get_core_directory(args.data)

        do_interactive_learning(args, stories_directory)

        shutil.rmtree(model_path)
    else:
        print_warning("No initial zipped trained model found.")
Ejemplo n.º 21
0
def interactive(args: argparse.Namespace):
    from rasa.core.train import do_interactive_learning

    args.finetune = False  # Don't support finetuning

    zipped_model = train.train(args)
    model_path = model.unpack_model(zipped_model)
    args.core, args.nlu = model.get_model_subdirectories(model_path)
    stories_directory = data.get_core_directory(args.data)

    do_interactive_learning(args, stories_directory)

    shutil.rmtree(model_path)
Ejemplo n.º 22
0
def run_nlu(args: argparse.Namespace):
    import rasa_nlu.server
    import tempfile

    args.model = get_validated_path(args.path, "path", DEFAULT_MODELS_PATH)

    model_archive = get_latest_model(args.model)
    working_directory = tempfile.mkdtemp()
    unpacked_model = model.unpack_model(model_archive, working_directory)
    args.path = os.path.dirname(unpacked_model)

    rasa_nlu.server.main(args)

    shutil.rmtree(unpacked_model)
Ejemplo n.º 23
0
def perform_interactive_learning(args, zipped_model):
    from rasa.core.train import do_interactive_learning

    if zipped_model and os.path.exists(zipped_model):
        args.model = zipped_model

        with model.unpack_model(zipped_model) as model_path:
            args.core, args.nlu = model.get_model_subdirectories(model_path)
            stories_directory = data.get_core_directory(args.data)

            do_interactive_learning(args, stories_directory)
    else:
        print_error(
            "Interactive learning process cannot be started as no initial model was "
            "found.  Use 'rasa train' to train a model.")
Ejemplo n.º 24
0
def perform_interactive_learning(args: argparse.Namespace, zipped_model: Text,
                                 file_importer: TrainingDataImporter) -> None:
    from rasa.core.train import do_interactive_learning

    args.model = zipped_model

    with model.unpack_model(zipped_model) as model_path:
        args.core, args.nlu = model.get_model_subdirectories(model_path)
        if args.core is None:
            utils.print_error_and_exit(
                "Can not run interactive learning on an NLU-only model.")

        args.endpoints = utils.get_validated_path(args.endpoints, "endpoints",
                                                  DEFAULT_ENDPOINTS_PATH, True)

        do_interactive_learning(args, file_importer)
Ejemplo n.º 25
0
def perform_interactive_learning(args, zipped_model):
    from rasa.core.train import do_interactive_learning

    if zipped_model and os.path.exists(zipped_model):
        args.model = zipped_model
        model_path = model.unpack_model(zipped_model)
        args.core, args.nlu = model.get_model_subdirectories(model_path)
        stories_directory = data.get_core_directory(args.data)

        do_interactive_learning(args, stories_directory)

        shutil.rmtree(model_path)
    else:
        print_error(
            "No initial zipped trained model found. Interactive learning process "
            "cannot be started.")
Ejemplo n.º 26
0
async def test_multi_project_training(trained_async):
    example_directory = "data/test_multi_domain"
    config_file = os.path.join(example_directory, "config.yml")
    domain_file = os.path.join(example_directory, "domain.yml")
    files_of_root_project = os.path.join(example_directory, "data")

    trained_stack_model_path = await trained_async(
        config=config_file,
        domain=domain_file,
        training_files=files_of_root_project,
        force_training=True,
        persist_nlu_training_data=True,
    )

    unpacked = model.unpack_model(trained_stack_model_path)

    domain_file = os.path.join(
        unpacked, DEFAULT_CORE_SUBDIRECTORY_NAME, DEFAULT_DOMAIN_PATH
    )
    domain = Domain.load(domain_file)

    expected_intents = {
        "greet",
        "goodbye",
        "affirm",
        "deny",
        "mood_great",
        "mood_unhappy",
    }

    assert all([i in domain.intents for i in expected_intents])

    nlu_training_data_file = os.path.join(unpacked, "nlu", "training_data.json")
    nlu_training_data = RasaReader().read(nlu_training_data_file)

    assert expected_intents == nlu_training_data.intents

    expected_actions = [
        "utter_greet",
        "utter_cheer_up",
        "utter_did_that_help",
        "utter_happy",
        "utter_goodbye",
    ]

    assert all([a in domain.action_names for a in expected_actions])
Ejemplo n.º 27
0
async def test_update_with_new_domain(trained_rasa_model: Text, tmpdir: Path):
    _ = model.unpack_model(trained_rasa_model, tmpdir)

    new_domain = Domain.empty()

    mocked_importer = Mock()

    async def get_domain() -> Domain:
        return new_domain

    mocked_importer.get_domain = get_domain

    await model.update_model_with_new_domain(mocked_importer, tmpdir)

    actual = Domain.load(tmpdir / DEFAULT_CORE_SUBDIRECTORY_NAME / DEFAULT_DOMAIN_PATH)

    assert actual.is_empty()
Ejemplo n.º 28
0
async def nlu_model_and_evaluation_files_from_archive(
        zipped_model_path: Text, directory: Text) -> Tuple[Text, List[Text]]:
    """Extract NLU model path and intent evaluation files zipped model.

    Returns a tuple containing the path to the nlu model and a list
    of paths to evaluation files.
    """

    # unzip and return NLU evaluation files contained in it
    unzipped_path = unpack_model(zipped_model_path, directory)

    # cast `unzipped_path` as str for py3.5 compatibility
    unzipped_path = str(unzipped_path)

    model_path = os.path.join(unzipped_path, "nlu")
    nlu_files = await find_nlu_files_in_path(unzipped_path)

    return model_path, nlu_files
Ejemplo n.º 29
0
    def load_local_model(dir: Text, component_builder: ComponentBuilder) -> "NLUModel":
        if os.path.isfile(dir):
            model_archive = dir
        else:
            model_archive = get_latest_model(dir)

        if model_archive is None:
            logger.warning("Could not load local model in '{}'".format(dir))
            return NLUModel.fallback_model(component_builder)

        working_directory = tempfile.mkdtemp()
        unpacked_model = model.unpack_model(model_archive, working_directory)
        _, nlu_model = model.get_model_subdirectories(unpacked_model)

        model_path = nlu_model if os.path.exists(nlu_model) else unpacked_model

        name = os.path.basename(model_archive)
        interpreter = interpreter_for_model(component_builder, model_path)

        return NLUModel(name, interpreter, model_path)
Ejemplo n.º 30
0
async def test_multi_skill_training():
    example_directory = "data/test_multi_domain"
    config_file = os.path.join(example_directory, "config.yml")
    trained_stack_model_path = await train_async(
        config=config_file, domain=None, training_files=None
    )

    unpacked = model.unpack_model(trained_stack_model_path)
    model_fingerprint = model.fingerprint_from_path(unpacked)

    assert len(model_fingerprint["messages"]) == 2
    assert len(model_fingerprint["stories"]) == 2

    domain_file = os.path.join(unpacked, "core", "domain.yml")
    domain = Domain.load(domain_file)

    expected_intents = [
        "greet",
        "goodbye",
        "affirm",
        "deny",
        "mood_great",
        "mood_unhappy",
    ]

    assert all([i in domain.intents for i in expected_intents])

    expected_actions = [
        "utter_greet",
        "utter_cheer_up",
        "utter_did_that_help",
        "utter_happy",
        "utter_goodbye",
    ]

    assert all([a in domain.action_names for a in expected_actions])