Create a cache for profile pictures (in RAM)

This is a performance optimization.
This commit is contained in:
oobabooga 2023-02-17 14:30:39 -03:00
parent 596732a981
commit 3923ac967f

View File

@ -6,12 +6,16 @@ This is a library for formatting GPT-4chan and chat outputs as nice HTML.
import base64 import base64
import copy import copy
import os
import re import re
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from PIL import Image from PIL import Image
# This is to store chat profile pictures as base64-encoded thumbnails
image_cache = {}
def generate_basic_html(s): def generate_basic_html(s):
css = """ css = """
.container { .container {
@ -182,11 +186,16 @@ def generate_4chan_html(f):
return output return output
def image_to_base64(path): def image_to_base64(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 = Image.open(path) img = Image.open(path)
img.thumbnail((100, 100)) img.thumbnail((100, 100))
img_buffer = BytesIO() img_buffer = BytesIO()
img.convert('RGB').save(img_buffer, format='PNG') img.convert('RGB').save(img_buffer, format='PNG')
return base64.b64encode(img_buffer.getvalue()).decode("utf-8") image_cache[path] = [mtime, base64.b64encode(img_buffer.getvalue()).decode("utf-8")]
return image_cache[path][1]
def generate_chat_html(history, name1, name2, character): def generate_chat_html(history, name1, name2, character):
css = """ css = """