Skip to content

Commit

Permalink
Merge pull request #6623 from oobabooga/dev
Browse files Browse the repository at this point in the history
Merge dev branch
  • Loading branch information
oobabooga authored Dec 31, 2024
2 parents 4d466d5 + 64853f8 commit 88a6331
Show file tree
Hide file tree
Showing 27 changed files with 246 additions and 206 deletions.
28 changes: 7 additions & 21 deletions css/html_instruct_style.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,22 @@
line-height: 28px !important;
}

.dark .chat .message-body p,
.dark .chat .message-body li,
.dark .chat .message-body q {
.dark .chat .message-body :is(p, li, q, h1, h2, h3, h4, h5, h6) {
color: #d1d5db !important;
}

.chat .message-body p,
.chat .message-body ul,
.chat .message-body ol {
margin-top: 1.25em !important;
margin-bottom: 1.25em !important;
.chat .message-body :is(p, ul, ol) {
margin: 1.25em 0 !important;
}

.chat .message-body p:first-child,
.chat .message-body ul:first-child,
.chat .message-body ol:first-child {
.chat .message-body :is(p, ul, ol):first-child {
margin-top: 0 !important;
}

.chat .message-body p:last-child,
.chat .message-body ul:last-child,
.chat .message-body ol:last-child {
.chat .message-body :is(p, ul, ol):last-child {
margin-bottom: 0 !important;
}

.chat .message-body li {
margin-top: 1.25em !important;
margin-bottom: 1.25em !important;
}

.user-message, .assistant-message {
font-family: Inter, Helvetica, Arial, sans-serif;
}
Expand All @@ -60,13 +46,13 @@
}

.chat .user-message {
background: #f4f4f4;
padding: 1.5rem 1rem;
border-radius: 0;
border-bottom-right-radius: 0;
}

.chat .assistant-message {
background: #f4f4f4;
padding: 1.5rem 1rem;
border-radius: 0;
border: 0;
Expand All @@ -82,7 +68,7 @@

.chat .user-message .text,
.chat .assistant-message .text {
max-width: 40.25rem;
max-width: 645px;
margin-left: auto;
margin-right: auto;
}
Expand Down
82 changes: 63 additions & 19 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ div.svelte-iyf88w {

/* "info" messages without a title above */
.block > .svelte-e8n7p6:not(:only-of-type, #chat-mode *) {
margin-bottom: 2px;
margin-bottom: 0;
}

.py-6 {
Expand Down Expand Up @@ -433,12 +433,56 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* {
padding-top: 20px;
}

.message-body h1,
.message-body h2,
.message-body h3,
.message-body {
font-size: 16px;
}

.dark .message-body :is(h1, h2, h3, h4, h5, h6) {
color: white !important;
}

.message-body h1 {
font-weight: 800;
font-size: 2.25em;
margin-top: 0;
margin-bottom: 0.8888889em;
line-height: 1.1111111;
}

.message-body h2 {
font-weight: 700;
font-size: 1.5em;
margin-top: 2em;
margin-bottom: 1em;
line-height: 1.3333333;
}

.message-body h3 {
font-weight: 600;
font-size: 1.25em;
margin-top: 1.6em;
margin-bottom: 0.6em;
line-height: 1.6;
}

.message-body h4 {
color: var(--body-text-color);
margin: 20px 0 10px;
font-weight: 600;
font-size: 1em;
margin-top: 1.5em;
margin-bottom: 0.5em;
line-height: 1.5;
}

.message-body h5 {
font-weight: normal;
font-size: 1em;
margin: 0;
}

.message-body h6 {
font-weight: normal;
font-size: 1em;
margin: 0;
}

.dark .message q {
Expand All @@ -451,29 +495,28 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* {

.message-body li {
list-style-position: outside;
margin-top: 0.5em !important;
margin-bottom: 0.5em !important;
}

.chat .message-body ul, .chat .message-body ol {
padding-inline-start: 2em;
.message-body ul.long-list li,
.message-body ol.long-list li {
margin-top: 1.25em !important;
margin-bottom: 1.25em !important;
}

.chat .message-body li:not(:last-child) {
margin-top: 0;
margin-bottom: 2px;
.message-body a {
font-weight: 500;
}

.chat .message-body li:last-child {
margin-bottom: 0 !important;
.chat .message-body ul, .chat .message-body ol {
padding-inline-start: 2em;
}

.message-body li > p {
display: inline !important;
}

.message-body ul, .message-body ol {
font-size: 15px !important;
}

.message-body ul {
list-style-type: disc !important;
}
Expand Down Expand Up @@ -786,8 +829,9 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* {
padding-bottom: 80px !important;
}

.chat ol, .chat ul {
margin-top: 6px !important;
.message-body ol, .message-body ul {
margin-top: 0 !important;
margin-bottom: 1.25em !important;
}

/* ----------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False, loading_mess

# Extract the reply
if state['mode'] in ['chat', 'chat-instruct']:
visible_reply = re.sub("(<USER>|<user>|{{user}})", state['name1'], reply + '')
visible_reply = re.sub("(<USER>|<user>|{{user}})", state['name1'], reply + '')
else:
visible_reply = reply + ''
visible_reply = reply + ''

visible_reply = html.escape(visible_reply)

if shared.stop_everything:
if output['visible'][-1][1].endswith(''):
if output['visible'][-1][1].endswith(''):
output['visible'][-1][1] = output['visible'][-1][1][:-1]

output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state, is_chat=True)
Expand All @@ -378,7 +378,7 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False, loading_mess
if is_stream:
yield output

if output['visible'][-1][1].endswith(''):
if output['visible'][-1][1].endswith(''):
output['visible'][-1][1] = output['visible'][-1][1][:-1]

output['visible'][-1][1] = apply_extensions('output', output['visible'][-1][1], state, is_chat=True)
Expand Down
76 changes: 60 additions & 16 deletions modules/html_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,52 @@ def replace_blockquote(m):
return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '')


def add_long_list_class(html):
'''
Adds a long-list class to <ul> or <ol> containing long <li> items.
These will receive a smaller margin/padding in the CSS.
'''

# Helper function to check if a tag is within <pre> or <code>
def is_within_block(start_idx, end_idx, block_matches):
return any(start < start_idx < end or start < end_idx < end for start, end in block_matches)

# Find all <pre>...</pre> and <code>...</code> blocks
pre_blocks = [(m.start(), m.end()) for m in re.finditer(r'<pre.*?>.*?</pre>', html, re.DOTALL)]
code_blocks = [(m.start(), m.end()) for m in re.finditer(r'<code.*?>.*?</code>', html, re.DOTALL)]
all_blocks = pre_blocks + code_blocks

# Pattern to find <ul>...</ul> and <ol>...</ol> blocks and their contents
list_pattern = re.compile(r'(<[uo]l.*?>)(.*?)(</[uo]l>)', re.DOTALL)
li_pattern = re.compile(r'<li.*?>(.*?)</li>', re.DOTALL)

def process_list(match):
start_idx, end_idx = match.span()
if is_within_block(start_idx, end_idx, all_blocks):
return match.group(0) # Leave the block unchanged if within <pre> or <code>

opening_tag = match.group(1)
list_content = match.group(2)
closing_tag = match.group(3)

# Find all list items within this list
li_matches = li_pattern.finditer(list_content)
has_long_item = any(len(li_match.group(1).strip()) > 224 for li_match in li_matches)

if has_long_item:
# Add class="long-list" to the opening tag if it doesn't already have a class
if 'class=' not in opening_tag:
opening_tag = opening_tag[:-1] + ' class="long-list">'
else:
# If there's already a class, append long-list to it
opening_tag = re.sub(r'class="([^"]*)"', r'class="\1 long-list"', opening_tag)

return opening_tag + list_content + closing_tag

# Process HTML and replace list blocks
return list_pattern.sub(process_list, html)


@functools.lru_cache(maxsize=None)
def convert_to_markdown(string):

Expand Down Expand Up @@ -104,7 +150,6 @@ def convert_to_markdown(string):
result = ''
is_code = False
is_latex = False
previous_line_empty = True

for line in string.split('\n'):
stripped_line = line.strip()
Expand All @@ -122,20 +167,16 @@ def convert_to_markdown(string):
elif stripped_line.endswith('\\\\]'):
is_latex = False

# Preserve indentation for lists and code blocks
if stripped_line.startswith('-') or stripped_line.startswith('*') or stripped_line.startswith('+') or stripped_line.startswith('>') or re.match(r'\d+\.', stripped_line):
result += line + '\n'
previous_line_empty = False
elif is_code or is_latex or line.startswith('|'):
result += line + '\n'
previous_line_empty = False
else:
if previous_line_empty:
result += line.strip() + '\n'
else:
result += line.strip() + '\n\n'
result += line

previous_line_empty = stripped_line == ''
# Don't add an extra \n for code, LaTeX, or tables
if is_code or is_latex or line.startswith('|'):
result += '\n'
# Also don't add an extra \n for lists
elif stripped_line.startswith('-') or stripped_line.startswith('*') or stripped_line.startswith('+') or stripped_line.startswith('>') or re.match(r'\d+\.', stripped_line):
result += '\n'
else:
result += ' \n'

result = result.strip()
if is_code:
Expand All @@ -154,20 +195,23 @@ def convert_to_markdown(string):
result = re.sub(list_item_pattern, r'\g<1> ' + delete_str, result)

# Convert to HTML using markdown
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'], tab_length=2)
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])

# Remove the delete string from the HTML output
pos = html_output.rfind(delete_str)
if pos > -1:
html_output = html_output[:pos] + html_output[pos + len(delete_str):]
else:
# Convert to HTML using markdown
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'], tab_length=2)
html_output = markdown.markdown(result, extensions=['fenced_code', 'tables'])

# Unescape code blocks
pattern = re.compile(r'<code[^>]*>(.*?)</code>', re.DOTALL)
html_output = pattern.sub(lambda x: html.unescape(x.group()), html_output)

# Add "long-list" class to <ul> or <ol> containing a long <li> item
html_output = add_long_list_class(html_output)

return html_output


Expand Down
1 change: 0 additions & 1 deletion modules/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
'compress_pos_emb',
'disable_exllama',
'disable_exllamav2',
'transformers_info',
],
'llama.cpp': [
'n_ctx',
Expand Down
Loading

0 comments on commit 88a6331

Please sign in to comment.