Пример #1
0
def generate_stories(languages_path: list, multi_language_bot: bool) -> None:
    for language in languages_path:
        lang = basename(language) if basename(language) != "bot" else "the"

        if multi_language_bot:
            stories_dir_path = join(abspath("."), "languages")
        else:
            stories_dir_path = join(language, "data")

        stories_exist = False
        # check if there's already a stories file - ask if it should be overriden?
        for filename in listdir(join(stories_dir_path)):
            if isfile(join(stories_dir_path, filename)):
                if "stories" in filename:
                    stories_exist = True
                    break
        if stories_exist:
            overwrite = click.confirm(
                "Stories file already exists. This action will overwrite it. Continue?"
            )
            if overwrite:
                generate_stories_md(language, multi_language_bot)
            else:
                exit(0)
        else:
            generate_stories_md(language, multi_language_bot)

        print_info(f"Generated stories for {lang} bot")
Пример #2
0
def _inform_language() -> None:
    """
    Inform the user no languages were passed when executing the train command.
    """
    print_info(
        "No language was provided. Will train all available languages inside provided bot folder."
    )
Пример #3
0
def _inform_language() -> None:
    """
    Auxiliary method to inform the user no languages were passed when executing the test command.
    """
    print_info("No language was provided but a multi-language bot was detected. "
               "Please choose one of the bots for the stories to be generated.\n")
    exit(0)
Пример #4
0
def command(language: tuple, bot_uuid: str):
    """
    display selected bot runtime logs.

    Args:
        language: language code of the bot to get logs from
        bot_uuid (str): optional argument stating the ID of the bot to get logs from
    """
    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 to check the logs.')
        exit(0)

    if not bot_uuid:
        bot_uuid = get_current_bot_uuid(bot_dir)

    # validate_bot(bot_uuid)

    with loading_indicator('Fetching logs from server...'):
        logs = get_runtime_logs(bot_uuid)

    print_info("Displaying logs for bot '{0}'\n".format(bot_uuid))
    print_message(logs + "\n")
Пример #5
0
def _inform_language() -> None:
    """
    Auxiliary method to inform the user no languages were passed when executing the test command.
    """
    print_info(
        "No language was provided but a multi-language bot was detected. "
        "Will test all available languages inside provided bot folder.\n"
    )
Пример #6
0
def which_environment(settings):
    current_environment = settings.get_current_environment()
    if not current_environment:
        print_info(
            "No environment is currently activated.\nRun 'roboai environment activate <env-name>' to activate"
            "an environment.")
    else:
        print_info(
            f"The '{current_environment.environment_name}' environment is currently activated."
        )
Пример #7
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! ***")
Пример #8
0
def test(languages_path: list, multi_language_bot: bool, cross_validation: bool, folds: int) -> None:
    timestamp = datetime.now().strftime("%d%m%Y-%H%M%S")
    for language in languages_path:
        makedirs(join(language, "results", timestamp), exist_ok=True)
        lang = basename(language) if basename(language) != "bot" else "the"
        print_info(f"Starting test process for {lang} bot")
        # Check if tests folder exists
        if exists(join(language, "tests")):
            # Check if tests folder contains any file
            if any(
                isfile(join(language, "tests", i))
                for i in listdir(join(language, "tests"))
            ):
                # Check if intents are already covered in the existing test files
                # If there are intents left to be tested, the user is prompted
                # with an option to continue testing
                if check_covered_intents(language):
                    test_bot(language, cross_validation, folds, timestamp)
                else:
                    continue
            # If tests folder is empty, generate a test stories file
            else:
                generate_conversation_md_from_stories(
                    language, multi_language_bot
                )
                if proceed_with_test(
                    "Test stories have been generated. Continue testing?\n"
                ):
                    test_bot(language, cross_validation, folds, timestamp)
                else:
                    continue
        # If tests folder doesn't exist, create it and generate a test stories file
        else:
            generate_conversation_md_from_stories(language, multi_language_bot)
            if proceed_with_test(
                "Test stories have been generated. Continue testing?\n"
            ):
                test_bot(language, cross_validation, folds, timestamp)
            else:
                continue
        format_results(language, timestamp)
        print_info(f"Finished testing {lang} bot")
Пример #9
0
def get_all_languages(path: str, languages: tuple):
    if len(languages) == 0:
        print_info("No language was provided. Will identify differences for \
                   all available language pairs inside provided bot folder.")
        languages_paths = [
            join(path, "languages", folder)
            for folder in os.listdir(join(path, "languages"))
            if os.path.isdir(os.path.join(path, "languages", folder))
        ]
    elif len(languages) == 1:
        print_info(
            "Unable to provide differences: at least two languages must be provided."
        )
        exit(0)
    else:
        languages_paths = [
            join(path, "languages", folder)
            for folder in os.listdir(join(path, "languages"))
            if os.path.isdir(os.path.join(path, "languages", folder))
            and folder in languages
        ]
    return languages_paths
Пример #10
0
def diff(domain_a: dict, domain_b: dict):
    def print_diff_list(first_list, second_list):
        sys.stdout.writelines(
            difflib.unified_diff([
                item +
                "\n" if isinstance(item, str) else list(item.keys())[0] + "\n"
                for item in first_list
            ], [
                item +
                "\n" if isinstance(item, str) else list(item.keys())[0] + "\n"
                for item in second_list
            ]))

    def print_diff_keys(first_dict, second_dict):
        sys.stdout.writelines(
            difflib.unified_diff([key + "\n" for key in first_dict.keys()],
                                 [key + "\n" for key in second_dict.keys()]))

    def print_diff_items(first_dict, second_dict):
        sys.stdout.writelines(
            difflib.unified_diff(
                [str(item) + "\n" for item in first_dict.items()],
                [str(item) + "\n" for item in second_dict.items()]))

    # --- all comparisons are explicit to enable future exception handeling if needed --- #
    # Compare the intents:
    domain_diff_q = False
    if domain_a.get("intents", None) != domain_b.get("intents", None):
        domain_diff_q = True
        print_info(" -- The intents differ by:")
        print_diff_list(domain_a.get("intents", []),
                        domain_b.get("intents", []))
    # Compare the actions:
    if domain_a.get("actions", None) != domain_b.get("actions", None):
        domain_diff_q = True
        print_info(" -- The actions differ by:")
        print_diff_list(domain_a.get("actions", []),
                        domain_b.get("actions", []))
    # Compare the forms:
    if domain_a.get("forms", None) != domain_b.get("forms", None):
        domain_diff_q = True
        print_info(" -- The forms differ by:")
        print_diff_list(domain_a.get("forms", []), domain_b.get("forms", []))
    # Compare the entities:
    if domain_a.get("entities", None) != domain_b.get("entities", None):
        domain_diff_q = True
        print_info(" -- The entities differ by:")
        print_diff_list(domain_a.get("entities", []),
                        domain_b.get("entities", []))
    # Compare the slots
    if domain_a.get("slots", None) != domain_b.get("slots", None):
        domain_diff_q = True
        print_info(" -- The slots differ by:")
        print_diff_keys(domain_a["slots"], domain_b["slots"])
    # Compare the responses
    if domain_a.get("responses", None) != domain_b.get("responses", None):
        domain_diff_q = True
        print_info(" -- The responses differ by:")
        print_diff_keys(domain_a["responses"], domain_b["responses"])
    # Compare session config
    if domain_a.get("session_config", None) != domain_b.get(
            "session_config", None):
        domain_diff_q = True
        print_info(" -- The session_config differ by:")
        print_diff_items(domain_a["session_config"],
                         domain_b["session_config"])

    return domain_diff_q
Пример #11
0
def _inform_language() -> None:
    """
    Inform the user no languages were passed when executing the seed command.
    """
    print_info("No language was provided. English will be the default.")