Pearx commited on
Commit
5600923
1 Parent(s): d5c98d3

Update helper.py

Browse files
Files changed (1) hide show
  1. helper.py +20 -7
helper.py CHANGED
@@ -69,6 +69,9 @@ def show_each_message(message: str, role: str, area=None):
69
  icon = gpt_svg
70
  name = gpt_name
71
  background_color = gpt_background_color
 
 
 
72
  area[0](f"\n<div class='avatar'>{icon}<h2>{name}:</h2></div>", unsafe_allow_html=True)
73
  area[1](f"""<div class='content-div' style='background-color: {background_color};'>\n\n{message}""",
74
  unsafe_allow_html=True)
@@ -96,9 +99,9 @@ def get_history_input(history: list, level: int) -> list:
96
 
97
 
98
  # 去除#号右边的空格
99
- def remove_hashtag_right__space(text: str) -> str:
100
- res = re.sub(r"(#+)\s*", r"\1", text)
101
- return res
102
 
103
 
104
  # 提取文本
@@ -117,6 +120,7 @@ def extract_chars(text: str, num: int) -> str:
117
  return chars
118
 
119
 
 
120
  def download_history(history: list):
121
  md_text = ""
122
  for msg in history:
@@ -137,8 +141,17 @@ def filename_correction(filename: str) -> str:
137
 
138
 
139
  def url_correction(text: str) -> str:
140
- pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+#]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
141
- links = re.findall(pattern, text)
142
- for link in links:
143
- text = text.replace(link, " " + link + " ")
 
 
 
 
 
 
 
 
 
144
  return text
 
69
  icon = gpt_svg
70
  name = gpt_name
71
  background_color = gpt_background_color
72
+ message = colon_correction(
73
+ url_correction(message)
74
+ )
75
  area[0](f"\n<div class='avatar'>{icon}<h2>{name}:</h2></div>", unsafe_allow_html=True)
76
  area[1](f"""<div class='content-div' style='background-color: {background_color};'>\n\n{message}""",
77
  unsafe_allow_html=True)
 
99
 
100
 
101
  # 去除#号右边的空格
102
+ # def remove_hashtag_right__space(text: str) -> str:
103
+ # text = re.sub(r"(#+)\s*", r"\1", text)
104
+ # return text
105
 
106
 
107
  # 提取文本
 
120
  return chars
121
 
122
 
123
+ @st.cache_data(max_entries=20, show_spinner=False)
124
  def download_history(history: list):
125
  md_text = ""
126
  for msg in history:
 
141
 
142
 
143
  def url_correction(text: str) -> str:
144
+ pattern = r'((?:http[s]?://|www\.)(?:[a-zA-Z0-9]|[$-_\~#!])+)'
145
+ text = re.sub(pattern, r' \g<1> ', text)
146
+ return text
147
+
148
+
149
+ # st的markdown会错误渲染英文引号加英文字符,例如 :abc
150
+ def colon_correction(text):
151
+ pattern = r':[a-zA-Z]'
152
+ if re.search(pattern, text):
153
+ text = text.replace(":", "&#58;")
154
+ pattern = r'`([^`]*)&#58;([^`]*)`|```([^`]*)&#58;([^`]*)```'
155
+ text = re.sub(pattern, lambda m: m.group(0).replace('&#58;', ':') if '&#58;' in m.group(0) else m.group(0),
156
+ text)
157
  return text