tori29umai commited on
Commit
ab30b3e
·
verified ·
1 Parent(s): c682ccc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -23
app.py CHANGED
@@ -543,16 +543,9 @@ def load_chat_log(file_name):
543
  return chat_history
544
 
545
  def save_and_download_chat_log(chat_history):
546
- # ログを保存
547
- current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
548
- filename = f"{current_time}.csv"
549
- logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
550
- if not os.path.exists(logs_dir):
551
- os.makedirs(logs_dir)
552
- file_path = os.path.join(logs_dir, filename)
553
-
554
- with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
555
- writer = csv.writer(csvfile)
556
  writer.writerow(["Role", "Message"])
557
  for user_message, assistant_message in chat_history:
558
  if user_message:
@@ -560,17 +553,11 @@ def save_and_download_chat_log(chat_history):
560
  if assistant_message:
561
  writer.writerow(["assistant", assistant_message])
562
 
563
- # ダウンロード用のファイルを生成
564
- output = io.StringIO()
565
- writer = csv.writer(output)
566
- writer.writerow(["Role", "Message"])
567
- for user_message, assistant_message in chat_history:
568
- if user_message:
569
- writer.writerow(["user", user_message])
570
- if assistant_message:
571
- writer.writerow(["assistant", assistant_message])
572
 
573
- return (output.getvalue(), filename)
574
 
575
  def resume_chat_from_log(chat_history):
576
  # チャットボットのUIを更新
@@ -717,15 +704,14 @@ def build_gradio_interface():
717
  download_log_output = gr.File(label="ダウンロード", visible=False)
718
 
719
  def update_download_output(chat_history):
720
- content, filename = save_and_download_chat_log(chat_history)
721
- return gr.File.update(value=content, visible=True, filename=filename)
722
 
723
  download_log_button.click(
724
  update_download_output,
725
  inputs=[chatbot],
726
  outputs=[download_log_output]
727
  )
728
-
729
  save_log_output = gr.Textbox(label="保存状態")
730
 
731
  with gr.Tab("文章生成"):
 
543
  return chat_history
544
 
545
  def save_and_download_chat_log(chat_history):
546
+ # 一時ファイルを作成
547
+ with tempfile.NamedTemporaryFile(mode='w+', newline='', encoding='utf-8', suffix='.csv', delete=False) as temp_file:
548
+ writer = csv.writer(temp_file)
 
 
 
 
 
 
 
549
  writer.writerow(["Role", "Message"])
550
  for user_message, assistant_message in chat_history:
551
  if user_message:
 
553
  if assistant_message:
554
  writer.writerow(["assistant", assistant_message])
555
 
556
+ # ファイル名を生成
557
+ current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
558
+ filename = f"chat_log_{current_time}.csv"
 
 
 
 
 
 
559
 
560
+ return temp_file.name, filename
561
 
562
  def resume_chat_from_log(chat_history):
563
  # チャットボットのUIを更新
 
704
  download_log_output = gr.File(label="ダウンロード", visible=False)
705
 
706
  def update_download_output(chat_history):
707
+ temp_file_path, filename = save_and_download_chat_log(chat_history)
708
+ return gr.File(value=temp_file_path, visible=True, label="ダウンロード準備完了", filename=filename)
709
 
710
  download_log_button.click(
711
  update_download_output,
712
  inputs=[chatbot],
713
  outputs=[download_log_output]
714
  )
 
715
  save_log_output = gr.Textbox(label="保存状態")
716
 
717
  with gr.Tab("文章生成"):