Tuchuanhuhuhu commited on
Commit
f4b17c4
1 Parent(s): 2572cda

bugfix: 第一次启动时报错

Browse files
ChuanhuChatbot.py CHANGED
@@ -76,7 +76,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
76
  historySelectList = gr.Radio(
77
  label=i18n("从列表中加载对话"),
78
  choices=get_history_names(),
79
- value=get_history_names()[0],
80
  # multiselect=False,
81
  container=False,
82
  elem_id="history-select-dropdown"
 
76
  historySelectList = gr.Radio(
77
  label=i18n("从列表中加载对话"),
78
  choices=get_history_names(),
79
+ value=get_first_history_name(),
80
  # multiselect=False,
81
  container=False,
82
  elem_id="history-select-dropdown"
modules/models/base_model.py CHANGED
@@ -208,7 +208,7 @@ class BaseLLMModel:
208
  self.api_key = None
209
  self.need_api_key = False
210
  self.single_turn = False
211
- self.history_file_path = get_history_names(user)[0]
212
 
213
  self.temperature = temperature
214
  self.top_p = top_p
@@ -625,8 +625,7 @@ class BaseLLMModel:
625
  self.history = []
626
  self.all_token_counts = []
627
  self.interrupted = False
628
- self.history_file_path = new_auto_history_filename(
629
- os.path.join(HISTORY_DIR, self.user_identifier))
630
  history_name = self.history_file_path[:-5]
631
  choices = [history_name] + get_history_names(self.user_identifier)
632
  return [], self.token_message([0]), gr.Radio.update(choices=choices, value=history_name)
@@ -671,7 +670,7 @@ class BaseLLMModel:
671
  self.history_file_path = filename
672
  save_file(filename, self.system_prompt, self.history, chatbot, user_name)
673
  return init_history_list(user_name)
674
-
675
  def auto_name_chat_history(self, user_question, chatbot, user_name):
676
  if chatbot == []:
677
  filename = user_question[:12] + ".json"
@@ -753,7 +752,12 @@ class BaseLLMModel:
753
  return i18n("对话历史")+filename+i18n("已经被删除啦"), get_history_list(user_name), []
754
 
755
  def auto_load(self):
756
- self.history_file_path = get_history_filepath(self.user_identifier)
 
 
 
 
 
757
  filename, system_prompt, chatbot = self.load_chat_history()
758
  filename = filename[:-5]
759
  return filename, system_prompt, chatbot
 
208
  self.api_key = None
209
  self.need_api_key = False
210
  self.single_turn = False
211
+ self.history_file_path = get_first_history_name(user)
212
 
213
  self.temperature = temperature
214
  self.top_p = top_p
 
625
  self.history = []
626
  self.all_token_counts = []
627
  self.interrupted = False
628
+ self.history_file_path = new_auto_history_filename(self.user_identifier)
 
629
  history_name = self.history_file_path[:-5]
630
  choices = [history_name] + get_history_names(self.user_identifier)
631
  return [], self.token_message([0]), gr.Radio.update(choices=choices, value=history_name)
 
670
  self.history_file_path = filename
671
  save_file(filename, self.system_prompt, self.history, chatbot, user_name)
672
  return init_history_list(user_name)
673
+
674
  def auto_name_chat_history(self, user_question, chatbot, user_name):
675
  if chatbot == []:
676
  filename = user_question[:12] + ".json"
 
752
  return i18n("对话历史")+filename+i18n("已经被删除啦"), get_history_list(user_name), []
753
 
754
  def auto_load(self):
755
+ filepath = get_history_filepath(self.user_identifier)
756
+ if not filepath:
757
+ self.history_file_path = new_auto_history_filename(
758
+ self.user_identifier)
759
+ else:
760
+ self.history_file_path = filepath
761
  filename, system_prompt, chatbot = self.load_chat_history()
762
  filename = filename[:-5]
763
  return filename, system_prompt, chatbot
modules/utils.py CHANGED
@@ -339,6 +339,8 @@ def save_file(filename, system, history, chatbot, user_name):
339
  filename = filename[:-3]
340
  if not filename.endswith(".json") and not filename.endswith(".md"):
341
  filename += ".json"
 
 
342
 
343
  json_s = {"system": system, "history": history, "chatbot": chatbot}
344
  if "/" in filename or "\\" in filename:
@@ -367,11 +369,8 @@ def sorted_by_last_modified_time(list, dir):
367
  def get_file_names_by_type(dir, filetypes=[".json"]):
368
  logging.debug(f"获取文件名列表,目录为{dir},文件类型为{filetypes}")
369
  files = []
370
- try:
371
- for type in filetypes:
372
- files += [f for f in os.listdir(dir) if f.endswith(type)]
373
- except FileNotFoundError:
374
- files = [""]
375
  logging.debug(f"files are:{files}")
376
  return files
377
 
@@ -397,12 +396,16 @@ def get_file_names_by_last_modified_time(dir, filetypes=[".json"]):
397
  def get_history_names(user_name=""):
398
  logging.debug(f"从用户 {user_name} 中获取历史记录文件名列表")
399
  if user_name == "" and hide_history_when_not_logged_in:
400
- return [""]
401
  else:
402
  history_files = get_file_names_by_last_modified_time(os.path.join(HISTORY_DIR, user_name))
403
  history_files = [f[:f.rfind(".")] for f in history_files]
404
  return history_files
405
 
 
 
 
 
406
  def get_history_list(user_name=""):
407
  history_names = get_history_names(user_name)
408
  return gr.Radio.update(choices=history_names)
@@ -657,10 +660,10 @@ def toggle_like_btn_visibility(selected_model_name):
657
  else:
658
  return gr.update(visible=False)
659
 
660
- def new_auto_history_filename(dirname):
661
- latest_file = get_file_names_by_last_modified_time(dirname)[0]
662
  if latest_file:
663
- with open(os.path.join(dirname, latest_file), 'r', encoding="utf-8") as f:
664
  if len(f.read()) == 0:
665
  return latest_file
666
  now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
@@ -669,9 +672,9 @@ def new_auto_history_filename(dirname):
669
  def get_history_filepath(username):
670
  dirname = os.path.join(HISTORY_DIR, username)
671
  os.makedirs(dirname, exist_ok=True)
672
- latest_file = get_file_names_by_last_modified_time(dirname)[0]
673
  if not latest_file:
674
- latest_file = new_auto_history_filename(dirname)
675
 
676
  latest_file = os.path.join(dirname, latest_file)
677
  return latest_file
 
339
  filename = filename[:-3]
340
  if not filename.endswith(".json") and not filename.endswith(".md"):
341
  filename += ".json"
342
+ if filename == ".json":
343
+ raise Exception("文件名不能为空")
344
 
345
  json_s = {"system": system, "history": history, "chatbot": chatbot}
346
  if "/" in filename or "\\" in filename:
 
369
  def get_file_names_by_type(dir, filetypes=[".json"]):
370
  logging.debug(f"获取文件名列表,目录为{dir},文件类型为{filetypes}")
371
  files = []
372
+ for type in filetypes:
373
+ files += [f for f in os.listdir(dir) if f.endswith(type)]
 
 
 
374
  logging.debug(f"files are:{files}")
375
  return files
376
 
 
396
  def get_history_names(user_name=""):
397
  logging.debug(f"从用户 {user_name} 中获取历史记录文件名列表")
398
  if user_name == "" and hide_history_when_not_logged_in:
399
+ return []
400
  else:
401
  history_files = get_file_names_by_last_modified_time(os.path.join(HISTORY_DIR, user_name))
402
  history_files = [f[:f.rfind(".")] for f in history_files]
403
  return history_files
404
 
405
+ def get_first_history_name(user_name=""):
406
+ history_names = get_history_names(user_name)
407
+ return history_names[0] if history_names else None
408
+
409
  def get_history_list(user_name=""):
410
  history_names = get_history_names(user_name)
411
  return gr.Radio.update(choices=history_names)
 
660
  else:
661
  return gr.update(visible=False)
662
 
663
+ def new_auto_history_filename(username):
664
+ latest_file = get_first_history_name(username)
665
  if latest_file:
666
+ with open(os.path.join(HISTORY_DIR, username, latest_file + ".json"), 'r', encoding="utf-8") as f:
667
  if len(f.read()) == 0:
668
  return latest_file
669
  now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
 
672
  def get_history_filepath(username):
673
  dirname = os.path.join(HISTORY_DIR, username)
674
  os.makedirs(dirname, exist_ok=True)
675
+ latest_file = get_first_history_name(username)
676
  if not latest_file:
677
+ latest_file = new_auto_history_filename(username)
678
 
679
  latest_file = os.path.join(dirname, latest_file)
680
  return latest_file