Skip to content

Commit 9ed2c45

Browse files
committed
Use markdown in the "HTML" tab
1 parent f0b2645 commit 9ed2c45

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

modules/chat.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,19 @@
1111
import modules.extensions as extensions_module
1212
import modules.shared as shared
1313
from modules.extensions import apply_extensions
14-
from modules.html_generator import generate_chat_html
14+
from modules.html_generator import fix_newlines, generate_chat_html
1515
from modules.text_generation import (encode, generate_reply,
1616
get_max_prompt_length)
1717

1818

19-
# This gets the new line characters right.
20-
def clean_chat_message(text):
21-
text = text.replace('\n', '\n\n')
22-
text = re.sub(r"\n{3,}", "\n\n", text)
23-
text = text.strip()
24-
return text
25-
2619
def generate_chat_output(history, name1, name2, character):
2720
if shared.args.cai_chat:
2821
return generate_chat_html(history, name1, name2, character)
2922
else:
3023
return history
3124

3225
def generate_chat_prompt(user_input, max_new_tokens, name1, name2, context, chat_prompt_size, impersonate=False):
33-
user_input = clean_chat_message(user_input)
26+
user_input = fix_newlines(user_input)
3427
rows = [f"{context.strip()}\n"]
3528

3629
if shared.soft_prompt:
@@ -83,7 +76,7 @@ def extract_message_from_reply(question, reply, name1, name2, check, impersonate
8376
if idx != -1:
8477
reply = reply[:idx]
8578
next_character_found = True
86-
reply = clean_chat_message(reply)
79+
reply = fix_newlines(reply)
8780

8881
# If something like "\nYo" is generated just before "\nYou:"
8982
# is completed, trim it

modules/html_generator.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
3-
This is a library for formatting GPT-4chan and chat outputs as nice HTML.
3+
This is a library for formatting text outputs as nice HTML.
44
55
'''
66

@@ -21,10 +21,26 @@
2121
with open(Path(__file__).resolve().parent / '../css/html_cai_style.css', 'r') as f:
2222
cai_css = f.read()
2323

24-
def generate_basic_html(s):
25-
s = '\n'.join([f'<p>{line}</p>' for line in s.split('\n')])
26-
s = f'<style>{readable_css}</style><div class="container">{s}</div>'
27-
return s
24+
def fix_newlines(string):
25+
string = string.replace('\n', '\n\n')
26+
string = re.sub(r"\n{3,}", "\n\n", string)
27+
string = string.strip()
28+
return string
29+
30+
# This could probably be generalized and improved
31+
def convert_to_markdown(string):
32+
string = string.replace('\\begin{code}', '```')
33+
string = string.replace('\\end{code}', '```')
34+
string = string.replace('\\begin{blockquote}', '> ')
35+
string = string.replace('\\end{blockquote}', '')
36+
string = re.sub(r"(.)```", r"\1\n```", string)
37+
# string = fix_newlines(string)
38+
return markdown.markdown(string, extensions=['fenced_code'])
39+
40+
def generate_basic_html(string):
41+
string = convert_to_markdown(string)
42+
string = f'<style>{readable_css}</style><div class="container">{string}</div>'
43+
return string
2844

2945
def process_post(post, c):
3046
t = post.split('\n')
@@ -108,7 +124,7 @@ def generate_chat_html(history, name1, name2, character):
108124
img_me = load_html_image(["img_me.png", "img_me.jpg", "img_me.jpeg"])
109125

110126
for i,_row in enumerate(history[::-1]):
111-
row = [markdown.markdown(re.sub(r"(.)```", r"\1\n```", entry), extensions=['fenced_code']) for entry in _row]
127+
row = [convert_to_markdown(entry) for entry in _row]
112128

113129
output += f"""
114130
<div class="message">

0 commit comments

Comments
 (0)