tori29umai commited on
Commit
b96dc4a
·
verified ·
1 Parent(s): 9ed7407

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -14,6 +14,8 @@ import threading
14
  import asyncio
15
  import csv
16
  from utils.dl_utils import dl_guff_model
 
 
17
 
18
  # 定数
19
  DEFAULT_INI_FILE = 'settings.ini'
@@ -35,6 +37,11 @@ model_path = os.path.join("models", model_filename)
35
  if not os.path.exists(model_path):
36
  dl_guff_model("models", f"https://huggingface.co/MCZK/EZO-Common-9B-gemma-2-it-GGUF/resolve/main/{model_filename}")
37
 
 
 
 
 
 
38
  class ConfigManager:
39
  @staticmethod
40
  def load_settings(filename):
@@ -442,20 +449,16 @@ class CharacterMaker:
442
 
443
  def make_prompt(self, input_str: str):
444
  prompt_template = """{{chat_author_description}}
445
-
446
  {{chat_instructions}}
447
-
448
  ・キャラクターの回答例
449
  {% for qa in example_qa %}
450
  {{qa}}
451
  {% endfor %}
452
-
453
  ・会話履歴
454
  {% for history in histories %}
455
  user: {{history.user}}
456
  assistant: {{history.assistant}}
457
  {% endfor %}
458
-
459
  user: {{input_str}}
460
  assistant:"""
461
 
@@ -509,6 +512,12 @@ def generate_text_wrapper(text, gen_characters, gen_token_multiplier, instructio
509
  return chat_or_gen(text=text, gen_characters=gen_characters, gen_token_multiplier=gen_token_multiplier, instruction=instruction, mode="gen")
510
 
511
  # ログ関連関数
 
 
 
 
 
 
512
  def list_log_files():
513
  logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
514
  if not os.path.exists(logs_dir):
@@ -533,7 +542,8 @@ def load_chat_log(file_name):
533
  chat_history.append([None, message])
534
  return chat_history
535
 
536
- def save_chat_log(chat_history):
 
537
  current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
538
  filename = f"{current_time}.csv"
539
  logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
@@ -550,7 +560,17 @@ def save_chat_log(chat_history):
550
  if assistant_message:
551
  writer.writerow(["assistant", assistant_message])
552
 
553
- return f"チャットログが {file_path} に保存されました。"
 
 
 
 
 
 
 
 
 
 
554
 
555
  def resume_chat_from_log(chat_history):
556
  # チャットボットのUIを更新
@@ -692,16 +712,18 @@ def build_gradio_interface():
692
  )
693
 
694
  with gr.Row():
695
- save_log_button = gr.Button("チャットログを保存")
696
 
697
- save_log_output = gr.Textbox(label="保存状態")
698
 
699
- save_log_button.click(
700
- save_chat_log,
701
  inputs=[chatbot],
702
- outputs=[save_log_output]
703
  )
704
-
 
 
705
  with gr.Tab("文章生成"):
706
  with gr.Row():
707
  with gr.Column(scale=2):
@@ -871,6 +893,10 @@ async def start_gradio():
871
  print(f"{DEFAULT_INI_FILE} をデフォルト設定で上書きします。")
872
  Settings.create_default_ini(DEFAULT_INI_FILE)
873
 
 
 
 
 
874
  config = ConfigManager.load_settings(DEFAULT_INI_FILE)
875
  settings = Settings._parse_config(config)
876
 
 
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'
 
37
  if not os.path.exists(model_path):
38
  dl_guff_model("models", f"https://huggingface.co/MCZK/EZO-Common-9B-gemma-2-it-GGUF/resolve/main/{model_filename}")
39
 
40
+
41
+
42
+
43
+
44
+
45
  class ConfigManager:
46
  @staticmethod
47
  def load_settings(filename):
 
449
 
450
  def make_prompt(self, input_str: str):
451
  prompt_template = """{{chat_author_description}}
 
452
  {{chat_instructions}}
 
453
  ・キャラクターの回答例
454
  {% for qa in example_qa %}
455
  {{qa}}
456
  {% endfor %}
 
457
  ・会話履歴
458
  {% for history in histories %}
459
  user: {{history.user}}
460
  assistant: {{history.assistant}}
461
  {% endfor %}
 
462
  user: {{input_str}}
463
  assistant:"""
464
 
 
512
  return chat_or_gen(text=text, gen_characters=gen_characters, gen_token_multiplier=gen_token_multiplier, instruction=instruction, mode="gen")
513
 
514
  # ログ関連関数
515
+ def delete_all_logs():
516
+ logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
517
+ if os.path.exists(logs_dir):
518
+ shutil.rmtree(logs_dir)
519
+ os.makedirs(logs_dir)
520
+
521
  def list_log_files():
522
  logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
523
  if not os.path.exists(logs_dir):
 
542
  chat_history.append([None, message])
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")
 
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 gr.File.update(value=output.getvalue(), visible=True, filename=filename)
574
 
575
  def resume_chat_from_log(chat_history):
576
  # チャットボットのUIを更新
 
712
  )
713
 
714
  with gr.Row():
715
+ download_log_button = gr.Button("チャットログをダウンロード")
716
 
717
+ download_log_output = gr.File(label="ダウンロード")
718
 
719
+ download_log_button.click(
720
+ save_and_download_chat_log,
721
  inputs=[chatbot],
722
+ outputs=[download_log_output]
723
  )
724
+
725
+ save_log_output = gr.Textbox(label="保存状態")
726
+
727
  with gr.Tab("文章生成"):
728
  with gr.Row():
729
  with gr.Column(scale=2):
 
893
  print(f"{DEFAULT_INI_FILE} をデフォルト設定で上書きします。")
894
  Settings.create_default_ini(DEFAULT_INI_FILE)
895
 
896
+ # 起動時にログを削除
897
+ delete_all_logs()
898
+ print("全てのログファイルが削除されました。")
899
+
900
  config = ConfigManager.load_settings(DEFAULT_INI_FILE)
901
  settings = Settings._parse_config(config)
902