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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -39
app.py CHANGED
@@ -15,7 +15,6 @@ 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'
@@ -30,7 +29,7 @@ if not os.path.exists("models"):
30
  os.makedirs("models")
31
 
32
  # 使用するモデルのファイル名を指定
33
- model_filename = "Mistral-Nemo-Instruct-2407-Q6_K.gguf"
34
  model_path = os.path.join("models", model_filename)
35
 
36
  # モデルファイルが存在しない場合はダウンロード
@@ -175,8 +174,8 @@ class Settings:
175
  "assistant: プロットについてコメントをする前に、まずこの物語の『売り』について簡単に説明してください",
176
  ],
177
  'gen_author_description': 'あなたは新進気鋭の和風伝奇ミステリー小説家で、細やかな筆致と巧みな構成で若い世代にとても人気があります。',
178
- 'DEFAULT_CHAT_MODEL': 'Mistral-Nemo-Instruct-2407-Q6_K.gguf',
179
- 'DEFAULT_GEN_MODEL': 'Mistral-Nemo-Instruct-2407-Q6_K.gguf',
180
  'chat_n_gpu_layers': -1,
181
  'chat_temperature': 0.35,
182
  'chat_top_p': 0.9,
@@ -508,18 +507,6 @@ def generate_text_wrapper(text, gen_characters, gen_token_multiplier, instructio
508
  return chat_or_gen(text=text, gen_characters=gen_characters, gen_token_multiplier=gen_token_multiplier, instruction=instruction, mode="gen")
509
 
510
  # ログ関連関数
511
- def delete_all_logs():
512
- logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
513
- if os.path.exists(logs_dir):
514
- shutil.rmtree(logs_dir)
515
- os.makedirs(logs_dir)
516
-
517
- def list_log_files():
518
- logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
519
- if not os.path.exists(logs_dir):
520
- return []
521
- return [f for f in os.listdir(logs_dir) if f.endswith('.csv')]
522
-
523
  def load_chat_log(file_name):
524
  file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs", file_name)
525
  chat_history = []
@@ -554,8 +541,21 @@ def save_and_download_chat_log(chat_history):
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を更新
561
  chatbot_ui = gr.update(value=chat_history)
@@ -699,15 +699,22 @@ def build_gradio_interface():
699
  download_log_button = gr.Button("チャットログをダウンロード")
700
 
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,
709
  inputs=[chatbot],
710
- outputs=[download_log_output]
 
 
 
 
 
 
711
  )
712
 
713
  with gr.Tab("文章生成"):
@@ -763,28 +770,35 @@ def build_gradio_interface():
763
  outputs=[gen_input_char_count]
764
  )
765
 
 
766
  with gr.Tab("ログ閲覧", id="log_view_tab") as log_view_tab:
767
  gr.Markdown("## チャットログ閲覧")
768
  chatbot_read = gr.Chatbot(elem_id="chatbot_read")
769
- log_file_dropdown = gr.Dropdown(label="ログファイル選択", choices=list_log_files())
770
- refresh_log_list_button = gr.Button("ログ��ァイルリストを更新")
771
  resume_chat_button = gr.Button("選択したログから会話を再開")
772
 
773
- def update_log_dropdown():
774
- return gr.update(choices=list_log_files())
775
-
776
- def load_and_display_chat_log(file_name):
777
- chat_history = load_chat_log(file_name)
 
 
 
 
 
 
 
 
 
 
 
 
778
  return gr.update(value=chat_history)
779
 
780
- refresh_log_list_button.click(
781
- update_log_dropdown,
782
- outputs=[log_file_dropdown]
783
- )
784
-
785
- log_file_dropdown.change(
786
- load_and_display_chat_log,
787
- inputs=[log_file_dropdown],
788
  outputs=[chatbot_read]
789
  )
790
 
@@ -879,10 +893,6 @@ async def start_gradio():
879
  print(f"{DEFAULT_INI_FILE} をデフォルト設定で上書きします。")
880
  Settings.create_default_ini(DEFAULT_INI_FILE)
881
 
882
- # 起動時にログを削除
883
- delete_all_logs()
884
- print("全てのログファイルが削除されました。")
885
-
886
  config = ConfigManager.load_settings(DEFAULT_INI_FILE)
887
  settings = Settings._parse_config(config)
888
 
 
15
  import csv
16
  from utils.dl_utils import dl_guff_model
17
  import tempfile
 
18
 
19
  # 定数
20
  DEFAULT_INI_FILE = 'settings.ini'
 
29
  os.makedirs("models")
30
 
31
  # 使用するモデルのファイル名を指定
32
+ model_filename = "Mistral-Nemo-Instruct-2407-Q8_0.gguf"
33
  model_path = os.path.join("models", model_filename)
34
 
35
  # モデルファイルが存在しない場合はダウンロード
 
174
  "assistant: プロットについてコメントをする前に、まずこの物語の『売り』について簡単に説明してください",
175
  ],
176
  'gen_author_description': 'あなたは新進気鋭の和風伝奇ミステリー小説家で、細やかな筆致と巧みな構成で若い世代にとても人気があります。',
177
+ 'DEFAULT_CHAT_MODEL': 'Mistral-Nemo-Instruct-2407-Q8_0.gguf',
178
+ 'DEFAULT_GEN_MODEL': 'Mistral-Nemo-Instruct-2407-Q8_0.gguf',
179
  'chat_n_gpu_layers': -1,
180
  'chat_temperature': 0.35,
181
  'chat_top_p': 0.9,
 
507
  return chat_or_gen(text=text, gen_characters=gen_characters, gen_token_multiplier=gen_token_multiplier, instruction=instruction, mode="gen")
508
 
509
  # ログ関連関数
 
 
 
 
 
 
 
 
 
 
 
 
510
  def load_chat_log(file_name):
511
  file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs", file_name)
512
  chat_history = []
 
541
  if assistant_message:
542
  writer.writerow(["assistant", assistant_message])
543
 
544
+ return file_path, filename # ファイルパスとファイル名を返す
545
+
546
+ def cleanup_temp_file(file_path):
547
+ try:
548
+ os.remove(file_path)
549
+ print(f"Temporary file {file_path} has been removed.")
550
+ except Exception as e:
551
+ print(f"Error removing temporary file {file_path}: {e}")
552
 
553
+
554
+ # チャットタブ内のダウンロードボタンのクリックイベントを更新
555
+ def update_download_output(chat_history):
556
+ file_path, filename = save_and_download_chat_log(chat_history)
557
+ return gr.File(value=file_path, visible=True, label="ダウンロード準備完了"), file_path
558
+
559
  def resume_chat_from_log(chat_history):
560
  # チャットボットのUIを更新
561
  chatbot_ui = gr.update(value=chat_history)
 
699
  download_log_button = gr.Button("チャットログをダウンロード")
700
 
701
  download_log_output = gr.File(label="ダウンロード", visible=False)
702
+ temp_file_path = gr.State() # 一時ファイルのパスを保存するための状態変数
703
 
704
  def update_download_output(chat_history):
705
+ file_path, filename = save_and_download_chat_log(chat_history)
706
+ return gr.File(value=file_path, visible=True, label="ダウンロード準備完了"), file_path
707
 
708
  download_log_button.click(
709
  update_download_output,
710
  inputs=[chatbot],
711
+ outputs=[download_log_output, temp_file_path]
712
+ )
713
+
714
+ # ダウンロード完了後に一時ファイルを削除
715
+ download_log_output.change(
716
+ cleanup_temp_file,
717
+ inputs=[temp_file_path]
718
  )
719
 
720
  with gr.Tab("文章生成"):
 
770
  outputs=[gen_input_char_count]
771
  )
772
 
773
+ # ログ閲覧タブの実装
774
  with gr.Tab("ログ閲覧", id="log_view_tab") as log_view_tab:
775
  gr.Markdown("## チャットログ閲覧")
776
  chatbot_read = gr.Chatbot(elem_id="chatbot_read")
777
+ log_file_upload = gr.File(label="ログファイルをアップロード", file_types=[".csv"])
 
778
  resume_chat_button = gr.Button("選択したログから会話を再開")
779
 
780
+ def load_and_display_uploaded_chat_log(file):
781
+ if file is None:
782
+ return gr.update(value=[])
783
+ chat_history = []
784
+ with open(file.name, 'r', encoding='utf-8') as csvfile:
785
+ reader = csv.reader(csvfile)
786
+ next(reader) # Skip header
787
+ for row in reader:
788
+ if len(row) == 2:
789
+ role, message = row
790
+ if role == "user":
791
+ chat_history.append([message, None])
792
+ elif role == "assistant":
793
+ if chat_history and chat_history[-1][1] is None:
794
+ chat_history[-1][1] = message
795
+ else:
796
+ chat_history.append([None, message])
797
  return gr.update(value=chat_history)
798
 
799
+ log_file_upload.change(
800
+ load_and_display_uploaded_chat_log,
801
+ inputs=[log_file_upload],
 
 
 
 
 
802
  outputs=[chatbot_read]
803
  )
804
 
 
893
  print(f"{DEFAULT_INI_FILE} をデフォルト設定で上書きします。")
894
  Settings.create_default_ini(DEFAULT_INI_FILE)
895
 
 
 
 
 
896
  config = ConfigManager.load_settings(DEFAULT_INI_FILE)
897
  settings = Settings._parse_config(config)
898