From 246f2e3df8de033a4a5e27440f292e565d09c80a Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Tue, 8 Oct 2024 21:37:39 +0200 Subject: [PATCH] fixed pyqt5 and other tools --- lollms/functions/generate_image.py | 6 +- lollms/functions/list_personalities.py | 10 +- lollms/functions/memes/memes_builder.py | 4 - lollms/functions/select_image_file.py | 18 +-- lollms/functions/summon_personality.py | 8 -- lollms/functions/take_a_photo.py | 141 ++++++++++++------------ lollms/functions/take_screen_shot.py | 95 ++++++++-------- lollms/functions/timers.py | 12 +- lollms/server/endpoints/lollms_tti.py | 19 +--- 9 files changed, 136 insertions(+), 177 deletions(-) diff --git a/lollms/functions/generate_image.py b/lollms/functions/generate_image.py index 89bc81a..f1a23c4 100644 --- a/lollms/functions/generate_image.py +++ b/lollms/functions/generate_image.py @@ -1,10 +1,6 @@ -from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url +from lollms.utilities import discussion_path_to_url from lollms.client_session import Client from lollms.personality import APScript -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") from ascii_colors import trace_exception from functools import partial from lollms.functions.prompting.image_gen_prompts import get_image_gen_prompt, get_random_image_gen_prompt diff --git a/lollms/functions/list_personalities.py b/lollms/functions/list_personalities.py index ee52573..2dee6a8 100644 --- a/lollms/functions/list_personalities.py +++ b/lollms/functions/list_personalities.py @@ -1,15 +1,7 @@ -from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url +from lollms.utilities import find_first_available_file_index, discussion_path_to_url from lollms.client_session import Client from lollms.personality import APScript -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") from ascii_colors import trace_exception -import cv2 -import time -from PyQt5 import QtWidgets, QtGui, QtCore -import sys from functools import partial from lollms.personality import AIPersonality from typing import List diff --git a/lollms/functions/memes/memes_builder.py b/lollms/functions/memes/memes_builder.py index ef259f0..dddcdfa 100644 --- a/lollms/functions/memes/memes_builder.py +++ b/lollms/functions/memes/memes_builder.py @@ -207,10 +207,6 @@ def two_paths_meme_generator_function(client: Client) -> Dict: from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url from lollms.client_session import Client from lollms.personality import APScript -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") from ascii_colors import trace_exception from functools import partial from PIL import Image, ImageDraw, ImageFont diff --git a/lollms/functions/select_image_file.py b/lollms/functions/select_image_file.py index 0b866f5..857f94f 100644 --- a/lollms/functions/select_image_file.py +++ b/lollms/functions/select_image_file.py @@ -1,20 +1,20 @@ from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url from lollms.client_session import Client -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") -if not PackageManager.check_package_installed("cv2"): - PackageManager.install_package("opencv-python") -import cv2 + + import time -from PyQt5 import QtWidgets, QtGui, QtCore import sys from functools import partial - +import pipmaster as pm def select_image_file(processor, client): + if not pm.is_installed("PyQt5"): + pm.install("PyQt5") + if not pm.is_installed("cv2"): + pm.install("opencv-python") + import cv2 + from PyQt5 import QtWidgets app = QtWidgets.QApplication(sys.argv) options = QtWidgets.QFileDialog.Options() options |= QtWidgets.QFileDialog.ReadOnly diff --git a/lollms/functions/summon_personality.py b/lollms/functions/summon_personality.py index 017c453..34ed295 100644 --- a/lollms/functions/summon_personality.py +++ b/lollms/functions/summon_personality.py @@ -1,15 +1,7 @@ from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url from lollms.client_session import Client from lollms.personality import APScript -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") from ascii_colors import trace_exception -import cv2 -import time -from PyQt5 import QtWidgets, QtGui, QtCore -import sys from functools import partial from lollms.personality import AIPersonality from typing import List diff --git a/lollms/functions/take_a_photo.py b/lollms/functions/take_a_photo.py index 6057f9d..1b851ea 100644 --- a/lollms/functions/take_a_photo.py +++ b/lollms/functions/take_a_photo.py @@ -1,78 +1,79 @@ -from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url +from lollms.utilities import find_first_available_file_index, discussion_path_to_url from lollms.client_session import Client -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") - -import cv2 -import time -from PyQt5 import QtWidgets, QtGui, QtCore import sys from functools import partial - -class CameraWindow(QtWidgets.QWidget): - def __init__(self): - super().__init__() - self.initUI() - self.cap = cv2.VideoCapture(0) - self.timer = QtCore.QTimer() - self.timer.timeout.connect(self.update_frame) - self.timer.start(50) - - def initUI(self): - self.setWindowTitle('Camera') - - self.image_label = QtWidgets.QLabel(self) - self.image_label.resize(640, 480) - - self.captured_image_label = QtWidgets.QLabel(self) - self.captured_image_label.resize(640, 480) - - self.take_shot_button = QtWidgets.QPushButton('Take Shot', self) - self.take_shot_button.clicked.connect(self.take_shot) - - self.ok_button = QtWidgets.QPushButton('OK', self) - self.ok_button.clicked.connect(self.ok) - - self.hbox_layout = QtWidgets.QHBoxLayout() - self.hbox_layout.addWidget(self.image_label) - self.hbox_layout.addWidget(self.captured_image_label) - - self.vbox_layout = QtWidgets.QVBoxLayout() - self.vbox_layout.addLayout(self.hbox_layout) - self.vbox_layout.addWidget(self.take_shot_button) - self.vbox_layout.addWidget(self.ok_button) - self.setLayout(self.vbox_layout) - - def update_frame(self): - ret, frame = self.cap.read() - if ret: - self.frame = frame - image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - height, width, channel = image.shape - step = channel * width - qImg = QtGui.QImage(image.data, width, height, step, QtGui.QImage.Format_RGB888) - self.image_label.setPixmap(QtGui.QPixmap.fromImage(qImg)) - - def take_shot(self): - if hasattr(self, 'frame'): - self.captured_frame = self.frame - image = cv2.cvtColor(self.captured_frame, cv2.COLOR_BGR2RGB) - height, width, channel = image.shape - step = channel * width - qImg = QtGui.QImage(image.data, width, height, step, QtGui.QImage.Format_RGB888) - self.captured_image_label.setPixmap(QtGui.QPixmap.fromImage(qImg)) - - def ok(self): - if hasattr(self, 'captured_frame'): - self.close() - - def closeEvent(self, event): - self.cap.release() - event.accept() +import pipmaster as pm def take_photo(processor, client, use_ui=False, use_a_single_photo_at_a_time=True): + if not pm.is_installed("PyQt5"): + pm.install("PyQt5") + if not pm.is_installed("cv2"): + pm.install("opencv-python") + + import cv2 + import time + from PyQt5 import QtWidgets, QtGui, QtCore + + class CameraWindow(QtWidgets.QWidget): + def __init__(self): + super().__init__() + self.initUI() + self.cap = cv2.VideoCapture(0) + self.timer = QtCore.QTimer() + self.timer.timeout.connect(self.update_frame) + self.timer.start(50) + + def initUI(self): + self.setWindowTitle('Camera') + + self.image_label = QtWidgets.QLabel(self) + self.image_label.resize(640, 480) + + self.captured_image_label = QtWidgets.QLabel(self) + self.captured_image_label.resize(640, 480) + + self.take_shot_button = QtWidgets.QPushButton('Take Shot', self) + self.take_shot_button.clicked.connect(self.take_shot) + + self.ok_button = QtWidgets.QPushButton('OK', self) + self.ok_button.clicked.connect(self.ok) + + self.hbox_layout = QtWidgets.QHBoxLayout() + self.hbox_layout.addWidget(self.image_label) + self.hbox_layout.addWidget(self.captured_image_label) + + self.vbox_layout = QtWidgets.QVBoxLayout() + self.vbox_layout.addLayout(self.hbox_layout) + self.vbox_layout.addWidget(self.take_shot_button) + self.vbox_layout.addWidget(self.ok_button) + self.setLayout(self.vbox_layout) + + def update_frame(self): + ret, frame = self.cap.read() + if ret: + self.frame = frame + image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + height, width, channel = image.shape + step = channel * width + qImg = QtGui.QImage(image.data, width, height, step, QtGui.QImage.Format_RGB888) + self.image_label.setPixmap(QtGui.QPixmap.fromImage(qImg)) + + def take_shot(self): + if hasattr(self, 'frame'): + self.captured_frame = self.frame + image = cv2.cvtColor(self.captured_frame, cv2.COLOR_BGR2RGB) + height, width, channel = image.shape + step = channel * width + qImg = QtGui.QImage(image.data, width, height, step, QtGui.QImage.Format_RGB888) + self.captured_image_label.setPixmap(QtGui.QPixmap.fromImage(qImg)) + + def ok(self): + if hasattr(self, 'captured_frame'): + self.close() + + def closeEvent(self, event): + self.cap.release() + event.accept() if use_ui: def run_app(): app = QtWidgets.QApplication(sys.argv) diff --git a/lollms/functions/take_screen_shot.py b/lollms/functions/take_screen_shot.py index c6110a1..f31d909 100644 --- a/lollms/functions/take_screen_shot.py +++ b/lollms/functions/take_screen_shot.py @@ -1,55 +1,56 @@ -from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url +from lollms.utilities import find_first_available_file_index, discussion_path_to_url from lollms.client_session import Client -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") +import pipmaster as pm -import pyautogui -from PyQt5 import QtWidgets, QtGui -from PyQt5.QtCore import Qt -import sys - -from functools import partial - - -class ScreenshotWindow(QtWidgets.QWidget): - def __init__(self, client, screenshot, fn_view, fn, use_a_single_photo_at_a_time= True): - super().__init__() - self.client = client - self.screenshot = screenshot - self.fn_view = fn_view - self.fn = fn - self.use_a_single_photo_at_a_time = use_a_single_photo_at_a_time - - self.initUI() - - def initUI(self): - self.setWindowTitle('Screenshot Viewer') - self.layout = QtWidgets.QVBoxLayout() - - self.label = QtWidgets.QLabel(self) - self.pixmap = QtGui.QPixmap(self.screenshot) - self.label.setPixmap(self.pixmap) - self.layout.addWidget(self.label) - - self.ok_button = QtWidgets.QPushButton('OK') - self.ok_button.clicked.connect(self.save_and_close) - self.layout.addWidget(self.ok_button) - - self.setLayout(self.layout) - - def save_and_close(self): - self.screenshot.save(self.fn_view) - self.screenshot.save(self.fn) - self.client.discussion.image_files.append(self.fn) - if self.use_a_single_photo_at_a_time: - self.client.discussion.image_files = [self.client.discussion.image_files[-1]] - - self.close() def take_screenshot(client: Client, use_ui: bool = False, use_a_single_photo_at_a_time= True): + + if not pm.is_installed("pyautogui"): + pm.install("pyautogui") + if not pm.is_installed("PyQt5"): + pm.install("PyQt5") + + import pyautogui + from PyQt5 import QtWidgets, QtGui + import sys + + class ScreenshotWindow(QtWidgets.QWidget): + def __init__(self, client, screenshot, fn_view, fn, use_a_single_photo_at_a_time= True): + super().__init__() + self.client = client + self.screenshot = screenshot + self.fn_view = fn_view + self.fn = fn + self.use_a_single_photo_at_a_time = use_a_single_photo_at_a_time + + self.initUI() + + def initUI(self): + self.setWindowTitle('Screenshot Viewer') + self.layout = QtWidgets.QVBoxLayout() + + self.label = QtWidgets.QLabel(self) + self.pixmap = QtGui.QPixmap(self.screenshot) + self.label.setPixmap(self.pixmap) + self.layout.addWidget(self.label) + + self.ok_button = QtWidgets.QPushButton('OK') + self.ok_button.clicked.connect(self.save_and_close) + self.layout.addWidget(self.ok_button) + + self.setLayout(self.layout) + + def save_and_close(self): + self.screenshot.save(self.fn_view) + self.screenshot.save(self.fn) + self.client.discussion.image_files.append(self.fn) + if self.use_a_single_photo_at_a_time: + self.client.discussion.image_files = [self.client.discussion.image_files[-1]] + + self.close() + + screenshot = pyautogui.screenshot() view_image = client.discussion.discussion_folder / "view_images" image = client.discussion.discussion_folder / "images" diff --git a/lollms/functions/timers.py b/lollms/functions/timers.py index cfe8176..af31d6a 100644 --- a/lollms/functions/timers.py +++ b/lollms/functions/timers.py @@ -19,17 +19,11 @@ from lollms.utilities import PackageManager from ascii_colors import trace_exception # Here is an example of how we install a non installed library using PackageManager -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") - +import pipmaster as pm # now we can import the libraries import threading import time import sys -from PyQt5.QtWidgets import QApplication, QMessageBox -import pyautogui # here is the core of the function to be built def set_timer_with_alert(duration: int, message: str) -> str: @@ -43,6 +37,10 @@ def set_timer_with_alert(duration: int, message: str) -> str: Returns: str: A success message indicating the timer has been set. """ + if not pm.is_installed("pyautogui"): + pm.install("pyautogui") + import pyautogui + def timer_callback(): try: time.sleep(duration) diff --git a/lollms/server/endpoints/lollms_tti.py b/lollms/server/endpoints/lollms_tti.py index 878c73c..1edf1b8 100644 --- a/lollms/server/endpoints/lollms_tti.py +++ b/lollms/server/endpoints/lollms_tti.py @@ -4,28 +4,11 @@ from typing import Optional from base64 import b64encode import io from PIL import Image -from fastapi import APIRouter, Request +from fastapi import APIRouter from lollms_webui import LOLLMSWebUI from pydantic import BaseModel -from starlette.responses import StreamingResponse -from lollms.types import MSG_OPERATION_TYPE -from lollms.utilities import detect_antiprompt, remove_text_from_string, trace_exception -from lollms.security import sanitize_path -from ascii_colors import ASCIIColors -from lollms.databases.discussions_database import DiscussionsDB, Discussion -from lollms.security import check_access -from typing import List -from lollms.utilities import PackageManager, find_first_available_file_index, discussion_path_to_url -from lollms.client_session import Client -from lollms.personality import APScript -if not PackageManager.check_package_installed("pyautogui"): - PackageManager.install_package("pyautogui") -if not PackageManager.check_package_installed("PyQt5"): - PackageManager.install_package("PyQt5") from ascii_colors import trace_exception -from functools import partial -from lollms.functions.prompting.image_gen_prompts import get_image_gen_prompt, get_random_image_gen_prompt router = APIRouter()