mirror of
https://github.com/Cinnamon/kotaemon.git
synced 2026-02-24 03:59:47 +01:00
feat: add demo mode
This commit is contained in:
@@ -21,6 +21,8 @@ from theflow.settings import settings as flowsettings
|
||||
|
||||
from ...utils.commands import WEB_SEARCH_COMMAND
|
||||
|
||||
KH_DEMO_MODE = getattr(flowsettings, "KH_DEMO_MODE", False)
|
||||
KH_SSO_ENABLED = getattr(flowsettings, "KH_SSO_ENABLED", False)
|
||||
DOWNLOAD_MESSAGE = "Start download"
|
||||
MAX_FILENAME_LENGTH = 20
|
||||
|
||||
@@ -138,7 +140,9 @@ class FileIndexPage(BasePage):
|
||||
# TODO: on_building_ui is not correctly named if it's always called in
|
||||
# the constructor
|
||||
self.public_events = [f"onFileIndex{index.id}Changed"]
|
||||
self.on_building_ui()
|
||||
|
||||
if not KH_DEMO_MODE:
|
||||
self.on_building_ui()
|
||||
|
||||
def upload_instruction(self) -> str:
|
||||
msgs = []
|
||||
@@ -211,10 +215,10 @@ class FileIndexPage(BasePage):
|
||||
|
||||
with gr.Accordion("Advance options", open=False):
|
||||
with gr.Row():
|
||||
# self.download_all_button = gr.DownloadButton(
|
||||
# "Download all files",
|
||||
# visible=False,
|
||||
# )
|
||||
if not KH_SSO_ENABLED:
|
||||
self.download_all_button = gr.DownloadButton(
|
||||
"Download all files",
|
||||
)
|
||||
self.delete_all_button = gr.Button(
|
||||
"Delete all files",
|
||||
variant="stop",
|
||||
@@ -334,6 +338,9 @@ class FileIndexPage(BasePage):
|
||||
|
||||
def on_subscribe_public_events(self):
|
||||
"""Subscribe to the declared public event of the app"""
|
||||
if KH_DEMO_MODE:
|
||||
return
|
||||
|
||||
self._app.subscribe_event(
|
||||
name=f"onFileIndex{self._index.id}Changed",
|
||||
definition={
|
||||
@@ -581,158 +588,7 @@ class FileIndexPage(BasePage):
|
||||
gr.update(visible=True),
|
||||
]
|
||||
|
||||
def on_register_events(self):
|
||||
"""Register all events to the app"""
|
||||
onDeleted = (
|
||||
self.delete_button.click(
|
||||
fn=self.delete_event,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=None,
|
||||
)
|
||||
.then(
|
||||
fn=lambda: (None, self.selected_panel_false),
|
||||
inputs=[],
|
||||
outputs=[self.selected_file_id, self.selected_panel],
|
||||
show_progress="hidden",
|
||||
)
|
||||
.then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
)
|
||||
.then(
|
||||
fn=self.file_selected,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=[
|
||||
self.chunks,
|
||||
self.deselect_button,
|
||||
self.delete_button,
|
||||
self.download_single_button,
|
||||
self.chat_button,
|
||||
],
|
||||
show_progress="hidden",
|
||||
)
|
||||
)
|
||||
for event in self._app.get_event(f"onFileIndex{self._index.id}Changed"):
|
||||
onDeleted = onDeleted.then(**event)
|
||||
|
||||
self.deselect_button.click(
|
||||
fn=lambda: (None, self.selected_panel_false),
|
||||
inputs=[],
|
||||
outputs=[self.selected_file_id, self.selected_panel],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self.file_selected,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=[
|
||||
self.chunks,
|
||||
self.deselect_button,
|
||||
self.delete_button,
|
||||
self.download_single_button,
|
||||
self.chat_button,
|
||||
],
|
||||
show_progress="hidden",
|
||||
)
|
||||
|
||||
self.chat_button.click(
|
||||
fn=self.set_file_id_selector,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=[
|
||||
self._index.get_selector_component_ui().selector,
|
||||
self._index.get_selector_component_ui().mode,
|
||||
self._app.tabs,
|
||||
],
|
||||
)
|
||||
|
||||
# self.download_all_button.click(
|
||||
# fn=self.download_all_files,
|
||||
# inputs=[],
|
||||
# outputs=self.download_all_button,
|
||||
# show_progress="hidden",
|
||||
# )
|
||||
|
||||
self.delete_all_button.click(
|
||||
self.show_delete_all_confirm,
|
||||
[self.file_list],
|
||||
[
|
||||
self.delete_all_button,
|
||||
self.delete_all_button_confirm,
|
||||
self.delete_all_button_cancel,
|
||||
],
|
||||
)
|
||||
self.delete_all_button_cancel.click(
|
||||
lambda: [
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=False),
|
||||
],
|
||||
None,
|
||||
[
|
||||
self.delete_all_button,
|
||||
self.delete_all_button_confirm,
|
||||
self.delete_all_button_cancel,
|
||||
],
|
||||
)
|
||||
|
||||
self.delete_all_button_confirm.click(
|
||||
fn=self.delete_all_files,
|
||||
inputs=[self.file_list],
|
||||
outputs=[],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
).then(
|
||||
lambda: [
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=False),
|
||||
],
|
||||
None,
|
||||
[
|
||||
self.delete_all_button,
|
||||
self.delete_all_button_confirm,
|
||||
self.delete_all_button_cancel,
|
||||
],
|
||||
)
|
||||
|
||||
# self.download_single_button.click(
|
||||
# fn=self.download_single_file,
|
||||
# inputs=[self.is_zipped_state, self.selected_file_id],
|
||||
# outputs=[self.is_zipped_state, self.download_single_button],
|
||||
# show_progress="hidden",
|
||||
# )
|
||||
self.download_single_button.click(
|
||||
fn=self.download_single_file_simple,
|
||||
inputs=[self.is_zipped_state, self.chunks, self.selected_file_id],
|
||||
outputs=[self.is_zipped_state, self.download_single_button],
|
||||
show_progress="hidden",
|
||||
)
|
||||
|
||||
onUploaded = (
|
||||
self.upload_button.click(
|
||||
fn=lambda: gr.update(visible=True),
|
||||
outputs=[self.upload_progress_panel],
|
||||
)
|
||||
.then(
|
||||
fn=self.index_fn,
|
||||
inputs=[
|
||||
self.files,
|
||||
self.urls,
|
||||
self.reindex,
|
||||
self._app.settings_state,
|
||||
self._app.user_id,
|
||||
],
|
||||
outputs=[self.upload_result, self.upload_info],
|
||||
concurrency_limit=20,
|
||||
)
|
||||
.then(
|
||||
fn=lambda: gr.update(value=""),
|
||||
outputs=[self.urls],
|
||||
)
|
||||
)
|
||||
|
||||
def on_register_quick_uploads(self):
|
||||
try:
|
||||
# quick file upload event registration of first Index only
|
||||
if self._index.id == 1:
|
||||
@@ -807,38 +663,48 @@ class FileIndexPage(BasePage):
|
||||
for event in self._app.get_event(f"onFileIndex{self._index.id}Changed"):
|
||||
quickURLUploadedEvent = quickURLUploadedEvent.then(**event)
|
||||
|
||||
quickUploadedEvent.success(
|
||||
quickUploadedEvent = quickUploadedEvent.success(
|
||||
fn=lambda x: x,
|
||||
inputs=self.quick_upload_state,
|
||||
outputs=self._app.chat_page._indices_input[1],
|
||||
).then(
|
||||
fn=lambda: gr.update(value="Indexing completed."),
|
||||
outputs=self._app.chat_page.quick_file_upload_status,
|
||||
).then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
concurrency_limit=20,
|
||||
).then(
|
||||
)
|
||||
|
||||
if not KH_DEMO_MODE:
|
||||
quickUploadedEvent = quickUploadedEvent.then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
concurrency_limit=20,
|
||||
)
|
||||
|
||||
quickUploadedEvent = quickUploadedEvent.then(
|
||||
fn=lambda: True,
|
||||
inputs=None,
|
||||
outputs=None,
|
||||
js=chat_input_focus_js_with_submit,
|
||||
)
|
||||
|
||||
quickURLUploadedEvent.success(
|
||||
quickURLUploadedEvent = quickURLUploadedEvent.success(
|
||||
fn=lambda x: x,
|
||||
inputs=self.quick_upload_state,
|
||||
outputs=self._app.chat_page._indices_input[1],
|
||||
).then(
|
||||
fn=lambda: gr.update(value="Indexing completed."),
|
||||
outputs=self._app.chat_page.quick_file_upload_status,
|
||||
).then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
concurrency_limit=20,
|
||||
).then(
|
||||
)
|
||||
|
||||
if not KH_DEMO_MODE:
|
||||
quickURLUploadedEvent = quickURLUploadedEvent.then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
concurrency_limit=20,
|
||||
)
|
||||
|
||||
quickURLUploadedEvent = quickURLUploadedEvent.then(
|
||||
fn=lambda: True,
|
||||
inputs=None,
|
||||
outputs=None,
|
||||
@@ -848,6 +714,166 @@ class FileIndexPage(BasePage):
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def on_register_events(self):
|
||||
"""Register all events to the app"""
|
||||
self.on_register_quick_uploads()
|
||||
|
||||
if KH_DEMO_MODE:
|
||||
return
|
||||
|
||||
onDeleted = (
|
||||
self.delete_button.click(
|
||||
fn=self.delete_event,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=None,
|
||||
)
|
||||
.then(
|
||||
fn=lambda: (None, self.selected_panel_false),
|
||||
inputs=[],
|
||||
outputs=[self.selected_file_id, self.selected_panel],
|
||||
show_progress="hidden",
|
||||
)
|
||||
.then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
)
|
||||
.then(
|
||||
fn=self.file_selected,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=[
|
||||
self.chunks,
|
||||
self.deselect_button,
|
||||
self.delete_button,
|
||||
self.download_single_button,
|
||||
self.chat_button,
|
||||
],
|
||||
show_progress="hidden",
|
||||
)
|
||||
)
|
||||
for event in self._app.get_event(f"onFileIndex{self._index.id}Changed"):
|
||||
onDeleted = onDeleted.then(**event)
|
||||
|
||||
self.deselect_button.click(
|
||||
fn=lambda: (None, self.selected_panel_false),
|
||||
inputs=[],
|
||||
outputs=[self.selected_file_id, self.selected_panel],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self.file_selected,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=[
|
||||
self.chunks,
|
||||
self.deselect_button,
|
||||
self.delete_button,
|
||||
self.download_single_button,
|
||||
self.chat_button,
|
||||
],
|
||||
show_progress="hidden",
|
||||
)
|
||||
|
||||
self.chat_button.click(
|
||||
fn=self.set_file_id_selector,
|
||||
inputs=[self.selected_file_id],
|
||||
outputs=[
|
||||
self._index.get_selector_component_ui().selector,
|
||||
self._index.get_selector_component_ui().mode,
|
||||
self._app.tabs,
|
||||
],
|
||||
)
|
||||
|
||||
if not KH_SSO_ENABLED:
|
||||
self.download_all_button.click(
|
||||
fn=self.download_all_files,
|
||||
inputs=[],
|
||||
outputs=self.download_all_button,
|
||||
show_progress="hidden",
|
||||
)
|
||||
|
||||
self.delete_all_button.click(
|
||||
self.show_delete_all_confirm,
|
||||
[self.file_list],
|
||||
[
|
||||
self.delete_all_button,
|
||||
self.delete_all_button_confirm,
|
||||
self.delete_all_button_cancel,
|
||||
],
|
||||
)
|
||||
self.delete_all_button_cancel.click(
|
||||
lambda: [
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=False),
|
||||
],
|
||||
None,
|
||||
[
|
||||
self.delete_all_button,
|
||||
self.delete_all_button_confirm,
|
||||
self.delete_all_button_cancel,
|
||||
],
|
||||
)
|
||||
|
||||
self.delete_all_button_confirm.click(
|
||||
fn=self.delete_all_files,
|
||||
inputs=[self.file_list],
|
||||
outputs=[],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
outputs=[self.file_list_state, self.file_list],
|
||||
).then(
|
||||
lambda: [
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=False),
|
||||
],
|
||||
None,
|
||||
[
|
||||
self.delete_all_button,
|
||||
self.delete_all_button_confirm,
|
||||
self.delete_all_button_cancel,
|
||||
],
|
||||
)
|
||||
|
||||
if not KH_SSO_ENABLED:
|
||||
self.download_single_button.click(
|
||||
fn=self.download_single_file,
|
||||
inputs=[self.is_zipped_state, self.selected_file_id],
|
||||
outputs=[self.is_zipped_state, self.download_single_button],
|
||||
show_progress="hidden",
|
||||
)
|
||||
else:
|
||||
self.download_single_button.click(
|
||||
fn=self.download_single_file_simple,
|
||||
inputs=[self.is_zipped_state, self.chunks, self.selected_file_id],
|
||||
outputs=[self.is_zipped_state, self.download_single_button],
|
||||
show_progress="hidden",
|
||||
)
|
||||
|
||||
onUploaded = (
|
||||
self.upload_button.click(
|
||||
fn=lambda: gr.update(visible=True),
|
||||
outputs=[self.upload_progress_panel],
|
||||
)
|
||||
.then(
|
||||
fn=self.index_fn,
|
||||
inputs=[
|
||||
self.files,
|
||||
self.urls,
|
||||
self.reindex,
|
||||
self._app.settings_state,
|
||||
self._app.user_id,
|
||||
],
|
||||
outputs=[self.upload_result, self.upload_info],
|
||||
concurrency_limit=20,
|
||||
)
|
||||
.then(
|
||||
fn=lambda: gr.update(value=""),
|
||||
outputs=[self.urls],
|
||||
)
|
||||
)
|
||||
|
||||
uploadedEvent = onUploaded.then(
|
||||
fn=self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
@@ -1003,6 +1029,9 @@ class FileIndexPage(BasePage):
|
||||
|
||||
def _on_app_created(self):
|
||||
"""Called when the app is created"""
|
||||
if KH_DEMO_MODE:
|
||||
return
|
||||
|
||||
self._app.app.load(
|
||||
self.list_file,
|
||||
inputs=[self._app.user_id, self.filter],
|
||||
|
||||
@@ -20,7 +20,7 @@ if config("KH_FIRST_SETUP", default=False, cast=bool):
|
||||
|
||||
def toggle_first_setup_visibility():
|
||||
global KH_APP_DATA_EXISTS
|
||||
is_first_setup = KH_DEMO_MODE or not KH_APP_DATA_EXISTS
|
||||
is_first_setup = not KH_DEMO_MODE and not KH_APP_DATA_EXISTS
|
||||
KH_APP_DATA_EXISTS = True
|
||||
return gr.update(visible=is_first_setup), gr.update(visible=not is_first_setup)
|
||||
|
||||
@@ -71,7 +71,7 @@ class App(BaseApp):
|
||||
"indices-tab",
|
||||
],
|
||||
id="indices-tab",
|
||||
visible=not self.f_user_management,
|
||||
visible=not self.f_user_management and not KH_DEMO_MODE,
|
||||
) as self._tabs[f"{index.id}-tab"]:
|
||||
page = index.get_index_page_ui()
|
||||
setattr(self, f"_index_{index.id}", page)
|
||||
@@ -81,7 +81,7 @@ class App(BaseApp):
|
||||
elem_id="indices-tab",
|
||||
elem_classes=["fill-main-area-height", "scrollable", "indices-tab"],
|
||||
id="indices-tab",
|
||||
visible=not self.f_user_management,
|
||||
visible=not self.f_user_management and not KH_DEMO_MODE,
|
||||
) as self._tabs["indices-tab"]:
|
||||
for index in self.index_manager.indices:
|
||||
with gr.Tab(
|
||||
@@ -91,24 +91,25 @@ class App(BaseApp):
|
||||
page = index.get_index_page_ui()
|
||||
setattr(self, f"_index_{index.id}", page)
|
||||
|
||||
if not KH_SSO_ENABLED:
|
||||
if not KH_DEMO_MODE:
|
||||
if not KH_SSO_ENABLED:
|
||||
with gr.Tab(
|
||||
"Resources",
|
||||
elem_id="resources-tab",
|
||||
id="resources-tab",
|
||||
visible=not self.f_user_management,
|
||||
elem_classes=["fill-main-area-height", "scrollable"],
|
||||
) as self._tabs["resources-tab"]:
|
||||
self.resources_page = ResourcesTab(self)
|
||||
|
||||
with gr.Tab(
|
||||
"Resources",
|
||||
elem_id="resources-tab",
|
||||
id="resources-tab",
|
||||
"Settings",
|
||||
elem_id="settings-tab",
|
||||
id="settings-tab",
|
||||
visible=not self.f_user_management,
|
||||
elem_classes=["fill-main-area-height", "scrollable"],
|
||||
) as self._tabs["resources-tab"]:
|
||||
self.resources_page = ResourcesTab(self)
|
||||
|
||||
with gr.Tab(
|
||||
"Settings",
|
||||
elem_id="settings-tab",
|
||||
id="settings-tab",
|
||||
visible=not self.f_user_management,
|
||||
elem_classes=["fill-main-area-height", "scrollable"],
|
||||
) as self._tabs["settings-tab"]:
|
||||
self.settings_page = SettingsPage(self)
|
||||
) as self._tabs["settings-tab"]:
|
||||
self.settings_page = SettingsPage(self)
|
||||
|
||||
with gr.Tab(
|
||||
"Help",
|
||||
|
||||
@@ -32,6 +32,7 @@ from .common import STATE
|
||||
from .control import ConversationControl
|
||||
from .report import ReportIssue
|
||||
|
||||
KH_DEMO_MODE = getattr(flowsettings, "KH_DEMO_MODE", False)
|
||||
KH_WEB_SEARCH_BACKEND = getattr(flowsettings, "KH_WEB_SEARCH_BACKEND", None)
|
||||
WebSearch = None
|
||||
if KH_WEB_SEARCH_BACKEND:
|
||||
@@ -409,6 +410,9 @@ class ChatPage(BasePage):
|
||||
self.plot_panel = gr.Plot(visible=False)
|
||||
self.info_panel = gr.HTML(elem_id="html-info-panel")
|
||||
|
||||
self.followup_questions = self.chat_suggestion.examples
|
||||
self.followup_questions_ui = self.chat_suggestion.accordion
|
||||
|
||||
def _json_to_plot(self, json_dict: dict | None):
|
||||
if json_dict:
|
||||
plot = from_json(json_dict)
|
||||
@@ -418,9 +422,6 @@ class ChatPage(BasePage):
|
||||
return plot
|
||||
|
||||
def on_register_events(self):
|
||||
self.followup_questions = self.chat_suggestion.examples
|
||||
self.followup_questions_ui = self.chat_suggestion.accordion
|
||||
|
||||
chat_event = (
|
||||
gr.on(
|
||||
triggers=[
|
||||
@@ -535,25 +536,26 @@ class ChatPage(BasePage):
|
||||
# )
|
||||
|
||||
# final data persist
|
||||
chat_event = chat_event.then(
|
||||
fn=self.persist_data_source,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self._app.user_id,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_panel.chatbot,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
outputs=[
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
],
|
||||
concurrency_limit=20,
|
||||
)
|
||||
if not KH_DEMO_MODE:
|
||||
chat_event = chat_event.then(
|
||||
fn=self.persist_data_source,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self._app.user_id,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_panel.chatbot,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
outputs=[
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
],
|
||||
concurrency_limit=20,
|
||||
)
|
||||
|
||||
self.chat_control.btn_info_expand.click(
|
||||
fn=lambda is_expanded: (
|
||||
@@ -572,100 +574,136 @@ class ChatPage(BasePage):
|
||||
inputs=[self.chat_control.conversation_id],
|
||||
outputs=None,
|
||||
)
|
||||
self.chat_control.btn_new.click(
|
||||
self.chat_control.new_conv,
|
||||
inputs=self._app.user_id,
|
||||
outputs=[self.chat_control.conversation_id, self.chat_control.conversation],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
self.chat_control.select_conv,
|
||||
inputs=[self.chat_control.conversation, self._app.user_id],
|
||||
outputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
self.chat_panel.chatbot,
|
||||
self.followup_questions,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_control.cb_is_public,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self._json_to_plot,
|
||||
inputs=self.state_plot_panel,
|
||||
outputs=self.plot_panel,
|
||||
).then(
|
||||
fn=None,
|
||||
inputs=None,
|
||||
js=chat_input_focus_js,
|
||||
)
|
||||
|
||||
self.chat_control.btn_del.click(
|
||||
lambda id: self.toggle_delete(id),
|
||||
inputs=[self.chat_control.conversation_id],
|
||||
outputs=[self.chat_control._new_delete, self.chat_control._delete_confirm],
|
||||
)
|
||||
self.chat_control.btn_del_conf.click(
|
||||
self.chat_control.delete_conv,
|
||||
inputs=[self.chat_control.conversation_id, self._app.user_id],
|
||||
outputs=[self.chat_control.conversation_id, self.chat_control.conversation],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
self.chat_control.select_conv,
|
||||
inputs=[self.chat_control.conversation, self._app.user_id],
|
||||
outputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
self.chat_panel.chatbot,
|
||||
self.followup_questions,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_control.cb_is_public,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self._json_to_plot,
|
||||
inputs=self.state_plot_panel,
|
||||
outputs=self.plot_panel,
|
||||
).then(
|
||||
lambda: self.toggle_delete(""),
|
||||
outputs=[self.chat_control._new_delete, self.chat_control._delete_confirm],
|
||||
)
|
||||
self.chat_control.btn_del_cnl.click(
|
||||
lambda: self.toggle_delete(""),
|
||||
outputs=[self.chat_control._new_delete, self.chat_control._delete_confirm],
|
||||
)
|
||||
self.chat_control.btn_conversation_rn.click(
|
||||
lambda: gr.update(visible=True),
|
||||
outputs=[
|
||||
self.chat_control.conversation_rn,
|
||||
],
|
||||
)
|
||||
self.chat_control.conversation_rn.submit(
|
||||
self.chat_control.rename_conv,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation_rn,
|
||||
gr.State(value=True),
|
||||
self._app.user_id,
|
||||
],
|
||||
outputs=[
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
],
|
||||
show_progress="hidden",
|
||||
)
|
||||
if KH_DEMO_MODE:
|
||||
self.chat_control.btn_new.click(
|
||||
fn=lambda: self.chat_control.select_conv("", None),
|
||||
outputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
self.chat_panel.chatbot,
|
||||
self.followup_questions,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_control.cb_is_public,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
)
|
||||
|
||||
if not KH_DEMO_MODE:
|
||||
self.chat_control.btn_new.click(
|
||||
self.chat_control.new_conv,
|
||||
inputs=self._app.user_id,
|
||||
outputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation,
|
||||
],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
self.chat_control.select_conv,
|
||||
inputs=[self.chat_control.conversation, self._app.user_id],
|
||||
outputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
self.chat_panel.chatbot,
|
||||
self.followup_questions,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_control.cb_is_public,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self._json_to_plot,
|
||||
inputs=self.state_plot_panel,
|
||||
outputs=self.plot_panel,
|
||||
).then(
|
||||
fn=None,
|
||||
inputs=None,
|
||||
js=chat_input_focus_js,
|
||||
)
|
||||
|
||||
self.chat_control.btn_del.click(
|
||||
lambda id: self.toggle_delete(id),
|
||||
inputs=[self.chat_control.conversation_id],
|
||||
outputs=[
|
||||
self.chat_control._new_delete,
|
||||
self.chat_control._delete_confirm,
|
||||
],
|
||||
)
|
||||
self.chat_control.btn_del_conf.click(
|
||||
self.chat_control.delete_conv,
|
||||
inputs=[self.chat_control.conversation_id, self._app.user_id],
|
||||
outputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation,
|
||||
],
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
self.chat_control.select_conv,
|
||||
inputs=[self.chat_control.conversation, self._app.user_id],
|
||||
outputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
self.chat_panel.chatbot,
|
||||
self.followup_questions,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_control.cb_is_public,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
show_progress="hidden",
|
||||
).then(
|
||||
fn=self._json_to_plot,
|
||||
inputs=self.state_plot_panel,
|
||||
outputs=self.plot_panel,
|
||||
).then(
|
||||
lambda: self.toggle_delete(""),
|
||||
outputs=[
|
||||
self.chat_control._new_delete,
|
||||
self.chat_control._delete_confirm,
|
||||
],
|
||||
)
|
||||
self.chat_control.btn_del_cnl.click(
|
||||
lambda: self.toggle_delete(""),
|
||||
outputs=[
|
||||
self.chat_control._new_delete,
|
||||
self.chat_control._delete_confirm,
|
||||
],
|
||||
)
|
||||
self.chat_control.btn_conversation_rn.click(
|
||||
lambda: gr.update(visible=True),
|
||||
outputs=[
|
||||
self.chat_control.conversation_rn,
|
||||
],
|
||||
)
|
||||
self.chat_control.conversation_rn.submit(
|
||||
self.chat_control.rename_conv,
|
||||
inputs=[
|
||||
self.chat_control.conversation_id,
|
||||
self.chat_control.conversation_rn,
|
||||
gr.State(value=True),
|
||||
self._app.user_id,
|
||||
],
|
||||
outputs=[
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
],
|
||||
show_progress="hidden",
|
||||
)
|
||||
|
||||
self.chat_control.conversation.select(
|
||||
self.chat_control.select_conv,
|
||||
@@ -722,9 +760,6 @@ class ChatPage(BasePage):
|
||||
outputs=[self._preview_links],
|
||||
js=pdfview_js,
|
||||
)
|
||||
# .then(
|
||||
# fn=None, inputs=None, outputs=None, js=chat_input_focus_js
|
||||
# )
|
||||
|
||||
self.chat_control.cb_is_public.change(
|
||||
self.on_set_public_conversation,
|
||||
@@ -870,13 +905,16 @@ class ChatPage(BasePage):
|
||||
raise gr.Error("Empty chat")
|
||||
|
||||
if not conv_id:
|
||||
id_, update = self.chat_control.new_conv(user_id)
|
||||
with Session(engine) as session:
|
||||
statement = select(Conversation).where(Conversation.id == id_)
|
||||
name = session.exec(statement).one().name
|
||||
new_conv_id = id_
|
||||
conv_update = update
|
||||
new_conv_name = name
|
||||
if not KH_DEMO_MODE:
|
||||
id_, update = self.chat_control.new_conv(user_id)
|
||||
with Session(engine) as session:
|
||||
statement = select(Conversation).where(Conversation.id == id_)
|
||||
name = session.exec(statement).one().name
|
||||
new_conv_id = id_
|
||||
conv_update = update
|
||||
new_conv_name = name
|
||||
else:
|
||||
new_conv_id, new_conv_name, conv_update = None, None, gr.update()
|
||||
else:
|
||||
new_conv_id = conv_id
|
||||
conv_update = gr.update()
|
||||
@@ -943,11 +981,13 @@ class ChatPage(BasePage):
|
||||
self.chat_control.conversation,
|
||||
self.chat_control.conversation_rn,
|
||||
self.chat_panel.chatbot,
|
||||
self.followup_questions,
|
||||
self.info_panel,
|
||||
self.state_plot_panel,
|
||||
self.state_retrieval_history,
|
||||
self.state_plot_history,
|
||||
self.chat_control.cb_is_public,
|
||||
self.state_chat,
|
||||
]
|
||||
+ self._indices_input,
|
||||
"show_progress": "hidden",
|
||||
|
||||
@@ -14,6 +14,8 @@ from .chat_suggestion import ChatSuggestion
|
||||
from .common import STATE
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
KH_DEMO_MODE = getattr(flowsettings, "KH_DEMO_MODE", False)
|
||||
ASSETS_DIR = "assets/icons"
|
||||
if not os.path.isdir(ASSETS_DIR):
|
||||
ASSETS_DIR = "libs/ktem/ktem/assets/icons"
|
||||
@@ -86,6 +88,14 @@ class ConversationControl(BasePage):
|
||||
)
|
||||
|
||||
with gr.Row() as self._new_delete:
|
||||
self.cb_suggest_chat = gr.Checkbox(
|
||||
value=False,
|
||||
label="Suggest chat",
|
||||
min_width=10,
|
||||
scale=6,
|
||||
elem_id="suggest-chat-checkbox",
|
||||
container=False,
|
||||
)
|
||||
self.cb_is_public = gr.Checkbox(
|
||||
value=False,
|
||||
label="Shared",
|
||||
@@ -95,39 +105,44 @@ class ConversationControl(BasePage):
|
||||
container=False,
|
||||
visible=False,
|
||||
)
|
||||
self.cb_suggest_chat = gr.Checkbox(
|
||||
value=False,
|
||||
label="Suggest chat",
|
||||
min_width=10,
|
||||
scale=6,
|
||||
elem_id="suggest-chat-checkbox",
|
||||
container=False,
|
||||
)
|
||||
self.btn_conversation_rn = gr.Button(
|
||||
value="",
|
||||
icon=f"{ASSETS_DIR}/rename.svg",
|
||||
min_width=2,
|
||||
scale=1,
|
||||
size="sm",
|
||||
elem_classes=["no-background", "body-text-color"],
|
||||
)
|
||||
self.btn_del = gr.Button(
|
||||
value="",
|
||||
icon=f"{ASSETS_DIR}/delete.svg",
|
||||
min_width=2,
|
||||
scale=1,
|
||||
size="sm",
|
||||
elem_classes=["no-background", "body-text-color"],
|
||||
)
|
||||
self.btn_new = gr.Button(
|
||||
value="",
|
||||
icon=f"{ASSETS_DIR}/new.svg",
|
||||
min_width=2,
|
||||
scale=1,
|
||||
size="sm",
|
||||
elem_classes=["no-background", "body-text-color"],
|
||||
elem_id="new-conv-button",
|
||||
)
|
||||
|
||||
if not KH_DEMO_MODE:
|
||||
self.btn_conversation_rn = gr.Button(
|
||||
value="",
|
||||
icon=f"{ASSETS_DIR}/rename.svg",
|
||||
min_width=2,
|
||||
scale=1,
|
||||
size="sm",
|
||||
elem_classes=["no-background", "body-text-color"],
|
||||
)
|
||||
self.btn_del = gr.Button(
|
||||
value="",
|
||||
icon=f"{ASSETS_DIR}/delete.svg",
|
||||
min_width=2,
|
||||
scale=1,
|
||||
size="sm",
|
||||
elem_classes=["no-background", "body-text-color"],
|
||||
)
|
||||
|
||||
if not KH_DEMO_MODE:
|
||||
self.btn_new = gr.Button(
|
||||
value="",
|
||||
icon=f"{ASSETS_DIR}/new.svg",
|
||||
min_width=2,
|
||||
scale=1,
|
||||
size="sm",
|
||||
elem_classes=["no-background", "body-text-color"],
|
||||
elem_id="new-conv-button",
|
||||
)
|
||||
else:
|
||||
self.btn_new = gr.Button(
|
||||
value="New chat",
|
||||
min_width=120,
|
||||
size="sm",
|
||||
scale=1,
|
||||
variant="primary",
|
||||
elem_id="new-conv-button",
|
||||
)
|
||||
|
||||
with gr.Row(visible=False) as self._delete_confirm:
|
||||
self.btn_del_conf = gr.Button(
|
||||
@@ -328,7 +343,7 @@ class ConversationControl(BasePage):
|
||||
|
||||
def rename_conv(self, conversation_id, new_name, is_renamed, user_id):
|
||||
"""Rename the conversation"""
|
||||
if not is_renamed:
|
||||
if not is_renamed or KH_DEMO_MODE:
|
||||
return (
|
||||
gr.update(),
|
||||
conversation_id,
|
||||
|
||||
@@ -87,9 +87,12 @@ class LoginPage(BasePage):
|
||||
)
|
||||
|
||||
def login(self, usn, pwd, request: gr.Request):
|
||||
import gradiologin as grlogin
|
||||
try:
|
||||
import gradiologin as grlogin
|
||||
|
||||
user = grlogin.get_user(request)
|
||||
user = grlogin.get_user(request)
|
||||
except (ImportError, AssertionError):
|
||||
user = None
|
||||
|
||||
if user:
|
||||
user_id = user["sub"]
|
||||
|
||||
@@ -8,7 +8,6 @@ from ktem.llms.manager import llms
|
||||
from ktem.rerankings.manager import reranking_models_manager as rerankers
|
||||
from theflow.settings import settings as flowsettings
|
||||
|
||||
KH_DEMO_MODE = getattr(flowsettings, "KH_DEMO_MODE", False)
|
||||
KH_OLLAMA_URL = getattr(flowsettings, "KH_OLLAMA_URL", "http://localhost:11434/v1/")
|
||||
DEFAULT_OLLAMA_URL = KH_OLLAMA_URL.replace("v1", "api")
|
||||
if DEFAULT_OLLAMA_URL.endswith("/"):
|
||||
@@ -144,17 +143,16 @@ class SetupPage(BasePage):
|
||||
outputs=[self.setup_log],
|
||||
show_progress="hidden",
|
||||
)
|
||||
if not KH_DEMO_MODE:
|
||||
onSkipSetup = gr.on(
|
||||
triggers=[self.btn_skip.click],
|
||||
fn=lambda: None,
|
||||
inputs=[],
|
||||
show_progress="hidden",
|
||||
outputs=[self.radio_model],
|
||||
)
|
||||
onSkipSetup = gr.on(
|
||||
triggers=[self.btn_skip.click],
|
||||
fn=lambda: None,
|
||||
inputs=[],
|
||||
show_progress="hidden",
|
||||
outputs=[self.radio_model],
|
||||
)
|
||||
|
||||
for event in self._app.get_event("onFirstSetupComplete"):
|
||||
onSkipSetup = onSkipSetup.success(**event)
|
||||
for event in self._app.get_event("onFirstSetupComplete"):
|
||||
onSkipSetup = onSkipSetup.success(**event)
|
||||
|
||||
onFirstSetupComplete = onFirstSetupComplete.success(
|
||||
fn=self.update_default_settings,
|
||||
@@ -183,10 +181,6 @@ class SetupPage(BasePage):
|
||||
google_api_key,
|
||||
radio_model_value,
|
||||
):
|
||||
# skip if KH_DEMO_MODE
|
||||
if KH_DEMO_MODE:
|
||||
raise gr.Error(DEMO_MESSAGE)
|
||||
|
||||
log_content = ""
|
||||
if not radio_model_value:
|
||||
gr.Info("Skip setup models.")
|
||||
|
||||
Reference in New Issue
Block a user