2023-08-16 12:23:29 -04:00
|
|
|
import html
|
2023-02-17 12:30:39 -05:00
|
|
|
import os
|
2023-01-06 21:14:08 -05:00
|
|
|
import re
|
2023-04-04 21:28:49 -04:00
|
|
|
import time
|
2023-01-15 10:20:04 -05:00
|
|
|
from pathlib import Path
|
2023-01-06 21:14:08 -05:00
|
|
|
|
2023-03-15 11:33:26 -04:00
|
|
|
import markdown
|
2023-04-04 21:52:15 -04:00
|
|
|
from PIL import Image, ImageOps
|
2023-02-17 08:58:54 -05:00
|
|
|
|
2023-08-09 10:47:19 -04:00
|
|
|
from modules.logging_colors import logger
|
2023-05-08 11:35:03 -04:00
|
|
|
from modules.utils import get_available_chat_styles
|
|
|
|
|
2023-02-27 11:41:00 -05:00
|
|
|
# This is to store the paths to the thumbnails of the profile pictures
|
2023-02-17 12:30:39 -05:00
|
|
|
image_cache = {}
|
|
|
|
|
2023-03-15 13:19:28 -04:00
|
|
|
with open(Path(__file__).resolve().parent / '../css/html_readable_style.css', 'r') as f:
|
|
|
|
readable_css = f.read()
|
|
|
|
with open(Path(__file__).resolve().parent / '../css/html_4chan_style.css', 'r') as css_f:
|
|
|
|
_4chan_css = css_f.read()
|
2023-04-05 10:49:59 -04:00
|
|
|
with open(Path(__file__).resolve().parent / '../css/html_instruct_style.css', 'r') as f:
|
|
|
|
instruct_css = f.read()
|
2023-03-15 11:33:26 -04:00
|
|
|
|
2023-05-08 11:35:03 -04:00
|
|
|
# Custom chat styles
|
|
|
|
chat_styles = {}
|
|
|
|
for k in get_available_chat_styles():
|
|
|
|
chat_styles[k] = open(Path(f'css/chat_style-{k}.css'), 'r').read()
|
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-03-17 15:06:11 -04:00
|
|
|
def fix_newlines(string):
|
|
|
|
string = string.replace('\n', '\n\n')
|
|
|
|
string = re.sub(r"\n{3,}", "\n\n", string)
|
|
|
|
string = string.strip()
|
|
|
|
return string
|
|
|
|
|
2023-04-16 20:26:19 -04:00
|
|
|
|
2023-04-16 17:00:12 -04:00
|
|
|
def replace_blockquote(m):
|
|
|
|
return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '')
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-04-16 20:26:19 -04:00
|
|
|
|
2023-03-17 15:06:11 -04:00
|
|
|
def convert_to_markdown(string):
|
2023-04-16 17:00:12 -04:00
|
|
|
|
|
|
|
# Blockquote
|
|
|
|
pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL)
|
|
|
|
string = pattern.sub(replace_blockquote, string)
|
|
|
|
|
|
|
|
# Code
|
2023-03-17 15:06:11 -04:00
|
|
|
string = string.replace('\\begin{code}', '```')
|
|
|
|
string = string.replace('\\end{code}', '```')
|
|
|
|
string = re.sub(r"(.)```", r"\1\n```", string)
|
2023-04-16 17:00:12 -04:00
|
|
|
|
2023-05-05 10:22:36 -04:00
|
|
|
result = ''
|
|
|
|
is_code = False
|
|
|
|
for line in string.split('\n'):
|
|
|
|
if line.lstrip(' ').startswith('```'):
|
|
|
|
is_code = not is_code
|
|
|
|
|
2023-08-17 00:08:01 -04:00
|
|
|
if is_code:
|
|
|
|
line = html.unescape(line)
|
|
|
|
|
2023-05-05 10:22:36 -04:00
|
|
|
result += line
|
2023-05-10 12:41:23 -04:00
|
|
|
if is_code or line.startswith('|'): # Don't add an extra \n for tables or code
|
2023-05-05 10:22:36 -04:00
|
|
|
result += '\n'
|
|
|
|
else:
|
|
|
|
result += '\n\n'
|
|
|
|
|
|
|
|
if is_code:
|
|
|
|
result = result + '```' # Unfinished code block
|
|
|
|
|
2023-08-03 20:10:57 -04:00
|
|
|
result = result.strip()
|
|
|
|
|
|
|
|
# Unfinished list, like "\n1.". A |delete| string is added and then
|
|
|
|
# removed to force a <ol> to be generated instead of a <p>.
|
|
|
|
if re.search(r'(\d+\.?)$', result):
|
|
|
|
delete_str = '|delete|'
|
|
|
|
|
|
|
|
if not result.endswith('.'):
|
|
|
|
result += '.'
|
|
|
|
|
|
|
|
result = re.sub(r'(\d+\.)$', r'\g<1> ' + delete_str, result)
|
|
|
|
|
2023-08-17 00:08:01 -04:00
|
|
|
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])
|
|
|
|
pos = html_output.rfind(delete_str)
|
2023-08-03 20:10:57 -04:00
|
|
|
if pos > -1:
|
2023-08-17 00:08:01 -04:00
|
|
|
html_output = html_output[:pos] + html_output[pos + len(delete_str):]
|
2023-08-03 20:10:57 -04:00
|
|
|
else:
|
2023-08-17 00:08:01 -04:00
|
|
|
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])
|
2023-08-03 20:10:57 -04:00
|
|
|
|
2023-08-17 00:08:01 -04:00
|
|
|
return html_output
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-03-17 15:06:11 -04:00
|
|
|
|
|
|
|
def generate_basic_html(string):
|
2023-08-16 12:23:29 -04:00
|
|
|
string = html.escape(string)
|
2023-03-17 15:06:11 -04:00
|
|
|
string = convert_to_markdown(string)
|
|
|
|
string = f'<style>{readable_css}</style><div class="container">{string}</div>'
|
|
|
|
return string
|
2023-01-15 14:43:31 -05:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-01-06 21:14:08 -05:00
|
|
|
def process_post(post, c):
|
|
|
|
t = post.split('\n')
|
|
|
|
number = t[0].split(' ')[1]
|
|
|
|
if len(t) > 1:
|
|
|
|
src = '\n'.join(t[1:])
|
|
|
|
else:
|
|
|
|
src = ''
|
|
|
|
src = re.sub('>', '>', src)
|
|
|
|
src = re.sub('(>>[0-9]*)', '<span class="quote">\\1</span>', src)
|
|
|
|
src = re.sub('\n', '<br>\n', src)
|
2023-08-13 22:24:09 -04:00
|
|
|
src = f'<blockquote class="message_4chan">{src}\n'
|
2023-01-06 21:14:08 -05:00
|
|
|
src = f'<span class="name">Anonymous </span> <span class="number">No.{number}</span>\n{src}'
|
|
|
|
return src
|
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-01-10 23:10:11 -05:00
|
|
|
def generate_4chan_html(f):
|
2023-01-06 21:14:08 -05:00
|
|
|
posts = []
|
|
|
|
post = ''
|
|
|
|
c = -2
|
|
|
|
for line in f.splitlines():
|
|
|
|
line += "\n"
|
|
|
|
if line == '-----\n':
|
|
|
|
continue
|
|
|
|
elif line.startswith('--- '):
|
|
|
|
c += 1
|
|
|
|
if post != '':
|
|
|
|
src = process_post(post, c)
|
|
|
|
posts.append(src)
|
|
|
|
post = line
|
|
|
|
else:
|
|
|
|
post += line
|
2023-08-09 10:47:19 -04:00
|
|
|
|
2023-01-06 21:14:08 -05:00
|
|
|
if post != '':
|
|
|
|
src = process_post(post, c)
|
|
|
|
posts.append(src)
|
|
|
|
|
|
|
|
for i in range(len(posts)):
|
|
|
|
if i == 0:
|
|
|
|
posts[i] = f'<div class="op">{posts[i]}</div>\n'
|
|
|
|
else:
|
|
|
|
posts[i] = f'<div class="reply">{posts[i]}</div>\n'
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-01-06 21:14:08 -05:00
|
|
|
output = ''
|
2023-03-15 13:19:28 -04:00
|
|
|
output += f'<style>{_4chan_css}</style><div id="parent"><div id="container">'
|
2023-01-06 21:14:08 -05:00
|
|
|
for post in posts:
|
|
|
|
output += post
|
2023-08-09 10:47:19 -04:00
|
|
|
|
2023-02-17 20:47:41 -05:00
|
|
|
output += '</div></div>'
|
2023-01-06 21:14:08 -05:00
|
|
|
output = output.split('\n')
|
|
|
|
for i in range(len(output)):
|
2023-01-06 23:20:10 -05:00
|
|
|
output[i] = re.sub(r'^(>(.*?)(<br>|</div>))', r'<span class="greentext">\1</span>', output[i])
|
2023-08-13 22:24:09 -04:00
|
|
|
output[i] = re.sub(r'^<blockquote class="message_4chan">(>(.*?)(<br>|</div>))', r'<blockquote class="message_4chan"><span class="greentext">\1</span>', output[i])
|
2023-01-06 21:14:08 -05:00
|
|
|
|
2023-08-09 10:47:19 -04:00
|
|
|
output = '\n'.join(output)
|
2023-01-06 21:14:08 -05:00
|
|
|
return output
|
2023-01-15 10:20:04 -05:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-04-04 22:03:58 -04:00
|
|
|
def make_thumbnail(image):
|
2023-04-06 23:15:45 -04:00
|
|
|
image = image.resize((350, round(image.size[1] / image.size[0] * 350)), Image.Resampling.LANCZOS)
|
2023-04-04 22:03:58 -04:00
|
|
|
if image.size[1] > 470:
|
2023-07-02 00:43:58 -04:00
|
|
|
image = ImageOps.fit(image, (350, 470), Image.LANCZOS)
|
2023-04-04 22:03:58 -04:00
|
|
|
|
|
|
|
return image
|
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-02-27 11:41:00 -05:00
|
|
|
def get_image_cache(path):
|
|
|
|
cache_folder = Path("cache")
|
|
|
|
if not cache_folder.exists():
|
|
|
|
cache_folder.mkdir()
|
2023-02-17 12:30:39 -05:00
|
|
|
|
2023-02-27 11:41:00 -05:00
|
|
|
mtime = os.stat(path).st_mtime
|
2023-02-17 12:30:39 -05:00
|
|
|
if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache):
|
2023-04-04 22:03:58 -04:00
|
|
|
img = make_thumbnail(Image.open(path))
|
2023-08-09 10:47:19 -04:00
|
|
|
|
|
|
|
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
|
2023-02-27 11:41:00 -05:00
|
|
|
img.convert('RGB').save(output_file, format='PNG')
|
|
|
|
image_cache[path] = [mtime, output_file.as_posix()]
|
2023-02-17 12:30:39 -05:00
|
|
|
|
|
|
|
return image_cache[path][1]
|
2023-02-17 08:58:54 -05:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-04-05 10:49:59 -04:00
|
|
|
def generate_instruct_html(history):
|
2023-08-05 12:53:54 -04:00
|
|
|
output = f'<style>{instruct_css}</style><div class="chat pretty_scrollbar" id="chat"><div class="messages">'
|
|
|
|
for i, _row in enumerate(history):
|
2023-04-05 10:49:59 -04:00
|
|
|
row = [convert_to_markdown(entry) for entry in _row]
|
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
if row[0]: # don't display empty user messages
|
|
|
|
output += f"""
|
|
|
|
<div class="user-message">
|
|
|
|
<div class="text">
|
|
|
|
<div class="message-body">
|
|
|
|
{row[0]}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-04-05 10:49:59 -04:00
|
|
|
</div>
|
2023-08-05 12:53:54 -04:00
|
|
|
"""
|
2023-04-05 10:49:59 -04:00
|
|
|
|
|
|
|
output += f"""
|
2023-08-05 12:53:54 -04:00
|
|
|
<div class="assistant-message">
|
2023-04-05 10:49:59 -04:00
|
|
|
<div class="text">
|
|
|
|
<div class="message-body">
|
2023-08-05 12:53:54 -04:00
|
|
|
{row[1]}
|
2023-04-05 10:49:59 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
output += "</div></div>"
|
2023-04-05 10:49:59 -04:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-05-08 11:35:03 -04:00
|
|
|
def generate_cai_chat_html(history, name1, name2, style, reset_cache=False):
|
2023-08-05 12:53:54 -04:00
|
|
|
output = f'<style>{chat_styles[style]}</style><div class="chat pretty_scrollbar" id="chat"><div class="messages">'
|
2023-03-24 16:18:27 -04:00
|
|
|
|
2023-04-10 14:48:07 -04:00
|
|
|
# We use ?name2 and ?time.time() to force the browser to reset caches
|
|
|
|
img_bot = f'<img src="file/cache/pfp_character.png?{name2}">' if Path("cache/pfp_character.png").exists() else ''
|
|
|
|
img_me = f'<img src="file/cache/pfp_me.png?{time.time() if reset_cache else ""}">' if Path("cache/pfp_me.png").exists() else ''
|
2023-01-25 17:37:44 -05:00
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
for i, _row in enumerate(history):
|
2023-03-17 15:06:11 -04:00
|
|
|
row = [convert_to_markdown(entry) for entry in _row]
|
2023-03-24 16:18:27 -04:00
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
if row[0]: # don't display empty user messages
|
|
|
|
output += f"""
|
|
|
|
<div class="message">
|
|
|
|
<div class="circle-you">
|
|
|
|
{img_me}
|
|
|
|
</div>
|
|
|
|
<div class="text">
|
|
|
|
<div class="username">
|
|
|
|
{name1}
|
|
|
|
</div>
|
|
|
|
<div class="message-body">
|
|
|
|
{row[0]}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
2023-01-15 10:20:04 -05:00
|
|
|
output += f"""
|
|
|
|
<div class="message">
|
|
|
|
<div class="circle-bot">
|
2023-03-12 15:34:09 -04:00
|
|
|
{img_bot}
|
2023-01-15 10:20:04 -05:00
|
|
|
</div>
|
|
|
|
<div class="text">
|
|
|
|
<div class="username">
|
|
|
|
{name2}
|
|
|
|
</div>
|
2023-02-15 18:20:56 -05:00
|
|
|
<div class="message-body">
|
2023-03-12 15:34:09 -04:00
|
|
|
{row[1]}
|
2023-01-15 10:20:04 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
output += "</div></div>"
|
2023-01-15 10:20:04 -05:00
|
|
|
return output
|
2023-04-05 10:49:59 -04:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-04-16 15:44:50 -04:00
|
|
|
def generate_chat_html(history, name1, name2, reset_cache=False):
|
2023-08-05 12:53:54 -04:00
|
|
|
output = f'<style>{chat_styles["wpp"]}</style><div class="chat pretty_scrollbar" id="chat"><div class="messages">'
|
2023-04-16 15:44:50 -04:00
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
for i, _row in enumerate(history):
|
2023-04-16 15:44:50 -04:00
|
|
|
row = [convert_to_markdown(entry) for entry in _row]
|
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
if row[0]: # don't display empty user messages
|
|
|
|
output += f"""
|
2023-04-16 15:44:50 -04:00
|
|
|
<div class="message">
|
2023-08-05 12:53:54 -04:00
|
|
|
<div class="text-you">
|
2023-04-16 15:44:50 -04:00
|
|
|
<div class="message-body">
|
2023-08-05 12:53:54 -04:00
|
|
|
{row[0]}
|
2023-04-16 15:44:50 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
output += f"""
|
2023-08-05 12:53:54 -04:00
|
|
|
<div class="message">
|
|
|
|
<div class="text-bot">
|
|
|
|
<div class="message-body">
|
|
|
|
{row[1]}
|
2023-04-16 15:44:50 -04:00
|
|
|
</div>
|
2023-08-05 12:53:54 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
2023-04-16 15:44:50 -04:00
|
|
|
|
2023-08-05 12:53:54 -04:00
|
|
|
output += "</div></div>"
|
2023-04-16 15:44:50 -04:00
|
|
|
return output
|
2023-04-05 10:49:59 -04:00
|
|
|
|
2023-04-06 23:15:45 -04:00
|
|
|
|
2023-05-08 11:35:03 -04:00
|
|
|
def chat_html_wrapper(history, name1, name2, mode, style, reset_cache=False):
|
|
|
|
if mode == 'instruct':
|
2023-07-03 23:03:30 -04:00
|
|
|
return generate_instruct_html(history['visible'])
|
2023-05-08 11:35:03 -04:00
|
|
|
elif style == 'wpp':
|
2023-07-03 23:03:30 -04:00
|
|
|
return generate_chat_html(history['visible'], name1, name2)
|
2023-04-05 10:49:59 -04:00
|
|
|
else:
|
2023-07-03 23:03:30 -04:00
|
|
|
return generate_cai_chat_html(history['visible'], name1, name2, style, reset_cache)
|