Spaces:
Sleeping
Sleeping
Tuchuanhuhuhu
commited on
Commit
·
30e4bc9
1
Parent(s):
4f6b126
feat: 加入 Markdown 下载功能
Browse files- ChuanhuChatbot.py +5 -0
- modules/utils.py +18 -15
- web_assets/javascript/utils.js +11 -1
ChuanhuChatbot.py
CHANGED
|
@@ -82,6 +82,9 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 82 |
with gr.Column(min_width=42, scale=1):
|
| 83 |
historyDownloadBtn = gr.Button(
|
| 84 |
i18n("⏬"), elem_id="gr-history-download-btn")
|
|
|
|
|
|
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column(scale=6):
|
| 87 |
saveFileName = gr.Textbox(
|
|
@@ -665,6 +668,8 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
| 665 |
saveFileName, systemPromptTxt, chatbot])
|
| 666 |
historyDownloadBtn.click(None, [
|
| 667 |
user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b);}')
|
|
|
|
|
|
|
| 668 |
historySearchTextbox.input(
|
| 669 |
filter_history,
|
| 670 |
[user_name, historySearchTextbox],
|
|
|
|
| 82 |
with gr.Column(min_width=42, scale=1):
|
| 83 |
historyDownloadBtn = gr.Button(
|
| 84 |
i18n("⏬"), elem_id="gr-history-download-btn")
|
| 85 |
+
with gr.Column(min_width=42, scale=1):
|
| 86 |
+
historyMarkdownDownloadBtn = gr.Button(
|
| 87 |
+
i18n("⤵️"), elem_id="gr-history-mardown-download-btn")
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column(scale=6):
|
| 90 |
saveFileName = gr.Textbox(
|
|
|
|
| 668 |
saveFileName, systemPromptTxt, chatbot])
|
| 669 |
historyDownloadBtn.click(None, [
|
| 670 |
user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b);}')
|
| 671 |
+
historyMarkdownDownloadBtn.click(None, [
|
| 672 |
+
user_name, historySelectList], None, _js='(a,b)=>{return downloadHistoryMarkdown(a,b);}')
|
| 673 |
historySearchTextbox.input(
|
| 674 |
filter_history,
|
| 675 |
[user_name, historySearchTextbox],
|
modules/utils.py
CHANGED
|
@@ -332,23 +332,26 @@ def construct_assistant(text):
|
|
| 332 |
|
| 333 |
def save_file(filename, system, history, chatbot, user_name):
|
| 334 |
os.makedirs(os.path.join(HISTORY_DIR, user_name), exist_ok=True)
|
|
|
|
|
|
|
| 335 |
if not filename.endswith(".json") and not filename.endswith(".md"):
|
| 336 |
filename += ".json"
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
|
|
|
| 352 |
return os.path.join(HISTORY_DIR, user_name, filename)
|
| 353 |
|
| 354 |
|
|
|
|
| 332 |
|
| 333 |
def save_file(filename, system, history, chatbot, user_name):
|
| 334 |
os.makedirs(os.path.join(HISTORY_DIR, user_name), exist_ok=True)
|
| 335 |
+
if filename.endswith(".md"):
|
| 336 |
+
filename = filename[:-3]
|
| 337 |
if not filename.endswith(".json") and not filename.endswith(".md"):
|
| 338 |
filename += ".json"
|
| 339 |
+
|
| 340 |
+
json_s = {"system": system, "history": history, "chatbot": chatbot}
|
| 341 |
+
if "/" in filename or "\\" in filename:
|
| 342 |
+
history_file_path = filename
|
| 343 |
+
else:
|
| 344 |
+
history_file_path = os.path.join(HISTORY_DIR, user_name, filename)
|
| 345 |
+
with open(history_file_path, "w", encoding='utf-8') as f:
|
| 346 |
+
json.dump(json_s, f, ensure_ascii=False)
|
| 347 |
+
|
| 348 |
+
filename = filename.split("/")[-1]
|
| 349 |
+
filename_md = filename[:-5] + ".md"
|
| 350 |
+
md_s = f"system: \n- {system} \n"
|
| 351 |
+
for data in history:
|
| 352 |
+
md_s += f"\n{data['role']}: \n- {data['content']} \n"
|
| 353 |
+
with open(os.path.join(HISTORY_DIR, user_name, filename_md), "w", encoding="utf8") as f:
|
| 354 |
+
f.write(md_s)
|
| 355 |
return os.path.join(HISTORY_DIR, user_name, filename)
|
| 356 |
|
| 357 |
|
web_assets/javascript/utils.js
CHANGED
|
@@ -22,7 +22,17 @@ function downloadHistory(username, historyname) {
|
|
| 22 |
} else {
|
| 23 |
fileUrl = `/file=./history/${username}/${historyname}`;
|
| 24 |
}
|
| 25 |
-
downloadFile(fileUrl, historyname);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
function downloadFile(fileUrl, filename="") {
|
|
|
|
| 22 |
} else {
|
| 23 |
fileUrl = `/file=./history/${username}/${historyname}`;
|
| 24 |
}
|
| 25 |
+
downloadFile(fileUrl + ".json", historyname + ".json");
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
function downloadHistoryMarkdown(username, historyname) {
|
| 29 |
+
let fileUrl;
|
| 30 |
+
if (username === null) {
|
| 31 |
+
fileUrl = `/file=./history/${historyname}`;
|
| 32 |
+
} else {
|
| 33 |
+
fileUrl = `/file=./history/${username}/${historyname}`;
|
| 34 |
+
}
|
| 35 |
+
downloadFile(fileUrl + ".md", historyname + ".md");
|
| 36 |
}
|
| 37 |
|
| 38 |
function downloadFile(fileUrl, filename="") {
|