Spaces:
Sleeping
Sleeping
Teng Tu
Chuan Hu
commited on
输出Markdown (#160)
Browse files* 增加将历史记录以markdown保存的功能,
* 解决了中文乱码问题,优化了UI显示
* Update ChuanhuChatbot.py
* Update utils.py
---------
Co-authored-by: Chuan Hu <[email protected]>
- ChuanhuChatbot.py +15 -10
- utils.py +27 -7
ChuanhuChatbot.py
CHANGED
|
@@ -99,21 +99,27 @@ with gr.Blocks(css=customCSS) as demo:
|
|
| 99 |
|
| 100 |
with gr.Tab(label="保存/加载"):
|
| 101 |
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
|
|
|
| 102 |
with gr.Column():
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column(scale=6):
|
| 105 |
saveFileName = gr.Textbox(
|
| 106 |
-
show_label=True, placeholder=f"
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
saveHistoryBtn = gr.Button("💾 保存对话")
|
|
|
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column(scale=6):
|
| 111 |
historyFileSelectDropdown = gr.Dropdown(label="从列表中加载对话", choices=get_history_names(plain=True), multiselect=False, value=get_history_names(plain=True)[0])
|
| 112 |
with gr.Column(scale=1):
|
| 113 |
historyRefreshBtn = gr.Button("🔄 刷新")
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
gr.Markdown(description)
|
| 116 |
|
|
|
|
| 117 |
user_input.submit(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
|
| 118 |
user_input.submit(reset_textbox, [], [user_input])
|
| 119 |
|
|
@@ -129,20 +135,19 @@ with gr.Blocks(css=customCSS) as demo:
|
|
| 129 |
|
| 130 |
reduceTokenBtn.click(reduce_token_size, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
|
|
|
|
|
|
| 135 |
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
| 136 |
-
|
| 137 |
historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
| 138 |
-
|
| 139 |
historyFileSelectDropdown.change(load_chat_history, [historyFileSelectDropdown, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
|
|
|
|
| 140 |
|
| 141 |
-
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
| 142 |
-
|
| 143 |
-
templateFileSelectDropdown.change(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
|
| 144 |
-
|
| 145 |
-
templateSelectDropdown.change(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
|
| 146 |
|
| 147 |
logging.info(colorama.Back.GREEN + "\n川虎的温馨提示:访问 http://localhost:7860 查看界面" + colorama.Style.RESET_ALL)
|
| 148 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
|
|
|
| 99 |
|
| 100 |
with gr.Tab(label="保存/加载"):
|
| 101 |
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
| 102 |
+
gr.Markdown("对话历史默认保存在history文件夹中。")
|
| 103 |
with gr.Column():
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column(scale=6):
|
| 106 |
saveFileName = gr.Textbox(
|
| 107 |
+
show_label=True, placeholder=f"设置文件名: 默认为.json,可选为.md", label="设置保存文件名", value="对话历史记录").style(container=True)
|
| 108 |
with gr.Column(scale=1):
|
| 109 |
saveHistoryBtn = gr.Button("💾 保存对话")
|
| 110 |
+
exportMarkdownBtn = gr.Button("📝 导出为Markdown")
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=6):
|
| 113 |
historyFileSelectDropdown = gr.Dropdown(label="从列表中加载对话", choices=get_history_names(plain=True), multiselect=False, value=get_history_names(plain=True)[0])
|
| 114 |
with gr.Column(scale=1):
|
| 115 |
historyRefreshBtn = gr.Button("🔄 刷新")
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column():
|
| 118 |
+
downloadFile = gr.File(interactive=True)
|
| 119 |
|
| 120 |
gr.Markdown(description)
|
| 121 |
|
| 122 |
+
# Chatbot
|
| 123 |
user_input.submit(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
|
| 124 |
user_input.submit(reset_textbox, [], [user_input])
|
| 125 |
|
|
|
|
| 135 |
|
| 136 |
reduceTokenBtn.click(reduce_token_size, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
|
| 137 |
|
| 138 |
+
# Template
|
| 139 |
+
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
| 140 |
+
templateFileSelectDropdown.change(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
|
| 141 |
+
templateSelectDropdown.change(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
|
| 142 |
|
| 143 |
+
# S&L
|
| 144 |
+
saveHistoryBtn.click(save_chat_history, [saveFileName, systemPromptTxt, history, chatbot], downloadFile, show_progress=True)
|
| 145 |
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
| 146 |
+
exportMarkdownBtn.click(export_markdown, [saveFileName, systemPromptTxt, history, chatbot], downloadFile, show_progress=True)
|
| 147 |
historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
|
|
|
| 148 |
historyFileSelectDropdown.change(load_chat_history, [historyFileSelectDropdown, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
|
| 149 |
+
downloadFile.change(load_chat_history, [downloadFile, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot])
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
logging.info(colorama.Back.GREEN + "\n川虎的温馨提示:访问 http://localhost:7860 查看界面" + colorama.Style.RESET_ALL)
|
| 153 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
utils.py
CHANGED
|
@@ -323,22 +323,42 @@ def delete_last_conversation(chatbot, history, previous_token_count):
|
|
| 323 |
return chatbot, history, previous_token_count, construct_token_message(sum(previous_token_count))
|
| 324 |
|
| 325 |
|
| 326 |
-
def
|
| 327 |
logging.info("保存对话历史中……")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
if filename == "":
|
| 329 |
return
|
| 330 |
if not filename.endswith(".json"):
|
| 331 |
filename += ".json"
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
| 338 |
|
| 339 |
|
| 340 |
def load_chat_history(filename, system, history, chatbot):
|
| 341 |
logging.info("加载对话历史中……")
|
|
|
|
|
|
|
| 342 |
try:
|
| 343 |
with open(os.path.join(HISTORY_DIR, filename), "r") as f:
|
| 344 |
json_s = json.load(f)
|
|
|
|
| 323 |
return chatbot, history, previous_token_count, construct_token_message(sum(previous_token_count))
|
| 324 |
|
| 325 |
|
| 326 |
+
def save_file(filename, system, history, chatbot):
|
| 327 |
logging.info("保存对话历史中……")
|
| 328 |
+
os.makedirs(HISTORY_DIR, exist_ok=True)
|
| 329 |
+
if filename.endswith(".json"):
|
| 330 |
+
json_s = {"system": system, "history": history, "chatbot": chatbot}
|
| 331 |
+
print(json_s)
|
| 332 |
+
with open(os.path.join(HISTORY_DIR, filename), "w") as f:
|
| 333 |
+
json.dump(json_s, f)
|
| 334 |
+
elif filename.endswith(".md"):
|
| 335 |
+
md_s = f"system: \n- {system} \n"
|
| 336 |
+
for data in history:
|
| 337 |
+
md_s += f"\n{data['role']}: \n- {data['content']} \n"
|
| 338 |
+
with open(os.path.join(HISTORY_DIR, filename), "w", encoding="utf8") as f:
|
| 339 |
+
f.write(md_s)
|
| 340 |
+
logging.info("保存对话历史完毕")
|
| 341 |
+
return os.path.join(HISTORY_DIR, filename)
|
| 342 |
+
|
| 343 |
+
def save_chat_history(filename, system, history, chatbot):
|
| 344 |
if filename == "":
|
| 345 |
return
|
| 346 |
if not filename.endswith(".json"):
|
| 347 |
filename += ".json"
|
| 348 |
+
return save_file(filename, system, history, chatbot)
|
| 349 |
+
|
| 350 |
+
def export_markdown(filename, system, history, chatbot):
|
| 351 |
+
if filename == "":
|
| 352 |
+
return
|
| 353 |
+
if not filename.endswith(".md"):
|
| 354 |
+
filename += ".md"
|
| 355 |
+
return save_file(filename, system, history, chatbot)
|
| 356 |
|
| 357 |
|
| 358 |
def load_chat_history(filename, system, history, chatbot):
|
| 359 |
logging.info("加载对话历史中……")
|
| 360 |
+
if type(filename) != str:
|
| 361 |
+
filename = filename.name
|
| 362 |
try:
|
| 363 |
with open(os.path.join(HISTORY_DIR, filename), "r") as f:
|
| 364 |
json_s = json.load(f)
|