From 44e523a9c2f4dca9de441e4f369c21cbd68665a8 Mon Sep 17 00:00:00 2001 From: Hugo Saporetti Junior Date: Wed, 11 Sep 2024 21:06:23 -0300 Subject: [PATCH] Fixups --- dependencies.hspd | 4 +-- src/demo/components/camera_demo.py | 13 ++++---- src/demo/components/scheduler_demo.py | 7 ++--- src/demo/others/screenshot_demo.py | 1 - src/demo/others/translate_demo.py | 3 +- src/main/askai/core/askai.py | 24 ++------------ src/main/askai/core/askai_cli.py | 17 +++++++++- .../core/commander/commands/settings_cmd.py | 3 +- src/main/askai/core/component/recorder.py | 12 +++---- src/main/askai/core/component/scheduler.py | 12 +++---- .../core/features/router/task_toolkit.py | 23 +++++++------- .../core/features/router/tools/vision.py | 12 +++---- .../language/translators/argos_translator.py | 13 ++++---- .../language/translators/deepl_translator.py | 7 ++--- .../language/translators/marian_translator.py | 7 ++--- src/main/askai/tui/askai_app.py | 31 +++++++++---------- src/main/requirements.txt | 4 +-- 17 files changed, 89 insertions(+), 104 deletions(-) diff --git a/dependencies.hspd b/dependencies.hspd index ca56202e..260cc3b6 100644 --- a/dependencies.hspd +++ b/dependencies.hspd @@ -8,8 +8,8 @@ */ /* HSPyLib projects */ -package: hspylib, version: 1.12.47, mode: ge -package: hspylib-clitt, version: 0.9.132, mode: ge +package: hspylib, version: 1.12.49, mode: ge +package: hspylib-clitt, version: 0.9.133, mode: ge package: hspylib-setman, version: 0.10.35, mode: ge /* General */ diff --git a/src/demo/components/camera_demo.py b/src/demo/components/camera_demo.py index 020ac648..1ce6b666 100644 --- a/src/demo/components/camera_demo.py +++ b/src/demo/components/camera_demo.py @@ -1,15 +1,14 @@ -import os -from textwrap import dedent - -from clitt.core.term.cursor import cursor -from clitt.core.tui.line_input.line_input import line_input -from hspylib.core.tools.text_tools import strip_escapes - from askai.core.component.camera import camera from askai.core.component.image_store import ImageMetadata, store from askai.core.features.router.tools.terminal import open_command +from clitt.core.term.cursor import cursor +from clitt.core.tui.line_input.line_input import line_input +from hspylib.core.tools.text_tools import strip_escapes +from textwrap import dedent from utils import init_context +import os + MENU = dedent( f"""Camera Demo options {'-' * 30} diff --git a/src/demo/components/scheduler_demo.py b/src/demo/components/scheduler_demo.py index c86644b6..af95fca9 100644 --- a/src/demo/components/scheduler_demo.py +++ b/src/demo/components/scheduler_demo.py @@ -12,13 +12,12 @@ Copyright (c) 2024, HomeSetup """ -import logging as log - from askai.core.component.scheduler import scheduler from hspylib.core.zoned_datetime import now, SIMPLE_DATETIME_FORMAT - from utils import init_context +import logging as log + def echo(msg: str): log.info(f"{msg} {now('[%H:%M:%S]')}") @@ -60,7 +59,7 @@ def after_20_seconds(): if __name__ == "__main__": - init_context("scheduler-demo", rich_logging=False, console_enable=True, log_level=log.INFO) + init_context("scheduler-demo", rich_logging=True, console_enable=True, log_level=log.INFO) log.info("-=" * 40) log.info(f"AskAI Scheduler Demo - {scheduler.now.strftime(SIMPLE_DATETIME_FORMAT)}") log.info("-=" * 40) diff --git a/src/demo/others/screenshot_demo.py b/src/demo/others/screenshot_demo.py index 6db675a2..fad2a431 100644 --- a/src/demo/others/screenshot_demo.py +++ b/src/demo/others/screenshot_demo.py @@ -1,6 +1,5 @@ from askai.core.features.router.tools.vision import take_screenshot from hspylib.core.tools.commons import sysout - from utils import init_context if __name__ == "__main__": diff --git a/src/demo/others/translate_demo.py b/src/demo/others/translate_demo.py index 9348ba39..90319d8f 100644 --- a/src/demo/others/translate_demo.py +++ b/src/demo/others/translate_demo.py @@ -1,9 +1,8 @@ -from hspylib.core.tools.commons import sysout - from askai.language.language import Language from askai.language.translators.argos_translator import ArgosTranslator from askai.language.translators.deepl_translator import DeepLTranslator from askai.language.translators.marian_translator import MarianTranslator +from hspylib.core.tools.commons import sysout if __name__ == "__main__": text = """ diff --git a/src/main/askai/core/askai.py b/src/main/askai/core/askai.py index f48bc150..cef55442 100644 --- a/src/main/askai/core/askai.py +++ b/src/main/askai/core/askai.py @@ -26,13 +26,8 @@ from askai.core.support.chat_context import ChatContext from askai.core.support.shared_instances import shared from askai.core.support.utilities import read_stdin -from askai.exception.exceptions import ( - ImpossibleQuery, - InaccurateResponse, - IntelligibleAudioError, - MaxInteractionsReached, - TerminatingQuery, -) +from askai.exception.exceptions import (ImpossibleQuery, InaccurateResponse, IntelligibleAudioError, + MaxInteractionsReached, TerminatingQuery) from askai.tui.app_icons import AppIcons from click import UsageError from enum import Enum @@ -40,7 +35,6 @@ from hspylib.core.tools.commons import file_is_not_empty, is_debugging from hspylib.core.zoned_datetime import DATE_FORMAT, now, TIME_FORMAT from hspylib.modules.application.exit_status import ExitStatus -from hspylib.modules.eventbus.event import Event from openai import RateLimitError from pathlib import Path from typing import List, Optional, TypeAlias @@ -212,17 +206,3 @@ def _reply_error(self, reply: AIReply) -> None: :param reply: The error reply message to be displayed to the user. """ ... - - def _cb_mode_changed_event(self, ev: Event) -> None: - """Callback to handle mode change events. - :param ev: The event object representing the mode change. - """ - self._mode: RouterMode = RouterMode.of_name(ev.args.mode) - if not self._mode.is_default: - sum_msg: str = ( - f"{msg.enter_qna()} \n" - f"```\nContext:  {ev.args.sum_path},  {ev.args.glob} \n```\n" - f"`{msg.press_esc_enter()}` \n\n" - f"> {msg.qna_welcome()}" - ) - events.reply.emit(reply=AIReply.info(sum_msg)) diff --git a/src/main/askai/core/askai_cli.py b/src/main/askai/core/askai_cli.py index 1bff768a..bbcd8fbe 100644 --- a/src/main/askai/core/askai_cli.py +++ b/src/main/askai/core/askai_cli.py @@ -21,6 +21,7 @@ from askai.core.component.cache_service import cache, CACHE_DIR from askai.core.component.recorder import recorder from askai.core.component.scheduler import scheduler +from askai.core.enums.router_mode import RouterMode from askai.core.model.ai_reply import AIReply from askai.core.support.shared_instances import shared from askai.core.support.text_formatter import text_formatter @@ -32,7 +33,7 @@ from pathlib import Path from rich.progress import Progress from threading import Thread -from typing import List, TypeAlias, Optional +from typing import List, Optional, TypeAlias import logging as log import nltk @@ -131,6 +132,20 @@ def _cb_reply_event(self, ev: Event) -> None: cursor.erase_line() self._reply(reply) + def _cb_mode_changed_event(self, ev: Event) -> None: + """Callback to handle mode change events. + :param ev: The event object representing the mode change. + """ + self._mode: RouterMode = RouterMode.of_name(ev.args.mode) + if not self._mode.is_default: + sum_msg: str = ( + f"{msg.enter_qna()} \n" + f"```\nContext:  {ev.args.sum_path},  {ev.args.glob} \n```\n" + f"`{msg.press_esc_enter()}` \n\n" + f"> {msg.qna_welcome()}" + ) + events.reply.emit(reply=AIReply.info(sum_msg)) + def _cb_mic_listening_event(self, ev: Event) -> None: """Callback to handle microphone listening events. :param ev: The event object representing the microphone listening event. diff --git a/src/main/askai/core/commander/commands/settings_cmd.py b/src/main/askai/core/commander/commands/settings_cmd.py index 995a4170..e0a0f9d6 100644 --- a/src/main/askai/core/commander/commands/settings_cmd.py +++ b/src/main/askai/core/commander/commands/settings_cmd.py @@ -14,14 +14,13 @@ """ from abc import ABC -from typing import Any, Optional - from askai.core.askai_configs import configs from askai.core.askai_settings import settings from askai.core.support.text_formatter import text_formatter from askai.core.support.utilities import display_text from hspylib.core.tools.commons import sysout from setman.settings.settings_entry import SettingsEntry +from typing import Any, Optional class SettingsCmd(ABC): diff --git a/src/main/askai/core/component/recorder.py b/src/main/askai/core/component/recorder.py index 9f6b124c..5a5d02c6 100644 --- a/src/main/askai/core/component/recorder.py +++ b/src/main/askai/core/component/recorder.py @@ -12,12 +12,6 @@ Copyright (c) 2024, HomeSetup """ -import logging as log -import operator -import sys -from pathlib import Path -from typing import Callable, Optional, TypeAlias - from askai.core.askai_configs import configs from askai.core.askai_events import events from askai.core.askai_messages import msg @@ -33,7 +27,13 @@ from hspylib.core.preconditions import check_argument, check_state from hspylib.core.zoned_datetime import now_ms from hspylib.modules.application.exit_status import ExitStatus +from pathlib import Path from speech_recognition import AudioData, Microphone, Recognizer, RequestError, UnknownValueError, WaitTimeoutError +from typing import Callable, Optional, TypeAlias + +import logging as log +import operator +import sys InputDevice: TypeAlias = tuple[int, str] diff --git a/src/main/askai/core/component/scheduler.py b/src/main/askai/core/component/scheduler.py index d72625eb..9a494dcf 100644 --- a/src/main/askai/core/component/scheduler.py +++ b/src/main/askai/core/component/scheduler.py @@ -12,18 +12,18 @@ Copyright (c) 2024, HomeSetup """ -import inspect -import os -import threading from datetime import datetime, timedelta +from hspylib.core.metaclass.singleton import Singleton +from hspylib.core.preconditions import check_argument +from hspylib.core.zoned_datetime import SIMPLE_DATETIME_FORMAT from threading import Thread from time import monotonic from typing import Any, Callable, Iterable, Mapping +import inspect +import os import pause -from hspylib.core.metaclass.singleton import Singleton -from hspylib.core.preconditions import check_argument -from hspylib.core.zoned_datetime import SIMPLE_DATETIME_FORMAT +import threading class Scheduler(Thread, metaclass=Singleton): diff --git a/src/main/askai/core/features/router/task_toolkit.py b/src/main/askai/core/features/router/task_toolkit.py index be1b472f..ac346e90 100644 --- a/src/main/askai/core/features/router/task_toolkit.py +++ b/src/main/askai/core/features/router/task_toolkit.py @@ -12,18 +12,6 @@ Copyright (c) 2024, HomeSetup """ -import inspect -import logging as log -from functools import lru_cache -from textwrap import dedent -from typing import Callable, Optional - -from clitt.core.tui.line_input.line_input import line_input -from hspylib.core.metaclass.classpath import AnyPath -from hspylib.core.metaclass.singleton import Singleton -from hspylib.core.tools.text_tools import ensure_endswith, ensure_startswith -from langchain_core.tools import BaseTool, StructuredTool - from askai.core.askai_messages import msg from askai.core.features.router.tools.analysis import query_output from askai.core.features.router.tools.browser import browse @@ -34,6 +22,17 @@ from askai.core.features.router.tools.vision import image_captioner, parse_caption from askai.core.features.router.tools.webcam import webcam_capturer, webcam_identifier from askai.exception.exceptions import TerminatingQuery +from clitt.core.tui.line_input.line_input import line_input +from functools import lru_cache +from hspylib.core.metaclass.classpath import AnyPath +from hspylib.core.metaclass.singleton import Singleton +from hspylib.core.tools.text_tools import ensure_endswith, ensure_startswith +from langchain_core.tools import BaseTool, StructuredTool +from textwrap import dedent +from typing import Callable, Optional + +import inspect +import logging as log class AgentToolkit(metaclass=Singleton): diff --git a/src/main/askai/core/features/router/tools/vision.py b/src/main/askai/core/features/router/tools/vision.py index 09cfad41..9675e9b3 100644 --- a/src/main/askai/core/features/router/tools/vision.py +++ b/src/main/askai/core/features/router/tools/vision.py @@ -1,9 +1,3 @@ -import os -from textwrap import indent - -import pyautogui -import torch -from PIL import Image from askai.core.askai_events import events from askai.core.askai_messages import msg from askai.core.component.cache_service import PICTURE_DIR @@ -16,8 +10,14 @@ from hspylib.core.enums.enumeration import Enumeration from hspylib.core.metaclass.classpath import AnyPath from hspylib.core.preconditions import check_argument +from PIL import Image +from textwrap import indent from transformers import BlipForConditionalGeneration, BlipProcessor +import os +import pyautogui +import torch + class HFModel(Enumeration): """Available Hugging Face models""" diff --git a/src/main/askai/language/translators/argos_translator.py b/src/main/askai/language/translators/argos_translator.py index 75bad2af..ca337223 100644 --- a/src/main/askai/language/translators/argos_translator.py +++ b/src/main/askai/language/translators/argos_translator.py @@ -13,18 +13,17 @@ Copyright (c) 2024, HomeSetup """ -import logging as log -import os -import sys -from functools import lru_cache -from typing import Optional - from argostranslate import package, translate from argostranslate.translate import ITranslation - from askai.exception.exceptions import TranslationPackageError from askai.language.ai_translator import AITranslator from askai.language.language import Language +from functools import lru_cache +from typing import Optional + +import logging as log +import os +import sys class ArgosTranslator(AITranslator): diff --git a/src/main/askai/language/translators/deepl_translator.py b/src/main/askai/language/translators/deepl_translator.py index eb8b6a76..73adc4f8 100644 --- a/src/main/askai/language/translators/deepl_translator.py +++ b/src/main/askai/language/translators/deepl_translator.py @@ -12,13 +12,12 @@ Copyright (c) 2024, HomeSetup """ -from functools import lru_cache - -import deepl - from askai.__classpath__ import API_KEYS from askai.language.ai_translator import AITranslator from askai.language.language import Language +from functools import lru_cache + +import deepl class DeepLTranslator(AITranslator): diff --git a/src/main/askai/language/translators/marian_translator.py b/src/main/askai/language/translators/marian_translator.py index fd6d5eac..8ac6c746 100644 --- a/src/main/askai/language/translators/marian_translator.py +++ b/src/main/askai/language/translators/marian_translator.py @@ -1,10 +1,9 @@ -import re +from askai.language.ai_translator import AITranslator +from askai.language.language import Language from functools import lru_cache - from transformers import MarianMTModel, MarianTokenizer -from askai.language.ai_translator import AITranslator -from askai.language.language import Language +import re class MarianTranslator(AITranslator): diff --git a/src/main/askai/tui/askai_app.py b/src/main/askai/tui/askai_app.py index c7bce9ca..8cb08ae7 100644 --- a/src/main/askai/tui/askai_app.py +++ b/src/main/askai/tui/askai_app.py @@ -12,8 +12,6 @@ Copyright (c) 2024, HomeSetup """ -from typing import Optional - from askai.__classpath__ import classpath from askai.core.askai import AskAi from askai.core.askai_configs import configs @@ -46,6 +44,7 @@ from textual.app import App, ComposeResult from textual.containers import ScrollableContainer from textual.widgets import Footer, Input, MarkdownViewer +from typing import Optional import logging as log import nltk @@ -335,20 +334,6 @@ def _cb_reply_event(self, ev: Event) -> None: if ev.args.reply.verbosity.match(configs.verbosity) or configs.is_debug: self._reply(reply) - def _cb_mic_listening_event(self, ev: Event) -> None: - """Callback to handle microphone listening events. - :param ev: The event object representing the microphone listening event. - """ - self.header.notifications.listening = ev.args.listening - if ev.args.listening: - self._reply(AIReply.info(msg.listening())) - - def _cb_device_changed_event(self, ev: Event) -> None: - """Callback to handle audio input device change events. - :param ev: The event object representing the device change. - """ - self._reply(AIReply.info(msg.device_switch(str(ev.args.device)))) - def _cb_mode_changed_event(self, ev: Event) -> None: """Callback to handle mode change events. :param ev: The event object representing the mode change. @@ -363,6 +348,20 @@ def _cb_mode_changed_event(self, ev: Event) -> None: ) events.reply.emit(reply=AIReply.info(sum_msg)) + def _cb_mic_listening_event(self, ev: Event) -> None: + """Callback to handle microphone listening events. + :param ev: The event object representing the microphone listening event. + """ + self.header.notifications.listening = ev.args.listening + if ev.args.listening: + self._reply(AIReply.info(msg.listening())) + + def _cb_device_changed_event(self, ev: Event) -> None: + """Callback to handle audio input device change events. + :param ev: The event object representing the device change. + """ + self._reply(AIReply.info(msg.device_switch(str(ev.args.device)))) + @work(thread=True, exclusive=True) def ask_and_reply(self, question: str) -> tuple[bool, Optional[str]]: """Ask the specified question to the AI and provide the reply. diff --git a/src/main/requirements.txt b/src/main/requirements.txt index 4adf4726..b2e02b4e 100644 --- a/src/main/requirements.txt +++ b/src/main/requirements.txt @@ -1,7 +1,7 @@ ###### AUTO-GENERATED Requirements file for: AskAI ###### -hspylib>=1.12.47 -hspylib-clitt>=0.9.132 +hspylib>=1.12.49 +hspylib-clitt>=0.9.133 hspylib-setman>=0.10.35 retry2>=0.9.5 pause>=0.3