Tuchuanhuhuhu commited on
Commit
39dd701
1 Parent(s): b9efa15

bugfix: 修复继续对话时会创建新文件的问题

Browse files
ChuanhuChatbot.py CHANGED
@@ -35,7 +35,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
35
  user_question = gr.State("")
36
  assert type(my_api_key)==str
37
  user_api_key = gr.State(my_api_key)
38
- current_model = gr.State(create_new_model)
39
 
40
  topic = gr.State(i18n("未命名对话历史记录"))
41
 
@@ -65,6 +65,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
65
  historyFileSelectDropdown = gr.Radio(
66
  label=i18n("从列表中加载对话"),
67
  choices=get_history_names(),
 
68
  # multiselect=False,
69
  container=False,
70
  elem_id="history-select-dropdown"
@@ -430,8 +431,8 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
430
  current_model = get_model(model_name = MODELS[DEFAULT_MODEL], access_key = my_api_key)[0]
431
  current_model.set_user_identifier(user_name)
432
  chatbot = gr.Chatbot.update(label=MODELS[DEFAULT_MODEL])
433
- return user_info, user_name, current_model, toggle_like_btn_visibility(DEFAULT_MODEL), *current_model.auto_load(), chatbot
434
- demo.load(create_greeting, inputs=None, outputs=[user_info, user_name, current_model, like_dislike_area, systemPromptTxt, chatbot, chatbot], api_name="load")
435
  chatgpt_predict_args = dict(
436
  fn=predict,
437
  inputs=[
@@ -652,9 +653,9 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
652
  )
653
  historySelectBtn.click( # This is an experimental feature... Not actually used.
654
  fn=load_chat_history,
655
- inputs=[current_model, historyFileSelectDropdown, user_name],
656
  outputs=[saveFileName, systemPromptTxt, chatbot],
657
- _js='(a,b,c)=>{return bgSelectHistory(a,b,c);}'
658
  )
659
 
660
 
 
35
  user_question = gr.State("")
36
  assert type(my_api_key)==str
37
  user_api_key = gr.State(my_api_key)
38
+ current_model = gr.State()
39
 
40
  topic = gr.State(i18n("未命名对话历史记录"))
41
 
 
65
  historyFileSelectDropdown = gr.Radio(
66
  label=i18n("从列表中加载对话"),
67
  choices=get_history_names(),
68
+ value=get_history_names()[0],
69
  # multiselect=False,
70
  container=False,
71
  elem_id="history-select-dropdown"
 
431
  current_model = get_model(model_name = MODELS[DEFAULT_MODEL], access_key = my_api_key)[0]
432
  current_model.set_user_identifier(user_name)
433
  chatbot = gr.Chatbot.update(label=MODELS[DEFAULT_MODEL])
434
+ return user_info, user_name, current_model, toggle_like_btn_visibility(DEFAULT_MODEL), *current_model.auto_load(), chatbot, init_history_list(user_name)
435
+ demo.load(create_greeting, inputs=None, outputs=[user_info, user_name, current_model, like_dislike_area, systemPromptTxt, chatbot, chatbot, historyFileSelectDropdown], api_name="load")
436
  chatgpt_predict_args = dict(
437
  fn=predict,
438
  inputs=[
 
653
  )
654
  historySelectBtn.click( # This is an experimental feature... Not actually used.
655
  fn=load_chat_history,
656
+ inputs=[current_model, historyFileSelectDropdown],
657
  outputs=[saveFileName, systemPromptTxt, chatbot],
658
+ _js='(a,b)=>{return bgSelectHistory(a,b);}'
659
  )
660
 
661
 
modules/models/base_model.py CHANGED
@@ -207,6 +207,7 @@ class BaseLLMModel:
207
  self.api_key = None
208
  self.need_api_key = False
209
  self.single_turn = False
 
210
 
211
  self.temperature = temperature
212
  self.top_p = top_p
@@ -623,8 +624,8 @@ class BaseLLMModel:
623
  self.history = []
624
  self.all_token_counts = []
625
  self.interrupted = False
626
- pathlib.Path(os.path.join(HISTORY_DIR, self.user_identifier, new_auto_history_filename(
627
- os.path.join(HISTORY_DIR, self.user_identifier)))).touch()
628
  return [], self.token_message([0])
629
 
630
  def delete_first_conversation(self):
@@ -666,8 +667,7 @@ class BaseLLMModel:
666
  return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
667
 
668
  def auto_save(self, chatbot):
669
- history_file_path = get_history_filepath(self.user_identifier)
670
- save_file(history_file_path, self.system_prompt,
671
  self.history, chatbot, self.user_identifier)
672
 
673
  def export_markdown(self, filename, chatbot, user_name):
@@ -677,18 +677,21 @@ class BaseLLMModel:
677
  filename += ".md"
678
  return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
679
 
680
- def load_chat_history(self, filename, user_name):
681
- logging.debug(f"{user_name} 加载对话历史中……")
682
- logging.info(f"filename: {filename}")
683
- if type(filename) != str and filename is not None:
684
- filename = filename.name
 
 
 
685
  try:
686
- if "/" not in filename:
687
  history_file_path = os.path.join(
688
- HISTORY_DIR, user_name, filename)
689
  else:
690
- history_file_path = filename
691
- if not filename.endswith(".json"):
692
  history_file_path += ".json"
693
  with open(history_file_path, "r", encoding="utf-8") as f:
694
  json_s = json.load(f)
@@ -705,12 +708,12 @@ class BaseLLMModel:
705
  logging.info(new_history)
706
  except:
707
  pass
708
- logging.debug(f"{user_name} 加载对话历史完毕")
709
  self.history = json_s["history"]
710
- return os.path.basename(filename), json_s["system"], json_s["chatbot"]
711
  except:
712
  # 没有对话历史或者对话历史解析失败
713
- logging.info(f"没有找到对话历史记录 {filename}")
714
  return gr.update(), self.system_prompt, gr.update()
715
 
716
  def delete_chat_history(self, filename, user_name):
@@ -735,9 +738,8 @@ class BaseLLMModel:
735
  if self.user_identifier == "":
736
  self.reset()
737
  return self.system_prompt, gr.update()
738
- history_file_path = get_history_filepath(self.user_identifier)
739
- filename, system_prompt, chatbot = self.load_chat_history(
740
- history_file_path, self.user_identifier)
741
  return system_prompt, chatbot
742
 
743
  def like(self):
 
207
  self.api_key = None
208
  self.need_api_key = False
209
  self.single_turn = False
210
+ self.history_file_path = None
211
 
212
  self.temperature = temperature
213
  self.top_p = top_p
 
624
  self.history = []
625
  self.all_token_counts = []
626
  self.interrupted = False
627
+ self.history_file_path = new_auto_history_filename(
628
+ os.path.join(HISTORY_DIR, self.user_identifier))
629
  return [], self.token_message([0])
630
 
631
  def delete_first_conversation(self):
 
667
  return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
668
 
669
  def auto_save(self, chatbot):
670
+ save_file(self.history_file_path, self.system_prompt,
 
671
  self.history, chatbot, self.user_identifier)
672
 
673
  def export_markdown(self, filename, chatbot, user_name):
 
677
  filename += ".md"
678
  return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
679
 
680
+ def load_chat_history(self, new_history_file_path=None, username=None):
681
+ logging.debug(f"{self.user_identifier} 加载对话历史中……")
682
+ logging.info(f"filename: {self.history_file_path}")
683
+ if new_history_file_path is not None:
684
+ if type(new_history_file_path) != str:
685
+ self.history_file_path = new_history_file_path.name
686
+ else:
687
+ self.history_file_path = new_history_file_path
688
  try:
689
+ if "/" not in self.history_file_path:
690
  history_file_path = os.path.join(
691
+ HISTORY_DIR, self.user_identifier, self.history_file_path)
692
  else:
693
+ history_file_path = self.history_file_path
694
+ if not self.history_file_path.endswith(".json"):
695
  history_file_path += ".json"
696
  with open(history_file_path, "r", encoding="utf-8") as f:
697
  json_s = json.load(f)
 
708
  logging.info(new_history)
709
  except:
710
  pass
711
+ logging.debug(f"{self.user_identifier} 加载对话历史完毕")
712
  self.history = json_s["history"]
713
+ return os.path.basename(self.history_file_path), json_s["system"], json_s["chatbot"]
714
  except:
715
  # 没有对话历史或者对话历史解析失败
716
+ logging.info(f"没有找到对话历史记录 {self.history_file_path}")
717
  return gr.update(), self.system_prompt, gr.update()
718
 
719
  def delete_chat_history(self, filename, user_name):
 
738
  if self.user_identifier == "":
739
  self.reset()
740
  return self.system_prompt, gr.update()
741
+ self.history_file_path = get_history_filepath(self.user_identifier)
742
+ filename, system_prompt, chatbot = self.load_chat_history()
 
743
  return system_prompt, chatbot
744
 
745
  def like(self):
modules/utils.py CHANGED
@@ -331,14 +331,16 @@ def construct_assistant(text):
331
 
332
 
333
  def save_file(filename, system, history, chatbot, user_name):
334
- logging.debug(f"{user_name} 保存对话历史中……")
335
  os.makedirs(os.path.join(HISTORY_DIR, user_name), exist_ok=True)
 
 
336
  if filename.endswith(".json"):
337
  json_s = {"system": system, "history": history, "chatbot": chatbot}
338
  if "/" in filename or "\\" in filename:
339
  history_file_path = filename
340
  else:
341
  history_file_path = os.path.join(HISTORY_DIR, user_name, filename)
 
342
  with open(history_file_path, "w", encoding='utf-8') as f:
343
  json.dump(json_s, f, ensure_ascii=False)
344
  elif filename.endswith(".md"):
@@ -347,7 +349,6 @@ def save_file(filename, system, history, chatbot, user_name):
347
  md_s += f"\n{data['role']}: \n- {data['content']} \n"
348
  with open(os.path.join(HISTORY_DIR, user_name, filename), "w", encoding="utf8") as f:
349
  f.write(md_s)
350
- logging.debug(f"{user_name} 保存对话历史完毕")
351
  return os.path.join(HISTORY_DIR, user_name, filename)
352
 
353
 
@@ -400,6 +401,9 @@ def get_history_list(user_name=""):
400
  history_names = get_history_names(user_name)
401
  return gr.Radio.update(choices=history_names)
402
 
 
 
 
403
 
404
  def load_template(filename, mode=0):
405
  logging.debug(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")
@@ -640,7 +644,7 @@ def toggle_like_btn_visibility(selected_model_name):
640
  return gr.update(visible=False)
641
 
642
  def new_auto_history_filename(dirname):
643
- latest_file = get_latest_filepath(dirname)
644
  if latest_file:
645
  with open(os.path.join(dirname, latest_file), 'r', encoding="utf-8") as f:
646
  if len(f.read()) == 0:
@@ -648,25 +652,10 @@ def new_auto_history_filename(dirname):
648
  now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
649
  return f'{now}.json'
650
 
651
- def get_latest_filepath(dirname):
652
- pattern = re.compile(r'\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}')
653
- latest_time = None
654
- latest_file = None
655
- for filename in os.listdir(dirname):
656
- if os.path.isfile(os.path.join(dirname, filename)):
657
- match = pattern.search(filename)
658
- if match and match.group(0) == filename[:19]:
659
- time_str = filename[:19]
660
- filetime = datetime.datetime.strptime(time_str, '%Y-%m-%d_%H-%M-%S')
661
- if not latest_time or filetime > latest_time:
662
- latest_time = filetime
663
- latest_file = filename
664
- return latest_file
665
-
666
  def get_history_filepath(username):
667
  dirname = os.path.join(HISTORY_DIR, username)
668
  os.makedirs(dirname, exist_ok=True)
669
- latest_file = get_latest_filepath(dirname)
670
  if not latest_file:
671
  latest_file = new_auto_history_filename(dirname)
672
 
 
331
 
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"):
 
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
 
 
401
  history_names = get_history_names(user_name)
402
  return gr.Radio.update(choices=history_names)
403
 
404
+ 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为返回字典)")
 
644
  return gr.update(visible=False)
645
 
646
  def new_auto_history_filename(dirname):
647
+ latest_file = get_file_names_by_last_modified_time(dirname)[0]
648
  if latest_file:
649
  with open(os.path.join(dirname, latest_file), 'r', encoding="utf-8") as f:
650
  if len(f.read()) == 0:
 
652
  now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
653
  return f'{now}.json'
654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655
  def get_history_filepath(username):
656
  dirname = os.path.join(HISTORY_DIR, username)
657
  os.makedirs(dirname, exist_ok=True)
658
+ latest_file = get_file_names_by_last_modified_time(dirname)[0]
659
  if not latest_file:
660
  latest_file = new_auto_history_filename(dirname)
661
 
web_assets/javascript/fake-gradio.js CHANGED
@@ -45,7 +45,7 @@ function transUpload() {
45
 
46
  // let uploaderEvents = ["click", "drag", "dragend", "dragenter", "dragleave", "dragover", "dragstart", "drop"];
47
  // transEventListeners(chatbotUploader, grUploader, uploaderEvents);
48
-
49
  chatbotUploader.addEventListener('click', handleClick);
50
  }
51
 
@@ -101,8 +101,8 @@ function transEventListeners(target, source, events) {
101
  /* 事实上,我发现这样写的大多数gradio组件并不适用。。所以。。。生气 */
102
  }
103
 
104
- function bgSelectHistory(a,b,c){
105
  const historySelectorInput = gradioApp().querySelector('#history-select-dropdown input');
106
  let file = historySelectorInput.value;
107
- return [a,file,c]
108
  }
 
45
 
46
  // let uploaderEvents = ["click", "drag", "dragend", "dragenter", "dragleave", "dragover", "dragstart", "drop"];
47
  // transEventListeners(chatbotUploader, grUploader, uploaderEvents);
48
+
49
  chatbotUploader.addEventListener('click', handleClick);
50
  }
51
 
 
101
  /* 事实上,我发现这样写的大多数gradio组件并不适用。。所以。。。生气 */
102
  }
103
 
104
+ function bgSelectHistory(a,b){
105
  const historySelectorInput = gradioApp().querySelector('#history-select-dropdown input');
106
  let file = historySelectorInput.value;
107
+ return [a,file]
108
  }