Keldos commited on
Commit
da0fe02
·
1 Parent(s): ca2c7ff

fix: 修正复制对话内容按钮可能获取渲染后内容的问题

Browse files

- 主要是解决latex复制原始输出的问题
- 使用了gradio 3.40.0自带的复制功能来修复

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, show_copy_button=True)
56
  with gr.Row():
57
  with gr.Column(min_width=225, scale=12):
58
  user_input = gr.Textbox(
assets/custom.css CHANGED
@@ -416,7 +416,7 @@ hr.append-display {
416
  }
417
 
418
  #submit_btn, #cancel_btn {
419
- height: 42px !important;
420
  }
421
  #submit_btn::before {
422
  content: url("data:image/svg+xml, %3Csvg width='21px' height='20px' viewBox='0 0 21 20' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Cg id='page' stroke='none' stroke-width='1' fill='none' fill-rule='evenodd'%3E %3Cg id='send' transform='translate(0.435849, 0.088463)' fill='%23FFFFFF' fill-rule='nonzero'%3E %3Cpath d='M0.579148261,0.0428666046 C0.301105539,-0.0961547561 -0.036517765,0.122307382 0.0032026237,0.420210298 L1.4927172,18.1553639 C1.5125774,18.4334066 1.79062012,18.5922882 2.04880264,18.4929872 L8.24518329,15.8913017 L11.6412765,19.7441794 C11.8597387,19.9825018 12.2370824,19.8832008 12.3165231,19.5852979 L13.9450591,13.4882182 L19.7839562,11.0255541 C20.0619989,10.8865327 20.0818591,10.4694687 19.7839562,10.3105871 L0.579148261,0.0428666046 Z M11.6138902,17.0883151 L9.85385903,14.7195502 L0.718169621,0.618812241 L12.69945,12.9346347 L11.6138902,17.0883151 Z' id='shape'%3E%3C/path%3E %3C/g%3E %3C/g%3E %3C/svg%3E");
@@ -530,7 +530,11 @@ ol:not(.options), ul:not(.options) {
530
  padding: 0 !important;
531
  white-space: pre-wrap;
532
  }
533
- .raw-message.hideM, .md-message.hideM {
 
 
 
 
534
  display: none;
535
  }
536
 
 
416
  }
417
 
418
  #submit_btn, #cancel_btn {
419
+ height: 40px !important;
420
  }
421
  #submit_btn::before {
422
  content: url("data:image/svg+xml, %3Csvg width='21px' height='20px' viewBox='0 0 21 20' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Cg id='page' stroke='none' stroke-width='1' fill='none' fill-rule='evenodd'%3E %3Cg id='send' transform='translate(0.435849, 0.088463)' fill='%23FFFFFF' fill-rule='nonzero'%3E %3Cpath d='M0.579148261,0.0428666046 C0.301105539,-0.0961547561 -0.036517765,0.122307382 0.0032026237,0.420210298 L1.4927172,18.1553639 C1.5125774,18.4334066 1.79062012,18.5922882 2.04880264,18.4929872 L8.24518329,15.8913017 L11.6412765,19.7441794 C11.8597387,19.9825018 12.2370824,19.8832008 12.3165231,19.5852979 L13.9450591,13.4882182 L19.7839562,11.0255541 C20.0619989,10.8865327 20.0818591,10.4694687 19.7839562,10.3105871 L0.579148261,0.0428666046 Z M11.6138902,17.0883151 L9.85385903,14.7195502 L0.718169621,0.618812241 L12.69945,12.9346347 L11.6138902,17.0883151 Z' id='shape'%3E%3C/path%3E %3C/g%3E %3C/g%3E %3C/svg%3E");
 
530
  padding: 0 !important;
531
  white-space: pre-wrap;
532
  }
533
+ .message .hideM {
534
+ display: none;
535
+ }
536
+
537
+ .message div.icon-button > button[title="copy"] {
538
  display: none;
539
  }
540
 
assets/custom.js CHANGED
@@ -405,10 +405,9 @@ function setSliderRange() {
405
  }
406
 
407
  function addChuanhuButton(botElement) {
408
- var rawMessage = null;
409
- var mdMessage = null;
410
- rawMessage = botElement.querySelector('.raw-message');
411
- mdMessage = botElement.querySelector('.md-message');
412
  if (!rawMessage) {
413
  var buttons = botElement.querySelectorAll('button.chuanhu-btn');
414
  for (var i = 0; i < buttons.length; i++) {
@@ -429,11 +428,24 @@ function addChuanhuButton(botElement) {
429
  copyButton.classList.add('copy-bot-btn');
430
  copyButton.setAttribute('aria-label', 'Copy');
431
  copyButton.innerHTML = copyIcon;
 
432
  copyButton.addEventListener('click', async () => {
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;
 
405
  }
406
 
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++) {
 
428
  copyButton.classList.add('copy-bot-btn');
429
  copyButton.setAttribute('aria-label', 'Copy');
430
  copyButton.innerHTML = copyIcon;
431
+
432
  copyButton.addEventListener('click', async () => {
433
  const textToCopy = rawMessage.innerText;
434
  try {
435
  if ("clipboard" in navigator) {
436
+ gradioCopyMsgBtn.click();
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
+ await navigator.clipboard.writeText(real_raw_message)
443
+ // console.log("Copied from gradio's clipboard");
444
+ } catch (error) {
445
+ await navigator.clipboard.writeText(textToCopy);
446
+ // console.log("Copied from rawtext clipboard");
447
+ }
448
+ // await navigator.clipboard.writeText(textToCopy);
449
  copyButton.innerHTML = copiedIcon;
450
  setTimeout(() => {
451
  copyButton.innerHTML = copyIcon;
modules/presets.py CHANGED
@@ -175,6 +175,8 @@ SUMMARIZE_PROMPT = """Write a concise summary of the following:
175
  CONCISE SUMMARY IN 中文:"""
176
 
177
  ALREADY_CONVERTED_MARK = "<!-- ALREADY CONVERTED BY PARSER. -->"
 
 
178
 
179
  small_and_beautiful_theme = gr.themes.Soft(
180
  primary_hue=gr.themes.Color(
 
175
  CONCISE SUMMARY IN 中文:"""
176
 
177
  ALREADY_CONVERTED_MARK = "<!-- ALREADY CONVERTED BY PARSER. -->"
178
+ START_OF_OUTPUT_MARK = "<!-- SOO IN MESSAGE -->"
179
+ END_OF_OUTPUT_MARK = "<!-- EOO IN MESSAGE -->"
180
 
181
  small_and_beautiful_theme = gr.themes.Soft(
182
  primary_hue=gr.themes.Color(
modules/utils.py CHANGED
@@ -221,6 +221,7 @@ def convert_bot_before_marked(chat_message):
221
  hr_pattern = r'\n\n<hr class="append-display no-in-raw" />(.*?)'
222
  hr_match = re.search(hr_pattern, chat_message, re.DOTALL)
223
  clip_hr = chat_message[:hr_match.start()] if hr_match else chat_message
 
224
  raw = f'<div class="raw-message hideM">{escape_markdown(clip_hr)}</div>'
225
  for non_code, code in zip(non_code_parts, code_blocks + [""]):
226
  if non_code.strip():
@@ -230,7 +231,8 @@ def convert_bot_before_marked(chat_message):
230
  result.append(code)
231
  result = "".join(result)
232
  md = f'<div class="md-message">{result}\n</div>'
233
- return raw + md
 
234
 
235
  def convert_user_before_marked(chat_message):
236
  if '<div class="user-message">' in chat_message:
 
221
  hr_pattern = r'\n\n<hr class="append-display no-in-raw" />(.*?)'
222
  hr_match = re.search(hr_pattern, chat_message, re.DOTALL)
223
  clip_hr = chat_message[:hr_match.start()] if hr_match else chat_message
224
+
225
  raw = f'<div class="raw-message hideM">{escape_markdown(clip_hr)}</div>'
226
  for non_code, code in zip(non_code_parts, code_blocks + [""]):
227
  if non_code.strip():
 
231
  result.append(code)
232
  result = "".join(result)
233
  md = f'<div class="md-message">{result}\n</div>'
234
+ really_raw = f'{START_OF_OUTPUT_MARK}<div class="really-raw hideM">{clip_hr}\n</div>{END_OF_OUTPUT_MARK}'
235
+ return raw + md + really_raw
236
 
237
  def convert_user_before_marked(chat_message):
238
  if '<div class="user-message">' in chat_message: