From 54591d660aaf93ee5f1cf25fd77e39cfe19f2a00 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Sun, 10 Dec 2023 22:20:58 +0100 Subject: [PATCH] Upgraded core --- lollms/app.py | 6 +-- lollms/binding.py | 96 +++++++++++++++++++++--------------------- lollms/com.py | 98 +++++++++++++++++++++++++++++++++++++++++++ lollms/helpers.py | 23 ---------- lollms/media.py | 17 +++++++- lollms/personality.py | 86 ++++++++++++++++++------------------- 6 files changed, 209 insertions(+), 117 deletions(-) create mode 100644 lollms/com.py diff --git a/lollms/app.py b/lollms/app.py index 1445806..e9ec005 100644 --- a/lollms/app.py +++ b/lollms/app.py @@ -4,8 +4,8 @@ from lollms.personality import PersonalityBuilder from lollms.binding import LLMBinding, BindingBuilder, ModelBuilder from lollms.extension import LOLLMSExtension, ExtensionBuilder from lollms.config import InstallOption -from lollms.helpers import NotificationType -from lollms.helpers import ASCIIColors, trace_exception, NotificationType, NotificationDisplayType +from lollms.helpers import ASCIIColors, trace_exception +from lollms.com import NotificationType, NotificationDisplayType, LoLLMsCom from lollms.terminal import MainMenu from lollms.utilities import PromptReshaper from safe_store import TextVectorizer, VectorizationMethod, VisualizationMethod @@ -18,7 +18,7 @@ import importlib import sys -class LollmsApplication: +class LollmsApplication(LoLLMsCom): def __init__( self, app_name:str, diff --git a/lollms/binding.py b/lollms/binding.py index 49227f5..34563e1 100644 --- a/lollms/binding.py +++ b/lollms/binding.py @@ -21,7 +21,7 @@ import importlib import subprocess from lollms.config import TypedConfig, InstallOption from lollms.main_config import LOLLMSConfig -from lollms.helpers import NotificationType, NotificationDisplayType +from lollms.com import NotificationType, NotificationDisplayType, LoLLMsCom import urllib import inspect from enum import Enum @@ -57,7 +57,7 @@ class LLMBinding: supported_file_extensions='*.bin', binding_type:BindingType=BindingType.TEXT_ONLY, models_dir_names:list=None, - app:Callable=None + lollmsCom:LoLLMsCom=None ) -> None: self.binding_type = binding_type @@ -68,6 +68,8 @@ class LLMBinding: self.config = config self.binding_config = binding_config + self.lollmsCom = lollmsCom + binding_config.addConfigs([ {"name":"clip_model_name","type":"str","value":'ViT-L-14/openai','options':["ViT-L-14/openai","ViT-H-14/laion2b_s32b_b79k"], "help":"Clip model to be used for images understanding"}, @@ -76,50 +78,8 @@ class LLMBinding: ]) self.interrogatorStorer = None - - - self.supported_file_extensions = supported_file_extensions - self.seed = config["seed"] - - if app is not None: - self.error:Callable = app.error - self.info:Callable = app.info - self.success:Callable = app.success - self.warning:Callable = app.warning - self.notify:Callable = app.notify - self.InfoMessage:Callable = app.InfoMessage - else: - def InfoMessage(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.white(content) - - def info(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.info(content) - - def warning(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.warning(content) - - def success(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.success(content) - - def error(content, duration:int=4, client_id=None): - ASCIIColors.error(content) - - def notify( - content, - notification_type:NotificationType=NotificationType.NOTIF_SUCCESS, - duration:int=4, - client_id=None, - display_type:NotificationDisplayType=NotificationDisplayType.TOAST, - verbose=True - ): - ASCIIColors.white(content) - - self.error:Callable = error - self.info:Callable = info - self.success:Callable = success - self.warning:Callable = warning - self.notify:Callable = notify - self.InfoMessage:Callable = InfoMessage + self.supported_file_extensions = supported_file_extensions + self.seed = config["seed"] self.configuration_file_path = lollms_paths.personal_configuration_path/"bindings"/self.binding_folder_name/f"config.yaml" self.configuration_file_path.parent.mkdir(parents=True, exist_ok=True) @@ -143,6 +103,46 @@ class LLMBinding: for models_folder in self.models_folders: models_folder.mkdir(parents=True, exist_ok=True) + + + def InfoMessage(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.lollmsCom: + return self.lollmsCom.InfoMessage(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.white(content) + + def info(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.lollmsCom: + return self.lollmsCom.info(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.info(content) + + def warning(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.lollmsCom: + return self.lollmsCom.warning(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.warning(content) + + def success(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.lollmsCom: + return self.lollmsCom.success(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.success(content) + + def error(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.lollmsCom: + return self.lollmsCom.error(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.error(content) + + def notify( self, + content, + notification_type:NotificationType=NotificationType.NOTIF_SUCCESS, + duration:int=4, + client_id=None, + display_type:NotificationDisplayType=NotificationDisplayType.TOAST, + verbose=True + ): + if self.lollmsCom: + return self.lollmsCom.error(content=content, notification_type=notification_type, duration=duration, client_id=client_id, display_type=display_type, verbose=verbose) + ASCIIColors.white(content) + + def settings_updated(self): """ To be implemented by the bindings @@ -545,7 +545,7 @@ class BindingBuilder: config: LOLLMSConfig, lollms_paths:LollmsPaths, installation_option:InstallOption=InstallOption.INSTALL_IF_NECESSARY, - app=None + lollmsCom=None )->LLMBinding: binding:LLMBinding = self.getBinding(config, lollms_paths, installation_option) @@ -553,7 +553,7 @@ class BindingBuilder: config, lollms_paths=lollms_paths, installation_option = installation_option, - app=app + lollmsCom=lollmsCom ) def getBinding( diff --git a/lollms/com.py b/lollms/com.py new file mode 100644 index 0000000..14c41da --- /dev/null +++ b/lollms/com.py @@ -0,0 +1,98 @@ +from ascii_colors import ASCIIColors +from enum import Enum +class NotificationType(Enum): + """Notification types.""" + + NOTIF_ERROR = 0 + """This is an error notification.""" + + NOTIF_SUCCESS = 1 + """This is a success notification.""" + + NOTIF_INFO = 2 + """This is an information notification.""" + + NOTIF_WARNING = 3 + """This is a warining notification.""" + +class NotificationDisplayType(Enum): + """Notification display types.""" + + TOAST = 0 + """This is a toast.""" + + MESSAGE_BOX = 1 + """This is a message box.""" + + +class LoLLMsCom: + def __init__(self) -> None: + pass + def InfoMessage(self, content, duration:int=4, client_id=None, verbose:bool=True): + self.notify( + content, + notification_type=NotificationType.NOTIF_SUCCESS, + duration=duration, + client_id=client_id, + display_type=NotificationDisplayType.MESSAGE_BOX, + verbose=verbose + ) + + def info(self, content, duration:int=4, client_id=None, verbose:bool=True): + self.notify( + content, + notification_type=NotificationType.NOTIF_SUCCESS, + duration=duration, + client_id=client_id, + display_type=NotificationDisplayType.TOAST, + verbose=verbose + ) + + def warning(self, content, duration:int=4, client_id=None, verbose:bool=True): + self.notify( + content, + notification_type=NotificationType.NOTIF_WARNING, + duration=duration, + client_id=client_id, + display_type=NotificationDisplayType.TOAST, + verbose=verbose + ) + + def success(self, content, duration:int=4, client_id=None, verbose:bool=True): + self.notify( + content, + notification_type=NotificationType.NOTIF_SUCCESS, + duration=duration, + client_id=client_id, + display_type=NotificationDisplayType.TOAST, + verbose=verbose + ) + + def error(self, content, duration:int=4, client_id=None): + self.notify( + content, + notification_type=NotificationType.NOTIF_ERROR, + duration=duration, + client_id=client_id, + display_type=NotificationDisplayType.TOAST + ) + + + def notify( + self, + content, + notification_type:NotificationType=NotificationType.NOTIF_SUCCESS, + duration:int=4, + client_id=None, + display_type:NotificationDisplayType=NotificationDisplayType.TOAST, + verbose=True + ): + if verbose: + if notification_type==NotificationType.NOTIF_SUCCESS: + ASCIIColors.success(content) + elif notification_type==NotificationType.NOTIF_INFO: + ASCIIColors.info(content) + elif notification_type==NotificationType.NOTIF_WARNING: + ASCIIColors.warning(content) + else: + ASCIIColors.red(content) \ No newline at end of file diff --git a/lollms/helpers.py b/lollms/helpers.py index b4dd130..82d7599 100644 --- a/lollms/helpers.py +++ b/lollms/helpers.py @@ -1,29 +1,6 @@ import traceback from ascii_colors import ASCIIColors from enum import Enum -class NotificationType(Enum): - """Notification types.""" - - NOTIF_ERROR = 0 - """This is an error notification.""" - - NOTIF_SUCCESS = 1 - """This is a success notification.""" - - NOTIF_INFO = 2 - """This is an information notification.""" - - NOTIF_WARNING = 3 - """This is a warining notification.""" - -class NotificationDisplayType(Enum): - """Notification display types.""" - - TOAST = 0 - """This is a toast.""" - - MESSAGE_BOX = 1 - """This is a message box.""" def get_trace_exception(ex): """ diff --git a/lollms/media.py b/lollms/media.py index 93af3f3..386f82e 100644 --- a/lollms/media.py +++ b/lollms/media.py @@ -12,12 +12,25 @@ if not PackageManager.check_package_installed("pygame"): import pygame else: import pygame + import threading if not PackageManager.check_package_installed("cv2"): PackageManager.install_package("opencv-python") import cv2 else: import cv2 + + +if not PackageManager.check_package_installed("pyaudio"): + PackageManager.install_package("pyaudio") + PackageManager.install_package("wave") + import pyaudio + import wave +else: + import pyaudio + import wave + +from lollms.com import LoLLMsCom import time import json import base64 @@ -25,7 +38,8 @@ import base64 class AudioRecorder: - def __init__(self, filename, channels=1, sample_rate=44100, chunk_size=1024, silence_threshold=0.01, silence_duration=2): + def __init__(self, socketio, filename, channels=1, sample_rate=44100, chunk_size=1024, silence_threshold=0.01, silence_duration=2, app:LoLLMsCom=None): + self.socketio = socketio self.filename = filename self.channels = channels self.sample_rate = sample_rate @@ -37,6 +51,7 @@ class AudioRecorder: self.silence_threshold = silence_threshold self.silence_duration = silence_duration self.last_sound_time = time.time() + self.app = app def start_recording(self): self.is_recording = True diff --git a/lollms/personality.py b/lollms/personality.py index d0014bd..37c8d3e 100644 --- a/lollms/personality.py +++ b/lollms/personality.py @@ -13,7 +13,7 @@ from lollms.main_config import LOLLMSConfig from lollms.paths import LollmsPaths from lollms.binding import LLMBinding, BindingType from lollms.utilities import PromptReshaper, PackageManager -from lollms.helpers import NotificationType, NotificationDisplayType +from lollms.com import NotificationType, NotificationDisplayType import pkg_resources from pathlib import Path @@ -33,7 +33,7 @@ import json from safe_store import TextVectorizer, GenericDataLoader, VisualizationMethod, VectorizationMethod from functools import partial import sys - +from lollms.com import LoLLMsCom from lollms.helpers import trace_exception from lollms.utilities import PackageManager def is_package_installed(package_name): @@ -75,7 +75,7 @@ class AIPersonality: lollms_paths:LollmsPaths, config:LOLLMSConfig, model:LLMBinding=None, - app=None, + app:LoLLMsCom=None, run_scripts=True, selected_language=None, is_relative_path=True, @@ -98,45 +98,6 @@ class AIPersonality: self.config = config self.callback = callback self.app = app - if app is not None: - self.error:Callable = app.error - self.info:Callable = app.info - self.success:Callable = app.success - self.warning:Callable = app.warning - self.notify:Callable = app.notify - self.InfoMessage:Callable = app.InfoMessage - else: - def InfoMessage(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.white(content) - - def info(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.info(content) - - def warning(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.warning(content) - - def success(content, duration:int=4, client_id=None, verbose:bool=True): - ASCIIColors.success(content) - - def error(content, duration:int=4, client_id=None): - ASCIIColors.error(content) - - def notify( - content, - notification_type:NotificationType=NotificationType.NOTIF_SUCCESS, - duration:int=4, - client_id=None, - display_type:NotificationDisplayType=NotificationDisplayType.TOAST, - verbose=True - ): - ASCIIColors.white(content) - - self.error:Callable = error - self.info:Callable = info - self.success:Callable = success - self.warning:Callable = warning - self.notify:Callable = notify - self.InfoMessage:Callable = InfoMessage self.text_files = [] self.image_files = [] @@ -232,6 +193,47 @@ Date: {{date}} self.personality_output_folder = lollms_paths.personal_outputs_path/self.name self.personality_output_folder.mkdir(parents=True, exist_ok=True) + + + def InfoMessage(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.app: + return self.app.InfoMessage(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.white(content) + + def info(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.app: + return self.app.info(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.info(content) + + def warning(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.app: + return self.app.warning(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.warning(content) + + def success(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.app: + return self.app.success(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.success(content) + + def error(self, content, duration:int=4, client_id=None, verbose:bool=True): + if self.app: + return self.app.error(content=content, duration=duration, client_id=client_id, verbose=verbose) + ASCIIColors.error(content) + + def notify( self, + content, + notification_type:NotificationType=NotificationType.NOTIF_SUCCESS, + duration:int=4, + client_id=None, + display_type:NotificationDisplayType=NotificationDisplayType.TOAST, + verbose=True + ): + if self.app: + return self.app.error(content=content, notification_type=notification_type, duration=duration, client_id=client_id, display_type=display_type, verbose=verbose) + ASCIIColors.white(content) + + + def new_message(self, message_text:str, message_type:MSG_TYPE= MSG_TYPE.MSG_TYPE_FULL, metadata=[], callback: Callable[[str, int, dict, list, Any], bool]=None): """This sends step rogress to front end