Provide a developer mode when running ktem (#135)
Implement and utilize `on_app_created` to support the developer mode.
This commit is contained in:
parent
2dd531114f
commit
ebc61400d8
|
@ -11,6 +11,7 @@ user_cache_dir.mkdir(parents=True, exist_ok=True)
|
|||
|
||||
|
||||
COHERE_API_KEY = config("COHERE_API_KEY", default="")
|
||||
# KH_MODE = "dev"
|
||||
KH_DATABASE = f"sqlite:///{user_cache_dir / 'sql.db'}"
|
||||
KH_DOCSTORE = {
|
||||
"__type__": "kotaemon.storages.SimpleFileDocumentStore",
|
||||
|
|
|
@ -34,6 +34,8 @@ class BaseApp:
|
|||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.dev_mode = getattr(settings, "KH_MODE", "") == "dev"
|
||||
|
||||
dir_assets = Path(__file__).parent / "assets"
|
||||
with (dir_assets / "css" / "main.css").open() as fi:
|
||||
self._css = fi.read()
|
||||
|
@ -56,7 +58,7 @@ class BaseApp:
|
|||
self.default_settings.index.finalize()
|
||||
|
||||
self.settings_state = gr.State(self.default_settings.flatten())
|
||||
self.user_id = gr.State(None)
|
||||
self.user_id = gr.State(1 if self.dev_mode else None)
|
||||
|
||||
def register_indices(self):
|
||||
"""Register the index components from app settings"""
|
||||
|
@ -155,6 +157,10 @@ class BaseApp:
|
|||
if isinstance(value, BasePage):
|
||||
value.register_events()
|
||||
|
||||
for value in self.__dict__.values():
|
||||
if isinstance(value, BasePage):
|
||||
value.on_app_created()
|
||||
|
||||
demo.load(lambda: None, None, None, js=f"() => {{{self._js}}}")
|
||||
|
||||
return demo
|
||||
|
@ -177,6 +183,9 @@ class BasePage:
|
|||
def on_register_events(self):
|
||||
"""Register all events to the app"""
|
||||
|
||||
def _on_app_created(self):
|
||||
"""Called when the app is created"""
|
||||
|
||||
def declare_public_events(self):
|
||||
"""Declare an event for the app"""
|
||||
for event in self.public_events:
|
||||
|
@ -199,3 +208,10 @@ class BasePage:
|
|||
for value in self.__dict__.values():
|
||||
if isinstance(value, BasePage):
|
||||
value.register_events()
|
||||
|
||||
def on_app_created(self):
|
||||
"""Execute on app created callbacks"""
|
||||
self._on_app_created()
|
||||
for value in self.__dict__.values():
|
||||
if isinstance(value, BasePage):
|
||||
value.on_app_created()
|
||||
|
|
|
@ -191,3 +191,11 @@ class ConversationControl(BasePage):
|
|||
|
||||
history = self.load_chat_history(user_id)
|
||||
return gr.update(choices=history), conversation_id
|
||||
|
||||
def _on_app_created(self):
|
||||
"""Reload the conversation once the app is created"""
|
||||
self._app.app.load(
|
||||
self.reload_conv,
|
||||
inputs=[self._app.user_id],
|
||||
outputs=[self.conversation],
|
||||
)
|
||||
|
|
|
@ -68,6 +68,7 @@ class SettingsPage(BasePage):
|
|||
|
||||
def on_building_ui(self):
|
||||
self.setting_save_btn = gr.Button("Save settings")
|
||||
if not self._app.dev_mode:
|
||||
with gr.Tab("User settings"):
|
||||
self.user_tab()
|
||||
with gr.Tab("General application settings"):
|
||||
|
@ -86,6 +87,13 @@ class SettingsPage(BasePage):
|
|||
inputs=[self._user_id] + self.components(),
|
||||
outputs=self._settings_state,
|
||||
)
|
||||
self._components["reasoning.use"].change(
|
||||
self.change_reasoning_mode,
|
||||
inputs=[self._components["reasoning.use"]],
|
||||
outputs=list(self._reasoning_mode.values()),
|
||||
show_progress="hidden",
|
||||
)
|
||||
if not self._app.dev_mode:
|
||||
self.password_change_btn.click(
|
||||
self.change_password,
|
||||
inputs=[
|
||||
|
@ -96,12 +104,6 @@ class SettingsPage(BasePage):
|
|||
outputs=None,
|
||||
show_progress="hidden",
|
||||
)
|
||||
self._components["reasoning.use"].change(
|
||||
self.change_reasoning_mode,
|
||||
inputs=[self._components["reasoning.use"]],
|
||||
outputs=list(self._reasoning_mode.values()),
|
||||
show_progress="hidden",
|
||||
)
|
||||
|
||||
onSignInClick = self.signin.click(
|
||||
self.sign_in,
|
||||
|
@ -163,7 +165,9 @@ class SettingsPage(BasePage):
|
|||
onSignOutClick = self.signout.click(
|
||||
self.sign_out,
|
||||
inputs=None,
|
||||
outputs=[self._user_id] + self.signed_in_state() + [self.user_out_state],
|
||||
outputs=[self._user_id]
|
||||
+ self.signed_in_state()
|
||||
+ [self.user_out_state],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
self.load_setting,
|
||||
|
|
Loading…
Reference in New Issue
Block a user