示例#1
0
def command(languages: tuple, path: str):
    """
    Checks for differences in bots under the same multi-lingual
    structure.
    Args:
        languages: language code of the bots to be checked.
        path (str): optional argument stating the root directory of the bot.
    """
    languages_paths = get_all_languages(path=path, languages=languages)
    comparisons_list = itertools.combinations(languages_paths, 2)
    print_info(
        f"Starting diff comparison for the following bots: \n{languages_paths}\n\n ***   ***   ***\n"
    )
    for path_a, path_b in comparisons_list:
        domain_a_json = load_yaml(join(path_a, "domain.yml"))
        domain_b_json = load_yaml(join(path_b, "domain.yml"))
        print_info(
            f"Comparing {os.path.basename(path_a)} and {os.path.basename(path_b)} bots:\n ------"
        )
        is_diff = diff(domain_a_json, domain_b_json)
        print_info(f" ------\nEnd of {os.path.basename(path_a)} \
                   and {os.path.basename(path_b)} bots comparison.\n *** \n")
        if not is_diff:
            print_info(
                f"No differences found between {os.path.basename(path_a)} \
                       and {os.path.basename(path_b)} bots.\n")
    print_success("*** Difference comparison succeded! ***")
示例#2
0
def command():
    """
    Clean (remove) the package if this is available in the bot dir.
    """
    print_message("Cleaning the packaging...")
    with loading_indicator("Cleaning package files..."):
        if os.path.isdir(BUILD_DIR):
            shutil.rmtree(BUILD_DIR)
    print_success("Successfully cleaned")
示例#3
0
def init_project(path: str, languages: tuple) -> None:
    """
    Create the initial structure of the project.

    Args:
        path (str): path of the project given by the user
        languages (tuple): requested languages to be included in the initial project
    """
    create_initial_project(path, languages)
    print_success(f"Created project directory at '{abspath(path)}'.")
示例#4
0
def command():
    """
    Close the current session in the ROBO.AI platform.
    """
    settings = ToolSettings()
    current_environment = settings.get_current_environment()
    current_environment.api_auth_token = None
    current_environment.api_key = None
    settings.update_environment(current_environment)
    settings.set_current_environment(None)
    print_success('Your session was successfully terminated.\n')
示例#5
0
def coref_resolution_setup(path: str) -> None:
    """
    Setup coreference resolution module into the bot pipeline.

    Args:
        path (str): path of the project given by the user.
    """
    folder_list = [
        join(path, "languages", folder)
        for folder in os.listdir(join(path, "languages"))
        if os.path.isdir(os.path.join(path, "languages", folder))
    ]
    if join(path, "languages", "en") in folder_list:
        en_path = join(path, "languages", "en")
        # add coref resolution to config.yml
        cfg_yaml = load_yaml(join(en_path, "config.yml"))
        components_list = cfg_yaml["pipeline"]
        # remove whitespacetokenizer from the pipeline
        whitespace_idx = get_index(components_list, "WhitespaceTokenizer")
        components_list.pop(whitespace_idx)
        # add spacy model and spacy tokenizer
        components_list.insert(
            0,
            {
                "name":
                "custom.components.spacy_nlp.\
                                   spacy_tokenizer_neuralcoref.SpacyTokenizerNeuralCoref"
            },
        )
        components_list.insert(
            0,
            {
                "name": "custom.components.spacy_nlp.\
                                   spacy_nlp_neuralcoref.SpacyNLPNeuralCoref",
                "model": "en_neuralcoref",
            },
        )
        cfg_yaml["pipeline"] = components_list
        save_yaml(path=join(en_path, "config.yml"), yaml_dict=cfg_yaml)
        print_success(
            "Coreference resolution model successfully integrated into 'en' bot!"
        )
    # add requirements to requirements.txt
    write_requirements(
        join(path, "requirements.txt"),
        [
            "neuralcoref",
            "en-neuralcoref",
            "en-core-web-sm @ https://github.com/explosion/spacy-models/releases/\
                       download/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz",
        ],
    )
示例#6
0
def _ask_overwrite(path: str) -> None:
    """
    Inform the user the given path is not empty and if it still shoudl be used to proceed with the initial project structure creation.
    In case the answer is negative the user is informed a different path should be chosen.

    Args
        path (str): path of the project given by the user
    """
    overwrite = click.confirm(f"Directory '{path}' is not empty. Continue?")
    if not overwrite:
        print_success(
            "To proceed with a fresh bot, "
            "please choose a different installation directory and then rerun 'roboai seed'"
        )
        exit(0)
示例#7
0
def activate_environment(settings, env_name):
    env_name = env_name[0]
    environment = settings.get_environment(env_name)
    if not environment:
        print_error(
            "The environment you're trying to activate is not configured yet.\n"
        )
        exit(0)
    verify_endpoint_validity(environment.base_url,
                             environment.api_auth_setting.username,
                             environment.api_auth_setting.password,
                             environment)
    settings.set_current_environment(environment)
    print_success(
        f"The connection to the {env_name} environment was successfully established."
    )
示例#8
0
def command(language: tuple, skip_packaging: bool, package_file: str,
            bot_uuid: str, runtime_base_version: str, model: str):
    """
    Deploy a bot into the ROBO.AI platform.

    Args:
        language: language code of the bot to be deployed
        skip_packaging (bool): optional flag indicating whether packaging should be skipped
        package_file (str): optional flag stating where the package file is stored
        bot_uuid (str): optional argument stating the ID of the bot
        runtime_base_version (str): optional argument stating the runtime version
        model (str): optional argument with the path to the model to be packaged.
                    If no model is passed then the latest one is picked up.
    """
    validate_robo_session()

    if len(language) == 0:
        bot_dir = bot_ignore_dir = abspath(".")
    elif len(language) == 1:
        bot_dir = abspath(join(".", "languages", language[0]))
        bot_ignore_dir = dirname(dirname(bot_dir))
    else:
        print_message("Please select only one bot to deploy.\n")
        exit(0)

    if not bot_uuid:
        bot_uuid = get_current_bot_uuid(bot_dir)

    if not runtime_base_version:
        runtime_base_version = get_current_bot_base_version(bot_dir)

    if package_file:
        validate_package_file(package_file)
    elif not skip_packaging:
        package_file = create_package(bot_dir, bot_ignore_dir, model)
    elif not package_file:
        package_file = get_default_package_path()

    validate_package_file(package_file)

    if does_the_runtime_exist(bot_uuid):
        update_runtime(bot_uuid, package_file, runtime_base_version)
    else:
        create_runtime(bot_uuid, package_file, runtime_base_version)

    print_success("Deployment complete.\n")
示例#9
0
def create_environment(settings, new_env_name, base_url, username, password):
    new_env_name = new_env_name[0]
    verify_endpoint_validity(base_url, username, password)

    settings = ToolSettings()
    if username and password:
        env_auth = EnvironmentAuth(username, password)
    else:
        env_auth = None
    environment = Environment(environment_name=new_env_name,
                              base_url=base_url,
                              api_auth_setting=env_auth)
    settings.update_environment(environment)

    print_success(
        f"The environment was successfully created.\nYou can now activate it by running "
        f"'roboai environment activate {new_env_name}'.\n")
示例#10
0
def _ask_create_path(path: str) -> None:
    """
    Inform the user the given path is non-existant and if it should be created to proceed with the initial project structure creation.
    In case the answer is negative the user is informed a different path should be chosen.

    Args:
        path (str): path of the project given by the user
    """
    should_create = click.confirm(
        f"Path '{path}' does not exist 🧐. Create path?")
    if should_create:
        os.makedirs(path)
    else:
        print_success(
            "To proceed with a fresh bot, "
            "please choose a different installation directory and then rerun 'roboai seed'"
        )
        exit(0)
示例#11
0
def command(
    language: tuple,
    bot_uuid: str,
    target_dir: str,
    base_version: str = "rasa-1.10.0",
):
    """
    Connect a local bot to a ROBO.AI server bot instance.
    This instance must be already created in the ROBO.AI platform.
    This command will generate a JSON file (robo-manifest) with metadata about the bot to be deployed.

    Args:
        language: language code of the bot to be connected.
        bot_uuid (str): optional argument stating the ID of the bot.
        target_dir (str): optional argument stating where the robo-manifest file should be stored.
        base_version (str): optional argument stating the engine base version. Defaults to 'rasa-1.10.0'
    """
    validate_robo_session()

    if not bot_uuid:
        bot_uuid = get_bot_uuid()

    # validate_bot(bot_uuid)

    if len(language) == 0:
        bot_dir = bot_ignore_dir = (os.path.abspath(target_dir)
                                    if target_dir else
                                    create_implementation_directory(bot_uuid))
    elif len(language) == 1:
        bot_dir = (os.path.abspath(join(target_dir, "languages", language[0]))
                   if target_dir else create_implementation_directory(bot_uuid)
                   )  # TODO check what this does
        bot_ignore_dir = join(dirname(dirname(bot_dir)))
    else:
        print_message("Please select only one bot to connect to.")
        exit(0)

    print_message("Bot target directory: {0}".format(bot_dir))

    create_bot_manifest(bot_dir, bot_uuid, base_version)
    create_bot_ignore(bot_ignore_dir)
    print_success("The bot was successfully initialized.")
示例#12
0
def chit_chat_setup(path: str) -> None:
    """
    Setup the cit-chat module.

    Args:
        path (str): path of the project given by the user.
    """
    folder_list = [
        join(path, "languages", folder)
        for folder in os.listdir(join(path, "languages"))
        if os.path.isdir(os.path.join(path, "languages", folder))
    ]
    if join(path, "languages", "en") in folder_list:
        en_path = join(path, "languages", "en")
        # add chitchat fallback action in config.yml
        cfg_yaml = load_yaml(join(en_path, "config.yml"))
        policies_list = cfg_yaml["policies"]
        policies_list.append({
            "name": "FallbackPolicy",
            "nlu_threshold": 0.55,
            "core_threshold": 0.3,
            "fallback_action_name": "action_parlai_fallback",
        })
        cfg_yaml["policies"] = policies_list
        save_yaml(path=join(en_path, "config.yml"), yaml_dict=cfg_yaml)
        # add chitchat fallback action and nedded slots in domain.yml
        domain_yaml = load_yaml(join(en_path, "domain.yml"))
        domain_yaml["actions"] = ["action_parlai_fallback"]
        slots = domain_yaml["slots"]
        slots.update({
            "parlai_world_created": {
                "type": "bool",
                "initial_value": False
            },
            "parlai_world": {
                "type": "text"
            }
        })
        domain_yaml["slots"] = slots
        save_yaml(path=join(en_path, "domain.yml"), yaml_dict=domain_yaml)
        print_success(
            "Chit-chat fallback action successfully integrated into 'en' bot!")
示例#13
0
def language_detection_setup(path: str) -> None:
    """
    Setup language detection module.

    Args:
        path (str): path of the project given by the user.
    """

    # include command in config yaml pipeline
    folder_list = [
        join(path, "languages", folder)
        for folder in os.listdir(join(path, "languages"))
        if os.path.isdir(os.path.join(path, "languages", folder))
    ]
    for folder in folder_list:
        # add Language Detection Policy as last Policy in config.yml
        cfg_yaml = load_yaml(join(folder, "config.yml"))
        policies_list = cfg_yaml["policies"]
        policies_list.append({
            "name":
            "custom.policies.language_detection.lang_change_policy.LangChangePolicy",
            "lang_detect_threshold":
            0.8,
            "fallback_action_name":
            "utter_bot_languages",
            "model_path":
            "./custom/policies/language_detection/lid.176.ftz",
        })
        cfg_yaml["policies"] = policies_list
        save_yaml(path=join(folder, "config.yml"), yaml_dict=cfg_yaml)
        # add utter example for Labguage Detection Policy fallback in domain.yml
        domain_yaml = load_yaml(join(folder, "domain.yml"))
        responses_dict = domain_yaml["responses"]
        responses_dict["utter_bot_languages"] = [{
            "text":
            "Do you want to speak with me in another language?"
        }]
        domain_yaml["responses"] = responses_dict
        save_yaml(path=join(folder, "domain.yml"), yaml_dict=domain_yaml)
        # add requirements to requirements.txt
    write_requirements(join(path, "requirements.txt"), ["fasttext==0.9.2"])
    print_success("Language detection successfully integrated!")
示例#14
0
def command(language: tuple, model: str):
    """

    Package the required bot and make it ready for deployment.

    Args:
        language (tuple): language code of the bot to create a package of.
    """
    if len(language) == 0:
        bot_dir = bot_ignore_dir = abspath(".")
    elif len(language) == 1:
        bot_dir = abspath(join(".", "languages", language[0]))
        bot_ignore_dir = dirname(dirname(bot_dir))
    else:
        print_message("Please select only one bot to package.")
        exit(0)

    package_path = create_package(bot_dir, bot_ignore_dir, model)
    print_message("Package file: {0}".format(package_path))
    print_success("The packaging is complete.\n")
示例#15
0
def command(api_key: str):
    """
    Initialize a new session using a ROBO.AI API key,

    Args:
        api_key (str): API key
    """
    settings = ToolSettings()
    current_environment = settings.get_current_environment(
    )  # this should be an object now
    robo = get_robo_client(current_environment)
    try:
        with loading_indicator('Authenticating...'):
            tokens = robo.oauth.authenticate(api_key)

        current_environment.api_key = api_key
        current_environment.api_auth_token = tokens.access_token
        settings.update_environment(current_environment)
        print_success('Successfully authenticated!\n')

    except InvalidCredentialsError:
        print_error('The API key is invalid.\n')
示例#16
0
def command(language: tuple, bot_uuid: str):
    """
    Remove a deployed bot from the ROBO.AI platform

    Args:
        language (tuple): language code of the bot to be removed
        bot_uuid (str): optional argument stating the ID of the bot to be removed
    """
    validate_robo_session()
    if len(language) == 0:
        bot_dir = abspath(".")
    elif len(language) == 1:
        bot_dir = abspath(join(".", "languages", language[0]))
    else:
        print_message("Please select only one bot runtime to be removed.\n")
        exit(0)
    if not bot_uuid:
        bot_uuid = get_current_bot_uuid(bot_dir)

    # validate_bot(bot_uuid)
    remove_runtime(bot_uuid)
    print_success("The bot runtime was successfully removed.\n")
示例#17
0
def command(language: tuple, bot_uuid: str):
    """
    Stop a bot running in the ROBO.AI platform.

    Args:
        language: language code of the bot to be stopped
        bot_uuid (str): optional argument stating the bot ID to be stopped.
    """
    validate_robo_session()

    if len(language) == 0:
        bot_dir = abspath(".")
    elif len(language) == 1:
        bot_dir = abspath(join(".", "languages", language[0]))
    else:
        print_message("Please select only one bot runtime to be stopped.\n")
        exit(0)

    if not bot_uuid:
        bot_uuid = get_current_bot_uuid(bot_dir)

    # validate_bot(bot_uuid)
    stop_runtime(bot_uuid)
    print_success("The bot runtime was successfully stopped.\n")
示例#18
0
def remove_environment(settings, env_name):
    env_name = env_name[0]
    settings.remove_environment(env_name)
    print_success(f"'{env_name}' has been removed successfully.")