2024-01-08 00:08:47 +00:00
|
|
|
"""
|
|
|
|
project: lollms
|
2024-05-07 17:11:52 +00:00
|
|
|
file: lollms_chatbox_events.py
|
2024-01-08 00:08:47 +00:00
|
|
|
author: ParisNeo
|
|
|
|
description:
|
|
|
|
This module contains a set of Socketio routes that provide information about the Lord of Large Language and Multimodal Systems (LoLLMs) Web UI
|
2024-05-07 17:11:52 +00:00
|
|
|
application. These routes are specific to chatbox operation
|
2024-01-08 00:08:47 +00:00
|
|
|
|
|
|
|
"""
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
from functools import partial
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import List
|
|
|
|
|
2024-01-08 00:08:47 +00:00
|
|
|
import pkg_resources
|
2024-12-19 12:48:57 +00:00
|
|
|
import socketio
|
|
|
|
from ascii_colors import ASCIIColors
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
2024-01-08 00:08:47 +00:00
|
|
|
from fastapi.responses import FileResponse
|
|
|
|
from lollms.binding import BindingBuilder, InstallOption
|
2024-12-19 12:48:57 +00:00
|
|
|
from lollms.databases.discussions_database import Discussion
|
|
|
|
from lollms.internet import scrape_and_save
|
2024-08-14 20:15:45 +00:00
|
|
|
from lollms.personality import AIPersonality
|
2024-12-19 12:48:57 +00:00
|
|
|
from lollms.security import forbid_remote_access
|
|
|
|
from lollms.server.elf_server import LOLLMSElfServer
|
2024-08-14 20:15:45 +00:00
|
|
|
from lollms.types import MSG_OPERATION_TYPE, SENDER_TYPES
|
2024-12-19 12:48:57 +00:00
|
|
|
from lollms.utilities import (PackageManager, convert_language_name,
|
|
|
|
find_first_available_file_index, gc, load_config,
|
|
|
|
run_async, trace_exception)
|
|
|
|
from pydantic import BaseModel
|
2024-01-08 00:08:47 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
from lollms_webui import LOLLMSWebUI
|
2024-01-08 00:08:47 +00:00
|
|
|
|
|
|
|
router = APIRouter()
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer: LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
2024-01-08 00:08:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------- events -----------------------------------------
|
2024-12-19 12:48:57 +00:00
|
|
|
def add_events(sio: socketio):
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
@sio.on("create_empty_message")
|
2024-01-08 00:08:47 +00:00
|
|
|
def create_empty_message(sid, data):
|
|
|
|
client_id = sid
|
2024-12-19 12:48:57 +00:00
|
|
|
type = int(data.get("type", 0))
|
|
|
|
message = data.get("message", "")
|
|
|
|
if type == 0:
|
2024-01-08 00:08:47 +00:00
|
|
|
ASCIIColors.info(f"Building empty User message requested by : {client_id}")
|
|
|
|
# send the message to the bot
|
|
|
|
print(f"Creating an empty message for AI answer orientation")
|
2024-02-26 00:55:44 +00:00
|
|
|
if lollmsElfServer.session.get_client(client_id).discussion:
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.new_message(
|
|
|
|
client_id,
|
|
|
|
lollmsElfServer.config.user_name,
|
|
|
|
message,
|
|
|
|
sender_type=SENDER_TYPES.SENDER_TYPES_USER,
|
|
|
|
open=True,
|
|
|
|
)
|
2024-01-08 00:08:47 +00:00
|
|
|
else:
|
|
|
|
if lollmsElfServer.personality is None:
|
|
|
|
lollmsElfServer.warning("Select a personality")
|
|
|
|
return
|
|
|
|
ASCIIColors.info(f"Building empty AI message requested by : {client_id}")
|
|
|
|
# send the message to the bot
|
|
|
|
print(f"Creating an empty message for AI answer orientation")
|
2024-02-26 00:55:44 +00:00
|
|
|
if lollmsElfServer.session.get_client(client_id).discussion:
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.new_message(
|
|
|
|
client_id,
|
|
|
|
lollmsElfServer.personality.name,
|
|
|
|
"[edit this to put your ai answer start]",
|
|
|
|
open=True,
|
|
|
|
)
|
2024-01-09 22:26:41 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
@sio.on("add_webpage")
|
2024-01-09 22:26:41 +00:00
|
|
|
def add_webpage(sid, data):
|
2024-03-17 02:25:52 +00:00
|
|
|
lollmsElfServer.ShowBlockingMessage("Scraping web page\nPlease wait...")
|
2024-01-09 22:26:41 +00:00
|
|
|
ASCIIColors.yellow("Scaping web page")
|
2024-02-26 07:56:42 +00:00
|
|
|
client = lollmsElfServer.session.get_client(sid)
|
2024-12-19 12:48:57 +00:00
|
|
|
url = data["url"]
|
|
|
|
index = find_first_available_file_index(
|
|
|
|
lollmsElfServer.lollms_paths.personal_uploads_path, "web_", ".txt"
|
|
|
|
)
|
|
|
|
file_path = (
|
|
|
|
lollmsElfServer.lollms_paths.personal_uploads_path / f"web_{index}.txt"
|
|
|
|
)
|
2024-03-17 14:10:24 +00:00
|
|
|
scrape_and_save(url=url, file_path=file_path)
|
2024-01-09 22:26:41 +00:00
|
|
|
try:
|
|
|
|
if not lollmsElfServer.personality.processor is None:
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.personality.processor.add_file(
|
|
|
|
file_path,
|
|
|
|
client,
|
|
|
|
partial(lollmsElfServer.process_data, client_id=sid),
|
|
|
|
)
|
2024-01-09 22:26:41 +00:00
|
|
|
# File saved successfully
|
2024-12-19 12:48:57 +00:00
|
|
|
run_async(
|
|
|
|
partial(
|
|
|
|
sio.emit,
|
|
|
|
"web_page_added",
|
|
|
|
{
|
|
|
|
"status": True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2024-01-09 22:26:41 +00:00
|
|
|
else:
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer.personality.add_file(
|
|
|
|
file_path,
|
|
|
|
client,
|
|
|
|
partial(lollmsElfServer.process_data, client_id=sid),
|
|
|
|
)
|
2024-01-09 22:26:41 +00:00
|
|
|
# File saved successfully
|
2024-12-19 12:48:57 +00:00
|
|
|
run_async(partial(sio.emit, "web_page_added", {"status": True}))
|
2024-03-17 02:25:52 +00:00
|
|
|
lollmsElfServer.HideBlockingMessage()
|
2024-01-09 22:26:41 +00:00
|
|
|
except Exception as e:
|
|
|
|
# Error occurred while saving the file
|
2024-12-19 12:48:57 +00:00
|
|
|
run_async(partial(sio.emit, "web_page_added", {"status": False}))
|
2024-03-17 02:25:52 +00:00
|
|
|
lollmsElfServer.HideBlockingMessage()
|
2024-01-09 22:26:41 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
@sio.on("take_picture")
|
2024-01-09 22:26:41 +00:00
|
|
|
def take_picture(sid):
|
|
|
|
try:
|
2024-02-26 18:29:37 +00:00
|
|
|
client = lollmsElfServer.session.get_client(sid)
|
2024-04-10 07:52:04 +00:00
|
|
|
if client is None:
|
|
|
|
lollmsElfServer.error("Client not recognized.\nTry refreshing the page")
|
|
|
|
return
|
2024-01-09 22:26:41 +00:00
|
|
|
lollmsElfServer.info("Loading camera")
|
|
|
|
if not PackageManager.check_package_installed("cv2"):
|
|
|
|
PackageManager.install_package("opencv-python")
|
|
|
|
import cv2
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-01-09 22:26:41 +00:00
|
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
n = time.time()
|
|
|
|
lollmsElfServer.info("Stand by for taking a shot in 2s")
|
2024-12-19 12:48:57 +00:00
|
|
|
while time.time() - n < 2:
|
2024-01-09 22:26:41 +00:00
|
|
|
_, frame = cap.read()
|
|
|
|
_, frame = cap.read()
|
|
|
|
cap.release()
|
|
|
|
lollmsElfServer.info("Shot taken")
|
2024-02-26 18:29:37 +00:00
|
|
|
cam_shot_path = client.discussion.discussion_images_folder
|
2024-01-09 22:26:41 +00:00
|
|
|
cam_shot_path.mkdir(parents=True, exist_ok=True)
|
2024-12-19 12:48:57 +00:00
|
|
|
filename = find_first_available_file_index(
|
|
|
|
cam_shot_path, "cam_shot_", extension=".png"
|
|
|
|
)
|
|
|
|
save_path = (
|
|
|
|
cam_shot_path / f"cam_shot_{filename}.png"
|
|
|
|
) # Specify the desired folder path
|
2024-01-09 22:26:41 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
cv2.imwrite(str(save_path), frame)
|
|
|
|
if not lollmsElfServer.personality.processor is None:
|
|
|
|
lollmsElfServer.info("Sending file to scripted persona")
|
2024-12-19 12:48:57 +00:00
|
|
|
client.discussion.add_file(
|
|
|
|
save_path,
|
|
|
|
client,
|
|
|
|
lollmsElfServer.tasks_library,
|
|
|
|
partial(lollmsElfServer.process_data, client_id=sid),
|
|
|
|
)
|
2024-08-15 15:28:33 +00:00
|
|
|
# lollmsElfServer.personality.processor.add_file(save_path, client, partial(lollmsElfServer.process_data, client_id = sid))
|
2024-01-09 22:26:41 +00:00
|
|
|
# File saved successfully
|
2024-12-19 12:48:57 +00:00
|
|
|
run_async(
|
|
|
|
partial(
|
|
|
|
sio.emit, "picture_taken", {"status": True, "progress": 100}
|
|
|
|
)
|
|
|
|
)
|
2024-01-09 22:26:41 +00:00
|
|
|
lollmsElfServer.info("File sent to scripted persona")
|
|
|
|
else:
|
|
|
|
lollmsElfServer.info("Sending file to persona")
|
2024-12-19 12:48:57 +00:00
|
|
|
client.discussion.add_file(
|
|
|
|
save_path,
|
|
|
|
client,
|
|
|
|
lollmsElfServer.tasks_library,
|
|
|
|
partial(lollmsElfServer.process_data, client_id=sid),
|
|
|
|
)
|
|
|
|
# lollmsElfServer.personality.add_file(save_path, client, partial(lollmsElfServer.process_data, client_id = sid))
|
2024-01-09 22:26:41 +00:00
|
|
|
# File saved successfully
|
2024-12-19 12:48:57 +00:00
|
|
|
run_async(
|
|
|
|
partial(
|
|
|
|
sio.emit, "picture_taken", {"status": True, "progress": 100}
|
|
|
|
)
|
|
|
|
)
|
2024-01-09 22:26:41 +00:00
|
|
|
lollmsElfServer.info("File sent to persona")
|
|
|
|
except Exception as e:
|
|
|
|
trace_exception(e)
|
|
|
|
# Error occurred while saving the file
|
2024-12-19 12:48:57 +00:00
|
|
|
run_async(
|
|
|
|
partial(
|
|
|
|
sio.emit, "picture_taken", {"status": False, "error": str(e)}
|
|
|
|
)
|
|
|
|
)
|
2024-01-09 22:26:41 +00:00
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
trace_exception(ex)
|
|
|
|
lollmsElfServer.error("Couldn't use the webcam")
|