diff --git a/README.md b/README.md index 837ecec..095f9f4 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ It comes with 13 reliable mutually exclusive control messages that can be record * CONTROL_SKIP_QUESTION * CONTROL_REQUEST_THE_CURRENT_MODE * CONTROL_ENTER_THE_NORMAL_MODE -* CONTROL_ENTER_THE_CONVERSATIONAL_MODE +* CONTROL_ENTER_THE_CONVERSATION_MODE * CONTROL_ENTER_THE_EXPERT_MODE -* CONTROL_ENTER_THE_FOLLOW_UP_MODE +* CONTROL_ENTER_THE_FOLLOW_MODE ## Examples diff --git a/commbase_env.yaml b/commbase_env.yaml index 9dc5ff5..67cb342 100644 --- a/commbase_env.yaml +++ b/commbase_env.yaml @@ -16,10 +16,10 @@ dependencies: - brotli-bin=1.0.9=h5eee18b_7 - brotlipy=0.7.0=py311h5eee18b_1002 - bzip2=1.0.8=h7b6447c_0 - - ca-certificates=2023.05.30=h06a4308_0 + - ca-certificates=2023.08.22=h06a4308_0 - cairo=1.16.0=hb05425b_4 - cchardet=2.1.7=py311h6a678d5_0 - - certifi=2023.5.7=py311h06a4308_0 + - certifi=2023.7.22=py311h06a4308_0 - cffi=1.15.1=py311h5eee18b_3 - chardet=4.0.0=py311h06a4308_1003 - charset-normalizer=2.0.4=pyhd3eb1b0_0 @@ -53,6 +53,7 @@ dependencies: - hdf5=1.10.6=h3ffc7dd_1 - icu=58.2=he6710b0_3 - idna=3.4=py311h06a4308_0 + - iniconfig=1.1.1=pyhd3eb1b0_0 - joblib=1.2.0=py311h06a4308_0 - jpeg=9e=h5eee18b_1 - kiwisolver=1.4.4=py311h6a678d5_0 @@ -60,7 +61,9 @@ dependencies: - lame=3.100=h7b6447c_0 - lcms2=2.12=h3be6417_0 - ld_impl_linux-64=2.38=h1181459_1 + - leptonica=1.82.0=he027829_1 - lerc=3.0=h295c915_0 + - libarchive=3.6.2=hb4bd9a0_1 - libbrotlicommon=1.0.9=h5eee18b_7 - libbrotlidec=1.0.9=h5eee18b_7 - libbrotlienc=1.0.9=h5eee18b_7 @@ -111,7 +114,7 @@ dependencies: - openh264=2.1.1=h4ff587b_0 - openjpeg=2.4.0=h3ad879b_0 - openpyxl=3.0.10=py311h5eee18b_0 - - openssl=1.1.1t=h7f8727e_0 + - openssl=1.1.1w=h7f8727e_0 - packaging=23.0=py311h06a4308_0 - pandas=1.5.3=py311hba01205_0 - pandas-stubs=1.5.3.230203=py311h06a4308_0 @@ -120,6 +123,7 @@ dependencies: - pip=23.1.2=py311h06a4308_0 - pixman=0.40.0=h7f8727e_1 - plotly=5.9.0=py311h06a4308_0 + - pluggy=1.0.0=py311h06a4308_1 - pooch=1.4.0=pyhd3eb1b0_0 - portaudio=19.6.0=h7b6447c_4 - pyaudio=0.2.11=py311h5eee18b_2 @@ -128,6 +132,8 @@ dependencies: - pyopenssl=23.0.0=py311h06a4308_0 - pyparsing=3.0.9=py311h06a4308_0 - pysocks=1.7.1=py311h06a4308_0 + - pytesseract=0.3.10=py311h06a4308_0 + - pytest=7.4.0=py311h06a4308_0 - python=3.11.3=h7a1cb2a_0 - python-dateutil=2.8.2=pyhd3eb1b0_0 - pytz=2022.7=py311h06a4308_0 @@ -146,6 +152,7 @@ dependencies: - sqlite=3.41.2=h5eee18b_0 - tbb=2021.8.0=hdb19cb5_0 - tenacity=8.2.2=py311h06a4308_0 + - tesseract=5.2.0=h6a678d5_0 - threadpoolctl=2.2.0=pyh0d69192_0 - tk=8.6.12=h1ccaba5_0 - tqdm=4.65.0=py311h92b7b1e_0 diff --git a/commbase_stt_vosk_p.py b/commbase_stt_vosk_p.py index 919a184..db66076 100755 --- a/commbase_stt_vosk_p.py +++ b/commbase_stt_vosk_p.py @@ -1,8 +1,8 @@ #!/usr/bin/env python ################################################################################ -# commbase-stt-vosk # +# commbase-stt-vosk-p # # # -# Conversational AI Assistant and AI Hub for Computers and Droids # +# An ASR (Automatic Speech Recognition) engine. # # # # Change History # # 04/29/2023 Esteban Herrera Original code. # @@ -50,579 +50,691 @@ from config import CONFIG_FILE_PATH from file_paths import ( - get_ascii_art_file_path, - get_assistant_microphone_instruction_file, - get_control_accept_changes_patterns_file, - get_control_deny_changes_patterns_file, - get_control_enter_the_conversational_mode_patterns_file, - get_control_enter_the_expert_mode_patterns_file, - get_control_enter_the_follow_up_mode_patterns_file, - get_control_enter_the_normal_mode_patterns_file, - get_control_request_the_current_mode_patterns_file, - get_control_select_option_number_four_patterns_file, - get_control_select_option_number_one_patterns_file, - get_control_select_option_number_three_patterns_file, - get_control_select_option_number_two_patterns_file, - get_control_skip_question_patterns_file, - get_control_stop_previous_command_patterns_file, - get_previous_result_message_recording_file, - get_result_message_recording_file, - get_result_messages_history_file, - get_secrets_file_path, - get_vosk_ml_model_directory, + get_ascii_art_file_path, + get_assistant_microphone_instruction_file, + get_control_accept_changes_patterns_file, + get_control_deny_changes_patterns_file, + get_control_enter_the_conversation_mode_patterns_file, + get_control_enter_the_expert_mode_patterns_file, + get_control_enter_the_follow_mode_patterns_file, + get_control_enter_the_normal_mode_patterns_file, + get_control_request_the_current_mode_patterns_file, + get_control_select_option_number_four_patterns_file, + get_control_select_option_number_one_patterns_file, + get_control_select_option_number_three_patterns_file, + get_control_select_option_number_two_patterns_file, + get_control_skip_question_patterns_file, + get_control_stop_previous_command_patterns_file, + get_controller_message_recording_file, + get_previous_result_message_recording_file, + get_result_message_recording_file, + get_result_messages_history_file, + get_secrets_file_path, + get_vosk_ml_model_directory, ) from functions import ( - find_text, - get_chat_participant_names, - get_commbase_stt_vosk_p_parse_control_messages_on, - get_manage_result_message_on_and_output_skill_errors_in_pane_on, - get_tts_engine_string, - int_or_str, - read_lines_from_file, - read_plain_text_file, - strip_string, + find_text, + get_chat_participant_names, + get_commbase_stt_vosk_p_parse_control_messages_on, + get_manage_result_message_on_and_output_skill_errors_in_pane_on, + get_tts_engine_string, + int_or_str, + read_lines_from_file, + read_plain_text_file, + strip_string, ) from text_formatting import ( - get_assistant_avatar_color, - get_chat_participant_colors, - get_terminal_colors, - set_assistant_avatar_color, - set_assistant_user_background_color, - set_assistant_user_text_color, - set_end_user_background_color, - set_end_user_text_color, - set_system_user_background_color, - set_system_user_text_color, + get_assistant_avatar_color, + get_chat_participant_colors, + get_terminal_colors, + set_assistant_avatar_color, + set_assistant_user_background_color, + set_assistant_user_text_color, + set_end_user_background_color, + set_end_user_text_color, + set_system_user_background_color, + set_system_user_text_color, ) def commbase_stt_vosk_p(): - """ - Takes audio input, processes it, and outputs the recognized text. The + """ + Takes audio input, processes it, and outputs the recognized text. The recognized text is then cleaned up, and saved in file(s). - """ - # Assign the result of calling get_vosk_ml_model_directory() - vosk_ml_model_directory = get_vosk_ml_model_directory() - - # Define control messages - # To change a control command do not do that here; use its patterns file - # instead - CONTROL_STOP_PREVIOUS_COMMAND = "okay stop" - CONTROL_ACCEPT_CHANGES = "okay accept" - CONTROL_DENY_CHANGES = "okay deny" - CONTROL_SELECT_OPTION_NUMBER_ONE = "okay select the option number one" - CONTROL_SELECT_OPTION_NUMBER_TWO = "okay select the option number two" - CONTROL_SELECT_OPTION_NUMBER_THREE = "okay select the option number three" - CONTROL_SELECT_OPTION_NUMBER_FOUR = "okay select the option number four" - CONTROL_SKIP_QUESTION = "okay skip that question" - CONTROL_REQUEST_THE_CURRENT_MODE = "okay which mode are you in" - CONTROL_ENTER_THE_NORMAL_MODE = "okay enter the normal mode" - CONTROL_ENTER_THE_CONVERSATIONAL_MODE = "okay enter the conversational mode" - CONTROL_ENTER_THE_EXPERT_MODE = "okay enter the expert mode" - CONTROL_ENTER_THE_FOLLOW_UP_MODE = "okay enter the follow up mode" - - # q is used to store a Queue object, which is then used to keep track of the - # nodes that need to be visited during the breadth-first search algorithm. - q = queue.Queue() - - - def callback(indata, frames, time, status): - """ - This function is called (from a separate thread) for each audio block. - - This function puts the input data into a queue for processing. - If the status parameter is not None, it prints an error message to the - standard error stream. - - Parameters: - indata : numpy.ndarray - The input audio data as a NumPy array. - frames : int - The number of frames in the input data. - time : CData - A ctypes structure containing timing information about the input - data. - status : CallbackFlags - A flag that indicates the status of the input data. - - Returns: - None. - """ - if status: - print(status, file=sys.stderr) - q.put(bytes(indata)) - - - def display_assistant_avatar(): - """ - Displays the assistant avatar in the terminal. - - This function retrieves the ASCII art avatar of the assistant, assigns the - appropriate color based on the configured avatar color, and then prints it - to the terminal. - - Note: - The color codes used in the avatar display are obtained from the - `get_terminal_colors()` function. - The avatar color is obtained from the `get_assistant_avatar_color()` - function. - The ASCII art avatar is obtained from the `get_assistant_avatar()` - function. - - Example: - Terminal output: - - >>> display_assistant_avatar() - [COLOR CODES] ASCII ART [RESET] - """ - # Load an ASCII art file, store its content in a variable, and then print - # it in a specific color using terminal escape sequences. - assistant_avatar = read_plain_text_file(ascii_art_file_path) - print(f'\033[{avatar_color_start}\033[{assistant_avatar}\033[{color_code_end}') - - - def print_result(): - """ - Prints the result on the screen and write to files. - - Parameters: - None. - - Returns: - None. - """ - # The original result - string = rec.Result() - - # The modified result - trimmed_string = strip_string(string) - - # Check if the trimmed_string is not empty and control message parsing is - # enabled for commbase-stt-vosk-p engine - if trimmed_string != '' and commbase_stt_vosk_p_parse_control_messages_on == "True": - - # Define the control messages dictionary - control_dictionary = { - 'CONTROL_STOP_PREVIOUS_COMMAND': { - 'patterns': control_stop_previous_command_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_STOP_PREVIOUS_COMMAND}" for processing.' - }, - 'CONTROL_ACCEPT_CHANGES': { - 'patterns': control_accept_changes_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_ACCEPT_CHANGES}" for processing.' - }, - 'CONTROL_DENY_CHANGES': { - 'patterns': control_deny_changes_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_DENY_CHANGES}" for processing.' - }, - 'CONTROL_SELECT_OPTION_NUMBER_ONE': { - 'patterns': control_select_option_number_one_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_ONE}" for processing.' - }, - 'CONTROL_SELECT_OPTION_NUMBER_TWO': { - 'patterns': control_select_option_number_two_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_TWO}" for processing.' - }, - 'CONTROL_SELECT_OPTION_NUMBER_THREE': { - 'patterns': control_select_option_number_three_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_THREE}" for processing.' - }, - 'CONTROL_SELECT_OPTION_NUMBER_FOUR': { - 'patterns': control_select_option_number_four_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_FOUR}" for processing.' - }, - 'CONTROL_SKIP_QUESTION': { - 'patterns': control_skip_question_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_SKIP_QUESTION}" for processing.' - }, - 'CONTROL_REQUEST_THE_CURRENT_MODE': { - 'patterns': control_request_the_current_mode_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_REQUEST_THE_CURRENT_MODE}" for processing.' - }, - 'CONTROL_ENTER_THE_NORMAL_MODE': { - 'patterns': control_enter_the_normal_mode_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_NORMAL_MODE}" for processing.' - }, - 'CONTROL_ENTER_THE_CONVERSATIONAL_MODE': { - 'patterns': control_enter_the_conversational_mode_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_CONVERSATIONAL_MODE}" for processing.' - }, - 'CONTROL_ENTER_THE_EXPERT_MODE': { - 'patterns': control_enter_the_expert_mode_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_EXPERT_MODE}" for processing.' - }, - 'CONTROL_ENTER_THE_FOLLOW_UP_MODE': { - 'patterns': control_enter_the_follow_up_mode_patterns, - 'message': f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_FOLLOW_UP_MODE}" for processing.' - } - } - - # The control message and END USER message matching - found_match = False - - # Check if the trimmed_string is not empty - for control, info in control_dictionary.items(): - patterns = info['patterns'] # Get the patterns for the current control - message = info['message'] # Get the message for the current control - - for line in patterns: - if trimmed_string == line.strip(): # Check for exact match - # If a match is found, print the message and record the control - # message - print(f'\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} {message}\033[{color_code_end}') - # Create the JSON object - json_data = { - "control": control - } - # Record the control message string to result_message_recording_file - # as a JSON object. - with open(result_message_recording_file, 'w') as f: - json.dump(json_data, f) - found_match = True - if manage_result_message_on_and_output_skill_errors_in_pane_on == "True": - # Manage the result message - subprocess.run(['bash', os.environ["COMMBASE_APP_DIR"] + '/src/skill']) - break # Exit the inner loop if a match is found - - if found_match: - break # Exit the outer loop if a match is found - - # Handle the case when no match is found - if not found_match: - # Record a normal END USER message instead of a control message - print(f'\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} I am dispatching "{trimmed_string}" for processing.\033[{color_code_end}') - # Create the JSON object - json_data = { - "message": trimmed_string - } - # Record the control message string to result_message_recording_file - # as a JSON object. - with open(result_message_recording_file, 'w') as f: - json.dump(json_data, f) - with open(previous_result_message_recording_file, 'w') as f: - json.dump(json_data, f) - if manage_result_message_on_and_output_skill_errors_in_pane_on == "True": - # Manage the result message - subprocess.run(['bash', os.environ["COMMBASE_APP_DIR"] + '/src/skill']) - - # Check if the trimmed_string is not empty and control message parsing is - # disabled for commbase-stt-vosk-p engine - elif trimmed_string != '' and commbase_stt_vosk_p_parse_control_messages_on == "False": - print(f'\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} I am dispatching "{trimmed_string}" no controls for processing.\033[{color_code_end}') - # Create the JSON object - json_data = { - "message": trimmed_string - } - # Record the control message string to result_message_recording_file - # as a JSON object. - with open(result_message_recording_file, 'w') as f: - json.dump(json_data, f) - with open(previous_result_message_recording_file, 'w') as f: - json.dump(json_data, f) - if manage_result_message_on_and_output_skill_errors_in_pane_on == "True": - # Manage the result message - subprocess.run(['bash', os.environ["COMMBASE_APP_DIR"] + '/bundles/built-in/broker/libcommbase/libcommbase/controller/controller']) - - - # Create ArgumentParser object with add_help=False to disable default help - # message. - parser = argparse.ArgumentParser(add_help=False) - - # Add a command-line argument for listing audio devices - parser.add_argument( - '-l', '--list-devices', action='store_true', # store_true indicates that the argument does not require a value and sets it to True if present - help='show list of audio devices and exit' - ) - - # Parse the command-line arguments, with any unrecognized arguments returned - # as a list in 'remaining'. - args, remaining = parser.parse_known_args() - - # If the 'list-devices' argument was passed, print the list of audio devices - # and exit the program. - if args.list_devices: - print(sd.query_devices()) - parser.exit(0) # exit the program with status code 0 - - # create a new ArgumentParser object with a description and formatter - parser = argparse.ArgumentParser( - description=__doc__, # use the script's docstring as the description - formatter_class=argparse.RawDescriptionHelpFormatter, # use a raw formatter for the description - parents=[parser] # inherit the 'list-devices' argument from the previous parser - ) - - # Add additional arguments for specifying a filename, model path, input - # device, and sampling rate. - parser.add_argument( - '-f', '--filename', type=str, metavar='FILENAME', - help='audio file to store recording to' - ) - parser.add_argument( - '-m', '--model', type=str, metavar='MODEL_PATH', - help='Path to the model' - ) - parser.add_argument( - '-d', '--device', type=int_or_str, - help='input device (numeric ID or substring)' - ) - parser.add_argument( - '-r', '--samplerate', type=int, help='sampling rate' - ) - - # Parse the remaining command-line arguments - args = parser.parse_args(remaining) - - - try: - if args.model is None: - args.model = string.Template(vosk_ml_model_directory).substitute(os.environ) - if not os.path.exists(args.model): - print ("Please download a model for your language from https://alphacephei.com/vosk/models") - print ("and unpack as 'model' in the current folder.") - parser.exit(0) - if args.samplerate is None: - device_info = sd.query_devices(args.device, 'input') - # soundfile expects an int, sounddevice provides a float: - args.samplerate = int(device_info['default_samplerate']) - - model = vosk.Model(args.model) - - if args.filename: - dump_fn = open(args.filename, "wb") - else: - dump_fn = None - - with sd.RawInputStream(samplerate=args.samplerate, blocksize = 8000, device=args.device, dtype='int16', - channels=1, callback=callback): - # Assign functions imported from text_formatting - - # Set the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the values returned by get_chat_participant_colors() - end_user_background_color, assistant_background_color, system_background_color, end_user_text_color, assistant_text_color, system_text_color = get_chat_participant_colors() - - # Set the values returned by get_assistant_avatar_color() - avatar_color = get_assistant_avatar_color() - - # Set the background color for the end user - end_user_background_color_start = set_end_user_background_color(end_user_background_color) - - # Set the background color for the assistant user - assistant_background_color_start = set_assistant_user_background_color(assistant_background_color) - - # Set the background color for the system user - system_background_color_start = set_system_user_background_color(system_background_color) - - # Set the text color for the end user - end_user_text_color_start = set_end_user_text_color(end_user_text_color) - - # Set the text color for the assistant user - assistant_text_color_start = set_assistant_user_text_color(assistant_text_color) - - # Set the text color for the system user - system_text_color_start = set_system_user_text_color(system_text_color) - - # Set the color of the assistant's avatar - avatar_color_start = set_assistant_avatar_color(avatar_color) - - - # Assign functions imported from functions - - # Set the values returned by get_tts_engine_string() - tts_engine_str = get_tts_engine_string() - - # Set the values returned by get_chat_participant_names() - end_user_name, assistant_name, system_name = get_chat_participant_names() - - # Set the value of manage_result_message_on_and_output_skill_errors_in_pane_on - manage_result_message_on_and_output_skill_errors_in_pane_on = get_manage_result_message_on_and_output_skill_errors_in_pane_on() - - # Set the value of commbase_stt_vosk_p_parse_control_messages_on - commbase_stt_vosk_p_parse_control_messages_on = get_commbase_stt_vosk_p_parse_control_messages_on() - - - # Assign functions imported from file_paths - - # Set the values returned by get_secrets_file_path() - secrets_file_path = get_secrets_file_path() - - # Set the values returned by get_ascii_art_file_path() - ascii_art_file_path = get_ascii_art_file_path() - - # Set the values returned by get_assistant_microphone_instruction_file() - assistant_microphone_instruction_file = get_assistant_microphone_instruction_file() - - # Set the values returned by get_result_message_recording_file() - result_message_recording_file = get_result_message_recording_file() - - # Set the values returned by get_previous_result_message_recording_file() - previous_result_message_recording_file = get_previous_result_message_recording_file() - - # Set the values returned by get_result_messages_history_file() - result_messages_history_file = get_result_messages_history_file() - - # Set the values returned by get_control_stop_previous_command_patterns_file() - control_stop_previous_command_patterns_file = get_control_stop_previous_command_patterns_file() - - # Set the values returned by get_control_accept_changes_patterns_file() - control_accept_changes_patterns_file = get_control_accept_changes_patterns_file() - - # Set the values returned by get_control_deny_changes_patterns_file() - control_deny_changes_patterns_file = get_control_deny_changes_patterns_file() - - # Set the values returned by get_control_select_option_number_one_patterns_file() - control_select_option_number_one_patterns_file = get_control_select_option_number_one_patterns_file() - - # Set the values returned by get_control_select_option_number_two_patterns_file() - control_select_option_number_two_patterns_file = get_control_select_option_number_two_patterns_file() - - # Set the values returned by get_control_select_option_number_three_patterns_file() - control_select_option_number_three_patterns_file = get_control_select_option_number_three_patterns_file() - - # Set the values returned by get_control_select_option_number_four_patterns_file() - control_select_option_number_four_patterns_file = get_control_select_option_number_four_patterns_file() - - # Set the values returned by get_control_skip_question_patterns_file() - control_skip_question_patterns_file = get_control_skip_question_patterns_file() - - # Set the values returned by get_control_request_the_current_mode_patterns_file() - control_request_the_current_mode_patterns_file = get_control_request_the_current_mode_patterns_file() - - # Set the values returned by get_control_enter_the_normal_mode() - control_enter_the_normal_mode_patterns_file = get_control_enter_the_normal_mode_patterns_file() - - # Set the values returned by get_control_enter_the_conversational_mode_patterns_file() - control_enter_the_conversational_mode_patterns_file = get_control_enter_the_conversational_mode_patterns_file() - - # Set the values returned by get_control_enter_the_expert_mode_patterns_file() - control_enter_the_expert_mode_patterns_file = get_control_enter_the_expert_mode_patterns_file() - - # Set the values returned by get_control_enter_the_follow_up_mode_patterns_file() - control_enter_the_follow_up_mode_patterns_file = get_control_enter_the_follow_up_mode_patterns_file() - - - # Preload all the control pattern files - - # CONTROL_STOP_PREVIOUS_COMMAND - # Load the control patterns file and store its content in a variable - control_stop_previous_command_patterns = read_lines_from_file(control_stop_previous_command_patterns_file) - - # CONTROL_ACCEPT_CHANGES - # Load the control patterns file and store its content in a variable - control_accept_changes_patterns = read_lines_from_file(control_accept_changes_patterns_file) - - # CONTROL_DENY_CHANGES - # Load the control patterns file and store its content in a variable - control_deny_changes_patterns = read_lines_from_file(control_deny_changes_patterns_file) - - # CONTROL_SELECT_OPTION_NUMBER_ONE - # Load the control patterns file and store its content in a variable - control_select_option_number_one_patterns = read_lines_from_file(control_select_option_number_one_patterns_file) - - # CONTROL_SELECT_OPTION_NUMBER_TWO - # Load the control patterns file and store its content in a variable - control_select_option_number_two_patterns = read_lines_from_file(control_select_option_number_two_patterns_file) - - # CONTROL_SELECT_OPTION_NUMBER_THREE - # Load the patterns file and store its content in a variable - control_select_option_number_three_patterns = read_lines_from_file(control_select_option_number_three_patterns_file) - - # CONTROL_SELECT_OPTION_NUMBER_FOUR - # Load the control patterns file and store its content in a variable - control_select_option_number_four_patterns = read_lines_from_file(control_select_option_number_four_patterns_file) - - # CONTROL_SKIP_QUESTION - # Load the control patterns file and store its content in a variable - control_skip_question_patterns = read_lines_from_file(control_skip_question_patterns_file) - - # CONTROL_REQUEST_THE_CURRENT_MODE - # Load the control patterns file and store its content in a variable - control_request_the_current_mode_patterns = read_lines_from_file(control_request_the_current_mode_patterns_file) - - # CONTROL_ENTER_THE_NORMAL_MODE - # Load the control patterns file and store its content in a variable - control_enter_the_normal_mode_patterns = read_lines_from_file(control_enter_the_normal_mode_patterns_file) - - # CONTROL_ENTER_THE_CONVERSATIONAL_MODE - - control_enter_the_conversational_mode_patterns = read_lines_from_file(control_enter_the_conversational_mode_patterns_file) - - # CONTROL_ENTER_THE_EXPERT_MODE - # Load the control patterns file and store its content in a variable - control_enter_the_expert_mode_patterns = read_lines_from_file(control_enter_the_expert_mode_patterns_file) - - # CONTROL_ENTER_THE_FOLLOW_UP_MODE - # Load the control patterns file and store its content in a variable - control_enter_the_follow_up_mode_patterns = read_lines_from_file(control_enter_the_follow_up_mode_patterns_file) - - - # Show avatar - - # Display the assitant avatar - display_assistant_avatar() - - - # Read the content of a file that provides instructions about muting the - # microphone to pause recording. It then prints the content, including the - # formatted assistant name and colors. - discourse = read_plain_text_file(assistant_microphone_instruction_file) - print(f'\n\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} {discourse}\033[{color_code_end}') - # TODO: Replace system commands with new libcommbase routines mute and unmute - # Mute the microphone before the assistant speaks - subprocess.run('(amixer set Capture nocap)', stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True) - # Tell the end user to mute the microphone to pause the recording - subprocess.run(f'(echo "{discourse}" | {tts_engine_str})', shell=True) - # Unmute the microphone after the assistant speaks - subprocess.run('(amixer set Capture cap)', stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True) - - # Create a new instance of the KaldiRecognizer class from the Vosk library - rec = vosk.KaldiRecognizer(model, args.samplerate) - - while True: - data = q.get() - if rec.AcceptWaveform(data): - print_result() - #else - #print(rec.PartialResult()) - if dump_fn is not None: - dump_fn.write(data) - - - except KeyboardInterrupt: - print('\nDone') - parser.exit(0) - except Exception as e: - parser.exit(type(e).__name__ + ': ' + str(e)) + """ + # Assign the result of calling get_vosk_ml_model_directory() + vosk_ml_model_directory = get_vosk_ml_model_directory() + + # Define control messages + CONTROL_STOP_PREVIOUS_COMMAND = "okay stop" + CONTROL_ACCEPT_CHANGES = "okay accept" + CONTROL_DENY_CHANGES = "okay deny" + CONTROL_SELECT_OPTION_NUMBER_ONE = "okay select the option number one" + CONTROL_SELECT_OPTION_NUMBER_TWO = "okay select the option number two" + CONTROL_SELECT_OPTION_NUMBER_THREE = "okay select the option number three" + CONTROL_SELECT_OPTION_NUMBER_FOUR = "okay select the option number four" + CONTROL_SKIP_QUESTION = "okay skip that question" + CONTROL_REQUEST_THE_CURRENT_MODE = "okay what mode are you in" + CONTROL_ENTER_THE_NORMAL_MODE = "okay enter the normal mode" + CONTROL_ENTER_THE_CONVERSATION_MODE = "okay enter the conversation mode" + CONTROL_ENTER_THE_EXPERT_MODE = "okay enter the expert mode" + CONTROL_ENTER_THE_FOLLOW_MODE = "okay enter the follow mode" + + # q is used to store a Queue object, which is then used to keep track of the + # nodes that need to be visited during the breadth-first search algorithm. + q = queue.Queue() + + def callback(indata, frames, time, status): + """ + This function puts the input data into a queue for processing. + If the status parameter is not None, it prints an error message to the + standard error stream. + + This function is called (from a separate thread) for each audio block. + + Parameters: + indata : numpy.ndarray + The input audio data as a NumPy array. + frames : int + The number of frames in the input data. + time : CData + A ctypes structure containing timing information about the input + data. + status : CallbackFlags + A flag that indicates the status of the input data. + + Returns: + None. + """ + if status: + print(status, file=sys.stderr) + q.put(bytes(indata)) + + def display_assistant_avatar(): + """ + Displays the assistant avatar in the terminal. + + This function retrieves the ASCII art avatar of the assistant, assigns + the appropriate color based on the configured avatar color, and then + prints it to the terminal. + + Note: + The color codes used in the avatar display are obtained from the + `get_terminal_colors()` function. + The avatar color is obtained from the `get_assistant_avatar_color()` + function. + The ASCII art avatar is obtained from the `get_assistant_avatar()` + function. + + Example: + Terminal output: + >>> display_assistant_avatar() + [COLOR CODES] ASCII ART [RESET] + """ + # Load an ASCII art file, store its content in a variable, and then + # prints it in a specific color using terminal escape sequences. + assistant_avatar = read_plain_text_file(ascii_art_file_path) + print(f"\033[{avatar_color_start}\033[{assistant_avatar}\033[{color_code_end}") + + def print_result(): + """ + Prints the result on the screen and write to files. + + Parameters: + None. + + Returns: + None. + """ + # The original result + string = rec.Result() + + # The modified result + trimmed_string = strip_string(string) + + # Check if the trimmed_string is not empty and control message parsing + # is enabled for commbase-stt-vosk-p engine + if ( + trimmed_string != "" + and commbase_stt_vosk_p_parse_control_messages_on == "True" + ): + + # Define the control messages dictionary + control_dictionary = { + "CONTROL_STOP_PREVIOUS_COMMAND": { + "patterns": control_stop_previous_command_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_STOP_PREVIOUS_COMMAND}" for processing.', + }, + "CONTROL_ACCEPT_CHANGES": { + "patterns": control_accept_changes_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_ACCEPT_CHANGES}" for processing.', + }, + "CONTROL_DENY_CHANGES": { + "patterns": control_deny_changes_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_DENY_CHANGES}" for processing.', + }, + "CONTROL_SELECT_OPTION_NUMBER_ONE": { + "patterns": control_select_option_number_one_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_ONE}" for processing.', + }, + "CONTROL_SELECT_OPTION_NUMBER_TWO": { + "patterns": control_select_option_number_two_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_TWO}" for processing.', + }, + "CONTROL_SELECT_OPTION_NUMBER_THREE": { + "patterns": control_select_option_number_three_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_THREE}" for processing.', + }, + "CONTROL_SELECT_OPTION_NUMBER_FOUR": { + "patterns": control_select_option_number_four_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_SELECT_OPTION_NUMBER_FOUR}" for processing.', + }, + "CONTROL_SKIP_QUESTION": { + "patterns": control_skip_question_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_SKIP_QUESTION}" for processing.', + }, + "CONTROL_REQUEST_THE_CURRENT_MODE": { + "patterns": control_request_the_current_mode_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_REQUEST_THE_CURRENT_MODE}" for processing.', + }, + "CONTROL_ENTER_THE_NORMAL_MODE": { + "patterns": control_enter_the_normal_mode_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_NORMAL_MODE}" for processing.', + }, + "CONTROL_ENTER_THE_CONVERSATION_MODE": { + "patterns": control_enter_the_conversation_mode_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_CONVERSATION_MODE}" for processing.', + }, + "CONTROL_ENTER_THE_EXPERT_MODE": { + "patterns": control_enter_the_expert_mode_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_EXPERT_MODE}" for processing.', + }, + "CONTROL_ENTER_THE_FOLLOW_MODE": { + "patterns": control_enter_the_follow_mode_patterns, + "message": f'I am dispatching "{trimmed_string}" as control "{CONTROL_ENTER_THE_FOLLOW_MODE}" for processing.', + }, + } + + # The control message and END USER message matching + found_match = False + + # Check if the trimmed_string is not empty + for control, info in control_dictionary.items(): + patterns = info["patterns"] # Get the patterns for the current control + message = info["message"] # Get the message for the current control + + for line in patterns: + if trimmed_string == line.strip(): # Check for exact match + # If a match is found, print the message and record the + # control message + print( + f"\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} {message}\033[{color_code_end}" + ) + # Create the JSON object + json_data = {"control": control} + # Record the control message string to + # result_message_recording_file as a JSON object. + with open(result_message_recording_file, "w") as f: + json.dump(json_data, f) + found_match = True + if ( + manage_result_message_on_and_output_skill_errors_in_pane_on + == "True" + ): + # Manage the result message + subprocess.run( + ["bash", os.environ["COMMBASE_APP_DIR"] + "/src/skill"] + ) + break # Exit the inner loop if a match is found + + if found_match: + break # Exit the outer loop if a match is found + + # Handle the case when no match is found + if not found_match: + # Record a normal END USER message instead of a control message + print( + f'\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} I am dispatching "{trimmed_string}" for processing.\033[{color_code_end}' + ) + # Create the JSON object + json_data = {"message": trimmed_string} + # Record the control message string to + # result_message_recording_file and + # previous_result_message_recording_file as a JSON object. + with open(result_message_recording_file, "w") as f: + json.dump(json_data, f) + with open(previous_result_message_recording_file, "w") as f: + json.dump(json_data, f) + if ( + manage_result_message_on_and_output_skill_errors_in_pane_on + == "True" + ): + # Manage the result message + subprocess.run( + ["bash", os.environ["COMMBASE_APP_DIR"] + "/src/skill"] + ) + + # Check if the trimmed_string is not empty and control message parsing + # is disabled for commbase-stt-vosk-p engine. + elif ( + trimmed_string != "" + and commbase_stt_vosk_p_parse_control_messages_on == "False" + ): + print( + f'\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} I am dispatching "{trimmed_string}" no controls for processing.\033[{color_code_end}' + ) + # Create the JSON object + json_data = {"message": trimmed_string} + # Record the control message string to + # controller_message_recording_file as a JSON object. + with open(controller_message_recording_file, "w") as f: + json.dump(json_data, f) + if manage_result_message_on_and_output_skill_errors_in_pane_on == "True": + # Manage the result message + subprocess.run( + [ + "bash", + os.environ["COMMBASE_APP_DIR"] + + "/bundles/built-in/broker/libcommbase/libcommbase/controller/controller", + ] + ) + + # Create ArgumentParser object with add_help=False to disable default help + # message. + parser = argparse.ArgumentParser(add_help=False) + + # Add a command-line argument for listing audio devices + parser.add_argument( + "-l", + "--list-devices", + action="store_true", # store_true indicates that the argument does not require a value and sets it to True if present + help="show list of audio devices and exit", + ) + + # Parse the command-line arguments, with any unrecognized arguments returned + # as a list in 'remaining'. + args, remaining = parser.parse_known_args() + + # If the 'list-devices' argument was passed, print the list of audio devices + # and exit the program. + if args.list_devices: + print(sd.query_devices()) + parser.exit(0) # exit the program with status code 0 + + # create a new ArgumentParser object with a description and formatter + parser = argparse.ArgumentParser( + description=__doc__, # use the script's docstring as the description + formatter_class=argparse.RawDescriptionHelpFormatter, # use a raw formatter for the description + parents=[ + parser + ], # inherit the 'list-devices' argument from the previous parser + ) + + # Add additional arguments for specifying a filename, model path, input + # device, and sampling rate. + parser.add_argument( + "-f", + "--filename", + type=str, + metavar="FILENAME", + help="audio file to store recording to", + ) + parser.add_argument( + "-m", "--model", type=str, metavar="MODEL_PATH", help="Path to the model" + ) + parser.add_argument( + "-d", "--device", type=int_or_str, help="input device (numeric ID or substring)" + ) + parser.add_argument("-r", "--samplerate", type=int, help="sampling rate") + + # Parse the remaining command-line arguments + args = parser.parse_args(remaining) + + try: + if args.model is None: + args.model = string.Template(vosk_ml_model_directory).substitute(os.environ) + if not os.path.exists(args.model): + print( + "Please download a model for your language from https://alphacephei.com/vosk/models" + ) + print("and unpack as 'model' in the current folder.") + parser.exit(0) + if args.samplerate is None: + device_info = sd.query_devices(args.device, "input") + # soundfile expects an int, sounddevice provides a float: + args.samplerate = int(device_info["default_samplerate"]) + + model = vosk.Model(args.model) + + if args.filename: + dump_fn = open(args.filename, "wb") + else: + dump_fn = None + + with sd.RawInputStream( + samplerate=args.samplerate, + blocksize=8000, + device=args.device, + dtype="int16", + channels=1, + callback=callback, + ): + # Assign functions imported from text_formatting + + # Set the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the values returned by get_chat_participant_colors() + ( + end_user_background_color, + assistant_background_color, + system_background_color, + end_user_text_color, + assistant_text_color, + system_text_color, + ) = get_chat_participant_colors() + + # Set the values returned by get_assistant_avatar_color() + avatar_color = get_assistant_avatar_color() + + # Set the background color for the assistant user + assistant_background_color_start = set_assistant_user_background_color( + assistant_background_color + ) + + # Set the text color for the assistant user + assistant_text_color_start = set_assistant_user_text_color( + assistant_text_color + ) + + # Set the color of the assistant's avatar + avatar_color_start = set_assistant_avatar_color(avatar_color) + + # Assign functions imported from functions + + # Set the values returned by get_tts_engine_string() + tts_engine_str = get_tts_engine_string() + + # Set the values returned by get_chat_participant_names() + end_user_name, assistant_name, system_name = get_chat_participant_names() + + # Set the value of manage_result_message_on_and_output_skill_errors_in_pane_on + manage_result_message_on_and_output_skill_errors_in_pane_on = ( + get_manage_result_message_on_and_output_skill_errors_in_pane_on() + ) + + # Set the value of commbase_stt_vosk_p_parse_control_messages_on + commbase_stt_vosk_p_parse_control_messages_on = ( + get_commbase_stt_vosk_p_parse_control_messages_on() + ) + + # Assign functions imported from file_paths + + # Set the values returned by get_secrets_file_path() + # secrets_file_path = get_secrets_file_path() + + # Set the values returned by get_ascii_art_file_path() + ascii_art_file_path = get_ascii_art_file_path() + + # Set the values returned by get_assistant_microphone_instruction_file() + assistant_microphone_instruction_file = ( + get_assistant_microphone_instruction_file() + ) + + # Set the values returned by get_controller_message_recording_file() + controller_message_recording_file = get_controller_message_recording_file() + + # Set the values returned by get_result_message_recording_file() + result_message_recording_file = get_result_message_recording_file() + + # Set the values returned by get_previous_result_message_recording_file() + previous_result_message_recording_file = ( + get_previous_result_message_recording_file() + ) + + # Set the values returned by get_result_messages_history_file() + # result_messages_history_file = get_result_messages_history_file() + + # Set the values returned by get_control_stop_previous_command_patterns_file() + control_stop_previous_command_patterns_file = ( + get_control_stop_previous_command_patterns_file() + ) + + # Set the values returned by get_control_accept_changes_patterns_file() + control_accept_changes_patterns_file = ( + get_control_accept_changes_patterns_file() + ) + + # Set the values returned by get_control_deny_changes_patterns_file() + control_deny_changes_patterns_file = ( + get_control_deny_changes_patterns_file() + ) + + # Set the values returned by get_control_select_option_number_one_patterns_file() + control_select_option_number_one_patterns_file = ( + get_control_select_option_number_one_patterns_file() + ) + + # Set the values returned by get_control_select_option_number_two_patterns_file() + control_select_option_number_two_patterns_file = ( + get_control_select_option_number_two_patterns_file() + ) + + # Set the values returned by get_control_select_option_number_three_patterns_file() + control_select_option_number_three_patterns_file = ( + get_control_select_option_number_three_patterns_file() + ) + + # Set the values returned by get_control_select_option_number_four_patterns_file() + control_select_option_number_four_patterns_file = ( + get_control_select_option_number_four_patterns_file() + ) + + # Set the values returned by get_control_skip_question_patterns_file() + control_skip_question_patterns_file = ( + get_control_skip_question_patterns_file() + ) + + # Set the values returned by get_control_request_the_current_mode_patterns_file() + control_request_the_current_mode_patterns_file = ( + get_control_request_the_current_mode_patterns_file() + ) + + # Set the values returned by get_control_enter_the_normal_mode() + control_enter_the_normal_mode_patterns_file = ( + get_control_enter_the_normal_mode_patterns_file() + ) + + # Set the values returned by get_control_enter_the_conversation_mode_patterns_file() + control_enter_the_conversation_mode_patterns_file = ( + get_control_enter_the_conversation_mode_patterns_file() + ) + + # Set the values returned by get_control_enter_the_expert_mode_patterns_file() + control_enter_the_expert_mode_patterns_file = ( + get_control_enter_the_expert_mode_patterns_file() + ) + + # Set the values returned by get_control_enter_the_follow_mode_patterns_file() + control_enter_the_follow_mode_patterns_file = ( + get_control_enter_the_follow_mode_patterns_file() + ) + + # Preload all the control pattern files + + # CONTROL_STOP_PREVIOUS_COMMAND + # Load the control patterns file and store its content in a variable + control_stop_previous_command_patterns = read_lines_from_file( + control_stop_previous_command_patterns_file + ) + + # CONTROL_ACCEPT_CHANGES + # Load the control patterns file and store its content in a variable + control_accept_changes_patterns = read_lines_from_file( + control_accept_changes_patterns_file + ) + + # CONTROL_DENY_CHANGES + # Load the control patterns file and store its content in a variable + control_deny_changes_patterns = read_lines_from_file( + control_deny_changes_patterns_file + ) + + # CONTROL_SELECT_OPTION_NUMBER_ONE + # Load the control patterns file and store its content in a variable + control_select_option_number_one_patterns = read_lines_from_file( + control_select_option_number_one_patterns_file + ) + + # CONTROL_SELECT_OPTION_NUMBER_TWO + # Load the control patterns file and store its content in a variable + control_select_option_number_two_patterns = read_lines_from_file( + control_select_option_number_two_patterns_file + ) + + # CONTROL_SELECT_OPTION_NUMBER_THREE + # Load the patterns file and store its content in a variable + control_select_option_number_three_patterns = read_lines_from_file( + control_select_option_number_three_patterns_file + ) + + # CONTROL_SELECT_OPTION_NUMBER_FOUR + # Load the control patterns file and store its content in a variable + control_select_option_number_four_patterns = read_lines_from_file( + control_select_option_number_four_patterns_file + ) + + # CONTROL_SKIP_QUESTION + # Load the control patterns file and store its content in a variable + control_skip_question_patterns = read_lines_from_file( + control_skip_question_patterns_file + ) + + # CONTROL_REQUEST_THE_CURRENT_MODE + # Load the control patterns file and store its content in a variable + control_request_the_current_mode_patterns = read_lines_from_file( + control_request_the_current_mode_patterns_file + ) + + # CONTROL_ENTER_THE_NORMAL_MODE + # Load the control patterns file and store its content in a variable + control_enter_the_normal_mode_patterns = read_lines_from_file( + control_enter_the_normal_mode_patterns_file + ) + + # CONTROL_ENTER_THE_CONVERSATION_MODE + # Load the control patterns file and store its content in a variable + control_enter_the_conversation_mode_patterns = read_lines_from_file( + control_enter_the_conversation_mode_patterns_file + ) + + # CONTROL_ENTER_THE_EXPERT_MODE + # Load the control patterns file and store its content in a variable + control_enter_the_expert_mode_patterns = read_lines_from_file( + control_enter_the_expert_mode_patterns_file + ) + + # CONTROL_ENTER_THE_FOLLOW_MODE + # Load the control patterns file and store its content in a variable + control_enter_the_follow_mode_patterns = read_lines_from_file( + control_enter_the_follow_mode_patterns_file + ) + + # Show avatar + + # Display the assitant avatar + display_assistant_avatar() + + # Read the content of a file that provides instructions about muting + # the microphone to pause recording. It then prints the content, + # including the formatted assistant name and colors. + discourse = read_plain_text_file(assistant_microphone_instruction_file) + print( + f"\n\033[{assistant_background_color_start}\033[{assistant_text_color_start}{assistant_name}:\033[{color_code_end}\033[{color_code_end}\033[{assistant_text_color_start} {discourse}\033[{color_code_end}" + ) + # TODO: Replace system commands with new libcommbase routines mute + # and unmute Mute the microphone before the assistant speaks. + subprocess.run( + "(amixer set Capture nocap)", + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + shell=True, + ) + # Tell the end user to mute the microphone to pause the recording + subprocess.run(f'(echo "{discourse}" | {tts_engine_str})', shell=True) + # Unmute the microphone after the assistant speaks + subprocess.run( + "(amixer set Capture cap)", + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + shell=True, + ) + + # Create a new instance of the KaldiRecognizer class from the Vosk + # library. + rec = vosk.KaldiRecognizer(model, args.samplerate) + + while True: + data = q.get() + if rec.AcceptWaveform(data): + print_result() + # else + # print(rec.PartialResult()) + if dump_fn is not None: + dump_fn.write(data) + + except KeyboardInterrupt: + print("\nDone") + parser.exit(0) + except Exception as e: + parser.exit(type(e).__name__ + ": " + str(e)) def main(): - """ - Entry point of the program. + """ + Entry point of the program. - This function serves as the entry point for the program. It is responsible for - initiating the execution of the program and coordinating the different - components or functions within it. + This function serves as the entry point for the program. It is responsible + for initiating the execution of the program and coordinating the different + components or functions within it. - Usage: - - Ensure that all required dependencies are installed before running this - program. - - Run this script using the Python interpreter: `commbase-stt-vosk-p.py` + Usage: + - Ensure that all required dependencies are installed before running + this program. + - Run this script using the Python interpreter: `commbase-stt-vosk-p.py` - Paramenters: - None + Paramenters: + None. - Returns: - None - """ - # Global declarations - global q + Returns: + None. + """ + # Global declarations + global q + + # Call commbase_stt_vosk_p + commbase_stt_vosk_p() - # Call commbase_stt_vosk_p - commbase_stt_vosk_p() # Ensure that the main() function is executed only when the script is run # directly as the main program. -if __name__ == '__main__': - main() - +if __name__ == "__main__": + main() diff --git a/config.py b/config.py index 2cc104c..6971b71 100644 --- a/config.py +++ b/config.py @@ -1,10 +1,10 @@ ################################################################################ -# commbase-stt-vosk # +# commbase-stt-vosk-p # # # -# Conversational AI Assistant and AI Hub for Computers and Droids # +# An ASR (Automatic Speech Recognition) engine. # # # # Change History # -# 05/20/2023 Esteban Herrera Original code. # +# 04/29/2023 Esteban Herrera Original code. # # Add new history entries as needed. # # # # # @@ -38,5 +38,4 @@ # The path to the .env configuration file CONFIG_FILE_DIR = os.environ["COMMBASE_APP_DIR"] -CONFIG_FILE_PATH = os.path.join(CONFIG_FILE_DIR, 'config/app.conf') - +CONFIG_FILE_PATH = os.path.join(CONFIG_FILE_DIR, "config/app.conf") diff --git a/file_paths.py b/file_paths.py index 92208cc..2cfcff9 100644 --- a/file_paths.py +++ b/file_paths.py @@ -1,10 +1,10 @@ ################################################################################ -# commbase-stt-vosk # +# commbase-stt-vosk-p # # # -# Conversational AI Assistant and AI Hub for Computers and Droids # +# An ASR (Automatic Speech Recognition) engine. # # # # Change History # -# 05/20/2023 Esteban Herrera Original code. # +# 04/29/2023 Esteban Herrera Original code. # # Add new history entries as needed. # # # # # @@ -31,609 +31,638 @@ # file_paths.py # This file stores functions related to loading and managing file paths +# Requires os.path already imported # Requirements -import os.path from config import CONFIG_FILE_DIR, CONFIG_FILE_PATH def get_vosk_ml_model_directory(): - """ - Retrieve the directory path of the Vosk ML model from the configuration file. - - Returns: - str or None: The directory path of the Vosk ML model if found, or None if - not found. - """ - # Initialize variable for the tts engine string - model_directory = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'VOSK_ML_MODEL_DIRECTORY': - # Remove the quotes from the value of the variable - model_directory = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if model_directory is not None: - return model_directory - - # If the variable was not found, return None - return None + """ + Retrieves the directory path of the Vosk ML model from the configuration + file. + + Returns: + str or None: The directory path of the Vosk ML model if found, or None + if not found. + """ + # Initialize variable for the tts engine string + model_directory = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "VOSK_ML_MODEL_DIRECTORY": + # Remove the quotes from the value of the variable + model_directory = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if model_directory is not None: + return model_directory + + # If the variable was not found, return None + return None def get_secrets_file_path(): - """ - Retrieve the value of the SECRETS_FILE_PATH variable from the configuration - file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - secrets_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'SECRETS_FILE_PATH': - # Remove the quotes from the value of the variable - secrets_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if secrets_file is not None: - return secrets_file - - # If the variable was not found, return None - return None + """ + Retrieves the value of the SECRETS_FILE_PATH variable from the configuration + file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + secrets_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "SECRETS_FILE_PATH": + # Remove the quotes from the value of the variable + secrets_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if secrets_file is not None: + return secrets_file + + # If the variable was not found, return None + return None def get_ascii_art_file_path(): - """ - Retrieve the value of the SASCII_ART_FILE_PATH variable from the configuration - file. + """ + Retrieves the value of the SASCII_ART_FILE_PATH variable from the + configuration file. - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - ascii_art_file = None + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + ascii_art_file = None - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") - # Check if the variable we are looking for exists in the line - if variable_name == 'ASCII_ART_FILE_PATH': - # Remove the quotes from the value of the variable - ascii_art_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if ascii_art_file is not None: - return ascii_art_file + # Check if the variable we are looking for exists in the line + if variable_name == "ASCII_ART_FILE_PATH": + # Remove the quotes from the value of the variable + ascii_art_file = CONFIG_FILE_DIR + value.strip()[1:-1] - # If the variable was not found, return None - return None + # Check if the variable was found + if ascii_art_file is not None: + return ascii_art_file + # If the variable was not found, return None + return None def get_assistant_microphone_instruction_file(): - """ - Retrieve the value of the SASSISTANT_MICROPHONE_INSTRUCTION_FILE variable from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - instruction_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'ASSISTANT_MICROPHONE_INSTRUCTION_FILE': - # Remove the quotes from the value of the variable - instruction_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if instruction_file is not None: - return instruction_file - - # If the variable was not found, return None - return None + """ + Retrieves the value of the SASSISTANT_MICROPHONE_INSTRUCTION_FILE variable + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + instruction_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "ASSISTANT_MICROPHONE_INSTRUCTION_FILE": + # Remove the quotes from the value of the variable + instruction_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if instruction_file is not None: + return instruction_file + + # If the variable was not found, return None + return None def get_result_message_recording_file(): - """ - Retrieve the value of the SRESULT_MESSAGE_RECORDING_FILE variable from the - configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - recording_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'RESULT_MESSAGE_RECORDING_FILE': - # Remove the quotes from the value of the variable - recording_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if recording_file is not None: - return recording_file - - # If the variable was not found, return None - return None + """ + Retrieves the value of the SRESULT_MESSAGE_RECORDING_FILE variable from the + configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + recording_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "RESULT_MESSAGE_RECORDING_FILE": + # Remove the quotes from the value of the variable + recording_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if recording_file is not None: + return recording_file + + # If the variable was not found, return None + return None def get_previous_result_message_recording_file(): - """ - Retrieve the value of the SPREVIOUS_RESULT_MESSAGE_RECORDING_FILE variable - from the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - recording_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'PREVIOUS_RESULT_MESSAGE_RECORDING_FILE': - # Remove the quotes from the value of the variable - recording_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if recording_file is not None: - return recording_file - - # If the variable was not found, return None - return None + """ + Retrieves the value of the SPREVIOUS_RESULT_MESSAGE_RECORDING_FILE variable + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + recording_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "PREVIOUS_RESULT_MESSAGE_RECORDING_FILE": + # Remove the quotes from the value of the variable + recording_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if recording_file is not None: + return recording_file + + # If the variable was not found, return None + return None + + +def get_controller_message_recording_file(): + """ + Retrieves the value of the CONTROLLER_MESSAGE_RECORDING_FILE variable from + the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + recording_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROLLER_MESSAGE_RECORDING_FILE": + # Remove the quotes from the value of the variable + recording_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if recording_file is not None: + return recording_file + + # If the variable was not found, return None + return None def get_result_messages_history_file(): - """ - Retrieve the value of the SRESULT_MESSAGES_HISTORY_FILE variable from the - configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - history_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'RESULT_MESSAGES_HISTORY_FILE': - # Remove the quotes from the value of the variable - history_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if history_file is not None: - return history_file - - # If the variable was not found, return None - return None + """ + Retrieves the value of the SRESULT_MESSAGES_HISTORY_FILE variable from the + configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + history_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "RESULT_MESSAGES_HISTORY_FILE": + # Remove the quotes from the value of the variable + history_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if history_file is not None: + return history_file + + # If the variable was not found, return None + return None def get_control_stop_previous_command_patterns_file(): - """ - Retrieve the value of the SCONTROL_STOP_PREVIOUS_COMMAND_PATTERNS_FILE from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_STOP_PREVIOUS_COMMAND_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None + """ + Retrieves the value of the SCONTROL_STOP_PREVIOUS_COMMAND_PATTERNS_FILE + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_STOP_PREVIOUS_COMMAND_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None def get_control_accept_changes_patterns_file(): - """ - Retrieve the value of the SCONTROL_ACCEPT_CHANGES_PATTERNS_FILE from the - configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_ACCEPT_CHANGES_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_ACCEPT_CHANGES_PATTERNS_FILE from the + configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_ACCEPT_CHANGES_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_deny_changes_patterns_file(): - """ - Retrieve the value of the SCONTROL_DENY_CHANGES_PATTERNS_FILE from the - configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_DENY_CHANGES_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_DENY_CHANGES_PATTERNS_FILE from the + configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_DENY_CHANGES_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_select_option_number_one_patterns_file(): - """ - Retrieve the value of the SCONTROL_SELECT_OPTION_NUMBER_ONE_PATTERNS_FILE from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_SELECT_OPTION_NUMBER_ONE_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_SELECT_OPTION_NUMBER_ONE_PATTERNS_FILE + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_SELECT_OPTION_NUMBER_ONE_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_select_option_number_two_patterns_file(): - """ - Retrieve the value of the SCONTROL_SELECT_OPTION_NUMBER_TWO_PATTERNS_FILE from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_SELECT_OPTION_NUMBER_TWO_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_SELECT_OPTION_NUMBER_TWO_PATTERNS_FILE + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_SELECT_OPTION_NUMBER_TWO_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_select_option_number_three_patterns_file(): - """ - Retrieve the value of the SCONTROL_SELECT_OPTION_NUMBER_THREE_PATTERNS_FILE - from the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_SELECT_OPTION_NUMBER_THREE_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_SELECT_OPTION_NUMBER_THREE_PATTERNS_FILE + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_SELECT_OPTION_NUMBER_THREE_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_select_option_number_four_patterns_file(): - """ - Retrieve the value of the SCONTROL_SELECT_OPTION_NUMBER_FOUR_PATTERNS_FILE - from the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_SELECT_OPTION_NUMBER_FOUR_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_SELECT_OPTION_NUMBER_FOUR_PATTERNS_FILE + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_SELECT_OPTION_NUMBER_FOUR_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_skip_question_patterns_file(): - """ - Retrieve the value of the SCONTROL_SKIP_QUESTION_PATTERNS_FILE from the - configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_SKIP_QUESTION_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_SKIP_QUESTION_PATTERNS_FILE from the + configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_SKIP_QUESTION_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_request_the_current_mode_patterns_file(): - """ - Retrieve the value of the SCONTROL_REQUEST_THE_CURRENT_MODE_PATTERNS_FILE from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_REQUEST_THE_CURRENT_MODE_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the SCONTROL_REQUEST_THE_CURRENT_MODE_PATTERNS_FILE + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_REQUEST_THE_CURRENT_MODE_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + def get_control_enter_the_normal_mode_patterns_file(): - """ - Retrieve the value of the SCONTROL_ENTER_THE_NORMAL_MODE_PATTERNS_FILE from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_ENTER_THE_NORMAL_MODE_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - - -def get_control_enter_the_conversational_mode_patterns_file(): - """ - Retrieve the value of the SCONTROL_ENTER_THE_CONVERSATIONAL_MODE_PATTERNS_FILE - from the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_ENTER_THE_CONVERSATIONAL_MODE_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None + """ + Retrieves the value of the SCONTROL_ENTER_THE_NORMAL_MODE_PATTERNS_FILE + from the configuration file. + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None -def get_control_enter_the_expert_mode_patterns_file(): - """ - Retrieve the value of the SCONTROL_ENTER_THE_EXPERT_MODE_PATTERNS_FILE from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_ENTER_THE_EXPERT_MODE_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None - - -def get_control_enter_the_follow_up_mode_patterns_file(): - """ - Retrieve the value of the SCONTROL_ENTER_THE_FOLLOW_UP_MODE_PATTERNS_FILE from - the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - patterns_file = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'CONTROL_ENTER_THE_FOLLOW_UP_MODE_PATTERNS_FILE': - # Remove the quotes from the value of the variable - patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] - - # Check if the variable was found - if patterns_file is not None: - return patterns_file - - # If the variable was not found, return None - return None + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_ENTER_THE_NORMAL_MODE_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + + +def get_control_enter_the_conversation_mode_patterns_file(): + """ + Retrieves the value of the SCONTROL_ENTER_THE_CONVERSATION_MODE_PATTERNS_FILE + from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_ENTER_THE_CONVERSATION_MODE_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + + +def get_control_enter_the_expert_mode_patterns_file(): + """ + Retrieves the value of the SCONTROL_ENTER_THE_EXPERT_MODE_PATTERNS_FILE from + the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_ENTER_THE_EXPERT_MODE_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None + + +def get_control_enter_the_follow_mode_patterns_file(): + """ + Retrieves the value of the SCONTROL_ENTER_THE_FOLLOW_MODE_PATTERNS_FILE from + the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + patterns_file = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "CONTROL_ENTER_THE_FOLLOW_MODE_PATTERNS_FILE": + # Remove the quotes from the value of the variable + patterns_file = CONFIG_FILE_DIR + value.strip()[1:-1] + + # Check if the variable was found + if patterns_file is not None: + return patterns_file + + # If the variable was not found, return None + return None diff --git a/functions.py b/functions.py index a6b5b0b..56e481b 100644 --- a/functions.py +++ b/functions.py @@ -1,10 +1,10 @@ ################################################################################ -# commbase-stt-vosk # +# commbase-stt-vosk-p # # # -# Conversational AI Assistant and AI Hub for Computers and Droids # +# An ASR (Automatic Speech Recognition) engine. # # # # Change History # -# 05/20/2023 Esteban Herrera Original code. # +# 04/29/2023 Esteban Herrera Original code. # # Add new history entries as needed. # # # # # @@ -41,286 +41,294 @@ def read_plain_text_file(file_path): - """ - Read the contents of a plain text file and return it as a string. + """ + Reads the contents of a plain text file and return it as a string. - Parameters: - file_path (str): The path to the plain text file. + Parameters: + file_path (str): The path to the plain text file. - Returns: - str: The content of the file as a string. - """ - file_variable = "" + Returns: + str: The content of the file as a string. + """ + file_variable = "" - # Open the file in read mode - with open(file_path, "r") as file: - # Read the entire contents of the file - file_variable = file.read().rstrip() + # Open the file in read mode + with open(file_path, "r") as file: + # Read the entire contents of the file + file_variable = file.read().rstrip() - # Return the variable with the file contents - return file_variable + # Return the variable with the file contents + return file_variable def read_lines_from_file(file_path): - """ - Read all lines from a file and return them as a list. + """ + Reads all lines from a file and return them as a list. - Parameters: - file_path (str): The path to the file to be read. + Parameters: + file_path (str): The path to the file to be read. - Returns: - list: A list containing all the lines from the file. + Returns: + list: A list containing all the lines from the file. - Example: - >>> lines = read_lines_from_file('file.txt') - >>> print(lines) - ['This is line 1.\n', 'This is line 2.\n', 'This is line 3.\n'] - """ - with open(file_path, 'r') as f: - # Store all the file lines in a list - lines = f.readlines() # Read all lines from the file + Example: + >>> lines = read_lines_from_file('file.txt') + >>> print(lines) + ['This is line 1.\n', 'This is line 2.\n', 'This is line 3.\n'] + """ + with open(file_path, "r") as f: + # Store all the file lines in a list + lines = f.readlines() # Read all lines from the file - # Return all the lines in a list - return lines + # Return all the lines in a list + return lines def load_config_file(): - """ - Loads the configuration file path. + """ + Loads the configuration file path. - Returns: - str: The path of the configuration file. + Returns: + str: The path of the configuration file. - Raises: + Raises: KeyError: If the environment variable 'COMMBASE_APP_DIR' is not set. - """ - try: - # Specify the path of the env file containing the variables - CONFIG_FILE_PATH = os.environ["COMMBASE_APP_DIR"] + '/config/app.conf' - return CONFIG_FILE_PATH - except KeyError: - print("Error: Environment variable 'COMMBASE_APP_DIR' is not set.") - # You can choose to handle the error in different ways, such as returning a default value or raising an exception. - # Here, we choose to return None to indicate the absence of the configuration file path. - return None + """ + try: + # Specify the path of the env file containing the variables + CONFIG_FILE_PATH = os.environ["COMMBASE_APP_DIR"] + "/config/app.conf" + return CONFIG_FILE_PATH + except KeyError: + print("Error: Environment variable 'COMMBASE_APP_DIR' is not set.") + # You can choose to handle the error in different ways, such as + # returning a default value or raising an exception. + # Here, we choose to return None to indicate the absence of the + # configuration file path. + return None def int_or_str(text): - """ - Parses a string input and returns either an integer or the original string. + """ + Parses a string input and returns either an integer or the original string. - This function takes a string input and tries to convert it to an integer. If - successful, it returns the integer value. If not, it returns the original - string. + This function takes a string input and tries to convert it to an integer. If + successful, it returns the integer value. If not, it returns the original + string. - Parameters: - text (str): The input string to be parsed. + Parameters: + text (str): The input string to be parsed. - Returns: - int or str: If the input string can be converted to an integer, the - function returns the integer value. If not, the function returns the - original string. + Returns: + int or str: If the input string can be converted to an integer, the + function returns the integer value. If not, the function returns the + original string. - Raises: - None. - """ - try: - return int(text) - except ValueError: - return text + Raises: + None. + """ + try: + return int(text) + except ValueError: + return text def find_text(string): - """ - Finds the index of the 'text' substring in the input string. + """ + Finds the index of the 'text' substring in the input string. - Parameters: - string (str): The string to search for 'text'. + Parameters: + string (str): The string to search for 'text'. - Returns: - int: The index of the start of 'text' substring in the input string. - If 'text' substring is not found, returns None. - """ - text_start = string.find('"text" : "') - return text_start if text_start != -1 else None + Returns: + int: The index of the start of 'text' substring in the input string. + If 'text' substring is not found, returns None. + """ + text_start = string.find('"text" : "') + return text_start if text_start != -1 else None def strip_string(string): - """ - Strip unwanted characters and whitespaces from the 'text' field of a JSON - string. + """ + Strips unwanted characters and whitespaces from the 'text' field of a JSON + string. - Parameters: - string (str): The input string, assumed to be a valid JSON string with a - 'text' field. + Parameters: + string (str): The input string, assumed to be a valid JSON string with a + 'text' field. - Returns: - str or None: The resulting string after being stripped of unwanted - characters and whitespaces. - Returns None if the input string is None, is not a valid JSON string, or - does not contain - a 'text' field. - """ - if string is None: - return None + Returns: + str or None: The resulting string after being stripped of unwanted + characters and whitespaces. + Returns None if the input string is None, is not a valid JSON string, or + does not contain a 'text' field. + """ + if string is None: + return None - # Load the JSON string into a dictionary - try: - data = json.loads(string) - except ValueError: - return None + # Load the JSON string into a dictionary + try: + data = json.loads(string) + except ValueError: + return None - # Get the text field from the dictionary - if "text" not in data: - return None - text = data["text"] + # Get the text field from the dictionary + if "text" not in data: + return None + text = data["text"] - # Replace unwanted characters and whitespaces - text = text.replace('"', '').strip() + # Replace unwanted characters and whitespaces + text = text.replace('"', "").strip() - # Remove 'the' from the beginning of the text, if present - if text.startswith('the'): - text = text[3:].strip() + # Remove 'the' from the beginning of the text, if present + if text.startswith("the"): + text = text[3:].strip() - # Remove 'the' from the end of the text, if present - if text.endswith('the'): - text = text[:-3].strip() + # Remove 'the' from the end of the text, if present + if text.endswith("the"): + text = text[:-3].strip() - return text + return text def get_chat_participant_names(): - """ - Gets the chat participant names from the config file. - - Reads the 'ASSISTANT_NAME_IN_CHAT_PANE', 'SYSTEM_NAME_IN_CHAT_PANE', and - 'END_USER_NAME_IN_CHAT_PANE' variables from the environment configuration - file. Returns a tuple containing the string values of the variables if - found, or None if any of the variables are not present. - - Returns: - tuple or None: A tuple containing the assistant, system, and end user - names in the chat pane, or None, if any of the variables are not found. - """ - # Initialize variables for the chat names - assistant_name = None - system_name = None - end_user_name = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'END_USER_NAME_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - end_user_name = value.strip()[1:-1] - - elif variable_name == 'ASSISTANT_NAME_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - assistant_name = value.strip()[1:-1] - - elif variable_name == 'SYSTEM_NAME_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - system_name = value.strip()[1:-1] - - # Check if all three variables were found - if assistant_name is not None and system_name is not None and end_user_name is not None: - return end_user_name, assistant_name, system_name - - # If any of the variables are not found, return None - return None + """ + Gets the chat participant names from the config file. + + Reads the 'ASSISTANT_NAME_IN_CHAT_PANE', 'SYSTEM_NAME_IN_CHAT_PANE', and + 'END_USER_NAME_IN_CHAT_PANE' variables from the environment configuration + file. Returns a tuple containing the string values of the variables if + found, or None if any of the variables are not present. + + Returns: + tuple or None: A tuple containing the assistant, system, and end user + names in the chat pane, or None, if any of the variables are not found. + """ + # Initialize variables for the chat names + assistant_name = None + system_name = None + end_user_name = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "END_USER_NAME_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + end_user_name = value.strip()[1:-1] + + elif variable_name == "ASSISTANT_NAME_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + assistant_name = value.strip()[1:-1] + + elif variable_name == "SYSTEM_NAME_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + system_name = value.strip()[1:-1] + + # Check if all three variables were found + if ( + assistant_name is not None + and system_name is not None + and end_user_name is not None + ): + return end_user_name, assistant_name, system_name + + # If any of the variables are not found, return None + return None def get_tts_engine_string(): - """ - Retrieves the TTS (Text-to-Speech) engine string from the configuration file. + """ + Retrieves the TTS (Text-to-Speech) engine string from the configuration + file. - Returns: - str or None: The TTS engine string if found in the configuration file, - otherwise None. - """ - # Initialize variable for the tts engine string - tts_engine_str = None + Returns: + str or None: The TTS engine string if found in the configuration file, + otherwise None. + """ + # Initialize variable for the tts engine string + tts_engine_str = None - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") - # Check if the variable we are looking for exists in the line - if variable_name == 'TTS_ENGINE_STRING': - # Remove the quotes from the value of the variable - tts_engine_str = value.strip()[1:-1] + # Check if the variable we are looking for exists in the line + if variable_name == "TTS_ENGINE_STRING": + # Remove the quotes from the value of the variable + tts_engine_str = value.strip()[1:-1] - # Check if the variable was found - if tts_engine_str is not None: - return tts_engine_str + # Check if the variable was found + if tts_engine_str is not None: + return tts_engine_str - # If the variable was not found, return None - return None + # If the variable was not found, return None + return None def get_manage_result_message_on_and_output_skill_errors_in_pane_on(): - """ - Retrieve the value of the - MANAGE_RESULT_MESSAGE_ON_AND_OUTPUT_SKILL_ERRORS_IN_PANE_ON variable from the - configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - true_or_false_str = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'MANAGE_RESULT_MESSAGE_ON_AND_OUTPUT_SKILL_ERRORS_IN_PANE_ON': - # Remove the quotes from the value of the variable - true_or_false_str = value.strip()[1:-1] - - # Check if the variable was found - if true_or_false_str is not None: - return true_or_false_str - - # If the variable was not found, return None - return None + """ + Retrieves the value of the + MANAGE_RESULT_MESSAGE_ON_AND_OUTPUT_SKILL_ERRORS_IN_PANE_ON variable from + the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + true_or_false_str = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if ( + variable_name + == "MANAGE_RESULT_MESSAGE_ON_AND_OUTPUT_SKILL_ERRORS_IN_PANE_ON" + ): + # Remove the quotes from the value of the variable + true_or_false_str = value.strip()[1:-1] + + # Check if the variable was found + if true_or_false_str is not None: + return true_or_false_str + + # If the variable was not found, return None + return None def get_commbase_stt_vosk_p_parse_control_messages_on(): - """ - Retrieve the value of the COMMBASE_STT_VOSK_P_PARSE_CONTROL_MESSAGES_ON - variable from the configuration file. - - Returns: - str or None: The value of the variable if found, or None if not found. - """ - # Initialize variable for the tts engine string - parse_control_messages = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'COMMBASE_STT_VOSK_P_PARSE_CONTROL_MESSAGES_ON': - # Remove the quotes from the value of the variable - parse_control_messages = value.strip()[1:-1] - - # Check if the variable was found - if parse_control_messages is not None: - return parse_control_messages - - # If the variable was not found, return None - return None - + """ + Retrieves the value of the COMMBASE_STT_VOSK_P_PARSE_CONTROL_MESSAGES_ON + variable from the configuration file. + + Returns: + str or None: The value of the variable if found, or None if not found. + """ + # Initialize variable for the tts engine string + parse_control_messages = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "COMMBASE_STT_VOSK_P_PARSE_CONTROL_MESSAGES_ON": + # Remove the quotes from the value of the variable + parse_control_messages = value.strip()[1:-1] + + # Check if the variable was found + if parse_control_messages is not None: + return parse_control_messages + + # If the variable was not found, return None + return None diff --git a/text_formatting.py b/text_formatting.py index 7d90d51..45b3bf3 100644 --- a/text_formatting.py +++ b/text_formatting.py @@ -1,10 +1,10 @@ ################################################################################ -# commbase-stt-vosk # +# commbase-stt-vosk-p # # # -# Conversational AI Assistant and AI Hub for Computers and Droids # +# An ASR (Automatic Speech Recognition) engine. # # # # Change History # -# 05/20/2023 Esteban Herrera Original code. # +# 04/29/2023 Esteban Herrera Original code. # # Add new history entries as needed. # # # # # @@ -31,508 +31,685 @@ # text_formatting.py # This file provides utility functions for working with terminal formatting. It -# includes functions to modify the text color, background color, and text +# includes functions to modify the text color, background color, and text # formatting in the terminal output. These functions can be used to enhance the # visual appearance and readability of command-line interfaces. +# Requires os.path already imported +# Requires to import load_config_file from functions in case CONFIG_FILE_PATH is +# not used # Requirements -import os.path from config import CONFIG_FILE_PATH -from functions import load_config_file def get_terminal_colors(): - """ - Gets the terminal colors from the config file. - - Reads the environment variables from the environment configuration file. - Returns a tuple containing the string values of the variables if found, or - None if any of the variables are not present. - - Returns: - tuple or None: A tuple containing the terminal colors, or None, if any - of the variables are not found. - """ - # Initialize variables for the background colors - red_background_color_code_start = None - green_background_color_code_start = None - yellow_background_color_code_start = None - blue_background_color_code_start = None - magenta_background_color_code_start = None - cyan_background_color_code_start = None - white_background_color_code_start = None - black_background_color_code_start = None - - # Initialize variables for the text colors - red_text_color_code_start = None - green_text_color_code_start = None - yellow_text_color_code_start = None - blue_text_color_code_start = None - magenta_text_color_code_start = None - cyan_text_color_code_start = None - white_text_color_code_start = None - black_text_color_code_start = None - - # Initialize the varoable for the end of the color - color_code_end = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the file - if variable_name == 'TERMINAL_RED_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - red_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_GREEN_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - green_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_YELLOW_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - yellow_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_BLUE_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - blue_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_MAGENTA_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - magenta_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_CYAN_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - cyan_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_WHITE_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - white_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_BLACK_BACKGROUND_COLOR_CODE_START': - # Remove the quotes from the value of the variable - black_background_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_RED_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - red_text_color_code_start = value.strip()[1:-1] - - # Check if the variable we are looking for exists in the line - elif variable_name == 'TERMINAL_GREEN_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - green_text_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_YELLOW_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - yellow_text_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_BLUE_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - blue_text_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_MAGENTA_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - magenta_text_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_CYAN_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - cyan_text_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_WHITE_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - white_text_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_BLACK_TEXT_COLOR_CODE_START': - # Remove the quotes from the value of the variable - black_text_color_code_start = value.strip()[1:-1] - - elif variable_name == 'TERMINAL_COLOR_CODE_END': - # Remove the quotes from the value of the variable - color_code_end = value.strip()[1:-1] - - # Check if all the variables were found - if red_background_color_code_start is not None and green_background_color_code_start is not None and yellow_background_color_code_start is not None and blue_background_color_code_start is not None and magenta_background_color_code_start is not None and cyan_background_color_code_start is not None and white_background_color_code_start is not None and black_background_color_code_start is not None and red_text_color_code_start is not None and green_text_color_code_start is not None and yellow_text_color_code_start is not None and blue_text_color_code_start is not None and magenta_text_color_code_start is not None and cyan_text_color_code_start is not None and white_text_color_code_start is not None and black_text_color_code_start is not None and color_code_end is not None: - return red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end - - # If any of the variables are not found, return None - return None + """ + Gets the terminal colors from the config file. + + Reads the environment variables from the environment configuration file. + Returns a tuple containing the string values of the variables if found, or + None if any of the variables are not present. + + Returns: + tuple or None: A tuple containing the terminal colors, or None, if any + of the variables are not found. + """ + # Initialize variables for the background colors + red_background_color_code_start = None + green_background_color_code_start = None + yellow_background_color_code_start = None + blue_background_color_code_start = None + magenta_background_color_code_start = None + cyan_background_color_code_start = None + white_background_color_code_start = None + black_background_color_code_start = None + + # Initialize variables for the text colors + red_text_color_code_start = None + green_text_color_code_start = None + yellow_text_color_code_start = None + blue_text_color_code_start = None + magenta_text_color_code_start = None + cyan_text_color_code_start = None + white_text_color_code_start = None + black_text_color_code_start = None + + # Initialize the variable for the end of the color + color_code_end = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the file + if variable_name == "TERMINAL_RED_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + red_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_GREEN_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + green_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_YELLOW_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + yellow_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_BLUE_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + blue_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_MAGENTA_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + magenta_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_CYAN_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + cyan_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_WHITE_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + white_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_BLACK_BACKGROUND_COLOR_CODE_START": + # Remove the quotes from the value of the variable + black_background_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_RED_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + red_text_color_code_start = value.strip()[1:-1] + + # Check if the variable we are looking for exists in the line + elif variable_name == "TERMINAL_GREEN_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + green_text_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_YELLOW_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + yellow_text_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_BLUE_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + blue_text_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_MAGENTA_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + magenta_text_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_CYAN_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + cyan_text_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_WHITE_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + white_text_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_BLACK_TEXT_COLOR_CODE_START": + # Remove the quotes from the value of the variable + black_text_color_code_start = value.strip()[1:-1] + + elif variable_name == "TERMINAL_COLOR_CODE_END": + # Remove the quotes from the value of the variable + color_code_end = value.strip()[1:-1] + + # Check if all the variables were found + if ( + red_background_color_code_start is not None + and green_background_color_code_start is not None + and yellow_background_color_code_start is not None + and blue_background_color_code_start is not None + and magenta_background_color_code_start is not None + and cyan_background_color_code_start is not None + and white_background_color_code_start is not None + and black_background_color_code_start is not None + and red_text_color_code_start is not None + and green_text_color_code_start is not None + and yellow_text_color_code_start is not None + and blue_text_color_code_start is not None + and magenta_text_color_code_start is not None + and cyan_text_color_code_start is not None + and white_text_color_code_start is not None + and black_text_color_code_start is not None + and color_code_end is not None + ): + return ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) + + # If any of the variables are not found, return None + return None def get_chat_participant_colors(): - """ - Gets the chat participant background and text colors from the config file. - - Reads the assistant, system, and end user variables from the environment - configuration file. Returns a tuple containing the string values of the - variables if found, or None if any of the variables are not present. - - Returns: - tuple or None: A tuple containing the assistant, system, and end user - background and text colors in the chat pane, or None, if any of the - variables are not found. - """ - # Initialize variables for the colors of the chat participants - end_user_background_color = None - assistant_background_color = None - system_background_color = None - end_user_text_color = None - assistant_text_color = None - system_text_color = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'END_USER_BACKGROUND_COLOR_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - end_user_background_color = value.strip()[1:-1] - - elif variable_name == 'ASSISTANT_BACKGROUND_COLOR_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - assistant_background_color = value.strip()[1:-1] - - elif variable_name == 'SYSTEM_BACKGROUND_COLOR_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - system_background_color = value.strip()[1:-1] - - elif variable_name == 'END_USER_TEXT_COLOR_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - end_user_text_color = value.strip()[1:-1] - - elif variable_name == 'ASSISTANT_TEXT_COLOR_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - assistant_text_color = value.strip()[1:-1] - - elif variable_name == 'SYSTEM_TEXT_COLOR_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - system_text_color = value.strip()[1:-1] - - # Check if all six variables were found - if end_user_background_color is not None and assistant_background_color is not None and system_background_color is not None and end_user_text_color is not None and assistant_text_color is not None and system_text_color is not None: - return end_user_background_color, assistant_background_color, system_background_color, end_user_text_color, assistant_text_color, system_text_color - - # If any of the variables are not found, return None - return None + """ + Gets the chat participant background and text colors from the config file. + + Reads the assistant, system, and end user variables from the environment + configuration file. Returns a tuple containing the string values of the + variables if found, or None if any of the variables are not present. + + Returns: + tuple or None: A tuple containing the assistant, system, and end user + background and text colors in the chat pane, or None, if any of the + variables are not found. + """ + # Initialize variables for the colors of the chat participants + end_user_background_color = None + assistant_background_color = None + system_background_color = None + end_user_text_color = None + assistant_text_color = None + system_text_color = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "END_USER_BACKGROUND_COLOR_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + end_user_background_color = value.strip()[1:-1] + + elif variable_name == "ASSISTANT_BACKGROUND_COLOR_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + assistant_background_color = value.strip()[1:-1] + + elif variable_name == "SYSTEM_BACKGROUND_COLOR_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + system_background_color = value.strip()[1:-1] + + elif variable_name == "END_USER_TEXT_COLOR_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + end_user_text_color = value.strip()[1:-1] + + elif variable_name == "ASSISTANT_TEXT_COLOR_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + assistant_text_color = value.strip()[1:-1] + + elif variable_name == "SYSTEM_TEXT_COLOR_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + system_text_color = value.strip()[1:-1] + + # Check if all six variables were found + if ( + end_user_background_color is not None + and assistant_background_color is not None + and system_background_color is not None + and end_user_text_color is not None + and assistant_text_color is not None + and system_text_color is not None + ): + return ( + end_user_background_color, + assistant_background_color, + system_background_color, + end_user_text_color, + assistant_text_color, + system_text_color, + ) + + # If any of the variables are not found, return None + return None def get_assistant_avatar_color(): - """ - Gets the assistant avatar color from the config file. - - Reads the 'ASSISTANT_AVATAR_COLOR_IN_CHAT_PANE' variable from the - environment configuration file. Returns the string value of the variable if - found, or None if the variable is not present. - - Returns: - srt or None: A string containing the assitant avatar color in the chat - pane, or None, if the variable is not found. - """ - # Initialize variable for the avatar color - avatar_color = None - - # Open the file and read its contents - with open(CONFIG_FILE_PATH, 'r') as f: - for line in f: - # Split the line into variable name and value - variable_name, value = line.strip().split('=') - - # Check if the variable we are looking for exists in the line - if variable_name == 'ASSISTANT_AVATAR_COLOR_IN_CHAT_PANE': - # Remove the quotes from the value of the variable - avatar_color = value.strip()[1:-1] - - # Check if the variable was found - if avatar_color is not None: - return avatar_color - - # If the variable was not found, return None - return None + """ + Gets the assistant avatar color from the config file. + + Reads the 'ASSISTANT_AVATAR_COLOR_IN_CHAT_PANE' variable from the + environment configuration file. Returns the string value of the variable if + found, or None if the variable is not present. + + Returns: + srt or None: A string containing the assitant avatar color in the chat + pane, or None, if the variable is not found. + """ + # Initialize variable for the avatar color + avatar_color = None + + # Open the file and read its contents + with open(CONFIG_FILE_PATH, "r") as f: + for line in f: + # Split the line into variable name and value + variable_name, value = line.strip().split("=") + + # Check if the variable we are looking for exists in the line + if variable_name == "ASSISTANT_AVATAR_COLOR_IN_CHAT_PANE": + # Remove the quotes from the value of the variable + avatar_color = value.strip()[1:-1] + + # Check if the variable was found + if avatar_color is not None: + return avatar_color + + # If the variable was not found, return None + return None def set_end_user_background_color(end_user_background_color): - """ - Sets the background color for the END USER. - - Parameters: - end_user_background_color (str): The desired background color for the - END USER. - Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', - 'cyan', 'white', 'black'. - - Returns: - str: The start code for the selected background color. - - Notes: - This function internally uses the get_terminal_colors() function to - obtain the necessary color codes. - """ - # Assign the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the END USER user background color - if end_user_background_color == 'red': - end_user_background_color_start = red_background_color_code_start - elif end_user_background_color == 'green': - end_user_background_color_start = green_background_color_code_start - elif end_user_background_color == 'yellow': - end_user_background_color_start = yellow_background_color_code_start - elif end_user_background_color == 'blue': - end_user_background_color_start = blue_background_color_code_start - elif end_user_background_color == 'magenta': - end_user_background_color_start = magenta_background_color_code_start - elif end_user_background_color == 'cyan': - end_user_background_color_start = cyan_background_color_code_start - elif end_user_background_color == 'white': - end_user_background_color_start = white_background_color_code_start - elif end_user_background_color == 'black': - end_user_background_color_start = black_background_color_code_start - - return end_user_background_color_start + """ + Sets the background color for the END USER. + + Parameters: + end_user_background_color (str): The desired background color for the + END USER. + Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', + 'cyan', 'white', 'black'. + + Returns: + str: The start code for the selected background color. + + Notes: + This function internally uses the get_terminal_colors() function to + obtain the necessary color codes. + """ + # Assign the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the END USER user background color + if end_user_background_color == "red": + end_user_background_color_start = red_background_color_code_start + elif end_user_background_color == "green": + end_user_background_color_start = green_background_color_code_start + elif end_user_background_color == "yellow": + end_user_background_color_start = yellow_background_color_code_start + elif end_user_background_color == "blue": + end_user_background_color_start = blue_background_color_code_start + elif end_user_background_color == "magenta": + end_user_background_color_start = magenta_background_color_code_start + elif end_user_background_color == "cyan": + end_user_background_color_start = cyan_background_color_code_start + elif end_user_background_color == "white": + end_user_background_color_start = white_background_color_code_start + elif end_user_background_color == "black": + end_user_background_color_start = black_background_color_code_start + + return end_user_background_color_start def set_assistant_user_background_color(assistant_background_color): - """ - Sets the background color for the ASSISTANT user. - - Parameters: - assistant_background_color (str): The desired background color for the - ASSISTANT user. - Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', - 'cyan', 'white', 'black'. - - Returns: - str: The start code for the selected background color. - - Notes: - This function internally uses the get_terminal_colors() function to obtain - the necessary color codes. - """ - # Assign the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the ASSISTANT user background color - if assistant_background_color == 'red': - assistant_background_color_start = red_background_color_code_start - elif assistant_background_color == 'green': - assistant_background_color_start = green_background_color_code_start - elif assistant_background_color == 'yellow': - assistant_background_color_start = yellow_background_color_code_start - elif assistant_background_color == 'blue': - assistant_background_color_start = blue_background_color_code_start - elif assistant_background_color == 'magenta': - assistant_background_color_start = magenta_background_color_code_start - elif assistant_background_color == 'cyan': - assistant_background_color_start = cyan_background_color_code_start - elif assistant_background_color == 'white': - assistant_background_color_start = white_background_color_code_start - elif assistant_background_color == 'black': - assistant_background_color_start = black_background_color_code_start - - return assistant_background_color_start + """ + Sets the background color for the ASSISTANT user. + + Parameters: + assistant_background_color (str): The desired background color for the + ASSISTANT user. + Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', + 'cyan', 'white', 'black'. + + Returns: + str: The start code for the selected background color. + + Notes: + This function internally uses the get_terminal_colors() function to + obtain the necessary color codes. + """ + # Assign the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the ASSISTANT user background color + if assistant_background_color == "red": + assistant_background_color_start = red_background_color_code_start + elif assistant_background_color == "green": + assistant_background_color_start = green_background_color_code_start + elif assistant_background_color == "yellow": + assistant_background_color_start = yellow_background_color_code_start + elif assistant_background_color == "blue": + assistant_background_color_start = blue_background_color_code_start + elif assistant_background_color == "magenta": + assistant_background_color_start = magenta_background_color_code_start + elif assistant_background_color == "cyan": + assistant_background_color_start = cyan_background_color_code_start + elif assistant_background_color == "white": + assistant_background_color_start = white_background_color_code_start + elif assistant_background_color == "black": + assistant_background_color_start = black_background_color_code_start + + return assistant_background_color_start def set_system_user_background_color(system_background_color): - """ - Sets the background color for the SYSTEM user. - - Parameters: - system_background_color (str): The desired background color for the SYSTEM - user. - Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', - 'cyan', 'white', 'black'. - - Returns: - str: The start code for the selected background color. - - Notes: - This function internally uses the get_terminal_colors() function to obtain - the necessary color codes. - """ - # Assign the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the SYSTEM user background color - if system_background_color == 'red': - system_background_color_start = red_background_color_code_start - elif system_background_color == 'green': - system_background_color_start = green_background_color_code_start - elif system_background_color == 'yellow': - system_background_color_start = yellow_background_color_code_start - elif system_background_color == 'blue': - system_background_color_start = blue_background_color_code_start - elif system_background_color == 'magenta': - system_background_color_start = magenta_background_color_code_start - elif system_background_color == 'cyan': - system_background_color_start = cyan_background_color_code_start - elif system_background_color == 'white': - system_background_color_start = white_background_color_code_start - elif system_background_color == 'black': - system_background_color_start = black_background_color_code_start - - return system_background_color_start + """ + Sets the background color for the SYSTEM user. + + Parameters: + system_background_color (str): The desired background color for the + SYSTEM user. + Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', + 'cyan', 'white', 'black'. + + Returns: + str: The start code for the selected background color. + + Notes: + This function internally uses the get_terminal_colors() function to + obtain the necessary color codes. + """ + # Assign the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the SYSTEM user background color + if system_background_color == "red": + system_background_color_start = red_background_color_code_start + elif system_background_color == "green": + system_background_color_start = green_background_color_code_start + elif system_background_color == "yellow": + system_background_color_start = yellow_background_color_code_start + elif system_background_color == "blue": + system_background_color_start = blue_background_color_code_start + elif system_background_color == "magenta": + system_background_color_start = magenta_background_color_code_start + elif system_background_color == "cyan": + system_background_color_start = cyan_background_color_code_start + elif system_background_color == "white": + system_background_color_start = white_background_color_code_start + elif system_background_color == "black": + system_background_color_start = black_background_color_code_start + + return system_background_color_start def set_end_user_text_color(end_user_text_color): - """ - Sets the text color for the END USER user. - - Parameters: - end_user_text_color (str): The desired text color for the END USER user. - Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', - 'cyan', 'white', 'black'. - - Returns: - str: The start code for the selected text color. - - Notes: - This function internally uses the get_terminal_colors() function to obtain - the necessary color codes. - """ - # Assign the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the END USER user text color - if end_user_text_color == 'red': - end_user_text_color_start = red_text_color_code_start - elif end_user_text_color == 'green': - end_user_text_color_start = green_text_color_code_start - elif end_user_text_color == 'yellow': - end_user_text_color_start = yellow_text_color_code_start - elif end_user_text_color == 'blue': - end_user_text_color_start = blue_text_color_code_start - elif end_user_text_color == 'magenta': - end_user_text_color_start = magenta_text_color_code_start - elif end_user_text_color == 'cyan': - end_user_text_color_start = cyan_text_color_code_start - elif end_user_text_color == 'white': - end_user_text_color_start = white_text_color_code_start - elif end_user_text_color == 'black': - end_user_text_color_start = black_text_color_code_start - - return end_user_text_color_start + """ + Sets the text color for the END USER user. + + Parameters: + end_user_text_color (str): The desired text color for the END USER user. + Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', + 'cyan', 'white', 'black'. + + Returns: + str: The start code for the selected text color. + + Notes: + This function internally uses the get_terminal_colors() function to + obtain the necessary color codes. + """ + # Assign the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the END USER user text color + if end_user_text_color == "red": + end_user_text_color_start = red_text_color_code_start + elif end_user_text_color == "green": + end_user_text_color_start = green_text_color_code_start + elif end_user_text_color == "yellow": + end_user_text_color_start = yellow_text_color_code_start + elif end_user_text_color == "blue": + end_user_text_color_start = blue_text_color_code_start + elif end_user_text_color == "magenta": + end_user_text_color_start = magenta_text_color_code_start + elif end_user_text_color == "cyan": + end_user_text_color_start = cyan_text_color_code_start + elif end_user_text_color == "white": + end_user_text_color_start = white_text_color_code_start + elif end_user_text_color == "black": + end_user_text_color_start = black_text_color_code_start + + return end_user_text_color_start def set_assistant_user_text_color(assistant_text_color): - """ - Sets the text color for the ASSISTANT user. - - Parameters: - assistant_text_color (str): The desired text color for the ASSISTANT user. - Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', - 'cyan', 'white', 'black'. - - Returns: - str: The start code for the selected text color. - - Notes: - This function internally uses the get_terminal_colors() function to obtain - the necessary color codes. - """ - # Assign the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the ASSISTANT user text color - if assistant_text_color == 'red': - assistant_text_color_start = red_text_color_code_start - elif assistant_text_color == 'green': - assistant_text_color_start = green_text_color_code_start - elif assistant_text_color == 'yellow': - assistant_text_color_start = yellow_text_color_code_start - elif assistant_text_color == 'blue': - assistant_text_color_start = blue_text_color_code_start - elif assistant_text_color == 'magenta': - assistant_text_color_start = magenta_text_color_code_start - elif assistant_text_color == 'cyan': - assistant_text_color_start = cyan_text_color_code_start - elif assistant_text_color == 'white': - assistant_text_color_start = white_text_color_code_start - elif assistant_text_color == 'black': - assistant_text_color_start = black_text_color_code_start - - return assistant_text_color_start + """ + Sets the text color for the ASSISTANT user. + + Parameters: + assistant_text_color (str): The desired text color for the ASSISTANT + user. + Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', + 'cyan', 'white', 'black'. + + Returns: + str: The start code for the selected text color. + + Notes: + This function internally uses the get_terminal_colors() function to + obtain the necessary color codes. + """ + # Assign the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the ASSISTANT user text color + if assistant_text_color == "red": + assistant_text_color_start = red_text_color_code_start + elif assistant_text_color == "green": + assistant_text_color_start = green_text_color_code_start + elif assistant_text_color == "yellow": + assistant_text_color_start = yellow_text_color_code_start + elif assistant_text_color == "blue": + assistant_text_color_start = blue_text_color_code_start + elif assistant_text_color == "magenta": + assistant_text_color_start = magenta_text_color_code_start + elif assistant_text_color == "cyan": + assistant_text_color_start = cyan_text_color_code_start + elif assistant_text_color == "white": + assistant_text_color_start = white_text_color_code_start + elif assistant_text_color == "black": + assistant_text_color_start = black_text_color_code_start + + return assistant_text_color_start def set_system_user_text_color(system_text_color): - """ - Sets the text color for the SYSTEM user. - - Parameters: - system_text_color (str): The desired text color for the SYSTEM user. - Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', - 'cyan', 'white', 'black'. - - Returns: - str: The start code for the selected text color. - - Notes: - This function internally uses the get_terminal_colors() function to obtain - the necessary color codes. - """ - # Assign the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the SYSTEM user text color - if system_text_color == 'red': - system_text_color_start = red_text_color_code_start - elif system_text_color == 'green': - system_text_color_start = green_text_color_code_start - elif system_text_color == 'yellow': - system_text_color_start = yellow_text_color_code_start - elif system_text_color == 'blue': - system_text_color_start = blue_text_color_code_start - elif system_text_color == 'magenta': - system_text_color_start = magenta_text_color_code_start - elif system_text_color == 'cyan': - system_background_color_start = cyan_text_color_code_start - elif system_text_color == 'white': - system_text_color_start = white_text_color_code_start - elif system_text_color == 'black': - system_text_color_start = black_text_color_code_start - - return system_text_color_start + """ + Sets the text color for the SYSTEM user. + + Parameters: + system_text_color (str): The desired text color for the SYSTEM user. + Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', + 'cyan', 'white', 'black'. + + Returns: + str: The start code for the selected text color. + + Notes: + This function internally uses the get_terminal_colors() function to + obtain the necessary color codes. + """ + # Assign the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the SYSTEM user text color + if system_text_color == "red": + system_text_color_start = red_text_color_code_start + elif system_text_color == "green": + system_text_color_start = green_text_color_code_start + elif system_text_color == "yellow": + system_text_color_start = yellow_text_color_code_start + elif system_text_color == "blue": + system_text_color_start = blue_text_color_code_start + elif system_text_color == "magenta": + system_text_color_start = magenta_text_color_code_start + elif system_text_color == "cyan": + system_text_color_start = cyan_text_color_code_start + elif system_text_color == "white": + system_text_color_start = white_text_color_code_start + elif system_text_color == "black": + system_text_color_start = black_text_color_code_start + + return system_text_color_start def set_assistant_avatar_color(avatar_color): - """ - Sets the avatar color for the assistant. - - Parameters: - avatar_color (str): The desired color for the assistant's avatar. - Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', - 'cyan', 'white', 'black'. - - Returns: - str: The start code for the selected avatar color. - - Notes: - This function internally uses the get_terminal_colors() function to obtain - the necessary color codes. - """ - # Assign the values returned by get_terminal_colors() - red_background_color_code_start, green_background_color_code_start, yellow_background_color_code_start, blue_background_color_code_start, magenta_background_color_code_start, cyan_background_color_code_start, white_background_color_code_start, black_background_color_code_start, red_text_color_code_start, green_text_color_code_start, yellow_text_color_code_start, blue_text_color_code_start, magenta_text_color_code_start, cyan_text_color_code_start, white_text_color_code_start, black_text_color_code_start, color_code_end = get_terminal_colors() - - # Set the assistant avatar color - if avatar_color == "red": - avatar_color_start = red_text_color_code_start - elif avatar_color == "green": - avatar_color_start = green_text_color_code_start - elif avatar_color == "yellow": - avatar_color_start = yellow_text_color_code_start - elif avatar_color == "blue": - avatar_color_start = blue_text_color_code_start - elif avatar_color == "magenta": - avatar_color_start = magenta_text_color_code_start - elif avatar_color == "cyan": - avatar_color_start = cyan_text_color_code_start - elif avatar_color == "white": - avatar_color_start = white_text_color_code_start - elif avatar_color == "black": - avatar_color_start = black_text_color_code_start - - return avatar_color_start - + """ + Sets the avatar color for the assistant. + + Parameters: + avatar_color (str): The desired color for the assistant's avatar. + Valid options are: 'red', 'green', 'yellow', 'blue', 'magenta', + 'cyan', 'white', 'black'. + + Returns: + str: The start code for the selected avatar color. + + Notes: + This function internally uses the get_terminal_colors() function to + obtain the necessary color codes. + """ + # Assign the values returned by get_terminal_colors() + ( + red_background_color_code_start, + green_background_color_code_start, + yellow_background_color_code_start, + blue_background_color_code_start, + magenta_background_color_code_start, + cyan_background_color_code_start, + white_background_color_code_start, + black_background_color_code_start, + red_text_color_code_start, + green_text_color_code_start, + yellow_text_color_code_start, + blue_text_color_code_start, + magenta_text_color_code_start, + cyan_text_color_code_start, + white_text_color_code_start, + black_text_color_code_start, + color_code_end, + ) = get_terminal_colors() + + # Set the assistant avatar color + if avatar_color == "red": + avatar_color_start = red_text_color_code_start + elif avatar_color == "green": + avatar_color_start = green_text_color_code_start + elif avatar_color == "yellow": + avatar_color_start = yellow_text_color_code_start + elif avatar_color == "blue": + avatar_color_start = blue_text_color_code_start + elif avatar_color == "magenta": + avatar_color_start = magenta_text_color_code_start + elif avatar_color == "cyan": + avatar_color_start = cyan_text_color_code_start + elif avatar_color == "white": + avatar_color_start = white_text_color_code_start + elif avatar_color == "black": + avatar_color_start = black_text_color_code_start + + return avatar_color_start