coolstuff / services /open_webui_service.py
SamuelLance73's picture
Automated deployment update from ML build
2fc86a1 verified
Raw
History Blame Contribute Delete
2.41 kB
_A='127.0.0.1'
import os,subprocess,threading,time
from pathlib import Path
from loguru import logger
METRICS_DIR='/home/user/.torch_metrics'
PORT=3000
PREFIX='[open-webui]'
VENV_DIR='/home/user/.venv-openwebui'
OWUI_BIN=f"{VENV_DIR}/bin/open-webui"
def _ensure_installed(log=None):
A=log
if Path(OWUI_BIN).exists():logger.debug(f"{PREFIX} open-webui binary found at {OWUI_BIN}, skipping install.");return OWUI_BIN
logger.info(f"{PREFIX} open-webui not found — running first-boot install into {VENV_DIR} ...");logger.info(f"{PREFIX} This will take a few minutes on the first start only.")
def B(cmd,**C):
B=subprocess.Popen(cmd,stdout=A if A else subprocess.PIPE,stderr=A if A else subprocess.STDOUT,**C);B.wait()
if B.returncode!=0:raise RuntimeError(f"{PREFIX} Install step failed: {" ".join(cmd)} (exit {B.returncode})")
C=time.time();B(['uv','venv',VENV_DIR]);B(['uv','pip','install','--python',VENV_DIR,'--no-cache-dir','open-webui']);D=time.time()-C;logger.success(f"{PREFIX} open-webui installed in {D:.1f}s — subsequent boots will be instant.");return OWUI_BIN
def _start_worker(log,env):A=log;B=_ensure_installed(A);C=[B,'serve','--host',_A,'--port',str(PORT)];logger.info(f"{PREFIX} Starting Open WebUI on 127.0.0.1:{PORT}...");D=subprocess.Popen(C,stdout=A,stderr=A,env=env);logger.success(f"{PREFIX} Open WebUI started (pid {D.pid}). Reachable at http://127.0.0.1:{PORT} exclusively over Tailscale / private overlay networks.")
def start(log):F='DATA_DIR';E='LITELLM_MASTER_KEY';D='http://127.0.0.1:8080/v1';C='none';B=True;Path(METRICS_DIR).mkdir(parents=B,exist_ok=B);A=os.environ.copy();A['OPENAI_API_BASE_URL']=D;A['OPENAI_API_KEY']=A.get(E,C)or C;A['WEBUI_AUTH']='False';A.setdefault('WEBUI_SECRET_KEY','sanctuary_owui_secret_key_server01');A.setdefault('OAUTH_SESSION_TOKEN_ENCRYPTION_KEY','sanctuary_owui_oauth_token_key_server01_32bytes');A[F]=str(Path(METRICS_DIR)/'open_webui_data');Path(A[F]).mkdir(parents=B,exist_ok=B);A['PORT']=str(PORT);A['HOST']=_A;A['UVICORN_HOST']=_A;A['UVICORN_PORT']=str(PORT);A['ENABLE_RAG']='True';A['RAG_EMBEDDING_ENGINE']='openai';A['RAG_EMBEDDING_OPENAI_API_BASE_URL']=D;A['RAG_EMBEDDING_OPENAI_API_KEY']=A.get(E,C)or C;A['AUDIO_STT_ENGINE']='webapi';A['AUDIO_TTS_ENGINE']='';G=threading.Thread(target=_start_worker,args=(log,A),daemon=B);G.start();logger.info(f"{PREFIX} install/launch dispatched to background thread (orchestrator unblocked).")