POLRAMBORA commited on
Commit
b596d5c
·
verified ·
1 Parent(s): 3d7ee6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -43
app.py CHANGED
@@ -6,6 +6,7 @@ import markdown
6
  from markdown.extensions.codehilite import CodeHiliteExtension
7
  import markdown.extensions.fenced_code
8
  import mistune
 
9
 
10
  API_URL = "https://host.palple.polrambora.com/pmsq"
11
 
@@ -123,60 +124,51 @@ def render_message(history):
123
  <div id="chatbox-container" class="chatbox" style="height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;">
124
  <div id="messages" style="display: block; margin-bottom: 10px;">"""
125
 
126
- seen_messages = set()
 
 
 
 
 
 
 
 
 
 
127
  for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
128
- if user_message and user_message not in seen_messages:
129
- seen_messages.add(user_message)
130
- user_message_html = escape_and_format(user_message)
 
 
 
 
131
  messages_html += f"""
132
- <div style='display: flex; align-items: center; margin-bottom: 10px;'>
133
- <img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
134
- <span style='color: white; white-space: pre-wrap;'>{user_message_html}</span>
135
  </div>"""
136
 
137
- if assistant_message and assistant_message not in seen_messages:
138
- seen_messages.add(assistant_message)
139
- assistant_message_html = escape_and_format(assistant_message)
 
 
 
 
140
  messages_html += f"""
141
- <div style='display: flex; align-items: center; margin-bottom: 10px;'>
142
- <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
143
- <span style='color: white; white-space: pre-wrap;'>{assistant_message_html}</span>
144
  </div>"""
145
 
146
  messages_html += "</div></div>"
147
  return messages_html
148
 
149
 
150
- def escape_and_format(text):
151
- allowed_tags = {'<b>', '</b>', '<i>', '</i>', '<h1>', '</h1>', '<h2>', '</h2>', '<h3>', '</h3>',
152
- '<code>', '</code>', '<br>', '<ul>', '</ul>', '<li>', '</li>', '<blockquote>', '</blockquote>', '<p>', '</p>'}
153
-
154
- markdown = mistune.create_markdown(renderer="html")
155
-
156
- html = markdown(text)
157
-
158
- filtered_html = ""
159
- i = 0
160
- while i < len(html):
161
- if html[i] == '<':
162
- tag_end = html.find('>', i) + 1
163
- if tag_end == 0:
164
- filtered_html += f"&#{ord(html[i])};"
165
- i += 1
166
- continue
167
-
168
- tag = html[i:tag_end]
169
- if tag in allowed_tags:
170
- filtered_html += tag
171
- else:
172
- filtered_html += ''.join(f"&#{ord(c)};" for c in tag)
173
- i = tag_end
174
- else:
175
- filtered_html += f"&#{ord(html[i])};"
176
- i += 1
177
-
178
- return filtered_html
179
-
180
  css="""
181
  .chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
182
  """
 
6
  from markdown.extensions.codehilite import CodeHiliteExtension
7
  import markdown.extensions.fenced_code
8
  import mistune
9
+ import bleach
10
 
11
  API_URL = "https://host.palple.polrambora.com/pmsq"
12
 
 
124
  <div id="chatbox-container" class="chatbox" style="height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;">
125
  <div id="messages" style="display: block; margin-bottom: 10px;">"""
126
 
127
+ seen_messages = set() # Track (role, message) pairs to avoid duplicates
128
+
129
+ allowed_tags = [
130
+ 'p', 'strong', 'em', 'ul', 'ol', 'li', 'a', 'code', 'pre', 'br', 'blockquote', 'hr', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img'
131
+ ]
132
+ allowed_attributes = {
133
+ '*': ['class', 'style'],
134
+ 'a': ['href', 'title'],
135
+ 'img': ['src', 'alt', 'title', 'width', 'height']
136
+ }
137
+
138
  for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
139
+ if user_message and ("user", user_message) not in seen_messages:
140
+ seen_messages.add(("user", user_message))
141
+ user_message_html = markdown.markdown(
142
+ escape_html(user_message),
143
+ extensions=["fenced_code", "codehilite"]
144
+ )
145
+ user_message_html = bleach.clean(user_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True)
146
  messages_html += f"""
147
+ <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
148
+ <img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
149
+ <div style='color: white; white-space: pre-wrap;'>{user_message_html}</div>
150
  </div>"""
151
 
152
+ if assistant_message and ("assistant", assistant_message) not in seen_messages:
153
+ seen_messages.add(("assistant", assistant_message))
154
+ assistant_message_html = markdown.markdown(
155
+ escape_html(assistant_message),
156
+ extensions=["fenced_code", "codehilite"]
157
+ )
158
+ assistant_message_html = bleach.clean(assistant_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True)
159
  messages_html += f"""
160
+ <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
161
+ <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
162
+ <div style='color: white; white-space: pre-wrap;'>{assistant_message_html}</div>
163
  </div>"""
164
 
165
  messages_html += "</div></div>"
166
  return messages_html
167
 
168
 
169
+ def escape_html(unsafe_text):
170
+ escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
171
+ return escaped_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  css="""
173
  .chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
174
  """