diff --git a/libs/ktem/ktem/main.py b/libs/ktem/ktem/main.py index 1d76d049..fa190b23 100644 --- a/libs/ktem/ktem/main.py +++ b/libs/ktem/ktem/main.py @@ -24,41 +24,57 @@ def ui(self): """Render the UI""" self._tabs = {} - if self.f_user_management: - from ktem.pages.login import LoginPage - - with gr.Tab("Welcome", elem_id="login-tab") as self._tabs["login-tab"]: - self.login_page = LoginPage(self) + with gr.Tabs() as self.tabs: + if self.f_user_management: + from ktem.pages.login import LoginPage - with gr.Tab( - "Chat", elem_id="chat-tab", visible=not self.f_user_management - ) as self._tabs["chat-tab"]: - self.chat_page = ChatPage(self) + with gr.Tab( + "Welcome", elem_id="login-tab", id="login-tab" + ) as self._tabs["login-tab"]: + self.login_page = LoginPage(self) - for index in self.index_manager.indices: with gr.Tab( - f"{index.name} Index", - elem_id=f"{index.id}-tab", - elem_classes="indices-tab", + "Chat", + elem_id="chat-tab", + id="chat-tab", visible=not self.f_user_management, - ) as self._tabs[f"{index.id}-tab"]: - page = index.get_index_page_ui() - setattr(self, f"_index_{index.id}", page) + ) as self._tabs["chat-tab"]: + self.chat_page = ChatPage(self) + + for index in self.index_manager.indices: + with gr.Tab( + f"{index.name} Index", + elem_id=f"{index.id}-tab", + elem_classes="indices-tab", + id=f"{index.id}-tab", + visible=not self.f_user_management, + ) as self._tabs[f"{index.id}-tab"]: + page = index.get_index_page_ui() + setattr(self, f"_index_{index.id}", page) - with gr.Tab( - "Admin", elem_id="admin-tab", visible=not self.f_user_management - ) as self._tabs["admin-tab"]: - self.admin_page = AdminPage(self) + with gr.Tab( + "Admin", + elem_id="admin-tab", + id="admin-tab", + visible=not self.f_user_management, + ) as self._tabs["admin-tab"]: + self.admin_page = AdminPage(self) - with gr.Tab( - "Settings", elem_id="settings-tab", visible=not self.f_user_management - ) as self._tabs["settings-tab"]: - self.settings_page = SettingsPage(self) + with gr.Tab( + "Settings", + elem_id="settings-tab", + id="settings-tab", + visible=not self.f_user_management, + ) as self._tabs["settings-tab"]: + self.settings_page = SettingsPage(self) - with gr.Tab( - "Help", elem_id="help-tab", visible=not self.f_user_management - ) as self._tabs["help-tab"]: - self.help_page = HelpPage(self) + with gr.Tab( + "Help", + elem_id="help-tab", + id="help-tab", + visible=not self.f_user_management, + ) as self._tabs["help-tab"]: + self.help_page = HelpPage(self) def on_subscribe_public_events(self): if self.f_user_management: @@ -75,7 +91,7 @@ def signed_in_out(user_id): else gr.update(visible=False) ) for k in self._tabs.keys() - ) + ) + [gr.update(selected="login-tab")] with Session(engine) as session: user = session.exec(select(User).where(User.id == user_id)).first() @@ -100,6 +116,8 @@ def signed_in_out(user_id): else: tabs_update.append(gr.update(visible=True)) + tabs_update.append(gr.update(selected="chat-tab")) + return tabs_update self.subscribe_event( @@ -107,7 +125,7 @@ def signed_in_out(user_id): definition={ "fn": signed_in_out, "inputs": [self.user_id], - "outputs": list(self._tabs.values()), + "outputs": list(self._tabs.values()) + [self.tabs], "show_progress": "hidden", }, ) @@ -117,7 +135,7 @@ def signed_in_out(user_id): definition={ "fn": signed_in_out, "inputs": [self.user_id], - "outputs": list(self._tabs.values()), + "outputs": list(self._tabs.values()) + [self.tabs], "show_progress": "hidden", }, ) diff --git a/libs/ktem/ktem/pages/chat/__init__.py b/libs/ktem/ktem/pages/chat/__init__.py index a83bd837..04c78eb7 100644 --- a/libs/ktem/ktem/pages/chat/__init__.py +++ b/libs/ktem/ktem/pages/chat/__init__.py @@ -304,6 +304,7 @@ def on_subscribe_public_events(self): self.chat_control.conversation, self.chat_control.conversation_rn, self.chat_panel.chatbot, + self.info_panel, ] + self._indices_input, "show_progress": "hidden", @@ -406,13 +407,19 @@ async def chat_fn(self, conversation_id, chat_history, settings, state, *selecte text, refs = "", "" len_ref = -1 # for logging purpose + msg_placeholder = getattr( + flowsettings, "KH_CHAT_MSG_PLACEHOLDER", "Thinking ..." + ) + print(msg_placeholder) while True: try: response = queue.get_nowait() except Exception: state[pipeline.get_info()["id"]] = reasoning_state["pipeline"] - yield chat_history + [(chat_input, text or "Thinking ...")], refs, state + yield chat_history + [ + (chat_input, text or msg_placeholder) + ], refs, state continue if response is None: diff --git a/libs/ktem/ktem/pages/settings.py b/libs/ktem/ktem/pages/settings.py index 20912cb4..1c9f55fe 100644 --- a/libs/ktem/ktem/pages/settings.py +++ b/libs/ktem/ktem/pages/settings.py @@ -72,6 +72,31 @@ def __init__(self, app): self._components = {} self._reasoning_mode = {} + # render application page if there are application settings + self._render_app_tab = False + if self._default_settings.application.settings: + self._render_app_tab = True + + # render index page if there are index settings (general and/or specific) + self._render_index_tab = False + if self._default_settings.index.settings: + self._render_index_tab = True + else: + for sig in self._default_settings.index.options.values(): + if sig.settings: + self._render_index_tab = True + break + + # render reasoning page if there are reasoning settings + self._render_reasoning_tab = False + if len(self._default_settings.reasoning.settings) > 1: + self._render_reasoning_tab = True + else: + for sig in self._default_settings.reasoning.options.values(): + if sig.settings: + self._render_reasoning_tab = True + break + self.on_building_ui() def on_building_ui(self): @@ -79,12 +104,9 @@ def on_building_ui(self): if self._app.f_user_management: with gr.Tab("User settings"): self.user_tab() - with gr.Tab("General application settings"): - self.app_tab() - with gr.Tab("Index settings"): - self.index_tab() - with gr.Tab("Reasoning settings"): - self.reasoning_tab() + self.app_tab() + self.index_tab() + self.reasoning_tab() def on_subscribe_public_events(self): """ @@ -221,9 +243,10 @@ def change_password(self, user_id, password, password_confirm): return "", "" def app_tab(self): - for n, si in self._default_settings.application.settings.items(): - obj = render_setting_item(si, si.value) - self._components[f"application.{n}"] = obj + with gr.Tab("General application settings", visible=self._render_app_tab): + for n, si in self._default_settings.application.settings.items(): + obj = render_setting_item(si, si.value) + self._components[f"application.{n}"] = obj def index_tab(self): # TODO: double check if we need general @@ -232,37 +255,39 @@ def index_tab(self): # obj = render_setting_item(si, si.value) # self._components[f"index.{n}"] = obj - for pn, sig in self._default_settings.index.options.items(): - with gr.Tab(f"Index {pn}"): - for n, si in sig.settings.items(): - obj = render_setting_item(si, si.value) - self._components[f"index.options.{pn}.{n}"] = obj + with gr.Tab("Index settings", visible=self._render_index_tab): + for pn, sig in self._default_settings.index.options.items(): + with gr.Tab(f"Index {pn}"): + for n, si in sig.settings.items(): + obj = render_setting_item(si, si.value) + self._components[f"index.options.{pn}.{n}"] = obj def reasoning_tab(self): - with gr.Group(): - for n, si in self._default_settings.reasoning.settings.items(): - if n == "use": - continue - obj = render_setting_item(si, si.value) - self._components[f"reasoning.{n}"] = obj + with gr.Tab("Reasoning settings", visible=self._render_reasoning_tab): + with gr.Group(): + for n, si in self._default_settings.reasoning.settings.items(): + if n == "use": + continue + obj = render_setting_item(si, si.value) + self._components[f"reasoning.{n}"] = obj - gr.Markdown("### Reasoning-specific settings") - self._components["reasoning.use"] = render_setting_item( - self._default_settings.reasoning.settings["use"], - self._default_settings.reasoning.settings["use"].value, - ) + gr.Markdown("### Reasoning-specific settings") + self._components["reasoning.use"] = render_setting_item( + self._default_settings.reasoning.settings["use"], + self._default_settings.reasoning.settings["use"].value, + ) - for idx, (pn, sig) in enumerate( - self._default_settings.reasoning.options.items() - ): - with gr.Group( - visible=idx == 0, - elem_id=pn, - ) as self._reasoning_mode[pn]: - gr.Markdown("**Name**: Description") - for n, si in sig.settings.items(): - obj = render_setting_item(si, si.value) - self._components[f"reasoning.options.{pn}.{n}"] = obj + for idx, (pn, sig) in enumerate( + self._default_settings.reasoning.options.items() + ): + with gr.Group( + visible=idx == 0, + elem_id=pn, + ) as self._reasoning_mode[pn]: + gr.Markdown("**Name**: Description") + for n, si in sig.settings.items(): + obj = render_setting_item(si, si.value) + self._components[f"reasoning.options.{pn}.{n}"] = obj def change_reasoning_mode(self, value): output = []