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