Tuchuanhuhuhu commited on
Commit
30e4bc9
1 Parent(s): 4f6b126

feat: 加入 Markdown 下载功能

Browse files
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
- if filename.endswith(".json"):
338
- json_s = {"system": system, "history": history, "chatbot": chatbot}
339
- if "/" in filename or "\\" in filename:
340
- history_file_path = filename
341
- else:
342
- history_file_path = os.path.join(HISTORY_DIR, user_name, filename)
343
- logging.info(f"保存文件,文件名为{filename},用户为{user_name}, 文件路径为{history_file_path}")
344
- with open(history_file_path, "w", encoding='utf-8') as f:
345
- json.dump(json_s, f, ensure_ascii=False)
346
- elif filename.endswith(".md"):
347
- md_s = f"system: \n- {system} \n"
348
- for data in history:
349
- md_s += f"\n{data['role']}: \n- {data['content']} \n"
350
- with open(os.path.join(HISTORY_DIR, user_name, filename), "w", encoding="utf8") as f:
351
- f.write(md_s)
 
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="") {