2024-01-09 22:26:41 +00:00
"""
project : lollms
file : lollms_discussion_events . py
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
application . These routes are specific to discussion operation
"""
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-09 22:26:41 +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-09 22:26:41 +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
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 check_access , 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 , add_period ,
convert_language_name ,
find_first_available_file_index , gc , load_config ,
run_async , trace_exception )
from pydantic import BaseModel
2024-01-09 22:26:41 +00:00
2024-12-19 12:48:57 +00:00
from lollms_webui import LOLLMSWebUI
2024-01-09 22:26:41 +00:00
router = APIRouter ( )
2024-12-19 12:48:57 +00:00
lollmsElfServer : LOLLMSWebUI = LOLLMSWebUI . get_instance ( )
2024-01-09 22:26:41 +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 ( " start_webcam_video_stream " )
2024-01-09 22:26:41 +00:00
def start_webcam_video_stream ( sid ) :
lollmsElfServer . info ( " Starting video capture " )
2024-01-29 21:21:45 +00:00
try :
from lollms . media import WebcamImageSender
2024-12-19 12:48:57 +00:00
lollmsElfServer . webcam = WebcamImageSender ( sio , lollmsCom = lollmsElfServer )
2024-01-29 21:21:45 +00:00
lollmsElfServer . webcam . start_capture ( )
except :
2024-12-19 12:48:57 +00:00
lollmsElfServer . InfoMessage (
" Couldn ' t load media library. \n You will not be able to perform any of the media linked operations. please verify the logs and install any required installations "
)
2024-01-09 22:26:41 +00:00
2024-12-19 12:48:57 +00:00
@sio.on ( " stop_webcam_video_stream " )
2024-01-09 22:26:41 +00:00
def stop_webcam_video_stream ( sid ) :
lollmsElfServer . info ( " Stopping video capture " )
lollmsElfServer . webcam . stop_capture ( )
2024-12-19 12:48:57 +00:00
@sio.on ( " start_bidirectional_audio_stream " )
2024-05-26 22:36:45 +00:00
def start_bidirectional_audio_stream ( sid ) :
2024-05-05 18:57:11 +00:00
client = check_access ( lollmsElfServer , sid )
2024-05-05 15:28:45 +00:00
if lollmsElfServer . config . headless_server_mode :
2024-12-19 12:48:57 +00:00
return {
" status " : False ,
" error " : " Start recording is blocked when in headless mode for obvious security reasons! " ,
}
if (
lollmsElfServer . config . host != " localhost "
and lollmsElfServer . config . host != " 127.0.0.1 "
) :
return {
" status " : False ,
" error " : " Start recording is blocked when the server is exposed outside for very obvious reasons! " ,
}
2024-05-05 15:28:45 +00:00
2024-01-09 22:26:41 +00:00
lollmsElfServer . info ( " Starting audio capture " )
2024-05-24 21:46:02 +00:00
if not lollmsElfServer . tts or not lollmsElfServer . stt :
2024-12-19 12:48:57 +00:00
lollmsElfServer . InfoMessage (
" TTS or STT are not configured. \n Please go to settings and configure them first "
)
return { " status " : False , " error " : " TTS or STT not configured " }
2024-05-24 21:46:02 +00:00
if not lollmsElfServer . tts . ready or not lollmsElfServer . stt . ready :
lollmsElfServer . InfoMessage ( " TTS is not ready yet. \n Please wait " )
2024-12-19 12:48:57 +00:00
return { " status " : False , " error " : " TTS not ready " }
2024-05-24 21:46:02 +00:00
2024-05-24 23:41:28 +00:00
if lollmsElfServer . rt_com :
2024-05-26 22:36:45 +00:00
lollmsElfServer . info ( " audio_mode is already on \n Turning it off " )
lollmsElfServer . info ( " Stopping audio capture " )
lollmsElfServer . rt_com . stop_recording ( )
lollmsElfServer . rt_com = None
2024-12-19 12:48:57 +00:00
lollmsElfServer . emit_socket_io_info (
" rtcom_status_changed " , { " status " : False } , client . client_id
)
return { " status " : False , " error " : " Already running " }
2024-05-24 21:46:02 +00:00
2024-01-29 21:21:45 +00:00
try :
2024-05-19 12:35:05 +00:00
from lollms . media import RTCom
2024-12-19 12:48:57 +00:00
lollmsElfServer . rec_output_folder = (
lollmsElfServer . lollms_paths . personal_outputs_path / " audio_rec "
)
2024-01-29 21:21:45 +00:00
lollmsElfServer . rec_output_folder . mkdir ( exist_ok = True , parents = True )
lollmsElfServer . summoned = False
2024-05-19 12:35:05 +00:00
lollmsElfServer . rt_com = RTCom (
2024-12-19 12:48:57 +00:00
lollmsElfServer ,
lollmsElfServer . sio ,
lollmsElfServer . personality ,
client = client ,
threshold = lollmsElfServer . config . stt_listening_threshold ,
silence_duration = lollmsElfServer . config . stt_silence_duration ,
sound_threshold_percentage = lollmsElfServer . config . stt_sound_threshold_percentage ,
gain = lollmsElfServer . config . stt_gain ,
rate = lollmsElfServer . config . stt_rate ,
channels = lollmsElfServer . config . stt_channels ,
buffer_size = lollmsElfServer . config . stt_buffer_size ,
snd_input_device = lollmsElfServer . config . stt_input_device ,
snd_output_device = lollmsElfServer . config . tts_output_device ,
logs_folder = lollmsElfServer . rec_output_folder ,
block_while_talking = True ,
use_keyword_audio = lollmsElfServer . config . stt_activate_word_detection ,
keyword_audio_path = lollmsElfServer . config . stt_word_detection_file ,
)
2024-05-19 12:35:05 +00:00
lollmsElfServer . rt_com . start_recording ( )
2024-12-19 12:48:57 +00:00
lollmsElfServer . emit_socket_io_info (
" rtcom_status_changed " , { " status " : True } , client . client_id
)
2024-05-05 18:57:11 +00:00
except Exception as ex :
trace_exception ( ex )
2024-12-19 12:48:57 +00:00
lollmsElfServer . InfoMessage (
" Couldn ' t load media library. \n You will not be able to perform any of the media linked operations. please verify the logs and install any required installations "
)
lollmsElfServer . emit_socket_io_info (
" rtcom_status_changed " , { " status " : False } , client . client_id
)
@sio.on ( " stop_bidirectional_audio_stream " )
2024-05-26 22:36:45 +00:00
def stop_bidirectional_audio_stream ( sid ) :
2024-05-05 18:57:11 +00:00
client = check_access ( lollmsElfServer , sid )
2024-01-09 22:26:41 +00:00
lollmsElfServer . info ( " Stopping audio capture " )
2024-05-19 12:35:05 +00:00
lollmsElfServer . rt_com . stop_recording ( )
lollmsElfServer . rt_com = None