2024-01-06 01:05:07 +00:00
|
|
|
"""
|
|
|
|
project: lollms_webui
|
|
|
|
file: lollms_webui_infos.py
|
|
|
|
author: ParisNeo
|
|
|
|
description:
|
|
|
|
This module contains a set of FastAPI routes that provide information about the Lord of Large Language and Multimodal Systems (LoLLMs) Web UI
|
|
|
|
application. These routes allow users to retrieve details such as the current version number, a list of available databases, and options for
|
|
|
|
updating or restarting the software.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
import sys
|
|
|
|
import time
|
2024-01-06 01:05:07 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import List
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
import pkg_resources
|
2024-01-13 23:42:18 +00:00
|
|
|
import socketio
|
2024-12-19 12:48:57 +00:00
|
|
|
from ascii_colors import ASCIIColors
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
from lollms.security import check_access, forbid_remote_access, sanitize_path
|
|
|
|
from lollms.utilities import load_config, run_async, show_yes_no_dialog
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
|
from lollms_webui import LOLLMSWebUI
|
|
|
|
|
2024-01-09 22:26:41 +00:00
|
|
|
# ----------------------- Defining router and main class ------------------------------
|
|
|
|
|
2024-01-06 01:05:07 +00:00
|
|
|
# Create an instance of the LoLLMSWebUI class
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer: LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
2024-01-06 01:05:07 +00:00
|
|
|
router = APIRouter()
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-11-25 21:48:47 +00:00
|
|
|
class LastViewedVideoUrlRequest(BaseModel):
|
2024-12-19 12:48:57 +00:00
|
|
|
client_id: str = Field(...)
|
2024-11-25 21:48:47 +00:00
|
|
|
last_viewed_video_url: str = Field(..., description="Last viewed model")
|
|
|
|
|
2024-03-18 21:30:18 +00:00
|
|
|
|
|
|
|
@router.get("/get_versionID")
|
2024-05-01 22:36:53 +00:00
|
|
|
async def get_lollms_version():
|
2024-12-19 12:48:57 +00:00
|
|
|
"""Get the version of the LoLLMs Web UI application."""
|
|
|
|
# Return the version string
|
|
|
|
return {"id": 9}
|
|
|
|
|
2024-11-17 23:29:18 +00:00
|
|
|
|
|
|
|
@router.get("/get_changeLog")
|
|
|
|
async def get_lollms_version():
|
2024-12-19 12:48:57 +00:00
|
|
|
"""Get the changelog."""
|
|
|
|
# Return the version string
|
|
|
|
with open("CHANGELOG.md", "r", encoding="utf8") as f:
|
|
|
|
infos = f.read()
|
|
|
|
return infos
|
2024-11-17 23:29:18 +00:00
|
|
|
|
2024-03-18 21:30:18 +00:00
|
|
|
|
2024-11-20 22:24:31 +00:00
|
|
|
@router.get("/get_news")
|
|
|
|
async def get_lollms_version():
|
|
|
|
"""Get the changelog."""
|
|
|
|
base_path = Path(__file__).parent
|
2024-12-19 12:48:57 +00:00
|
|
|
infos = base_path / "news" / "current.html"
|
2024-11-20 23:11:17 +00:00
|
|
|
return infos.read_text(encoding="utf8")
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
|
2024-11-25 21:48:47 +00:00
|
|
|
import json
|
2024-12-19 12:48:57 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-11-20 22:24:31 +00:00
|
|
|
|
2024-11-24 16:47:40 +00:00
|
|
|
@router.get("/get_last_video_url")
|
2024-11-25 21:48:47 +00:00
|
|
|
async def get_last_video_url():
|
|
|
|
"""Get the URL and type of the last video."""
|
|
|
|
base_path = Path(__file__).parent
|
|
|
|
info_file = base_path / "news" / "latest_video.json"
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-11-25 21:48:47 +00:00
|
|
|
try:
|
2024-12-19 12:48:57 +00:00
|
|
|
with open(info_file, "r", encoding="utf-8") as file:
|
2024-11-25 21:48:47 +00:00
|
|
|
video_info = json.load(file)
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
return {"url": video_info["url"], "type": video_info["type"]}
|
2024-11-25 21:48:47 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
return {"error": "Video information not found"}
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return {"error": "Invalid JSON format in the video information file"}
|
|
|
|
except KeyError as e:
|
|
|
|
return {"error": f"Missing key in JSON: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/get_last_viewed_video_url")
|
2024-11-24 16:47:40 +00:00
|
|
|
async def get_last_video_url():
|
|
|
|
"""Get the URL of the last video."""
|
|
|
|
# This is a static URL for demonstration purposes
|
2024-11-25 21:48:47 +00:00
|
|
|
return lollmsElfServer.config.last_viewed_video
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/set_last_viewed_video_url")
|
2024-12-19 12:48:57 +00:00
|
|
|
async def set_last_video_url(req: LastViewedVideoUrlRequest):
|
2024-11-25 21:48:47 +00:00
|
|
|
"""Get the URL of the last video."""
|
|
|
|
# This is a static URL for demonstration purposes
|
2024-12-19 12:48:57 +00:00
|
|
|
check_access(lollmsElfServer, req.client_id)
|
2024-11-25 21:48:47 +00:00
|
|
|
lollmsElfServer.config.last_viewed_video = req.last_viewed_video_url
|
|
|
|
lollmsElfServer.config.save_config()
|
2024-11-24 16:47:40 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-11-24 22:38:28 +00:00
|
|
|
@router.get("/get_themes")
|
|
|
|
async def get_themes():
|
|
|
|
"""Get the list of available themes."""
|
|
|
|
base_path = Path(__file__).parent.parent
|
|
|
|
themes_path = base_path / "web" / "dist" / "themes"
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-11-24 22:38:28 +00:00
|
|
|
# Get all .css files in the themes directory
|
2024-12-19 12:48:57 +00:00
|
|
|
theme_files = list(themes_path.glob("*.css"))
|
|
|
|
|
2024-11-24 22:38:28 +00:00
|
|
|
# Remove the .css extension from each file name
|
|
|
|
themes = [theme_file.stem for theme_file in theme_files]
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-11-24 22:38:28 +00:00
|
|
|
return themes
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-01-06 01:05:07 +00:00
|
|
|
@router.get("/get_lollms_webui_version")
|
|
|
|
async def get_lollms_webui_version():
|
2024-12-19 12:48:57 +00:00
|
|
|
"""Get the version of the LoLLMs Web UI application."""
|
|
|
|
# Return the version string
|
|
|
|
return lollmsElfServer.version
|
|
|
|
|
2024-01-06 01:05:07 +00:00
|
|
|
|
2024-05-01 22:36:53 +00:00
|
|
|
class Identification(BaseModel):
|
2024-12-19 12:48:57 +00:00
|
|
|
client_id: str
|
|
|
|
|
2024-01-06 01:05:07 +00:00
|
|
|
|
2024-05-01 22:36:53 +00:00
|
|
|
@router.post("/restart_program")
|
2024-12-19 12:48:57 +00:00
|
|
|
async def restart_program(data: Identification):
|
2024-05-01 22:36:53 +00:00
|
|
|
check_access(lollmsElfServer, data.client_id)
|
2024-01-24 11:08:07 +00:00
|
|
|
"""Restart the program."""
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-02-17 23:14:52 +00:00
|
|
|
if lollmsElfServer.config.headless_server_mode:
|
2024-12-19 12:48:57 +00:00
|
|
|
return {
|
|
|
|
"status": False,
|
|
|
|
"error": "Restarting app is blocked when in headless mode for obvious security reasons!",
|
|
|
|
}
|
2024-02-17 23:14:52 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
if (
|
|
|
|
lollmsElfServer.config.host != "localhost"
|
|
|
|
and lollmsElfServer.config.host != "127.0.0.1"
|
|
|
|
):
|
|
|
|
return {
|
|
|
|
"status": False,
|
|
|
|
"error": "Restarting app is blocked when the server is exposed outside for very obvious reasons!",
|
|
|
|
}
|
2024-02-17 23:14:52 +00:00
|
|
|
|
2024-05-01 22:36:53 +00:00
|
|
|
if lollmsElfServer.config.turn_on_setting_update_validation:
|
2024-12-19 12:48:57 +00:00
|
|
|
if not show_yes_no_dialog(
|
|
|
|
"Validation",
|
|
|
|
"Reboot requested from client\nDo you validate rebooting the app?",
|
|
|
|
):
|
|
|
|
return {"status": False, "error": "User refused the execution!"}
|
2024-05-01 22:36:53 +00:00
|
|
|
|
2024-01-24 11:08:07 +00:00
|
|
|
lollmsElfServer.ShowBlockingMessage("Restarting program.\nPlease stand by...")
|
|
|
|
# Stop the socketIO server
|
|
|
|
run_async(lollmsElfServer.sio.shutdown)
|
|
|
|
# Sleep for 1 second before rebooting
|
|
|
|
time.sleep(1)
|
|
|
|
lollmsElfServer.HideBlockingMessage()
|
|
|
|
# Reboot the program
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info(" ╔══════════════════════════════════════════════════╗")
|
|
|
|
ASCIIColors.info(" ║ Restarting backend ║")
|
|
|
|
ASCIIColors.info(" ╚══════════════════════════════════════════════════╝")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
lollmsElfServer.run_restart_script(lollmsElfServer.args)
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
|
2024-05-01 22:36:53 +00:00
|
|
|
@router.post("/update_software")
|
2024-12-19 12:48:57 +00:00
|
|
|
async def update_software(data: Identification):
|
2024-05-01 22:36:53 +00:00
|
|
|
check_access(lollmsElfServer, data.client_id)
|
2024-01-24 11:08:07 +00:00
|
|
|
"""Update the software."""
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-02-17 23:14:52 +00:00
|
|
|
if lollmsElfServer.config.headless_server_mode:
|
2024-12-19 12:48:57 +00:00
|
|
|
return {
|
|
|
|
"status": False,
|
|
|
|
"error": "Updating app 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": "Updating app is blocked when the server is exposed outside for very obvious reasons!",
|
|
|
|
}
|
2024-02-17 23:14:52 +00:00
|
|
|
|
2024-05-01 22:36:53 +00:00
|
|
|
if lollmsElfServer.config.turn_on_setting_update_validation:
|
2024-12-19 12:48:57 +00:00
|
|
|
if not show_yes_no_dialog(
|
|
|
|
"Validation",
|
|
|
|
"App upgrade requested from client\nDo you validate rebooting the app?",
|
|
|
|
):
|
|
|
|
return {"status": False, "error": "User refused the execution!"}
|
|
|
|
|
2024-01-24 11:08:07 +00:00
|
|
|
# Display an informative message
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("╔══════════════════════════════════════════════════╗")
|
|
|
|
ASCIIColors.info("║ Updating backend ║")
|
|
|
|
ASCIIColors.info("╚══════════════════════════════════════════════════╝")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
ASCIIColors.info("")
|
|
|
|
# Stop the socketIO server
|
|
|
|
await lollmsElfServer.sio.shutdown()
|
|
|
|
# Sleep for 1 second before rebooting
|
2024-12-19 12:48:57 +00:00
|
|
|
time.sleep(1)
|
2024-01-13 23:42:18 +00:00
|
|
|
|
2024-01-24 11:08:07 +00:00
|
|
|
# Run the update script using the provided arguments
|
|
|
|
lollmsElfServer.run_update_script(lollmsElfServer.args)
|
|
|
|
# Exit the program after successful update
|
|
|
|
sys.exit()
|
2024-01-06 01:05:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/check_update")
|
|
|
|
def check_update():
|
|
|
|
"""Checks if an update is available"""
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-02-17 23:14:52 +00:00
|
|
|
if lollmsElfServer.config.headless_server_mode:
|
2024-12-19 12:48:57 +00:00
|
|
|
return {
|
|
|
|
"status": False,
|
|
|
|
"error": "Checking updates 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": "Checking updates is blocked when the server is exposed outside for very obvious reasons!",
|
|
|
|
}
|
2024-02-17 23:14:52 +00:00
|
|
|
|
2024-01-06 01:05:07 +00:00
|
|
|
if lollmsElfServer.config.auto_update:
|
|
|
|
res = lollmsElfServer.check_update_()
|
2024-12-19 12:48:57 +00:00
|
|
|
return {"update_availability": res}
|
2024-01-06 01:05:07 +00:00
|
|
|
else:
|
2024-12-19 12:48:57 +00:00
|
|
|
return {"update_availability": False}
|