tori29umai commited on
Commit
91fc173
·
verified ·
1 Parent(s): 514bab1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -545,8 +545,13 @@ def load_chat_log(file_name):
545
 
546
  def save_and_download_chat_log(chat_history):
547
  # 一時ファイルを作成
548
- with tempfile.NamedTemporaryFile(mode='w+', newline='', encoding='utf-8', suffix='.csv', delete=False) as temp_file:
549
- writer = csv.writer(temp_file)
 
 
 
 
 
550
  writer.writerow(["Role", "Message"])
551
  for user_message, assistant_message in chat_history:
552
  if user_message:
@@ -554,11 +559,7 @@ def save_and_download_chat_log(chat_history):
554
  if assistant_message:
555
  writer.writerow(["assistant", assistant_message])
556
 
557
- # ファイル名を生成
558
- current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
559
- filename = f"chat_log_{current_time}.csv"
560
-
561
- return temp_file.name, filename
562
 
563
  def resume_chat_from_log(chat_history):
564
  # チャットボットのUIを更新
@@ -705,15 +706,14 @@ def build_gradio_interface():
705
  download_log_output = gr.File(label="ダウンロード", visible=False)
706
 
707
  def update_download_output(chat_history):
708
- temp_file_path, filename = save_and_download_chat_log(chat_history)
709
- return gr.File(value=temp_file_path, visible=True, label="ダウンロード準備完了", filename=filename)
710
 
711
  download_log_button.click(
712
  update_download_output,
713
  inputs=[chatbot],
714
  outputs=[download_log_output]
715
  )
716
- save_log_output = gr.Textbox(label="保存状態")
717
 
718
  with gr.Tab("文章生成"):
719
  with gr.Row():
 
545
 
546
  def save_and_download_chat_log(chat_history):
547
  # 一時ファイルを作成
548
+ current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
549
+ filename = f"chat_log_{current_time}.csv"
550
+ temp_dir = tempfile.gettempdir()
551
+ file_path = os.path.join(temp_dir, filename)
552
+
553
+ with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
554
+ writer = csv.writer(csvfile)
555
  writer.writerow(["Role", "Message"])
556
  for user_message, assistant_message in chat_history:
557
  if user_message:
 
559
  if assistant_message:
560
  writer.writerow(["assistant", assistant_message])
561
 
562
+ return file_path
 
 
 
 
563
 
564
  def resume_chat_from_log(chat_history):
565
  # チャットボットのUIを更新
 
706
  download_log_output = gr.File(label="ダウンロード", visible=False)
707
 
708
  def update_download_output(chat_history):
709
+ file_path = save_and_download_chat_log(chat_history)
710
+ return gr.File(value=file_path, visible=True, label="ダウンロード準備完了")
711
 
712
  download_log_button.click(
713
  update_download_output,
714
  inputs=[chatbot],
715
  outputs=[download_log_output]
716
  )
 
717
 
718
  with gr.Tab("文章生成"):
719
  with gr.Row():