def _create_skill_response(cls, response): # -------------------------------------------------------------------------------------------------------------- # For existing skills (basic skills) # -------------------------------------------------------------------------------------------------------------- basic_skills = db.get_documents(collection='enabled_basic_skills') response = response + basic_skills_format for skill_id, skill in enumerate(basic_skills, start=1): response = response + basic_skills_body_format.format(skill_id, skill.get('name'), skill.get('description'), skill.get('tags') ) # -------------------------------------------------------------------------------------------------------------- # For learned skills (created from 'learn' skill) # -------------------------------------------------------------------------------------------------------------- skills = db.get_documents(collection='learned_skills') response = response + learned_skills_format for skill_id, skill in enumerate(skills, start=1): response = response + learned_skills_body_format.format(skill_id, skill.get('name'), skill.get('tags'), skill.get('response') ) return response
def setUp(self): all_skills = { MongoCollections.CONTROL_SKILLS.value: CONTROL_SKILLS, MongoCollections.ENABLED_BASIC_SKILLS.value: ENABLED_BASIC_SKILLS, } for collection, documents in all_skills.items(): db.update_collection(collection, documents) default_assistant_name = settings.DEFAULT_GENERAL_SETTINGS[ 'assistant_name'] default_enabled_period = settings.DEFAULT_GENERAL_SETTINGS[ 'enabled_period'] default_input_mode = settings.DEFAULT_GENERAL_SETTINGS['input_mode'] default_response_in_speech = settings.DEFAULT_GENERAL_SETTINGS[ 'response_in_speech'] default_settings = { 'assistant_name': default_assistant_name, 'enabled_period': default_enabled_period, 'input_mode': default_input_mode, 'response_in_speech': default_response_in_speech, } db.update_collection( collection=MongoCollections.GENERAL_SETTINGS.value, documents=[default_settings]) # Add assistant name in skill 'enable_assistant' + 'assistant_check' tags assistant_name = db.get_documents( collection=MongoCollections.GENERAL_SETTINGS.value )[0]['assistant_name'] # Update enable_assistant skill existing_enable_assistant_tags = db.get_documents( collection=MongoCollections.CONTROL_SKILLS.value, key={'name': 'enable_assistant'})[0]['tags'] new_enable_assistant_tags = { 'tags': existing_enable_assistant_tags + ' ' + assistant_name } db.update_document(collection=MongoCollections.CONTROL_SKILLS.value, query={'name': 'enable_assistant'}, new_value=new_enable_assistant_tags) # Update assistant_check existing_assistant_check_tags = db.get_documents( collection=MongoCollections.ENABLED_BASIC_SKILLS.value, key={'name': 'assistant_check'})[0]['tags'] new_assistant_check_tags = { 'tags': existing_assistant_check_tags + ' ' + assistant_name } db.update_document( collection=MongoCollections.ENABLED_BASIC_SKILLS.value, query={'name': 'assistant_check'}, new_value=new_assistant_check_tags)
def enable_assistant(cls, **kwargs): """ Plays activation sound and creates the assistant response according to the day hour. """ input_mode = db.get_documents(collection=MongoCollections. GENERAL_SETTINGS.value)[0]['input_mode'] if input_mode == InputMode.VOICE.value: play_activation_sound()
def _create_skill_response(cls, response): # -------------------------------------------------------------------------------------------------------------- # For existing skills (basic skills) # -------------------------------------------------------------------------------------------------------------- basic_skills = db.get_documents(collection='enabled_basic_skills') response = response + '* Basic Enabled Skills:' + '\n' for skill_id, skill in enumerate(basic_skills, start=1): response = response + '{0}) '.format(skill_id) + skill.get('description') + '\n' # -------------------------------------------------------------------------------------------------------------- # For learned skills (created from 'learn' skill) # -------------------------------------------------------------------------------------------------------------- skills = db.get_documents(collection='learned_skills') response = response + '\n' + '* Learned Skills:' + '\n' for skill_id, skill in enumerate(skills, start=1): message = 'Learned skill - Q: ' + skill.get('tags') + ' | R: ' + skill.get('response') response = response + '{0}) '.format(skill_id) + message + '\n' return response
def show_history_log(cls, voice_transcript, skill): """ This method cls.consoles user commands history & assistant responses. """ limit = cls._extract_history_limit(voice_transcript, skill) limit = limit if limit else cls.default_limit documents = db.get_documents(collection='history', limit=limit) response = cls._create_response(documents) cls.console(response)
def enable_assistant(cls, **kwargs): """ Plays activation sound and creates the assistant response according to the day hour. """ input_mode = db.get_documents(collection=MongoCollections. GENERAL_SETTINGS.value)[0]['input_mode'] if input_mode == InputMode.VOICE.value: try: play_activation_sound() except Exception as e: logging.error( "Error with the execution of skill with message {0}". format(e)) cls.response("Sorry I faced an issue")
def console_output(self, text=''): clear() # ------------------------------------------------------------------------------------------------------------- # Logo sector # ------------------------------------------------------------------------------------------------------------- stdout_print(jarvis_logo) stdout_print(" NOTE: CTRL + C If you want to Quit.") # ------------------------------------------------------------------------------------------------------------- # General info sector # ------------------------------------------------------------------------------------------------------------- settings_documents = db.get_documents(collection=MongoCollections.GENERAL_SETTINGS.value) if settings_documents: settings = settings_documents[0] print(OutputStyler.HEADER + console.add_dashes('GENERAL INFO') + OutputStyler.ENDC) enabled = OutputStyler.GREEN + 'ENABLED' + OutputStyler.ENDC if settings['response_in_speech'] else OutputStyler.WARNING + 'NOT ENABLED' + OutputStyler.ENDC print(OutputStyler.BOLD + 'RESPONSE IN SPEECH: ' + enabled) print(OutputStyler.BOLD + 'ENABLED PERIOD: ' + OutputStyler.GREEN + '{0}'.format(str(settings['enabled_period'])) + OutputStyler.ENDC + OutputStyler.ENDC) print(OutputStyler.BOLD + 'INPUT MODE: ' + OutputStyler.GREEN + '{0}'.format(settings['input_mode'].upper() + OutputStyler.ENDC) + OutputStyler.ENDC) # ------------------------------------------------------------------------------------------------------------- # System info sector # ------------------------------------------------------------------------------------------------------------- print(OutputStyler.HEADER + console.add_dashes('SYSTEM') + OutputStyler.ENDC) print(OutputStyler.BOLD + 'RAM USAGE: {0:.2f} GB'.format(self._get_memory()) + OutputStyler.ENDC) # ------------------------------------------------------------------------------------------------------------- # Assistant logs sector # ------------------------------------------------------------------------------------------------------------- print(OutputStyler.HEADER + console.add_dashes('LOG') + OutputStyler.ENDC) lines = subprocess.check_output(['tail', '-10', ROOT_LOG_CONF['handlers']['file']['filename']]).decode("utf-8") print(OutputStyler.BOLD + lines + OutputStyler.ENDC) # ------------------------------------------------------------------------------------------------------------- # Assistant input/output sector # ------------------------------------------------------------------------------------------------------------- print(OutputStyler.HEADER + console.add_dashes('ASSISTANT') + OutputStyler.ENDC) text = text if text else '' if text: print(OutputStyler.BOLD + '> ' + text + '\r' + OutputStyler.ENDC) print(OutputStyler.HEADER + console.add_dashes('-') + OutputStyler.ENDC)
def set_engine(cls): if not cls.engine and not db.is_collection_empty( collection='general_settings'): cls.engine = engines.TTSEngine() if db.get_documents(collection='general_settings')[0]['response_in_speech']\ else engines.TTTEngine()
def console_output(self, text='', debug_log=None, info_log=None, warn_log=None, error_log=None, refresh_console=True): """ This method creates the assistant output. The output has four sectors: * GENERAL INFO: Info about assistant settigs * SYSTEM: System info e.g assistant memory usage * LOG: Assistant log last lines * ASSISTANT: Assistant response output Output example: ██╗ █████╗ ██████╗ ██╗ ██╗██╗███████╗ ██║██╔══██╗██╔══██╗██║ ██║██║██╔════╝ ██║███████║██████╔╝██║ ██║██║███████╗ ██ ██║██╔══██║██╔══██╗╚██╗ ██╔╝██║╚════██║ ╚█████╔╝██║ ██║██║ ██║ ╚████╔╝ ██║███████║ ╚════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═══╝ ╚═╝╚══════╝ NOTE: CTRL + C If you want to Quit. -------------------------------------- GENERAL INFO --------------------------------------------- RESPONSE IN SPEECH: NOT ENABLED INPUT MODE: TEXT ---------------------------------------- SYSTEM -------------------------------------------------- RAM USAGE: 0.14 GB ----------------------------------------- LOG ---------------------------------------------------- 2020-04-25 19:22:51,524 - root - INFO - Startup checks.. 2020-04-25 19:22:51,534 - root - DEBUG - Internet connection check.. 2020-04-25 19:22:51,773 - root - INFO - Internet connection passed! 2020-04-25 19:22:51,783 - root - INFO - Application started ---------------------------------------- ASSISTANT ------------------------------------------------ > The current date is: 2020-04-25 -------------------------------------------- - ---------------------------------------------------- """ if refresh_console: self.clear() # ---------------------------------------------------------------------------------------------------------- # Logo sector # ---------------------------------------------------------------------------------------------------------- self._stdout_print(jarvis_logo + start_text) self._stdout_print(" NOTE: CTRL + C If you want to Quit.") # ---------------------------------------------------------------------------------------------------------- # General info sector # ---------------------------------------------------------------------------------------------------------- settings_documents = db.get_documents(collection=MongoCollections.GENERAL_SETTINGS.value) if settings_documents: settings_ = settings_documents[0] print(OutputStyler.HEADER + headerize('GENERAL INFO') + OutputStyler.ENDC) enabled = OutputStyler.GREEN + 'ENABLED' + OutputStyler.ENDC if settings_['response_in_speech'] else OutputStyler.WARNING + 'NOT ENABLED' + OutputStyler.ENDC print(OutputStyler.BOLD + 'RESPONSE IN SPEECH: ' + enabled) print(OutputStyler.BOLD + 'INPUT MODE: ' + OutputStyler.GREEN + '{0}'.format(settings_['input_mode'].upper() + OutputStyler.ENDC) + OutputStyler.ENDC) if settings_['input_mode'] == InputMode.VOICE.value: print(OutputStyler.BOLD + 'NOTE: ' + OutputStyler.GREEN + "Include " + "'{0}'".format(settings_['assistant_name'].upper()) + " in you command to enable assistant" + OutputStyler.ENDC + OutputStyler.ENDC) # ---------------------------------------------------------------------------------------------------------- # System info sector # ---------------------------------------------------------------------------------------------------------- print(OutputStyler.HEADER + headerize('SYSTEM') + OutputStyler.ENDC) print(OutputStyler.BOLD + 'RAM USAGE: {0:.2f} GB'.format(self._get_memory()) + OutputStyler.ENDC) # ---------------------------------------------------------------------------------------------------------- # Assistant logs sector # ---------------------------------------------------------------------------------------------------------- if debug_log: logging.debug(debug_log) if info_log: logging.info(info_log) if warn_log: logging.warning(warn_log) if error_log: logging.error(error_log) MAX_NUMBER_OF_LOG_LINES = 25 log_path = settings.ROOT_LOG_CONF['handlers']['file']['filename'] lines = subprocess.check_output(['tail', '-' + str(MAX_NUMBER_OF_LOG_LINES), log_path]).decode("utf-8") actual_number_of_log_lines = len(lines) print(OutputStyler.HEADER + headerize('LOG -{0} (Total Lines: {1})'.format(log_path, actual_number_of_log_lines) ) + OutputStyler.ENDC) print(OutputStyler.BOLD + lines + OutputStyler.ENDC) # ---------------------------------------------------------------------------------------------------------- # Assistant input/output sector # ---------------------------------------------------------------------------------------------------------- print(OutputStyler.HEADER + headerize('ASSISTANT') + OutputStyler.ENDC) if text: print(OutputStyler.BOLD + '> ' + text + '\r' + OutputStyler.ENDC) print(OutputStyler.HEADER + headerize() + OutputStyler.ENDC) else: if text: print(OutputStyler.BOLD + text + '\r' + OutputStyler.ENDC)
import jarvis.engines as engines # ---------------------------------------------------------------------------------------------------------------------- # Create a Console & Rotating file logger # ---------------------------------------------------------------------------------------------------------------------- config.dictConfig(ROOT_LOG_CONF) # ---------------------------------------------------------------------------------------------------------------------- # Clear log file in each assistant fresh start # ---------------------------------------------------------------------------------------------------------------------- with open(ROOT_LOG_CONF['handlers']['file']['filename'], 'r+') as f: f.truncate(0) # ---------------------------------------------------------------------------------------------------------------------- # Configuare MongoDB, load skills and settings # ---------------------------------------------------------------------------------------------------------------------- configure_MongoDB(db, settings) # ---------------------------------------------------------------------------------------------------------------------- # Get assistant settings # ---------------------------------------------------------------------------------------------------------------------- input_mode = db.get_documents(collection='general_settings')[0]['input_mode'] response_in_speech = db.get_documents(collection='general_settings')[0]['response_in_speech'] assistant_name = db.get_documents(collection='general_settings')[0]['assistant_name'] # ---------------------------------------------------------------------------------------------------------------------- # Create assistant input and output engine instances # ---------------------------------------------------------------------------------------------------------------------- input_engine = engines.STTEngine() if input_mode == InputMode.VOICE.value else engines.TTTEngine() output_engine = engines.TTSEngine() if response_in_speech else engines.TTTEngine()
def skills(self): return db.get_documents(collection='control_skills')\ + db.get_documents(collection='enabled_basic_skills')\ + db.get_documents(collection='learned_skills')