From d986c17c520509443ab2a8e0e65ad6ca6fab4262 Mon Sep 17 00:00:00 2001 From: Axiom Wolf <30778583+UnskilledWolf@users.noreply.github.com> Date: Wed, 12 Jul 2023 05:10:36 +0200 Subject: [PATCH] Chat history download creates more detailed file names (#3051) --- modules/chat.py | 20 +++++++++++++++++++- server.py | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/chat.py b/modules/chat.py index c0635c2..be524ad 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -3,6 +3,7 @@ import copy import functools import json import re +from datetime import datetime from pathlib import Path import gradio as gr @@ -388,8 +389,25 @@ def load_history(file, history): return history +def save_history_at_user_request(history, character, mode): + def make_timestamp_path(character=None): + return f"logs/{character or ''}{'_' if character else ''}{datetime.now().strftime('%Y%m%d-%H%M%S')}.json" + + path = None + if mode in ['chat', 'chat-instruct'] and character not in ['', 'None', None]: + path = make_timestamp_path(character) + else: + # Try to use mode as the file name, otherwise just use the timestamp + try: + path = make_timestamp_path(mode.capitalize()) + except: + path = make_timestamp_path() + + return save_history(history, path) + + def save_persistent_history(history, character, mode): - if mode in ['chat', 'chat-instruct'] and character not in ['', 'None', None] and not shared.args.multi_user: + if mode in ['chat', 'chat-instruct'] and character not in ['', 'None', None] and not shared.args.multi_user: save_history(history, path=Path(f'logs/{character}_persistent.json')) diff --git a/server.py b/server.py index 489936e..edd91bc 100644 --- a/server.py +++ b/server.py @@ -982,7 +982,7 @@ def create_interface(): lambda: 'characters/instruction-following/', None, gradio('delete_root')).then( lambda: gr.update(visible=True), None, gradio('file_deleter')) - shared.gradio['download_button'].click(chat.save_history, gradio('history'), gradio('download')) + shared.gradio['download_button'].click(chat.save_history_at_user_request, gradio('history', 'character_menu', 'mode'), gradio('download')) shared.gradio['Submit character'].click(chat.upload_character, gradio('upload_json', 'upload_img_bot'), gradio('character_menu')) shared.gradio['upload_json'].upload(lambda: gr.update(interactive=True), None, gradio('Submit character')) shared.gradio['upload_json'].clear(lambda: gr.update(interactive=False), None, gradio('Submit character'))