enhanced library

This commit is contained in:
Saifeddine ALOUI 2023-08-01 00:15:55 +02:00
parent 0f16fe605a
commit 2cfebbbdb3
7 changed files with 92 additions and 28 deletions

View File

@ -6,6 +6,7 @@ from lollms.binding import LLMBinding, BindingBuilder, ModelBuilder
from lollms.config import InstallOption
from lollms.helpers import trace_exception
from lollms.terminal import MainMenu
from typing import Callable
import subprocess
@ -54,32 +55,33 @@ class LollmsApplication:
if try_select_binding:
ASCIIColors.info("Please select a valid model or install a new one from a url")
self.menu.select_binding()
else:
if load_binding:
try:
self.binding = self.load_binding()
except Exception as ex:
ASCIIColors.error(f"Failed to load binding.\nReturned exception: {ex}")
trace_exception(ex)
if load_binding:
try:
self.binding = self.load_binding()
except Exception as ex:
ASCIIColors.error(f"Failed to load binding.\nReturned exception: {ex}")
trace_exception(ex)
self.binding = None
if self.binding is not None:
ASCIIColors.success(f"Binding {self.config.binding_name} loaded successfully.")
if load_model:
if self.config.model_name is None:
ASCIIColors.warning(f"No model selected")
if try_select_model:
print("Please select a valid model")
self.menu.select_model()
if self.config.model_name is not None:
try:
self.model = self.load_model()
except Exception as ex:
ASCIIColors.error(f"Failed to load model.\nReturned exception: {ex}")
trace_exception(ex)
else:
ASCIIColors.warning(f"Couldn't load binding {self.config.binding_name}.")
if self.binding is not None:
ASCIIColors.success(f"Binding {self.config.binding_name} loaded successfully.")
if load_model:
if self.config.model_name is None:
ASCIIColors.warning(f"No model selected")
if try_select_model:
print("Please select a valid model")
self.menu.select_model()
if self.config.model_name is not None:
try:
self.model = self.load_model()
except Exception as ex:
ASCIIColors.error(f"Failed to load model.\nReturned exception: {ex}")
trace_exception(ex)
else:
ASCIIColors.warning(f"Couldn't load binding {self.config.binding_name}.")
self.mount_personalities()
def load_binding(self):
@ -150,7 +152,11 @@ class LollmsApplication:
self.personality = self.mounted_personalities[self.config.active_personality_id]
def set_personalities_callbacks(self, callback: Callable[[str, int, dict], bool]=None):
for personality in self.mount_personalities:
personality.setCallback(callback)
def unmount_personality(self, id:int)->bool:
if id<len(self.config.personalities):
del self.config.personalities[id]

View File

@ -14,6 +14,7 @@ from lollms.apps.console import MainMenu
from lollms.app import LollmsApplication
from lollms.utilities import TextVectorizer
from typing import List, Tuple
from typing import Callable
import importlib
from pathlib import Path
import argparse

View File

@ -10,6 +10,7 @@ from tqdm import tqdm
from lollms.personality import PersonalityBuilder
from lollms.apps.console import MainMenu
from lollms.app import LollmsApplication
from typing import Callable

View File

@ -147,9 +147,13 @@ class LLMBinding:
if self.config.model_name.endswith(".reference"):
ASCIIColors.yellow("Loading a reference model:")
with open(str(self.lollms_paths.personal_models_path / f"{self.binding_folder_name}/{self.config.model_name}"), 'r') as f:
model_path = Path(f.read())
ASCIIColors.yellow(model_path)
file_path = self.lollms_paths.personal_models_path / f"{self.binding_folder_name}/{self.config.model_name}"
if file_path.exists():
with open(str(file_path), 'r') as f:
model_path = Path(f.read())
ASCIIColors.yellow(model_path)
else:
return None
else:
model_path = Path(self.lollms_paths.personal_models_path / f"{self.binding_folder_name}/{self.config.model_name}")

View File

@ -947,7 +947,12 @@ class APScript(StateMachine):
self.models_folder = self.personality.lollms_paths.personal_models_path / self.personality.personality_folder_name
self.models_folder.mkdir(parents=True, exist_ok=True)
def setCallback(self, callback: Callable[[str, int, dict], bool]):
self.callback = callback
if self.process:
self.process.callback = callback
def load_personality_config(self):
"""
Load the content of local_config.yaml file.

View File

@ -14,6 +14,53 @@ class PackageManager:
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
class Image64BitsManager:
@staticmethod
def raw_b64_img(image) -> str:
try:
from PIL import Image, PngImagePlugin
import io
import base64
except:
PackageManager.install_package("pillow")
from PIL import Image
import io
import base64
# XXX controlnet only accepts RAW base64 without headers
with io.BytesIO() as output_bytes:
metadata = None
for key, value in image.info.items():
if isinstance(key, str) and isinstance(value, str):
if metadata is None:
metadata = PngImagePlugin.PngInfo()
metadata.add_text(key, value)
image.save(output_bytes, format="PNG", pnginfo=metadata)
bytes_data = output_bytes.getvalue()
return str(base64.b64encode(bytes_data), "utf-8")
@staticmethod
def img2b64(image) -> str:
return "data:image/png;base64," + Image64BitsManager.raw_b64_img(image)
@staticmethod
def b642img(b64img) -> str:
try:
from PIL import Image, PngImagePlugin
import io
import base64
except:
PackageManager.install_package("pillow")
from PIL import Image
import io
import base64
Image.open(io.BytesIO(base64.b64decode(b64img)))
class TFIDFLoader:
@staticmethod
def create_vectorizer_from_dict(tfidf_info):

View File

@ -26,7 +26,7 @@ def get_all_files(path):
setuptools.setup(
name="lollms",
version="2.2.1",
version="2.2.5",
author="Saifeddine ALOUI",
author_email="aloui.saifeddine@gmail.com",
description="A python library for AI personality definition",