lollms-webui/new_app.py

177 lines
7.3 KiB
Python
Raw Normal View History

2024-01-01 03:18:49 +00:00
"""
File: lollms_web_ui.py
Author: ParisNeo
Description: Singleton class for the LoLLMS web UI.
This file is the entry point to the webui.
"""
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
2024-01-05 02:40:55 +00:00
from fastapi.responses import HTMLResponse
2024-01-01 03:18:49 +00:00
from lollms.app import LollmsApplication
from lollms.paths import LollmsPaths
from lollms.main_config import LOLLMSConfig
2024-01-07 21:34:33 +00:00
from lollms.utilities import trace_exception
2024-01-04 02:57:42 +00:00
from lollms_webui import LOLLMSWebUI
2024-01-01 03:18:49 +00:00
from pathlib import Path
from ascii_colors import ASCIIColors
import socketio
import uvicorn
import argparse
2024-01-06 20:47:21 +00:00
from socketio import ASGIApp
2024-01-07 21:34:33 +00:00
import webbrowser
2024-01-01 03:18:49 +00:00
app = FastAPI()
2024-01-05 02:40:55 +00:00
# Create a Socket.IO server
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*") # Enable CORS for all origins
2024-01-01 03:18:49 +00:00
2024-01-05 02:40:55 +00:00
@sio.event
async def disconnect(sid):
2024-01-06 20:47:21 +00:00
ASCIIColors.yellow(f"Disconnected: {sid}")
2024-01-05 02:40:55 +00:00
@sio.event
async def message(sid, data):
2024-01-06 20:47:21 +00:00
ASCIIColors.yellow(f"Message from {sid}: {data}")
2024-01-05 02:40:55 +00:00
await sio.send(sid, "Message received!")
#app.mount("/socket.io", StaticFiles(directory="path/to/socketio.js"))
2024-01-01 03:18:49 +00:00
if __name__ == "__main__":
# Parsong parameters
parser = argparse.ArgumentParser(description="Start the chatbot FastAPI app.")
parser.add_argument(
"--host", type=str, default=None, help="the hostname to listen on"
)
parser.add_argument("--port", type=int, default=None, help="the port to listen on")
args = parser.parse_args()
root_path = Path(__file__).parent
lollms_paths = LollmsPaths.find_paths(force_local=True, custom_default_cfg_path="configs/config.yaml")
config = LOLLMSConfig.autoload(lollms_paths)
if args.host:
config.host=args.host
if args.port:
config.port=args.port
2024-01-04 02:57:42 +00:00
LOLLMSWebUI.build_instance(config=config, lollms_paths=lollms_paths, socketio=sio)
2024-01-07 21:34:33 +00:00
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
2024-01-04 02:57:42 +00:00
# Import all endpoints
from lollms.server.endpoints.lollms_binding_files_server import router as lollms_binding_files_server_router
2024-01-04 13:17:41 +00:00
from lollms.server.endpoints.lollms_infos import router as lollms_infos_router
from lollms.server.endpoints.lollms_hardware_infos import router as lollms_hardware_infos_router
from lollms.server.endpoints.lollms_binding_infos import router as lollms_binding_infos_router
from lollms.server.endpoints.lollms_models_infos import router as lollms_models_infos_router
from lollms.server.endpoints.lollms_personalities_infos import router as lollms_personalities_infos_router
from lollms.server.endpoints.lollms_extensions_infos import router as lollms_extensions_infos_router
2024-01-04 13:17:41 +00:00
from lollms.server.endpoints.lollms_generator import router as lollms_generator_router
from lollms.server.endpoints.lollms_configuration_infos import router as lollms_configuration_infos_router
2024-01-11 01:32:21 +00:00
from endpoints.lollms_webui_infos import router as lollms_webui_infos_router
2024-01-07 14:27:14 +00:00
from endpoints.lollms_discussion import router as lollms_discussion_router
from endpoints.lollms_message import router as lollms_message_router
from endpoints.lollms_user import router as lollms_user_router
from endpoints.lollms_advanced import router as lollms_advanced_router
2024-01-07 23:22:23 +00:00
from endpoints.chat_bar import router as chat_bar_router
2024-01-10 20:22:17 +00:00
from endpoints.lollms_xtts import router as lollms_xtts_add_router
2024-01-11 01:32:21 +00:00
from endpoints.lollms_sd import router as lollms_sd_router
from endpoints.lollms_ollama import router as lollms_ollama_router
2024-01-11 01:50:54 +00:00
from endpoints.lollms_playground import router as lollms_playground_router
from lollms.server.events.lollms_generation_events import add_events as lollms_generation_events_add
2024-01-08 00:08:47 +00:00
from lollms.server.events.lollms_personality_events import add_events as lollms_personality_events_add
2024-01-09 22:26:41 +00:00
from lollms.server.events.lollms_files_events import add_events as lollms_files_events_add
from events.lollms_generation_events import add_events as lollms_webui_generation_events_add
from events.lollms_discussion_events import add_events as lollms_webui_discussion_events_add
2024-01-08 00:08:47 +00:00
from events.lollms_chatbox_events import add_events as lollms_chatbox_events_add
2024-01-09 22:26:41 +00:00
from events.lollms_interactive_events import add_events as lollms_interactive_events_add
2024-01-08 00:08:47 +00:00
2024-01-04 13:17:41 +00:00
app.include_router(lollms_infos_router)
app.include_router(lollms_binding_files_server_router)
app.include_router(lollms_hardware_infos_router)
app.include_router(lollms_binding_infos_router)
app.include_router(lollms_models_infos_router)
app.include_router(lollms_personalities_infos_router)
app.include_router(lollms_extensions_infos_router)
2024-01-11 01:32:21 +00:00
app.include_router(lollms_webui_infos_router)
2024-01-04 13:17:41 +00:00
app.include_router(lollms_generator_router)
2024-01-05 02:40:55 +00:00
app.include_router(lollms_discussion_router)
2024-01-07 14:27:14 +00:00
app.include_router(lollms_message_router)
app.include_router(lollms_user_router)
app.include_router(lollms_advanced_router)
2024-01-07 23:22:23 +00:00
app.include_router(chat_bar_router)
2024-01-10 20:22:17 +00:00
app.include_router(lollms_xtts_add_router)
2024-01-11 01:32:21 +00:00
app.include_router(lollms_sd_router)
app.include_router(lollms_ollama_router)
2024-01-11 01:50:54 +00:00
app.include_router(lollms_playground_router)
2024-01-11 01:32:21 +00:00
2024-01-07 14:27:14 +00:00
2024-01-09 22:26:41 +00:00
app.include_router(lollms_configuration_infos_router)
lollms_generation_events_add(sio)
2024-01-08 00:08:47 +00:00
lollms_personality_events_add(sio)
2024-01-09 22:26:41 +00:00
lollms_files_events_add(sio)
lollms_webui_generation_events_add(sio)
lollms_webui_discussion_events_add(sio)
2024-01-08 00:08:47 +00:00
lollms_chatbox_events_add(sio)
2024-01-09 22:26:41 +00:00
lollms_interactive_events_add(sio)
2024-01-04 13:17:41 +00:00
2024-01-07 23:22:23 +00:00
app.mount("/extensions", StaticFiles(directory=Path(__file__).parent/"web"/"dist", html=True), name="extensions")
2024-01-07 19:44:59 +00:00
app.mount("/playground", StaticFiles(directory=Path(__file__).parent/"web"/"dist", html=True), name="playground")
app.mount("/settings", StaticFiles(directory=Path(__file__).parent/"web"/"dist", html=True), name="settings")
app.mount("/", StaticFiles(directory=Path(__file__).parent/"web"/"dist", html=True), name="static")
2024-01-06 20:47:21 +00:00
app = ASGIApp(socketio_server=sio, other_asgi_app=app)
lollmsElfServer.app = app
2024-01-01 03:18:49 +00:00
2024-01-07 21:34:33 +00:00
# if autoshow
if config.auto_show_browser:
if config['host']=="0.0.0.0":
2024-01-07 23:22:23 +00:00
webbrowser.open(f"http://localhost:{config['port']}")
#webbrowser.open(f"http://localhost:{6523}") # needed for debug (to be removed in production)
2024-01-07 21:34:33 +00:00
else:
2024-01-07 23:22:23 +00:00
webbrowser.open(f"http://{config['host']}:{config['port']}")
#webbrowser.open(f"http://{config['host']}:{6523}") # needed for debug (to be removed in production)
2024-01-07 21:34:33 +00:00
try:
sio.reboot = False
2024-01-07 23:22:23 +00:00
#uvicorn.run(app, host=config.host, port=6523)
uvicorn.run(app, host=config.host, port=config.port)
2024-01-07 21:34:33 +00:00
if sio.reboot:
ASCIIColors.info("")
ASCIIColors.info("")
ASCIIColors.info("")
ASCIIColors.info(" ╔══════════════════════════════════════════════════╗")
ASCIIColors.info(" ║ Restarting backend ║")
ASCIIColors.info(" ╚══════════════════════════════════════════════════╝")
ASCIIColors.info("")
ASCIIColors.info("")
ASCIIColors.info("")
lollmsElfServer.run_restart_script(args)
except Exception as ex:
trace_exception(ex)