Keldos commited on
Commit
1c70651
·
1 Parent(s): 438a056

BREAKING: 默认渲染LaTeX公式

Browse files

- 去除了页面中的勾选框,现在仅能在config中配置渲染与否
- 重写了js引入mathjax方法

ChuanhuChatbot.py CHANGED
@@ -93,9 +93,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
93
  autogpt_mode = gr.Checkbox(label=i18n("AutoGPT 模式"), value=True)
94
  single_turn_checkbox = gr.Checkbox(label=i18n("单轮对话"), value=False)
95
  use_websearch_checkbox = gr.Checkbox(label=i18n("使用在线搜索"), value=False)
96
- render_latex_checkbox = gr.Checkbox(
97
- label=i18n("渲染LaTeX公式"), value=render_latex, interactive=True, elem_id="render_latex_checkbox"
98
- )
99
  language_select_dropdown = gr.Dropdown(
100
  label=i18n("选择回复语言(针对搜索&索引功能)"),
101
  choices=REPLY_LANGUAGES,
 
93
  autogpt_mode = gr.Checkbox(label=i18n("AutoGPT 模式"), value=True)
94
  single_turn_checkbox = gr.Checkbox(label=i18n("单轮对话"), value=False)
95
  use_websearch_checkbox = gr.Checkbox(label=i18n("使用在线搜索"), value=False)
96
+ # render_latex_checkbox = gr.Checkbox(label=i18n("渲染LaTeX公式"), value=render_latex, interactive=True, elem_id="render_latex_checkbox")
 
 
97
  language_select_dropdown = gr.Dropdown(
98
  label=i18n("选择回复语言(针对搜索&索引功能)"),
99
  choices=REPLY_LANGUAGES,
assets/custom.js CHANGED
@@ -17,7 +17,7 @@ var chatbotWrap = null;
17
  var apSwitch = null;
18
  var empty_botton = null;
19
  var messageBotDivs = null;
20
- var renderLatex = null;
21
  var loginUserForm = null;
22
  var logginUser = null;
23
 
@@ -51,7 +51,7 @@ function gradioLoaded(mutations) {
51
  chatbot = document.querySelector('#chuanhu_chatbot');
52
  chatbotWrap = document.querySelector('#chuanhu_chatbot > .wrap');
53
  apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
54
- renderLatex = document.querySelector("#render_latex_checkbox > label > input");
55
  empty_botton = document.getElementById("empty_btn")
56
 
57
  if (loginUserForm) {
@@ -80,10 +80,10 @@ function gradioLoaded(mutations) {
80
  }
81
  setChatbotScroll();
82
  }
83
- if (renderLatex) { // renderLatex 加载出来了没?
84
- shouldRenderLatex = renderLatex.checked;
85
- updateMathJax();
86
- }
87
  if (empty_botton) {
88
  emptyHistory();
89
  }
@@ -98,7 +98,7 @@ function webLocale() {
98
  var forViewStyle = document.createElement('style');
99
  forViewStyle.innerHTML = '.wrap>.history-message>:last-child::after { content: "' + forView + '"!important; }';
100
  document.head.appendChild(forViewStyle);
101
- console.log("added forViewStyle", forView);
102
  }
103
  }
104
 
@@ -439,7 +439,7 @@ function renderMathJax() {
439
  }
440
  }
441
  mathjaxUpdated = true;
442
- console.log("MathJax Rendered")
443
  }
444
 
445
  function removeMathjax() {
@@ -455,22 +455,21 @@ function removeMathjax() {
455
  }
456
 
457
  function updateMathJax() {
458
- renderLatex.addEventListener("change", function() {
459
- shouldRenderLatex = renderLatex.checked;
460
- // console.log(shouldRenderLatex)
461
- if (!mathjaxUpdated) {
462
- if (shouldRenderLatex) {
463
- renderMathJax();
464
- } else {
465
- console.log("MathJax Disabled")
466
- removeMathjax();
467
- }
468
- } else {
469
- if (!shouldRenderLatex) {
470
- mathjaxUpdated = false; // reset
471
- }
472
- }
473
- });
474
  if (shouldRenderLatex && !mathjaxUpdated) {
475
  renderMathJax();
476
  }
@@ -534,7 +533,7 @@ var loadhistorytime = 0; // for debugging
534
  function saveHistoryHtml() {
535
  var historyHtml = document.querySelector('#chuanhu_chatbot > .wrap');
536
  localStorage.setItem('chatHistory', historyHtml.innerHTML);
537
- console.log("History Saved")
538
  historyLoaded = false;
539
  }
540
  function loadHistoryHtml() {
@@ -595,6 +594,7 @@ observer.observe(targetNode, { childList: true, subtree: true });
595
  window.addEventListener("DOMContentLoaded", function () {
596
  isInIframe = (window.self !== window.top);
597
  historyLoaded = false;
 
598
  });
599
  window.addEventListener('resize', setChatbotHeight);
600
  window.addEventListener('scroll', setChatbotHeight);
 
17
  var apSwitch = null;
18
  var empty_botton = null;
19
  var messageBotDivs = null;
20
+ // var renderLatex = null;
21
  var loginUserForm = null;
22
  var logginUser = null;
23
 
 
51
  chatbot = document.querySelector('#chuanhu_chatbot');
52
  chatbotWrap = document.querySelector('#chuanhu_chatbot > .wrap');
53
  apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
54
+ // renderLatex = document.querySelector("#render_latex_checkbox > label > input");
55
  empty_botton = document.getElementById("empty_btn")
56
 
57
  if (loginUserForm) {
 
80
  }
81
  setChatbotScroll();
82
  }
83
+ // if (renderLatex) { // renderLatex 加载出来了没?
84
+ // shouldRenderLatex = renderLatex.checked;
85
+ // updateMathJax();
86
+ // }
87
  if (empty_botton) {
88
  emptyHistory();
89
  }
 
98
  var forViewStyle = document.createElement('style');
99
  forViewStyle.innerHTML = '.wrap>.history-message>:last-child::after { content: "' + forView + '"!important; }';
100
  document.head.appendChild(forViewStyle);
101
+ // console.log("added forViewStyle", forView);
102
  }
103
  }
104
 
 
439
  }
440
  }
441
  mathjaxUpdated = true;
442
+ // console.log("MathJax Rendered")
443
  }
444
 
445
  function removeMathjax() {
 
455
  }
456
 
457
  function updateMathJax() {
458
+ // renderLatex.addEventListener("change", function() {
459
+ // shouldRenderLatex = renderLatex.checked;
460
+ // if (!mathjaxUpdated) {
461
+ // if (shouldRenderLatex) {
462
+ // renderMathJax();
463
+ // } else {
464
+ // console.log("MathJax Disabled")
465
+ // removeMathjax();
466
+ // }
467
+ // } else {
468
+ // if (!shouldRenderLatex) {
469
+ // mathjaxUpdated = false; // reset
470
+ // }
471
+ // }
472
+ // });
 
473
  if (shouldRenderLatex && !mathjaxUpdated) {
474
  renderMathJax();
475
  }
 
533
  function saveHistoryHtml() {
534
  var historyHtml = document.querySelector('#chuanhu_chatbot > .wrap');
535
  localStorage.setItem('chatHistory', historyHtml.innerHTML);
536
+ // console.log("History Saved")
537
  historyLoaded = false;
538
  }
539
  function loadHistoryHtml() {
 
594
  window.addEventListener("DOMContentLoaded", function () {
595
  isInIframe = (window.self !== window.top);
596
  historyLoaded = false;
597
+ shouldRenderLatex = !!document.querySelector('script[src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"]');
598
  });
599
  window.addEventListener('resize', setChatbotHeight);
600
  window.addEventListener('scroll', setChatbotHeight);
assets/external-scripts.js CHANGED
@@ -1,21 +1,2 @@
1
 
2
- // window.MathJax = {
3
- // tex: {
4
- // inlineMath: [['$', '$'], ['\\(', '\\)']]
5
- // },
6
- // svg: {
7
- // fontCache: 'global'
8
- // }
9
- // };
10
-
11
- (function () {
12
- var script = document.createElement('script');
13
- script.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-MML-AM_CHTML";
14
- script.async = true;
15
- document.head.appendChild(script);
16
-
17
- var config = document.createElement("script");
18
- config.type = "text/x-mathjax-config";
19
- config.text = "MathJax.Hub.Config({skipStartupTypeset: true, tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']],displayMath: [['$$','$$'], ['\\\\[','\\\\]']]}});";
20
- document.head.appendChild(config);
21
- })();
 
1
 
2
+ // external javascript here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
config_example.json CHANGED
@@ -10,7 +10,7 @@
10
  // "https_proxy": "http://127.0.0.1:1079",
11
  // "http_proxy": "http://127.0.0.1:1079",
12
  // 是否默认渲染LaTeX
13
- "render_latex": false,
14
  "users": [], // 用户列表,[[用户名1, 密码1], [用户名2, 密码2], ...]
15
  "local_embedding": false, //是否在本地编制索引
16
  "hide_history_when_not_logged_in": false, //未登录情况下是否不展示对话历史
 
10
  // "https_proxy": "http://127.0.0.1:1079",
11
  // "http_proxy": "http://127.0.0.1:1079",
12
  // 是否默认渲染LaTeX
13
+ "render_latex": true,
14
  "users": [], // 用户列表,[[用户名1, 密码1], [用户名2, 密码2], ...]
15
  "local_embedding": false, //是否在本地编制索引
16
  "hide_history_when_not_logged_in": false, //未登录情况下是否不展示对话历史
modules/config.py CHANGED
@@ -76,7 +76,7 @@ my_api_key = os.environ.get("OPENAI_API_KEY", my_api_key)
76
  xmchat_api_key = config.get("xmchat_api_key", "")
77
  os.environ["XMCHAT_API_KEY"] = xmchat_api_key
78
 
79
- render_latex = config.get("render_latex", False)
80
 
81
  if render_latex:
82
  os.environ["RENDER_LATEX"] = "yes"
 
76
  xmchat_api_key = config.get("xmchat_api_key", "")
77
  os.environ["XMCHAT_API_KEY"] = xmchat_api_key
78
 
79
+ render_latex = config.get("render_latex", True)
80
 
81
  if render_latex:
82
  os.environ["RENDER_LATEX"] = "yes"
modules/overwrites.py CHANGED
@@ -8,7 +8,7 @@ from gradio_client import utils as client_utils
8
 
9
  from modules.presets import *
10
  from modules.llama_func import *
11
-
12
 
13
  def compact_text_chunks(self, prompt: Prompt, text_chunks: List[str]) -> List[str]:
14
  logging.debug("Compacting text chunks...🚀🚀🚀")
@@ -85,6 +85,11 @@ with open("./assets/custom.js", "r", encoding="utf-8") as f, \
85
  def reload_javascript():
86
  print("Reloading javascript...")
87
  js = f'<script>{customJS}</script><script async>{externalScripts}</script>'
 
 
 
 
 
88
  def template_response(*args, **kwargs):
89
  res = GradioTemplateResponseOriginal(*args, **kwargs)
90
  res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))
 
8
 
9
  from modules.presets import *
10
  from modules.llama_func import *
11
+ from modules.config import render_latex
12
 
13
  def compact_text_chunks(self, prompt: Prompt, text_chunks: List[str]) -> List[str]:
14
  logging.debug("Compacting text chunks...🚀🚀🚀")
 
85
  def reload_javascript():
86
  print("Reloading javascript...")
87
  js = f'<script>{customJS}</script><script async>{externalScripts}</script>'
88
+ if render_latex:
89
+ js += """\
90
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-MML-AM_CHTML"></script>
91
+ <script type="text/x-mathjax-config">MathJax.Hub.Config({skipStartupTypeset: false, tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']],displayMath: [['$$','$$'], ['\\[','\\]']]}});</script>
92
+ """
93
  def template_response(*args, **kwargs):
94
  res = GradioTemplateResponseOriginal(*args, **kwargs)
95
  res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))