Tuchuanhuhuhu commited on
Commit
ff7d32c
1 Parent(s): 3646bb0

修复可能会生成多余的history文件的bug

Browse files
Files changed (2) hide show
  1. modules/models/base_model.py +1 -1
  2. modules/utils.py +13 -4
modules/models/base_model.py CHANGED
@@ -484,7 +484,7 @@ class BaseLLMModel:
484
  self.history = []
485
  self.all_token_counts = []
486
  self.interrupted = False
487
- pathlib.Path(os.path.join(HISTORY_DIR, self.user_identifier, new_auto_history_filename())).touch()
488
  return [], self.token_message([0])
489
 
490
  def delete_first_conversation(self):
 
484
  self.history = []
485
  self.all_token_counts = []
486
  self.interrupted = False
487
+ pathlib.Path(os.path.join(HISTORY_DIR, self.user_identifier, new_auto_history_filename(os.path.join(HISTORY_DIR, self.user_identifier)))).touch()
488
  return [], self.token_message([0])
489
 
490
  def delete_first_conversation(self):
modules/utils.py CHANGED
@@ -554,12 +554,16 @@ def toggle_like_btn_visibility(selected_model_name):
554
  else:
555
  return gr.update(visible=False)
556
 
557
- def new_auto_history_filename():
 
 
 
 
 
558
  now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
559
  return f'{now}.json'
560
 
561
- def get_history_filepath(username):
562
- dirname = os.path.join(HISTORY_DIR, username)
563
  pattern = re.compile(r'\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}')
564
  latest_time = None
565
  latest_file = None
@@ -572,8 +576,13 @@ def get_history_filepath(username):
572
  if not latest_time or filetime > latest_time:
573
  latest_time = filetime
574
  latest_file = filename
 
 
 
 
 
575
  if not latest_file:
576
- latest_file = new_auto_history_filename()
577
 
578
  latest_file = os.path.join(dirname, latest_file)
579
  return latest_file
 
554
  else:
555
  return gr.update(visible=False)
556
 
557
+ def new_auto_history_filename(dirname):
558
+ latest_file = get_latest_filepath(dirname)
559
+ if latest_file:
560
+ with open(os.path.join(dirname, latest_file), 'r') as f:
561
+ if len(f.read()) == 0:
562
+ return latest_file
563
  now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
564
  return f'{now}.json'
565
 
566
+ def get_latest_filepath(dirname):
 
567
  pattern = re.compile(r'\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}')
568
  latest_time = None
569
  latest_file = None
 
576
  if not latest_time or filetime > latest_time:
577
  latest_time = filetime
578
  latest_file = filename
579
+ return latest_file
580
+
581
+ def get_history_filepath(username):
582
+ dirname = os.path.join(HISTORY_DIR, username)
583
+ latest_file = get_latest_filepath(dirname)
584
  if not latest_file:
585
+ latest_file = new_auto_history_filename(dirname)
586
 
587
  latest_file = os.path.join(dirname, latest_file)
588
  return latest_file