POLRAMBORA commited on
Commit
b7116ff
·
verified ·
1 Parent(s): 127e9d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -43
app.py CHANGED
@@ -119,69 +119,48 @@ def respond(message, api_key, max_tokens, top_p, temperature):
119
 
120
 
121
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  def render_message(history):
123
  messages_html = """
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()
128
-
129
- allowed_tags = [
130
- 'p', 'strong', 'em', 'ul', 'ol', 'li', 'a', 'code', 'pre', 'br', 'blockquote', 'hr',
131
- 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img'
132
- ]
133
- allowed_attributes = {
134
- '*': ['class', 'style'],
135
- 'a': ['href', 'title'],
136
- 'img': ['src', 'alt', 'title', 'width', 'height']
137
- }
138
-
139
- for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
140
- if user_message and ("user", user_message) not in seen_messages:
141
- seen_messages.add(("user", user_message))
142
-
143
- user_message_html = markdown.markdown(
144
- user_message,
145
- extensions=["fenced_code", "codehilite"],
146
- output_format="html5"
147
- )
148
-
149
- user_message_html = bleach.clean(
150
- user_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True
151
- )
152
-
153
- user_message_html = escape(user_message_html)
154
 
155
  messages_html += f"""
156
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
157
  <img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
158
- <div style='color: white; white-space: pre-wrap;'>{user_message_html}</div>
159
  </div>"""
160
 
161
- if assistant_message and ("assistant", assistant_message) not in seen_messages:
162
- seen_messages.add(("assistant", assistant_message))
163
-
164
- assistant_message_html = markdown.markdown(
165
- assistant_message,
166
- extensions=["fenced_code", "codehilite"],
167
- output_format="html5"
168
- )
169
-
170
- assistant_message_html = bleach.clean(
171
- assistant_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True
172
- )
173
-
174
- assistant_message_html = escape(assistant_message_html)
175
 
176
  messages_html += f"""
177
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
178
  <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
179
- <div style='color: white; white-space: pre-wrap;'>{assistant_message_html}</div>
180
  </div>"""
181
 
182
  messages_html += "</div></div>"
183
  return messages_html
184
 
 
185
  def escape_html(unsafe_text):
186
  escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
187
  return escaped_text
 
119
 
120
 
121
 
122
+ import markdown
123
+ import bleach
124
+
125
+ # Allowed Markdown and HTML tags
126
+ allowed_tags = [
127
+ "p", "strong", "em", "ul", "ol", "li", "a", "code", "pre", "br", "blockquote", "h1", "h2", "h3", "h4", "h5", "h6"
128
+ ]
129
+ allowed_attributes = {
130
+ "a": ["href", "title"],
131
+ "img": ["src", "alt", "title", "width", "height"]
132
+ }
133
+
134
  def render_message(history):
135
  messages_html = """
136
  <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;">
137
  <div id="messages" style="display: block; margin-bottom: 10px;">"""
138
 
139
+ for user_message, assistant_message, user_pic, assistant_pic in history:
140
+ if user_message:
141
+ sanitized_message = bleach.clean(user_message, tags=allowed_tags, attributes=allowed_attributes, strip=True)
142
+ rendered_message = markdown.markdown(sanitized_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  messages_html += f"""
145
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
146
  <img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
147
+ <div style='color: white; white-space: pre-wrap;'>{rendered_message}</div>
148
  </div>"""
149
 
150
+ if assistant_message:
151
+ sanitized_message = bleach.clean(assistant_message, tags=allowed_tags, attributes=allowed_attributes, strip=True)
152
+ rendered_message = markdown.markdown(sanitized_message)
 
 
 
 
 
 
 
 
 
 
 
153
 
154
  messages_html += f"""
155
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
156
  <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
157
+ <div style='color: white; white-space: pre-wrap;'>{rendered_message}</div>
158
  </div>"""
159
 
160
  messages_html += "</div></div>"
161
  return messages_html
162
 
163
+
164
  def escape_html(unsafe_text):
165
  escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
166
  return escaped_text