Spaces:
Sleeping
Sleeping
Tuchuanhuhuhu
commited on
Commit
·
8154a1c
1
Parent(s):
d2092ab
feat: 给历史记录列表加入搜索框
Browse files- ChuanhuChatbot.py +50 -45
- modules/utils.py +5 -0
ChuanhuChatbot.py
CHANGED
|
@@ -57,42 +57,42 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 57 |
|
| 58 |
with gr.Column(elem_id="menu-area"):
|
| 59 |
with gr.Row(elem_id="chuanhu-history"):
|
| 60 |
-
with gr.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
with gr.Row():
|
| 73 |
-
with gr.Column(min_width=42, scale=1):
|
| 74 |
-
historyRefreshBtn = gr.Button(i18n("🔄"))
|
| 75 |
-
with gr.Column(min_width=42, scale=1):
|
| 76 |
-
historyDeleteBtn = gr.Button(i18n("🗑️"), elem_id="gr-history-delete-btn")
|
| 77 |
-
with gr.Column(min_width=42, scale=1):
|
| 78 |
-
historyDownloadBtn = gr.Button(i18n("⏬"), elem_id="gr-history-download-btn")
|
| 79 |
-
with gr.Row():
|
| 80 |
-
with gr.Column(scale=6):
|
| 81 |
-
saveFileName = gr.Textbox(
|
| 82 |
-
show_label=True,
|
| 83 |
-
placeholder=i18n("设置文件名: 默认为.json,可选为.md"),
|
| 84 |
-
label=i18n("设置保存文件名"),
|
| 85 |
-
value=i18n("对话历史记录"),
|
| 86 |
-
elem_classes="no-container"
|
| 87 |
-
# container=False,
|
| 88 |
-
)
|
| 89 |
-
with gr.Column(scale=1):
|
| 90 |
-
saveHistoryBtn = gr.Button(i18n("💾 保存对话"), elem_id="gr-history-save-btn")
|
| 91 |
-
exportMarkdownBtn = gr.Button(i18n("📝 导出为Markdown"))
|
| 92 |
-
gr.Markdown(i18n("默认保存于history文件夹"))
|
| 93 |
with gr.Row():
|
| 94 |
-
with gr.Column():
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
with gr.Column(elem_id="chuanhu-menu-footer"):
|
| 98 |
with gr.Row(elem_id="chuanhu-func-nav"):
|
|
@@ -436,7 +436,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 436 |
system_prompt = gr.update()
|
| 437 |
chatbot = gr.Chatbot.update(label=MODELS[DEFAULT_MODEL])
|
| 438 |
return user_info, user_name, current_model, toggle_like_btn_visibility(DEFAULT_MODEL), system_prompt, chatbot, init_history_list(user_name)
|
| 439 |
-
demo.load(create_greeting, inputs=None, outputs=[user_info, user_name, current_model, like_dislike_area, systemPromptTxt, chatbot,
|
| 440 |
chatgpt_predict_args = dict(
|
| 441 |
fn=predict,
|
| 442 |
inputs=[
|
|
@@ -477,12 +477,12 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 477 |
|
| 478 |
load_history_from_file_args = dict(
|
| 479 |
fn=load_chat_history,
|
| 480 |
-
inputs=[current_model,
|
| 481 |
outputs=[saveFileName, systemPromptTxt, chatbot]
|
| 482 |
)
|
| 483 |
|
| 484 |
refresh_history_args = dict(
|
| 485 |
-
fn=get_history_list, inputs=[user_name], outputs=[
|
| 486 |
)
|
| 487 |
|
| 488 |
|
|
@@ -501,7 +501,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 501 |
emptyBtn.click(
|
| 502 |
reset,
|
| 503 |
inputs=[current_model],
|
| 504 |
-
outputs=[chatbot, status_display,
|
| 505 |
show_progress=True,
|
| 506 |
_js='clearChatbot',
|
| 507 |
)
|
|
@@ -578,11 +578,11 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 578 |
saveHistoryBtn.click(
|
| 579 |
save_chat_history,
|
| 580 |
[current_model, saveFileName, chatbot, user_name],
|
| 581 |
-
[downloadFile,
|
| 582 |
show_progress=True,
|
| 583 |
_js='(a,b,c,d)=>{return saveChatHistory(a,b,c,d);}'
|
| 584 |
)
|
| 585 |
-
saveHistoryBtn.click(get_history_list, [user_name], [
|
| 586 |
exportMarkdownBtn.click(
|
| 587 |
export_markdown,
|
| 588 |
[current_model, saveFileName, chatbot, user_name],
|
|
@@ -590,10 +590,15 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 590 |
show_progress=True,
|
| 591 |
)
|
| 592 |
historyRefreshBtn.click(**refresh_history_args)
|
| 593 |
-
historyDeleteBtn.click(delete_chat_history, [current_model,
|
| 594 |
-
|
| 595 |
downloadFile.change(upload_chat_history, [current_model, downloadFile, user_name], [saveFileName, systemPromptTxt, chatbot])
|
| 596 |
-
historyDownloadBtn.click(None, [user_name,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
|
| 598 |
# Train
|
| 599 |
dataset_selection.upload(handle_dataset_selection, dataset_selection, [dataset_preview_json, upload_to_openai_btn, openai_train_status])
|
|
@@ -657,7 +662,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 657 |
)
|
| 658 |
historySelectBtn.click( # This is an experimental feature... Not actually used.
|
| 659 |
fn=load_chat_history,
|
| 660 |
-
inputs=[current_model,
|
| 661 |
outputs=[saveFileName, systemPromptTxt, chatbot],
|
| 662 |
_js='(a,b)=>{return bgSelectHistory(a,b);}'
|
| 663 |
)
|
|
|
|
| 57 |
|
| 58 |
with gr.Column(elem_id="menu-area"):
|
| 59 |
with gr.Row(elem_id="chuanhu-history"):
|
| 60 |
+
with gr.Column():
|
| 61 |
+
historySearchTextbox = gr.Textbox(show_label=False, placeholder=i18n("搜索..."), lines=1, elem_id="history-search-tb")
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column(scale=6, elem_id="history-select-wrap"):
|
| 64 |
+
historySelectList = gr.Radio(
|
| 65 |
+
label=i18n("从列表中加载对话"),
|
| 66 |
+
choices=get_history_names(),
|
| 67 |
+
value=get_history_names()[0],
|
| 68 |
+
# multiselect=False,
|
| 69 |
+
container=False,
|
| 70 |
+
elem_id="history-select-dropdown"
|
| 71 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Row():
|
| 73 |
+
with gr.Column(min_width=42, scale=1):
|
| 74 |
+
historyRefreshBtn = gr.Button(i18n("🔄"))
|
| 75 |
+
with gr.Column(min_width=42, scale=1):
|
| 76 |
+
historyDeleteBtn = gr.Button(i18n("🗑️"), elem_id="gr-history-delete-btn")
|
| 77 |
+
with gr.Column(min_width=42, scale=1):
|
| 78 |
+
historyDownloadBtn = gr.Button(i18n("⏬"), elem_id="gr-history-download-btn")
|
| 79 |
+
with gr.Row():
|
| 80 |
+
with gr.Column(scale=6):
|
| 81 |
+
saveFileName = gr.Textbox(
|
| 82 |
+
show_label=True,
|
| 83 |
+
placeholder=i18n("设置文件名: 默认为.json,可选为.md"),
|
| 84 |
+
label=i18n("设置保存文件名"),
|
| 85 |
+
value=i18n("对话历史记录"),
|
| 86 |
+
elem_classes="no-container"
|
| 87 |
+
# container=False,
|
| 88 |
+
)
|
| 89 |
+
with gr.Column(scale=1):
|
| 90 |
+
saveHistoryBtn = gr.Button(i18n("💾 保存对话"), elem_id="gr-history-save-btn")
|
| 91 |
+
exportMarkdownBtn = gr.Button(i18n("📝 导出为Markdown"))
|
| 92 |
+
gr.Markdown(i18n("默认保存于history文件夹"))
|
| 93 |
+
with gr.Row():
|
| 94 |
+
with gr.Column():
|
| 95 |
+
downloadFile = gr.File(interactive=True, label=i18n("下载/上传历史记录"))
|
| 96 |
|
| 97 |
with gr.Column(elem_id="chuanhu-menu-footer"):
|
| 98 |
with gr.Row(elem_id="chuanhu-func-nav"):
|
|
|
|
| 436 |
system_prompt = gr.update()
|
| 437 |
chatbot = gr.Chatbot.update(label=MODELS[DEFAULT_MODEL])
|
| 438 |
return user_info, user_name, current_model, toggle_like_btn_visibility(DEFAULT_MODEL), system_prompt, chatbot, init_history_list(user_name)
|
| 439 |
+
demo.load(create_greeting, inputs=None, outputs=[user_info, user_name, current_model, like_dislike_area, systemPromptTxt, chatbot, historySelectList], api_name="load")
|
| 440 |
chatgpt_predict_args = dict(
|
| 441 |
fn=predict,
|
| 442 |
inputs=[
|
|
|
|
| 477 |
|
| 478 |
load_history_from_file_args = dict(
|
| 479 |
fn=load_chat_history,
|
| 480 |
+
inputs=[current_model, historySelectList, user_name],
|
| 481 |
outputs=[saveFileName, systemPromptTxt, chatbot]
|
| 482 |
)
|
| 483 |
|
| 484 |
refresh_history_args = dict(
|
| 485 |
+
fn=get_history_list, inputs=[user_name], outputs=[historySelectList]
|
| 486 |
)
|
| 487 |
|
| 488 |
|
|
|
|
| 501 |
emptyBtn.click(
|
| 502 |
reset,
|
| 503 |
inputs=[current_model],
|
| 504 |
+
outputs=[chatbot, status_display, historySelectList],
|
| 505 |
show_progress=True,
|
| 506 |
_js='clearChatbot',
|
| 507 |
)
|
|
|
|
| 578 |
saveHistoryBtn.click(
|
| 579 |
save_chat_history,
|
| 580 |
[current_model, saveFileName, chatbot, user_name],
|
| 581 |
+
[downloadFile, historySelectList],
|
| 582 |
show_progress=True,
|
| 583 |
_js='(a,b,c,d)=>{return saveChatHistory(a,b,c,d);}'
|
| 584 |
)
|
| 585 |
+
saveHistoryBtn.click(get_history_list, [user_name], [historySelectList])
|
| 586 |
exportMarkdownBtn.click(
|
| 587 |
export_markdown,
|
| 588 |
[current_model, saveFileName, chatbot, user_name],
|
|
|
|
| 590 |
show_progress=True,
|
| 591 |
)
|
| 592 |
historyRefreshBtn.click(**refresh_history_args)
|
| 593 |
+
historyDeleteBtn.click(delete_chat_history, [current_model, historySelectList, user_name], [status_display, historySelectList, chatbot], _js='(a,b,c)=>{return showConfirmationDialog(a, b, c);}')
|
| 594 |
+
historySelectList.input(**load_history_from_file_args)
|
| 595 |
downloadFile.change(upload_chat_history, [current_model, downloadFile, user_name], [saveFileName, systemPromptTxt, chatbot])
|
| 596 |
+
historyDownloadBtn.click(None, [user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b);}')
|
| 597 |
+
historySearchTextbox.input(
|
| 598 |
+
filter_history,
|
| 599 |
+
[user_name, historySearchTextbox],
|
| 600 |
+
[historySelectList]
|
| 601 |
+
)
|
| 602 |
|
| 603 |
# Train
|
| 604 |
dataset_selection.upload(handle_dataset_selection, dataset_selection, [dataset_preview_json, upload_to_openai_btn, openai_train_status])
|
|
|
|
| 662 |
)
|
| 663 |
historySelectBtn.click( # This is an experimental feature... Not actually used.
|
| 664 |
fn=load_chat_history,
|
| 665 |
+
inputs=[current_model, historySelectList],
|
| 666 |
outputs=[saveFileName, systemPromptTxt, chatbot],
|
| 667 |
_js='(a,b)=>{return bgSelectHistory(a,b);}'
|
| 668 |
)
|
modules/utils.py
CHANGED
|
@@ -405,6 +405,11 @@ def init_history_list(user_name=""):
|
|
| 405 |
history_names = get_history_names(user_name)
|
| 406 |
return gr.Radio.update(choices=history_names, value=history_names[0] if history_names else "")
|
| 407 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
def load_template(filename, mode=0):
|
| 409 |
logging.debug(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")
|
| 410 |
lines = []
|
|
|
|
| 405 |
history_names = get_history_names(user_name)
|
| 406 |
return gr.Radio.update(choices=history_names, value=history_names[0] if history_names else "")
|
| 407 |
|
| 408 |
+
def filter_history(user_name, keyword):
|
| 409 |
+
history_names = get_history_names(user_name)
|
| 410 |
+
history_names = [name for name in history_names if re.search(keyword, name)]
|
| 411 |
+
return gr.update(choices=history_names)
|
| 412 |
+
|
| 413 |
def load_template(filename, mode=0):
|
| 414 |
logging.debug(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")
|
| 415 |
lines = []
|