tori29umai commited on
Commit
dc0647b
1 Parent(s): 1cda038

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -22
app.py CHANGED
@@ -14,8 +14,8 @@ import threading
14
  import asyncio
15
  import csv
16
  from utils.dl_utils import dl_guff_model
17
- import io
18
- import shutil
19
 
20
  # 定数
21
  DEFAULT_INI_FILE = 'settings.ini'
@@ -539,14 +539,12 @@ def load_chat_log(file_name):
539
  return chat_history
540
 
541
  def save_and_download_chat_log(chat_history):
542
- # ログを保存
543
  current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
544
- filename = f"{current_time}.csv"
545
- logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
546
- if not os.path.exists(logs_dir):
547
- os.makedirs(logs_dir)
548
- file_path = os.path.join(logs_dir, filename)
549
-
550
  with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
551
  writer = csv.writer(csvfile)
552
  writer.writerow(["Role", "Message"])
@@ -556,17 +554,7 @@ def save_and_download_chat_log(chat_history):
556
  if assistant_message:
557
  writer.writerow(["assistant", assistant_message])
558
 
559
- # ダウンロード用のファイルを生成
560
- output = io.StringIO()
561
- writer = csv.writer(output)
562
- writer.writerow(["Role", "Message"])
563
- for user_message, assistant_message in chat_history:
564
- if user_message:
565
- writer.writerow(["user", user_message])
566
- if assistant_message:
567
- writer.writerow(["assistant", assistant_message])
568
-
569
- return (output.getvalue(), filename)
570
 
571
  def resume_chat_from_log(chat_history):
572
  # チャットボットのUIを更新
@@ -713,8 +701,8 @@ def build_gradio_interface():
713
  download_log_output = gr.File(label="ダウンロード", visible=False)
714
 
715
  def update_download_output(chat_history):
716
- content, filename = save_and_download_chat_log(chat_history)
717
- return gr.File.update(value=content, visible=True, filename=filename)
718
 
719
  download_log_button.click(
720
  update_download_output,
 
14
  import asyncio
15
  import csv
16
  from utils.dl_utils import dl_guff_model
17
+ import tempfile
18
+ import os
19
 
20
  # 定数
21
  DEFAULT_INI_FILE = 'settings.ini'
 
539
  return chat_history
540
 
541
  def save_and_download_chat_log(chat_history):
542
+ # 一時ファイルを作成
543
  current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
544
+ filename = f"chat_log_{current_time}.csv"
545
+ temp_dir = tempfile.gettempdir()
546
+ file_path = os.path.join(temp_dir, filename)
547
+
 
 
548
  with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
549
  writer = csv.writer(csvfile)
550
  writer.writerow(["Role", "Message"])
 
554
  if assistant_message:
555
  writer.writerow(["assistant", assistant_message])
556
 
557
+ return file_path
 
 
 
 
 
 
 
 
 
 
558
 
559
  def resume_chat_from_log(chat_history):
560
  # チャットボットのUIを更新
 
701
  download_log_output = gr.File(label="ダウンロード", visible=False)
702
 
703
  def update_download_output(chat_history):
704
+ file_path = save_and_download_chat_log(chat_history)
705
+ return gr.File(value=file_path, visible=True, label="ダウンロード準備完了")
706
 
707
  download_log_button.click(
708
  update_download_output,