JunchuanYu commited on
Commit
f11d1ea
·
1 Parent(s): 4b8d068

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +79 -30
utils.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import logging
2
  import json
3
  import os
@@ -13,7 +14,8 @@ from markdown import markdown
13
  from pygments import highlight
14
  from pygments.lexers import get_lexer_by_name
15
  from pygments.formatters import HtmlFormatter
16
- from theme_dropdown import create_theme_dropdown # noqa: F401
 
17
 
18
 
19
  use_websearch_checkbox=False
@@ -59,6 +61,28 @@ The default model role of the app is the original assistant of ChatGPT, but you
59
 
60
  MODELS = ["gpt-3.5-turbo", "gpt-3.5-turbo-0301",]
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  if TYPE_CHECKING:
63
  from typing import TypedDict
64
 
@@ -73,7 +97,6 @@ def count_token(message):
73
  length = len(encoding.encode(input_str))
74
  return length
75
 
76
-
77
  def markdown_to_html_with_syntax_highlight(md_str):
78
  def replacer(match):
79
  lang = match.group(1) or "text"
@@ -95,7 +118,6 @@ def markdown_to_html_with_syntax_highlight(md_str):
95
  html_str = markdown(md_str)
96
  return html_str
97
 
98
-
99
  def normalize_markdown(md_text: str) -> str:
100
  lines = md_text.split("\n")
101
  normalized_lines = []
@@ -119,24 +141,6 @@ def normalize_markdown(md_text: str) -> str:
119
 
120
  return "\n".join(normalized_lines)
121
 
122
- def postprocess(
123
- self, y: List[Tuple[str | None, str | None]]
124
- ) -> List[Tuple[str | None, str | None]]:
125
- """
126
- Parameters:
127
- y: List of tuples representing the message and response pairs. Each message and response should be a string, which may be in Markdown format.
128
- Returns:
129
- List of tuples representing the message and response. Each message and response will be a string of HTML.
130
- """
131
- if y is None or y == []:
132
- return []
133
- tag_regex = re.compile(r"^<\w+>[^<]+</\w+>")
134
- if tag_regex.search(y[-1][1]):
135
- y[-1] = (convert_user(y[-1][0]), y[-1][1])
136
- else:
137
- y[-1] = (convert_user(y[-1][0]), convert_mdtext(y[-1][1]))
138
- return y
139
-
140
  def convert_mdtext(md_text):
141
  code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
142
  inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
@@ -183,7 +187,6 @@ def construct_assistant(text):
183
  def construct_token_message(token, stream=False):
184
  return f"Token count: {token}"
185
 
186
-
187
  def save_file(filename, system, history, chatbot):
188
  logging.info("saving......")
189
  os.makedirs(HISTORY_DIR, exist_ok=True)
@@ -200,7 +203,6 @@ def save_file(filename, system, history, chatbot):
200
  f.write(md_s)
201
  return os.path.join(HISTORY_DIR, filename)
202
 
203
-
204
  def save_chat_history(filename, system, history, chatbot):
205
  if filename == "":
206
  return
@@ -208,7 +210,6 @@ def save_chat_history(filename, system, history, chatbot):
208
  filename += ".json"
209
  return save_file(filename, system, history, chatbot)
210
 
211
-
212
  def export_markdown(filename, system, history, chatbot):
213
  if filename == "":
214
  return
@@ -216,7 +217,6 @@ def export_markdown(filename, system, history, chatbot):
216
  filename += ".md"
217
  return save_file(filename, system, history, chatbot)
218
 
219
-
220
  def load_chat_history(filename, system, history, chatbot):
221
  if type(filename) != str:
222
  filename = filename.name
@@ -239,7 +239,6 @@ def load_chat_history(filename, system, history, chatbot):
239
  except FileNotFoundError:
240
  return filename, system, history, chatbot
241
 
242
-
243
  def load_template(filename, mode=0):
244
  lines = []
245
  logging.info("Loading template...")
@@ -265,7 +264,6 @@ def load_template(filename, mode=0):
265
  def sorted_by_pinyin(list):
266
  return sorted(list, key=lambda char: lazy_pinyin(char)[0][0])
267
 
268
-
269
  def get_template_content(templates, selection, original_system_prompt):
270
  logging.info(f"Prompt: {selection}")
271
  try:
@@ -273,16 +271,13 @@ def get_template_content(templates, selection, original_system_prompt):
273
  except:
274
  return original_system_prompt
275
 
276
-
277
  def reset_state():
278
  logging.info("Reset")
279
  return [], [], [], construct_token_message(0)
280
 
281
-
282
  def reset_textbox():
283
  return gr.update(value="")
284
 
285
-
286
  def hide_middle_chars(s):
287
  if len(s) <= 8:
288
  return s
@@ -297,3 +292,57 @@ def submit_key(key):
297
  msg = f"API-Key: {hide_middle_chars(key)}"
298
  logging.info(msg)
299
  return key, msg
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
  import logging
3
  import json
4
  import os
 
14
  from pygments import highlight
15
  from pygments.lexers import get_lexer_by_name
16
  from pygments.formatters import HtmlFormatter
17
+ from theme_dropdown import create_theme_dropdown
18
+ from gradio.themes.utils import ThemeAsset
19
 
20
 
21
  use_websearch_checkbox=False
 
61
 
62
  MODELS = ["gpt-3.5-turbo", "gpt-3.5-turbo-0301",]
63
 
64
+
65
+
66
+ def postprocess(
67
+ self, y: List[Tuple[str | None, str | None]]
68
+ ) -> List[Tuple[str | None, str | None]]:
69
+ """
70
+ Parameters:
71
+ y: List of tuples representing the message and response pairs. Each message and response should be a string, which may be in Markdown format.
72
+ Returns:
73
+ List of tuples representing the message and response. Each message and response will be a string of HTML.
74
+ """
75
+ if y is None:
76
+ return []
77
+ for i, (message, response) in enumerate(y):
78
+ y[i] = (
79
+ # None if message is None else markdown.markdown(message),
80
+ # None if response is None else markdown.markdown(response),
81
+ None if message is None else message,
82
+ None if response is None else mdtex2html.convert(response),
83
+ )
84
+ return y
85
+
86
  if TYPE_CHECKING:
87
  from typing import TypedDict
88
 
 
97
  length = len(encoding.encode(input_str))
98
  return length
99
 
 
100
  def markdown_to_html_with_syntax_highlight(md_str):
101
  def replacer(match):
102
  lang = match.group(1) or "text"
 
118
  html_str = markdown(md_str)
119
  return html_str
120
 
 
121
  def normalize_markdown(md_text: str) -> str:
122
  lines = md_text.split("\n")
123
  normalized_lines = []
 
141
 
142
  return "\n".join(normalized_lines)
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  def convert_mdtext(md_text):
145
  code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
146
  inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
 
187
  def construct_token_message(token, stream=False):
188
  return f"Token count: {token}"
189
 
 
190
  def save_file(filename, system, history, chatbot):
191
  logging.info("saving......")
192
  os.makedirs(HISTORY_DIR, exist_ok=True)
 
203
  f.write(md_s)
204
  return os.path.join(HISTORY_DIR, filename)
205
 
 
206
  def save_chat_history(filename, system, history, chatbot):
207
  if filename == "":
208
  return
 
210
  filename += ".json"
211
  return save_file(filename, system, history, chatbot)
212
 
 
213
  def export_markdown(filename, system, history, chatbot):
214
  if filename == "":
215
  return
 
217
  filename += ".md"
218
  return save_file(filename, system, history, chatbot)
219
 
 
220
  def load_chat_history(filename, system, history, chatbot):
221
  if type(filename) != str:
222
  filename = filename.name
 
239
  except FileNotFoundError:
240
  return filename, system, history, chatbot
241
 
 
242
  def load_template(filename, mode=0):
243
  lines = []
244
  logging.info("Loading template...")
 
264
  def sorted_by_pinyin(list):
265
  return sorted(list, key=lambda char: lazy_pinyin(char)[0][0])
266
 
 
267
  def get_template_content(templates, selection, original_system_prompt):
268
  logging.info(f"Prompt: {selection}")
269
  try:
 
271
  except:
272
  return original_system_prompt
273
 
 
274
  def reset_state():
275
  logging.info("Reset")
276
  return [], [], [], construct_token_message(0)
277
 
 
278
  def reset_textbox():
279
  return gr.update(value="")
280
 
 
281
  def hide_middle_chars(s):
282
  if len(s) <= 8:
283
  return s
 
292
  msg = f"API-Key: {hide_middle_chars(key)}"
293
  logging.info(msg)
294
  return key, msg
295
+
296
+
297
+ def create_theme_dropdown():
298
+ import gradio as gr
299
+
300
+ asset_path = pathlib.Path(__file__).parent / "themes"
301
+ themes = []
302
+ for theme_asset in os.listdir(str(asset_path)):
303
+ themes.append(
304
+ (ThemeAsset(theme_asset), gr.Theme.load(str(asset_path / theme_asset)))
305
+ )
306
+
307
+ def make_else_if(theme_asset):
308
+ return f"""
309
+ else if (theme == '{str(theme_asset[0].version)}') {{
310
+ var theme_css = `{theme_asset[1]._get_theme_css()}`
311
+ }}"""
312
+
313
+ head, tail = themes[0], themes[1:]
314
+ if_statement = f"""
315
+ if (theme == "{str(head[0].version)}") {{
316
+ var theme_css = `{head[1]._get_theme_css()}`
317
+ }} {" ".join(make_else_if(t) for t in tail)}
318
+ """
319
+
320
+ latest_to_oldest = sorted([t[0] for t in themes], key=lambda asset: asset.version)[
321
+ ::-1
322
+ ]
323
+ latest_to_oldest = [str(t.version) for t in latest_to_oldest]
324
+
325
+ component = gr.Dropdown(
326
+ choices=latest_to_oldest,
327
+ value=latest_to_oldest[0],
328
+ render=False,
329
+ label="Select Version",
330
+ ).style(container=False)
331
+
332
+ return (
333
+ component,
334
+ f"""
335
+ (theme) => {{
336
+ if (!document.querySelector('.theme-css')) {{
337
+ var theme_elem = document.createElement('style');
338
+ theme_elem.classList.add('theme-css');
339
+ document.head.appendChild(theme_elem);
340
+ }} else {{
341
+ var theme_elem = document.querySelector('.theme-css');
342
+ }}
343
+ {if_statement}
344
+ theme_elem.innerHTML = theme_css;
345
+ }}
346
+ """,
347
+ )
348
+