From 80d2d36321b25456c4104071be021670073e3a69 Mon Sep 17 00:00:00 2001 From: Hugo Saporetti Junior Date: Fri, 6 Dec 2024 17:05:10 -0300 Subject: [PATCH] VSCode adjustments --- AskAI.code-workspace | 7 ++- src/main/askai/__main__.py | 36 +++++------- src/main/askai/core/askai_cli.py | 41 +++++++------ src/main/askai/core/support/utilities.py | 74 +++++++++++++----------- src/main/askai/tui/askai_app.py | 62 ++++++++++---------- src/main/requirements.txt | 49 ++++++++++++++++ 6 files changed, 159 insertions(+), 110 deletions(-) create mode 100644 src/main/requirements.txt diff --git a/AskAI.code-workspace b/AskAI.code-workspace index c95d1bfc..93054b2b 100644 --- a/AskAI.code-workspace +++ b/AskAI.code-workspace @@ -6,8 +6,6 @@ ], "settings": { "hide-files.files": [ - ".gradle", - ".idea", ".run", ".vscode", "run-configs", @@ -16,6 +14,9 @@ ".gitconfig", ".gitignore", ".gitmodules" + ], + "cSpell.words": [ + "destfile" ] } -} \ No newline at end of file +} diff --git a/src/main/askai/__main__.py b/src/main/askai/__main__.py index d3579a75..9e29b512 100755 --- a/src/main/askai/__main__.py +++ b/src/main/askai/__main__.py @@ -2,15 +2,15 @@ # -*- coding: utf-8 -*- """ - @project: hspylib - @package: hspylib - @file: __main__.py - @created: Fri, 5 Jan 2024 - @author: Hugo Saporetti Junior - @site: https://github.com/yorevs/askai - @license: MIT - Please refer to - - Copyright (c) 2024, AskAI +@project: hspylib +@package: hspylib + @file: __main__.py +@created: Fri, 5 Jan 2024 + @author: Hugo Saporetti Junior + @site: https://github.com/yorevs/askai +@license: MIT - Please refer to + +Copyright (c) 2024, AskAI """ import logging as log @@ -19,6 +19,8 @@ import sys import typing +from askai.__classpath__ import classpath +from askai.core.support.utilities import QueryString from clitt.core.tui.tui_application import TUIApplication from hspylib.core.enums.charset import Charset from hspylib.core.tools.commons import syserr, to_bool @@ -26,9 +28,6 @@ from hspylib.modules.application.exit_status import ExitStatus from hspylib.modules.application.version import Version -from askai.__classpath__ import classpath -from askai.core.support.utilities import QueryString - class Main(TUIApplication): """HomeSetup Ask-AI - Unleash the Power of AI in Your Terminal.""" @@ -54,11 +53,10 @@ def _execute_command(command_str: typing.AnyStr) -> ExitStatus: :return: The exit status of the command execution. """ import click - from askai.core.commander import commander try: - if command := re.search(commander.RE_ASKAI_CMD, command_str): + if command := re.search(commander.RE_ASKAI_CMD, str(command_str)): args: list[str] = list( filter( lambda a: a and a != "None", @@ -148,10 +146,9 @@ def _main(self, *params, **kwargs) -> ExitStatus: """ from textwrap import dedent - from hspylib.core.zoned_datetime import now - from askai.core.askai_configs import configs from askai.core.enums.run_modes import RunModes + from hspylib.core.zoned_datetime import now os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" is_new_ui: bool = to_bool(self._get_argument("ui", False)) @@ -192,7 +189,7 @@ def _main(self, *params, **kwargs) -> ExitStatus: ) else: os.environ["ASKAI_APP"] = RunModes.ASKAI_CMD.value - return self._execute_command(query_string) + return self._execute_command(query_string or "/help") # fmt: off log.debug(dedent(f"""\ @@ -252,7 +249,7 @@ def _get_mode_str(self) -> str: """ return self._get_argument("router", "default") - def _get_interactive(self, query_string: str) -> bool: + def _get_interactive(self, query_string: str | None) -> bool: """Return the interactive parameter if query_string is not specified; False otherwise. :param query_string: The query string to check for interactivity. :return: The value of the interactive parameter or False based on query_string presence. @@ -263,6 +260,3 @@ def _get_interactive(self, query_string: str) -> bool: # Application entry point if __name__ == "__main__": Main("AskAI").INSTANCE.run(sys.argv[1:]) -# Application entry point -if __name__ == "__main__": - Main("AskAI").INSTANCE.run(sys.argv[1:]) diff --git a/src/main/askai/core/askai_cli.py b/src/main/askai/core/askai_cli.py index 01807e2c..3c380b23 100644 --- a/src/main/askai/core/askai_cli.py +++ b/src/main/askai/core/askai_cli.py @@ -2,16 +2,17 @@ # -*- coding: utf-8 -*- """ - @project: HsPyLib-AskAI - @package: askai.core - @file: askai_cli.py - @created: Fri, 9 Aug 2024 - @author: Hugo Saporetti Junior - @site: https://github.com/yorevs/askai - @license: MIT - Please refer to +@project: HsPyLib-AskAI +@package: askai.core + @file: askai_cli.py +@created: Fri, 9 Aug 2024 + @author: Hugo Saporetti Junior + @site: https://github.com/yorevs/askai +@license: MIT - Please refer to - Copyright (c) 2024, AskAI +Copyright (c) 2024, AskAI """ + import logging as log import os import signal @@ -22,15 +23,6 @@ import nltk import pause -from clitt.core.term.cursor import cursor -from clitt.core.term.screen import screen -from clitt.core.tui.line_input.keyboard_input import KeyboardInput -from hspylib.core.enums.charset import Charset -from hspylib.core.tools.commons import console_out -from hspylib.core.zoned_datetime import now, TIME_FORMAT -from hspylib.modules.eventbus.event import Event -from rich.progress import Progress - from askai.core.askai import AskAi from askai.core.askai_configs import configs from askai.core.askai_events import * @@ -38,7 +30,7 @@ from askai.core.askai_prompt import prompt from askai.core.commander.commander import commands from askai.core.component.audio_player import player -from askai.core.component.cache_service import cache, CACHE_DIR +from askai.core.component.cache_service import CACHE_DIR, cache from askai.core.component.recorder import recorder from askai.core.component.scheduler import scheduler from askai.core.enums.router_mode import RouterMode @@ -46,6 +38,14 @@ from askai.core.support.shared_instances import shared from askai.core.support.utilities import display_text from askai.tui.app_icons import AppIcons +from clitt.core.term.cursor import cursor +from clitt.core.term.screen import screen +from clitt.core.tui.line_input.keyboard_input import KeyboardInput +from hspylib.core.enums.charset import Charset +from hspylib.core.tools.commons import console_out +from hspylib.core.zoned_datetime import TIME_FORMAT, now +from hspylib.modules.eventbus.event import Event +from rich.progress import Progress class AskAiCli(AskAi): @@ -57,16 +57,15 @@ def __init__( debug: bool, cacheable: bool, tempo: int, - query_prompt: str, + query_prompt: str | None, engine_name: str, model_name: str, query_string: str | None, mode: RouterMode, ): - super().__init__(speak, debug, cacheable, tempo, engine_name, model_name, mode) self._ready: bool = False - self._query_prompt: str = query_prompt + self._query_prompt: str | None = query_prompt self._query_string: str | None = query_string self._startup() diff --git a/src/main/askai/core/support/utilities.py b/src/main/askai/core/support/utilities.py index 4543171e..5d6b4f39 100644 --- a/src/main/askai/core/support/utilities.py +++ b/src/main/askai/core/support/utilities.py @@ -2,16 +2,28 @@ # -*- coding: utf-8 -*- """ - @project: HsPyLib-AskAI - @package: askai.core.support.utilities - @file: utilities.py - @created: Wed, 10 Jan 2024 - @author: Hugo Saporetti Junior - @site: https://github.com/yorevs/askai - @license: MIT - Please refer to - - Copyright (c) 2024, AskAI +@project: HsPyLib-AskAI +@package: askai.core.support.utilities + @file: utilities.py +@created: Wed, 10 Jan 2024 + @author: Hugo Saporetti Junior + @site: https://github.com/yorevs/askai +@license: MIT - Please refer to + +Copyright (c) 2024, AskAI """ + +import base64 +import mimetypes +import os +import re +import shlex +import shutil +import sys +from os.path import basename, dirname +from pathlib import Path +from typing import AnyStr, Optional, TypeAlias + from askai.core.support.text_formatter import text_formatter from clitt.core.term.cursor import cursor from hspylib.core.config.path_object import PathObject @@ -21,17 +33,6 @@ from hspylib.core.tools.commons import file_is_not_empty from hspylib.core.tools.text_tools import ensure_endswith, strip_escapes from hspylib.core.zoned_datetime import now_ms -from os.path import basename, dirname -from pathlib import Path -from typing import AnyStr, Optional, TypeAlias - -import base64 -import mimetypes -import os -import re -import shlex -import shutil -import sys QueryString: TypeAlias = None | str | list[str] @@ -67,13 +68,13 @@ def find_file(filename: AnyPath) -> Optional[Path]: :param filename: The name or path of the file to find. :return: The full path to the file if found, otherwise None. """ - prompt_path: Path = Path(filename) if filename else None + prompt_path: Path | None = Path(filename) if filename else None if prompt_path and not prompt_path.exists(): - prompt_path = Path(os.path.expandvars(os.path.expanduser(filename))) + prompt_path = Path(os.path.expandvars(os.path.expanduser(str(filename)))) if not prompt_path.exists(): - prompt_path = Path(os.path.join(os.getcwd(), filename)) + prompt_path = Path(os.path.join(os.getcwd(), str(filename))) if not prompt_path.exists(): - prompt_path = Path(os.path.join(Path.home(), filename)) + prompt_path = Path(os.path.join(Path.home(), str(filename))) return prompt_path if prompt_path and prompt_path.exists() else None @@ -83,13 +84,18 @@ def copy_file(srcfile: AnyPath | Path, destfile: AnyPath | Path) -> str: :param destfile: The destination path where the file should be copied. :return: The path of the copied file as a string. """ - filepath: PathObject = PathObject.of(srcfile) - dest_dir: PathObject = PathObject.of(destfile) + filepath: PathObject | None = PathObject.of(srcfile) + dest_dir: PathObject | None = PathObject.of(destfile) check_argument( - filepath.exists and filepath.is_file and dest_dir.exists and dest_dir.is_dir + filepath is not None + and filepath.exists + and filepath.is_file + and dest_dir is not None + and dest_dir.exists + and dest_dir.is_dir ) dest_file: str = os.path.join(dest_dir.abs_dir, filepath.filename) - shutil.copy(srcfile, dest_file) + shutil.copy(str(srcfile), str(dest_file)) return dest_file @@ -127,7 +133,7 @@ def read_resource(base_dir: AnyPath, filename: AnyPath, file_ext: str = ".txt") :param file_ext: The file extension of the file (default is ".txt"). :return: The content of the file as a string. """ - _, ext = os.path.splitext(filename) + _, ext = os.path.splitext(str(filename)) filename = ( f"{str(base_dir)}/{ensure_endswith(basename(str(filename)), ext or file_ext)}" ) @@ -144,10 +150,10 @@ def read_file(load_dir: AnyPath, path_name: str) -> Optional[str]: :param path_name: The path name of the file to read. :return: The contents of the file as a string, or None if the file cannot be read. """ - file: Path = find_file(Path(join_path(load_dir, path_name))) + file: Path | None = find_file(Path(join_path(load_dir, path_name))) return ( file.read_text(encoding=Charset.UTF_8.val) - if file_is_not_empty(str(file)) + if file and file_is_not_empty(str(file)) else "" ) @@ -205,9 +211,9 @@ def media_type_of(pathname: str) -> Optional[tuple[str | None, ...]]: :param pathname: The file path to check. :return: A tuple representing the media type (e.g., ("text/plain")), or None if guessing was not possible. """ - mtype, _ = mimetypes.guess_type(os.path.basename(pathname)) - if mtype: - return tuple(mtype.split("/")) + media_type, _ = mimetypes.guess_type(os.path.basename(pathname)) + if media_type: + return tuple(media_type.split("/")) return None diff --git a/src/main/askai/tui/askai_app.py b/src/main/askai/tui/askai_app.py index 68ea4229..63cd8bc5 100644 --- a/src/main/askai/tui/askai_app.py +++ b/src/main/askai/tui/askai_app.py @@ -2,34 +2,23 @@ # -*- coding: utf-8 -*- """ - @project: HsPyLib-AskAI - @package: askai.tui.askai_app - @file: askai_app.py - @created: Mon, 29 Apr 2024 - @author: Hugo Saporetti Junior - @site: https://github.com/yorevs/askai - @license: MIT - Please refer to - - Copyright (c) 2024, AskAI +@project: HsPyLib-AskAI +@package: askai.tui.askai_app + @file: askai_app.py +@created: Mon, 29 Apr 2024 + @author: Hugo Saporetti Junior + @site: https://github.com/yorevs/askai +@license: MIT - Please refer to + +Copyright (c) 2024, AskAI """ -from pathlib import Path -from typing import Optional + import logging as log import os +from pathlib import Path +from typing import Optional -from hspylib.core.enums.charset import Charset -from hspylib.core.tools.commons import file_is_not_empty -from hspylib.core.tools.text_tools import ensure_endswith -from hspylib.core.zoned_datetime import DATE_FORMAT, now, TIME_FORMAT -from hspylib.modules.application.version import Version -from hspylib.modules.cli.vt100.vt_color import VtColor -from hspylib.modules.eventbus.event import Event -from textual import on, work -from textual.app import App, ComposeResult -from textual.containers import ScrollableContainer -from textual.widgets import Footer, Input, MarkdownViewer import nltk - from askai.__classpath__ import classpath from askai.core.askai import AskAi from askai.core.askai_configs import configs @@ -38,7 +27,7 @@ from askai.core.askai_prompt import prompt from askai.core.commander.commander import commander_help from askai.core.component.audio_player import player -from askai.core.component.cache_service import cache, CACHE_DIR +from askai.core.component.cache_service import CACHE_DIR, cache from askai.core.component.recorder import recorder from askai.core.component.scheduler import scheduler from askai.core.engine.ai_engine import AIEngine @@ -53,10 +42,21 @@ AppHelp, AppInfo, AppSettings, - Splash, - InputArea, InputActions, + InputArea, + Splash, ) +from hspylib.core.enums.charset import Charset +from hspylib.core.tools.commons import file_is_not_empty +from hspylib.core.tools.text_tools import ensure_endswith +from hspylib.core.zoned_datetime import DATE_FORMAT, TIME_FORMAT, now +from hspylib.modules.application.version import Version +from hspylib.modules.cli.vt100.vt_color import VtColor +from hspylib.modules.eventbus.event import Event +from textual import on, work +from textual.app import App, ComposeResult +from textual.containers import ScrollableContainer +from textual.widgets import Footer, Input, MarkdownViewer SOURCE_DIR: Path = classpath.source_path @@ -156,7 +156,7 @@ def input_actions(self): return self.query_one(InputActions) @property - def suggester(self) -> Optional[InputSuggester]: + def suggester(self) -> InputSuggester: """Get the Input Suggester.""" return self.line_input.suggester @@ -242,9 +242,9 @@ def _invoke_later_(arg: bool = True) -> None: self.call_from_thread(_invoke_later_, enable) - def activate_markdown(self) -> None: + async def activate_markdown(self) -> None: """Activate the markdown console widget.""" - self.md_console.go(self.console_path) + await self.md_console.go(self.console_path) self.md_console.set_class(False, "-hidden") self.md_console.scroll_end(animate=False) @@ -435,11 +435,11 @@ def _startup(self) -> None: log.info("AskAI is ready to use!") @work(thread=True) - def _setup(self) -> None: + async def _setup(self) -> None: """Setup the TUI controls.""" player.start_delay() self.splash.set_class(True, "-hidden") - self.activate_markdown() + await self.activate_markdown() self.action_clear(overwrite=False) self.enable_controls() self.line_input.focus(False) diff --git a/src/main/requirements.txt b/src/main/requirements.txt new file mode 100644 index 00000000..b0fdb764 --- /dev/null +++ b/src/main/requirements.txt @@ -0,0 +1,49 @@ +###### AUTO-GENERATED Requirements file for: AskAI ###### + +hspylib>=1.12.53 +hspylib-clitt>=0.9.140 +hspylib-setman>=0.10.43 +retry2==0.9.5 +pause==0.3 +tqdm==4.66.5 +pyperclip==1.9.0 +python-magic==0.4.27 +pytz==2024.2 +transitions==0.9.2 +pydantic==2.10.2 +langchain==0.3.9 +langchain-openai>=0.2.10 +langchain-community==0.3.8 +langchain-google-community==2.0.3 +openai-whisper==20240930 +openai==1.55.3 +google-api-python-client==2.154.0 +fake_useragent==1.5.1 +requests==2.32.3 +urllib3==2.2.3 +protobuf==4.25.4 +aiohttp==3.10.5 +html2text==2024.2.26 +rich==13.8.1 +textual==0.87.1 +soundfile==0.12.1 +PyAudio==0.2.14 +SpeechRecognition==3.10.4 +opencv-python==4.10.0.84 +pyautogui==0.9.54 +torch>=2.2.2 +torchvision>=0.17.2 +open-clip-torch +opentelemetry-api==1.27.0 +opentelemetry-sdk==1.27.0 +opentelemetry-proto==1.27.0 +transformers==4.45.2 +unstructured==0.16.0 +unstructured[md]==0.16.0 +tiktoken==0.8.0 +stanza==1.1.1 +nltk==3.9.1 +faiss-cpu~=1.8.0 +chromadb==0.5.5 +deepl==1.18.0 +argostranslate==1.9.1