Spaces:
Sleeping
Sleeping
Keldos
commited on
Commit
·
0e763ee
1
Parent(s):
856fe04
fix: 修复显示raw message中latex也被渲染的问题
Browse files- ChuanhuChatbot.py +1 -1
- assets/custom.js +7 -16
- modules/utils.py +4 -3
ChuanhuChatbot.py
CHANGED
@@ -52,7 +52,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
52 |
with gr.Row(equal_height=True):
|
53 |
with gr.Column(scale=5):
|
54 |
with gr.Row():
|
55 |
-
chatbot = gr.Chatbot(label="Chuanhu Chat", elem_id="chuanhu_chatbot", latex_delimiters=latex_delimiters_set, height=700
|
56 |
with gr.Row():
|
57 |
with gr.Column(min_width=225, scale=12):
|
58 |
user_input = gr.Textbox(
|
|
|
52 |
with gr.Row(equal_height=True):
|
53 |
with gr.Column(scale=5):
|
54 |
with gr.Row():
|
55 |
+
chatbot = gr.Chatbot(label="Chuanhu Chat", elem_id="chuanhu_chatbot", latex_delimiters=latex_delimiters_set, height=700)
|
56 |
with gr.Row():
|
57 |
with gr.Column(min_width=225, scale=12):
|
58 |
user_input = gr.Textbox(
|
assets/custom.js
CHANGED
@@ -407,7 +407,7 @@ function setSliderRange() {
|
|
407 |
function addChuanhuButton(botElement) {
|
408 |
var rawMessage = botElement.querySelector('.raw-message');
|
409 |
var mdMessage = botElement.querySelector('.md-message');
|
410 |
-
var gradioCopyMsgBtn = botElement.querySelector('div.icon-button>button[title="copy"]'); // 获取 gradio 的 copy button,它可以读取真正的原始 message
|
411 |
if (!rawMessage) {
|
412 |
var buttons = botElement.querySelectorAll('button.chuanhu-btn');
|
413 |
for (var i = 0; i < buttons.length; i++) {
|
@@ -433,20 +433,7 @@ function addChuanhuButton(botElement) {
|
|
433 |
const textToCopy = rawMessage.innerText;
|
434 |
try {
|
435 |
if ("clipboard" in navigator) {
|
436 |
-
|
437 |
-
try {
|
438 |
-
// try to copy from gradio's clipboard, which is really raw message
|
439 |
-
const gradio_clipboard_content = await navigator.clipboard.readText();
|
440 |
-
const regex = /<!-- SOO IN MESSAGE --><div class="really-raw hideM">([\s\S]*?)\n<\/div><!-- EOO IN MESSAGE -->/;
|
441 |
-
const real_raw_message = gradio_clipboard_content.match(regex)[1];
|
442 |
-
const after_agent_prefix = str.replace(/<!-- S O PREFIX --><p class="agent-prefix">(.+?)<\/p><!-- E O PREFIX -->/g, '$1\n');
|
443 |
-
await navigator.clipboard.writeText(after_agent_prefix)
|
444 |
-
// console.log("Copied from gradio's clipboard");
|
445 |
-
} catch (error) {
|
446 |
-
await navigator.clipboard.writeText(textToCopy);
|
447 |
-
// console.log("Copied from rawtext clipboard");
|
448 |
-
}
|
449 |
-
// await navigator.clipboard.writeText(textToCopy);
|
450 |
copyButton.innerHTML = copiedIcon;
|
451 |
setTimeout(() => {
|
452 |
copyButton.innerHTML = copyIcon;
|
@@ -501,7 +488,11 @@ function renderMarkdownText(message) {
|
|
501 |
}
|
502 |
function removeMarkdownText(message) {
|
503 |
var rawDiv = message.querySelector('.raw-message');
|
504 |
-
if (rawDiv)
|
|
|
|
|
|
|
|
|
505 |
var mdDiv = message.querySelector('.md-message');
|
506 |
if (mdDiv) mdDiv.classList.add('hideM');
|
507 |
}
|
|
|
407 |
function addChuanhuButton(botElement) {
|
408 |
var rawMessage = botElement.querySelector('.raw-message');
|
409 |
var mdMessage = botElement.querySelector('.md-message');
|
410 |
+
// var gradioCopyMsgBtn = botElement.querySelector('div.icon-button>button[title="copy"]'); // 获取 gradio 的 copy button,它可以读取真正的原始 message
|
411 |
if (!rawMessage) {
|
412 |
var buttons = botElement.querySelectorAll('button.chuanhu-btn');
|
413 |
for (var i = 0; i < buttons.length; i++) {
|
|
|
433 |
const textToCopy = rawMessage.innerText;
|
434 |
try {
|
435 |
if ("clipboard" in navigator) {
|
436 |
+
await navigator.clipboard.writeText(textToCopy);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
copyButton.innerHTML = copiedIcon;
|
438 |
setTimeout(() => {
|
439 |
copyButton.innerHTML = copyIcon;
|
|
|
488 |
}
|
489 |
function removeMarkdownText(message) {
|
490 |
var rawDiv = message.querySelector('.raw-message');
|
491 |
+
if (rawDiv) {
|
492 |
+
rawPre = rawDiv.querySelector('pre');
|
493 |
+
if (rawPre) rawDiv.innerHTML = rawPre.innerHTML;
|
494 |
+
rawDiv.classList.remove('hideM');
|
495 |
+
}
|
496 |
var mdDiv = message.querySelector('.md-message');
|
497 |
if (mdDiv) mdDiv.classList.add('hideM');
|
498 |
}
|
modules/utils.py
CHANGED
@@ -235,8 +235,8 @@ def convert_bot_before_marked(chat_message):
|
|
235 |
if '<div class="md-message">' in chat_message:
|
236 |
return chat_message
|
237 |
else:
|
238 |
-
raw = f'<div class="raw-message hideM">{clip_rawtext(chat_message)}</div>'
|
239 |
-
really_raw = f'{START_OF_OUTPUT_MARK}<div class="really-raw hideM">{clip_rawtext(chat_message, need_escape=False)}\n</div>{END_OF_OUTPUT_MARK}'
|
240 |
|
241 |
code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
|
242 |
code_blocks = code_block_pattern.findall(chat_message)
|
@@ -250,7 +250,7 @@ def convert_bot_before_marked(chat_message):
|
|
250 |
result.append(code)
|
251 |
result = "".join(result)
|
252 |
md = f'<div class="md-message">{result}\n</div>'
|
253 |
-
return raw + md
|
254 |
|
255 |
def convert_user_before_marked(chat_message):
|
256 |
if '<div class="user-message">' in chat_message:
|
@@ -283,6 +283,7 @@ def escape_markdown(text):
|
|
283 |
'|': '|',
|
284 |
'$': '$',
|
285 |
':': ':',
|
|
|
286 |
}
|
287 |
text = text.replace(' ', ' ')
|
288 |
return ''.join(escape_chars.get(c, c) for c in text)
|
|
|
235 |
if '<div class="md-message">' in chat_message:
|
236 |
return chat_message
|
237 |
else:
|
238 |
+
raw = f'<div class="raw-message hideM"><pre>{clip_rawtext(chat_message)}</pre></div>'
|
239 |
+
# really_raw = f'{START_OF_OUTPUT_MARK}<div class="really-raw hideM">{clip_rawtext(chat_message, need_escape=False)}\n</div>{END_OF_OUTPUT_MARK}'
|
240 |
|
241 |
code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
|
242 |
code_blocks = code_block_pattern.findall(chat_message)
|
|
|
250 |
result.append(code)
|
251 |
result = "".join(result)
|
252 |
md = f'<div class="md-message">{result}\n</div>'
|
253 |
+
return raw + md
|
254 |
|
255 |
def convert_user_before_marked(chat_message):
|
256 |
if '<div class="user-message">' in chat_message:
|
|
|
283 |
'|': '|',
|
284 |
'$': '$',
|
285 |
':': ':',
|
286 |
+
'\n': '<br>',
|
287 |
}
|
288 |
text = text.replace(' ', ' ')
|
289 |
return ''.join(escape_chars.get(c, c) for c in text)
|