get / post / del config
parent
0fe079e616
commit
74090cbc28
|
|
@ -41,4 +41,5 @@ test-output
|
|||
|
||||
# misc
|
||||
# add other ignore file below
|
||||
__pycache__
|
||||
__pycache__
|
||||
/lobe_theme_config.json
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
def preload(parser):
|
||||
parser.add_argument(
|
||||
"--lobe-debug",
|
||||
action="store_true",
|
||||
help="Enable debug logging",
|
||||
)
|
||||
|
|
@ -1,6 +1,43 @@
|
|||
import json
|
||||
|
||||
from fastapi import FastAPI, Response, Request
|
||||
|
||||
from scripts.lib.config import LobeConfig
|
||||
from scripts.lib.lobe_log import LobeLog
|
||||
|
||||
|
||||
class LobeApi:
|
||||
def __init__(self, lobe_model_path):
|
||||
def __init__(self, config: LobeConfig):
|
||||
self.config = config
|
||||
pass
|
||||
|
||||
def create_api_route(self, app):
|
||||
pass
|
||||
def create_api_route(self, app: FastAPI):
|
||||
|
||||
@app.get("/lobe/config")
|
||||
async def lobe_config_get():
|
||||
LobeLog.debug("lobe_config_get")
|
||||
|
||||
if self.config.is_empty():
|
||||
return Response(content=self.config.json(), media_type="application/json", status_code=404)
|
||||
return Response(content=self.config.json(), media_type="application/json", status_code=200)
|
||||
|
||||
@app.post("/lobe/config")
|
||||
async def lobe_config_save(request: Request):
|
||||
LobeLog.debug("lobe_config_save")
|
||||
|
||||
data = await request.json()
|
||||
self.config.save(data)
|
||||
return Response(
|
||||
content=json.dumps({"message": "Config saved successfully"}),
|
||||
media_type="application/json", status_code=200
|
||||
)
|
||||
|
||||
@app.delete("/lobe/config")
|
||||
async def lobe_config_delete():
|
||||
LobeLog.debug("lobe_config_delete")
|
||||
|
||||
self.config.delete()
|
||||
return Response(
|
||||
content=json.dumps({"message": "Config deleted successfully"}),
|
||||
media_type="application/json", status_code=200
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from scripts.lib.lobe_log import LobeLog
|
||||
|
||||
EXTENSION_FOLDER = Path(__file__).parent.parent.parent
|
||||
CONFIG_FILENAME = Path(EXTENSION_FOLDER, "lobe_theme_config.json")
|
||||
|
||||
|
||||
LobeLog.debug(f"EXTENSION_FOLDER: {EXTENSION_FOLDER}")
|
||||
LobeLog.debug(f"CONFIG_FILENAME: {CONFIG_FILENAME}")
|
||||
|
||||
|
||||
class LobeConfig:
|
||||
def __init__(self):
|
||||
self.config_file = CONFIG_FILENAME
|
||||
self.config = None
|
||||
self.load_config()
|
||||
|
||||
def load_config(self):
|
||||
if os.path.exists(self.config_file):
|
||||
LobeLog.debug(f"Loading config from {self.config_file}")
|
||||
|
||||
with open(self.config_file, 'r') as f:
|
||||
self.config = json.load(f)
|
||||
else:
|
||||
LobeLog.debug(f"Config file not found")
|
||||
self.config = LobeConfig.default()
|
||||
|
||||
def is_empty(self):
|
||||
return "empty" in self.config and self.config['empty']
|
||||
|
||||
def json(self):
|
||||
return json.dumps(self.config)
|
||||
|
||||
def delete(self):
|
||||
if os.path.exists(self.config_file):
|
||||
os.remove(self.config_file)
|
||||
self.config = LobeConfig.default()
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def save(self, settings):
|
||||
self.config = settings
|
||||
with open(self.config_file, 'w') as f:
|
||||
f.write(json.dumps(self.config, indent=2))
|
||||
f.close()
|
||||
|
||||
@staticmethod
|
||||
def default():
|
||||
# default config is handled from client side @see src/store/index.tsx
|
||||
return {'empty': True}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
from modules import shared
|
||||
|
||||
|
||||
class LobeLogClass:
|
||||
|
||||
def __init__(self):
|
||||
# added a launch argument to enable debug mode @see preload.py
|
||||
# just add --lobe-debug to the launch arguments
|
||||
self.logging_enabled = shared.cmd_opts.lobe_debug
|
||||
|
||||
def debug(self, message: str):
|
||||
if self.logging_enabled:
|
||||
print(f"[Lobe:DEBUG]: {message}")
|
||||
|
||||
def info(self, message: str):
|
||||
print(f"[Lobe]: {message}")
|
||||
|
||||
|
||||
LobeLog = LobeLogClass()
|
||||
|
|
@ -1,10 +1,17 @@
|
|||
from fastapi import FastAPI
|
||||
|
||||
import modules.scripts as scripts
|
||||
import gradio as gr
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
from modules import shared
|
||||
from modules import script_callbacks
|
||||
|
||||
from scripts.lib.lobe_log import LobeLog
|
||||
from scripts.lib.api import LobeApi
|
||||
from scripts.lib.config import LobeConfig
|
||||
|
||||
|
||||
def on_ui_settings():
|
||||
section = ('lobe_theme', "Lobe Theme")
|
||||
|
|
@ -21,4 +28,13 @@ def on_ui_settings():
|
|||
)
|
||||
|
||||
|
||||
def init_lobe(_: Any, app: FastAPI, **kwargs):
|
||||
LobeLog.info("Initializing Lobe")
|
||||
|
||||
config = LobeConfig()
|
||||
api = LobeApi(config)
|
||||
api.create_api_route(app)
|
||||
|
||||
|
||||
script_callbacks.on_ui_settings(on_ui_settings)
|
||||
script_callbacks.on_app_started(init_lobe)
|
||||
|
|
|
|||
|
|
@ -119,6 +119,18 @@ export const useAppStore = create<AppState>()(
|
|||
},
|
||||
onSetSetting: (setting) => {
|
||||
localStorage.setItem(SETTING_KEY, JSON.stringify(setting));
|
||||
//TODO FIX ME (nevysha: I needed this for testing purpose)
|
||||
// sending settings to server.
|
||||
(async() => {
|
||||
await fetch('/lobe/config', {
|
||||
body: JSON.stringify(setting),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'POST',
|
||||
});
|
||||
})();
|
||||
|
||||
set(() => ({ setting }), false, 'onSetSetting');
|
||||
},
|
||||
onSetThemeMode: (themeMode) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue