Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -122,35 +122,39 @@ def render_message(history):
|
|
122 |
<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;">
|
123 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
124 |
|
125 |
-
seen_messages = set()
|
126 |
-
|
127 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
128 |
-
if user_message and
|
129 |
-
seen_messages.add(
|
130 |
-
user_message_html =
|
131 |
messages_html += f"""
|
132 |
-
<div style='display: flex;
|
133 |
-
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-
|
134 |
-
<
|
135 |
</div>"""
|
136 |
|
137 |
-
if assistant_message and
|
138 |
-
seen_messages.add(
|
139 |
-
assistant_message_html =
|
140 |
messages_html += f"""
|
141 |
-
<div style='display: flex;
|
142 |
-
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-
|
143 |
-
<
|
144 |
</div>"""
|
145 |
|
146 |
messages_html += "</div></div>"
|
147 |
return messages_html
|
148 |
|
149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
def escape_html(unsafe_text):
|
152 |
-
escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
|
153 |
-
return escaped_text
|
154 |
|
155 |
css="""
|
156 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
|
|
122 |
<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;">
|
123 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
124 |
|
125 |
+
seen_messages = set()
|
|
|
126 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
127 |
+
if user_message and user_message not in seen_messages:
|
128 |
+
seen_messages.add(user_message)
|
129 |
+
user_message_html = escape_and_format(user_message)
|
130 |
messages_html += f"""
|
131 |
+
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
132 |
+
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
133 |
+
<span style='color: white; white-space: pre-wrap;'>{user_message_html}</span>
|
134 |
</div>"""
|
135 |
|
136 |
+
if assistant_message and assistant_message not in seen_messages:
|
137 |
+
seen_messages.add(assistant_message)
|
138 |
+
assistant_message_html = escape_and_format(assistant_message)
|
139 |
messages_html += f"""
|
140 |
+
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
141 |
+
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
142 |
+
<span style='color: white; white-space: pre-wrap;'>{assistant_message_html}</span>
|
143 |
</div>"""
|
144 |
|
145 |
messages_html += "</div></div>"
|
146 |
return messages_html
|
147 |
|
148 |
|
149 |
+
def escape_and_format(text):
|
150 |
+
text = (
|
151 |
+
text.replace("**", "<b>").replace("__", "</b>")
|
152 |
+
.replace("`", "<code>").replace("```", "</code>")
|
153 |
+
.replace("\n", "<br>")
|
154 |
+
)
|
155 |
+
|
156 |
+
return ''.join(f"&#{ord(char)};" if char not in ('<', '>', '<br>', '<b>', '</b>', '<code>', '</code>') else char for char in text)
|
157 |
|
|
|
|
|
|
|
158 |
|
159 |
css="""
|
160 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|