2024-01-11 01:32:21 +00:00
|
|
|
"""
|
|
|
|
project: lollms_webui
|
|
|
|
file: lollms_xtts.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
|
|
|
|
|
|
|
|
"""
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
import json
|
2024-01-11 01:32:21 +00:00
|
|
|
import os
|
|
|
|
import platform
|
2024-12-19 12:48:57 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
from ascii_colors import ASCIIColors
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
|
|
from lollms.main_config import BaseConfig
|
|
|
|
from lollms.security import (check_access, forbid_remote_access,
|
|
|
|
sanitize_path_from_endpoint, validate_path)
|
|
|
|
from lollms.types import MSG_OPERATION_TYPE
|
|
|
|
from lollms.utilities import (PackageManager, add_period, detect_antiprompt,
|
|
|
|
find_first_available_file_index,
|
|
|
|
remove_text_from_string, trace_exception)
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from starlette.responses import StreamingResponse
|
|
|
|
|
|
|
|
from lollms_webui import LOLLMSWebUI
|
|
|
|
|
2024-01-11 01:32:21 +00:00
|
|
|
# ----------------------- Defining router and main class ------------------------------
|
|
|
|
|
|
|
|
router = APIRouter()
|
2024-12-19 12:48:57 +00:00
|
|
|
lollmsElfServer: LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
2024-01-11 01:32:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------- voice ------------------------------
|
2024-01-11 01:50:54 +00:00
|
|
|
@router.get("/get_presets")
|
2024-01-11 01:32:21 +00:00
|
|
|
def get_presets():
|
|
|
|
presets = []
|
2024-12-19 12:48:57 +00:00
|
|
|
presets_folder = Path("__file__").parent / "presets"
|
|
|
|
for filename in presets_folder.glob("*.yaml"):
|
|
|
|
with open(filename, "r", encoding="utf-8") as file:
|
2024-07-15 22:59:56 +00:00
|
|
|
try:
|
|
|
|
print(filename)
|
|
|
|
preset = yaml.safe_load(file)
|
|
|
|
if preset is not None:
|
|
|
|
presets.append(preset)
|
|
|
|
except Exception as ex:
|
|
|
|
trace_exception(ex)
|
2024-12-19 12:48:57 +00:00
|
|
|
presets_folder = (
|
|
|
|
lollmsElfServer.lollms_paths.personal_discussions_path
|
|
|
|
/ "lollms_playground_presets"
|
|
|
|
)
|
2024-01-11 01:32:21 +00:00
|
|
|
presets_folder.mkdir(exist_ok=True, parents=True)
|
2024-12-19 12:48:57 +00:00
|
|
|
for filename in presets_folder.glob("*.yaml"):
|
|
|
|
with open(filename, "r", encoding="utf-8") as file:
|
2024-01-11 01:32:21 +00:00
|
|
|
preset = yaml.safe_load(file)
|
|
|
|
if preset is not None:
|
|
|
|
presets.append(preset)
|
2024-01-11 01:50:54 +00:00
|
|
|
return presets
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-02-17 01:08:14 +00:00
|
|
|
class PresetData(BaseModel):
|
2024-03-31 19:18:26 +00:00
|
|
|
client_id: str
|
2024-02-17 01:08:14 +00:00
|
|
|
name: str = Field(..., min_length=1)
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-01-11 01:50:54 +00:00
|
|
|
@router.post("/add_preset")
|
2024-02-17 01:08:14 +00:00
|
|
|
async def add_preset(preset_data: PresetData):
|
2024-01-11 01:50:54 +00:00
|
|
|
"""
|
|
|
|
Changes current voice
|
|
|
|
|
|
|
|
:param request: The HTTP request object.
|
|
|
|
:return: A JSON response with the status of the operation.
|
|
|
|
"""
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-03-31 19:18:26 +00:00
|
|
|
check_access(lollmsElfServer, preset_data.client_id)
|
2024-02-17 01:08:14 +00:00
|
|
|
try:
|
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
presets_folder = (
|
|
|
|
lollmsElfServer.lollms_paths.personal_discussions_path
|
|
|
|
/ "lollms_playground_presets"
|
|
|
|
)
|
2024-02-17 01:08:14 +00:00
|
|
|
if not presets_folder.exists():
|
|
|
|
presets_folder.mkdir(exist_ok=True, parents=True)
|
|
|
|
|
|
|
|
# Ensure the name doesn't contain any path manipulation characters
|
2024-12-19 12:48:57 +00:00
|
|
|
sanitize_path_from_endpoint(
|
|
|
|
preset_data.name, exception_text="Invalid preset name"
|
|
|
|
)
|
2024-01-11 01:50:54 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
fn = preset_data.name.lower().replace(" ", "_")
|
|
|
|
filename = presets_folder / f"{fn}.yaml"
|
|
|
|
with open(filename, "w", encoding="utf-8") as file:
|
2024-02-17 01:08:14 +00:00
|
|
|
yaml.dump(preset_data, file)
|
|
|
|
return {"status": True}
|
|
|
|
except Exception as ex:
|
|
|
|
trace_exception(ex) # Assuming 'trace_exception' function logs the error
|
|
|
|
return {"status": False, "error": "There was an error adding the preset"}
|
2024-01-11 01:50:54 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
|
2024-01-11 01:50:54 +00:00
|
|
|
@router.post("/del_preset")
|
2024-02-17 01:08:14 +00:00
|
|
|
async def del_preset(preset_data: PresetData):
|
2024-01-11 01:50:54 +00:00
|
|
|
"""
|
|
|
|
Saves a preset to a file.
|
|
|
|
|
2024-02-17 01:08:14 +00:00
|
|
|
:param preset_data: The data of the preset.
|
2024-01-11 01:50:54 +00:00
|
|
|
:return: A JSON response with the status of the operation.
|
|
|
|
"""
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-03-31 19:18:26 +00:00
|
|
|
check_access(lollmsElfServer, preset_data.client_id)
|
2024-01-11 01:50:54 +00:00
|
|
|
# Get the JSON data from the POST request.
|
2024-02-17 01:08:14 +00:00
|
|
|
if preset_data.name is None:
|
2024-12-19 12:48:57 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=400, detail="Preset name is missing in the request"
|
|
|
|
)
|
|
|
|
|
2024-02-17 01:08:14 +00:00
|
|
|
# Ensure the name doesn't contain any path manipulation characters
|
2024-12-19 12:48:57 +00:00
|
|
|
sanitize_path_from_endpoint(preset_data.name, exception_text="Invalid preset name")
|
2024-02-17 01:08:14 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
presets_file = (
|
|
|
|
lollmsElfServer.lollms_paths.personal_discussions_path
|
|
|
|
/ "lollms_playground_presets"
|
|
|
|
/ preset_data.name
|
|
|
|
)
|
2024-02-17 01:08:14 +00:00
|
|
|
try:
|
|
|
|
presets_file.unlink()
|
2024-12-19 12:48:57 +00:00
|
|
|
return {"status": True}
|
2024-02-17 01:08:14 +00:00
|
|
|
except:
|
2024-12-19 12:48:57 +00:00
|
|
|
return {"status": False}
|
2024-01-11 01:50:54 +00:00
|
|
|
|
|
|
|
|
2024-02-18 23:23:15 +00:00
|
|
|
class PresetDataWithValue(BaseModel):
|
2024-03-31 19:18:26 +00:00
|
|
|
client_id: str
|
2024-02-18 23:23:15 +00:00
|
|
|
name: str = Field(..., min_length=1)
|
|
|
|
preset: str
|
2024-12-19 12:48:57 +00:00
|
|
|
|
|
|
|
|
2024-01-11 01:50:54 +00:00
|
|
|
@router.post("/save_presets")
|
2024-02-18 23:23:15 +00:00
|
|
|
async def save_presets(preset_data: PresetDataWithValue):
|
2024-01-11 01:50:54 +00:00
|
|
|
"""
|
|
|
|
Saves a preset to a file.
|
|
|
|
|
2024-02-17 01:08:14 +00:00
|
|
|
:param preset_data: The data of the preset.
|
2024-01-11 01:50:54 +00:00
|
|
|
:return: A JSON response with the status of the operation.
|
|
|
|
"""
|
2024-03-28 22:58:51 +00:00
|
|
|
forbid_remote_access(lollmsElfServer)
|
2024-03-31 19:18:26 +00:00
|
|
|
check_access(lollmsElfServer, preset_data.client_id)
|
2024-01-11 01:50:54 +00:00
|
|
|
# Get the JSON data from the POST request.
|
2024-02-17 01:08:14 +00:00
|
|
|
if preset_data.preset is None:
|
2024-12-19 12:48:57 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=400, detail="Preset data is missing in the request"
|
|
|
|
)
|
2024-02-25 09:50:44 +00:00
|
|
|
|
2024-02-23 20:20:37 +00:00
|
|
|
# Ensure the name doesn't contain any path manipulation characters
|
2024-12-19 12:48:57 +00:00
|
|
|
sanitize_path_from_endpoint(preset_data.name, exception_text="Invalid preset name")
|
2024-02-23 20:20:37 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
presets_file = (
|
|
|
|
lollmsElfServer.lollms_paths.personal_discussions_path / "presets.json"
|
|
|
|
)
|
2024-01-11 01:50:54 +00:00
|
|
|
# Save the JSON data to a file.
|
|
|
|
with open(presets_file, "w") as f:
|
2024-02-17 01:08:14 +00:00
|
|
|
json.dump(preset_data.preset, f, indent=4)
|
2024-01-11 01:50:54 +00:00
|
|
|
|
2024-12-19 12:48:57 +00:00
|
|
|
return {"status": True, "message": "Preset saved successfully!"}
|