Change the filenames for caches and histories

This commit is contained in:
oobabooga 2023-08-09 07:47:19 -07:00
parent 2255349f19
commit 6c6a52aaad
2 changed files with 22 additions and 5 deletions

View File

@ -395,6 +395,7 @@ def save_history(history, path=None):
p = path or Path('logs/exported_history.json')
if not p.parent.is_dir():
p.parent.mkdir(parents=True)
with open(p, 'w', encoding='utf-8') as f:
f.write(json.dumps(history, indent=4))
@ -415,7 +416,7 @@ def load_history(file, history):
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:
save_history(history, path=Path(f'logs/{character}_persistent.json'))
save_history(history, path=Path(f'logs/persistent_{character}.json'))
def load_persistent_history(state):
@ -428,8 +429,15 @@ def load_persistent_history(state):
character = state['character_menu']
greeting = replace_character_names(state['greeting'], state['name1'], state['name2'])
p = Path(f'logs/{character}_persistent.json')
if not shared.args.multi_user and character not in ['None', '', None] and p.exists():
should_load_history = (not shared.args.multi_user and character not in ['None', '', None])
old_p = Path(f'logs/{character}_persistent.json')
p = Path(f'logs/persistent_{character}.json')
if should_load_history and old_p.exists():
logger.warning(f"Renaming {old_p} to {p}")
old_p.rename(p)
if should_load_history and p.exists():
f = json.loads(open(p, 'rb').read())
if 'internal' in f and 'visible' in f:
history = f

View File

@ -6,6 +6,7 @@ from pathlib import Path
import markdown
from PIL import Image, ImageOps
from modules.logging_colors import logger
from modules.utils import get_available_chat_styles
# This is to store the paths to the thumbnails of the profile pictures
@ -120,6 +121,7 @@ def generate_4chan_html(f):
post = line
else:
post += line
if post != '':
src = process_post(post, c)
posts.append(src)
@ -134,13 +136,14 @@ def generate_4chan_html(f):
output += f'<style>{_4chan_css}</style><div id="parent"><div id="container">'
for post in posts:
output += post
output += '</div></div>'
output = output.split('\n')
for i in range(len(output)):
output[i] = re.sub(r'^(&gt;(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
output[i] = re.sub(r'^<blockquote class="message">(&gt;(.*?)(<br>|</div>))', r'<blockquote class="message"><span class="greentext">\1</span>', output[i])
output = '\n'.join(output)
output = '\n'.join(output)
return output
@ -160,7 +163,13 @@ def get_image_cache(path):
mtime = os.stat(path).st_mtime
if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache):
img = make_thumbnail(Image.open(path))
output_file = Path(f'cache/{path.name}_cache.png')
old_p = Path(f'cache/{path.name}_cache.png')
p = Path(f'cache/cache_{path.name}.png')
if old_p.exists():
old_p.rename(p)
output_file = p
img.convert('RGB').save(output_file, format='PNG')
image_cache[path] = [mtime, output_file.as_posix()]