Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,12 @@ import requests
|
|
3 |
import json
|
4 |
import os
|
5 |
import markdown
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
8 |
|
@@ -108,59 +114,49 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
108 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
109 |
|
110 |
|
111 |
-
def
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
117 |
-
if user_message:
|
118 |
-
user_message_html = markdown.markdown(
|
119 |
-
escape_html(user_message),
|
120 |
-
extensions=["fenced_code", "codehilite", "md_in_html"]
|
121 |
-
)
|
122 |
-
messages_html += f"""
|
123 |
-
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
124 |
-
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
125 |
-
<span style='color: white;'>{user_message_html}</span>
|
126 |
-
</div><br>"""
|
127 |
-
|
128 |
-
if assistant_message:
|
129 |
-
assistant_message_html = markdown.markdown(
|
130 |
-
escape_html(assistant_message),
|
131 |
-
extensions=["fenced_code", "codehilite", "md_in_html"]
|
132 |
-
)
|
133 |
-
messages_html += f"""
|
134 |
-
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
135 |
-
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
136 |
-
<span style='color: white;'>{assistant_message_html}</span>
|
137 |
-
</div><br>"""
|
138 |
-
|
139 |
-
messages_html += """
|
140 |
-
</div></div>
|
141 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.3/purify.min.js"></script>
|
142 |
-
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
|
143 |
-
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
144 |
-
<script>
|
145 |
-
function escapeHtml(unsafe) {
|
146 |
-
return unsafe
|
147 |
-
.replace(/&/g, "&")
|
148 |
-
.replace(/</g, "<")
|
149 |
-
.replace(/>/g, ">")
|
150 |
-
.replace(/"/g, """)
|
151 |
-
.replace(/'/g, "'");
|
152 |
-
}
|
153 |
-
// Sanitize messages using DOMPurify
|
154 |
-
let messages = document.querySelectorAll('#messages span');
|
155 |
-
messages.forEach((message) => {
|
156 |
-
message.innerHTML = DOMPurify.sanitize(message.innerHTML); // Sanitize content
|
157 |
-
});
|
158 |
-
// Render math using MathJax
|
159 |
-
MathJax.typeset();
|
160 |
-
</script>
|
161 |
"""
|
162 |
-
|
|
|
|
|
|
|
|
|
163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
def escape_html(unsafe_text):
|
166 |
"""Escape HTML special characters to prevent code injection."""
|
|
|
3 |
import json
|
4 |
import os
|
5 |
import markdown
|
6 |
+
import gradio as gr
|
7 |
+
import re
|
8 |
+
import markdown
|
9 |
+
from pygments import highlight
|
10 |
+
from pygments.lexers import HtmlLexer, PythonLexer, TextLexer
|
11 |
+
from pygments.formatters import HtmlFormatter
|
12 |
|
13 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
14 |
|
|
|
114 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
115 |
|
116 |
|
117 |
+
def detect_content_type(message):
|
118 |
+
"""
|
119 |
+
Automatically detect whether the message contains Math, HTML, or plain Markdown.
|
120 |
+
Returns a tuple: (is_math, is_html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
"""
|
122 |
+
is_math = bool(re.search(r"(\$\$.*?\$\$|\$.*?\$)", message, re.DOTALL))
|
123 |
+
|
124 |
+
is_html = bool(re.search(r"<\/?[a-z][\s\S]*?>", message))
|
125 |
+
|
126 |
+
return is_math, is_html
|
127 |
|
128 |
+
def render_message(message):
|
129 |
+
"""
|
130 |
+
Render a message dynamically, detecting if it's Math, HTML, or Markdown with code.
|
131 |
+
"""
|
132 |
+
is_math, is_html = detect_content_type(message)
|
133 |
+
|
134 |
+
if is_math:
|
135 |
+
return gr.HTML(f"""
|
136 |
+
<div style="font-size: 16px; font-family: Arial;">
|
137 |
+
<script type="text/javascript" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
138 |
+
{message}
|
139 |
+
</div>
|
140 |
+
""")
|
141 |
+
elif is_html:
|
142 |
+
formatter = HtmlFormatter(style="default", noclasses=True)
|
143 |
+
highlighted_html = highlight(message, HtmlLexer(), formatter)
|
144 |
+
return gr.HTML(f"""
|
145 |
+
<div style="background: #f5f5f5; border: 1px solid #ddd; padding: 10px; overflow-x: auto;">
|
146 |
+
{highlighted_html}
|
147 |
+
</div>
|
148 |
+
""")
|
149 |
+
else:
|
150 |
+
def markdown_with_code_blocks(md):
|
151 |
+
html = markdown.markdown(md, extensions=["fenced_code"])
|
152 |
+
return html
|
153 |
+
|
154 |
+
formatted_message = markdown_with_code_blocks(message)
|
155 |
+
return gr.HTML(f"""
|
156 |
+
<div style="font-size: 16px; font-family: Arial;">
|
157 |
+
{formatted_message}
|
158 |
+
</div>
|
159 |
+
""")
|
160 |
|
161 |
def escape_html(unsafe_text):
|
162 |
"""Escape HTML special characters to prevent code injection."""
|