Spaces:
Runtime error
Runtime error
Update helper.py
Browse files
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 |
-
|
101 |
-
|
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]
|
141 |
-
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(":", ":")
|
154 |
+
pattern = r'`([^`]*):([^`]*)`|```([^`]*):([^`]*)```'
|
155 |
+
text = re.sub(pattern, lambda m: m.group(0).replace(':', ':') if ':' in m.group(0) else m.group(0),
|
156 |
+
text)
|
157 |
return text
|