示例#1
0
    async def load_model(request: Request):
        """Loads a zipped model, replacing the existing one."""

        if "model" not in request.files:
            # model file is missing
            raise ErrorResponse(
                400,
                "InvalidParameter",
                "You did not supply a model as part of your request.",
                {"parameter": "model", "in": "body"},
            )

        model_file = request.files["model"]

        logger.info("Received new model through REST interface.")
        zipped_path = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
        zipped_path.close()
        model_directory = tempfile.mkdtemp()

        model_file.save(zipped_path.name)

        logger.debug("Downloaded model to {}".format(zipped_path.name))

        zip_ref = zipfile.ZipFile(zipped_path.name, "r")
        zip_ref.extractall(model_directory)
        zip_ref.close()
        logger.debug("Unzipped model to {}".format(os.path.abspath(model_directory)))

        domain_path = os.path.join(os.path.abspath(model_directory), "domain.yml")
        domain = Domain.load(domain_path)
        ensemble = PolicyEnsemble.load(model_directory)
        app.agent.update_model(domain, ensemble, None)
        logger.debug("Finished loading new agent.")
        return response.text("", 204)
示例#2
0
def load(config_file: Optional[Text]) -> List['Policy']:
    """Load policy data stored in the specified file."""
    from rasa.core.policies import PolicyEnsemble

    if config_file and os.path.isfile(config_file):
        config_data = rasa.utils.io.read_yaml_file(config_file)
    else:
        raise ValueError("You have to provide a valid path to a config file. "
                         "The file '{}' could not be found."
                         "".format(os.path.abspath(config_file)))

    return PolicyEnsemble.from_dict(config_data)