Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,6 @@ import requests
|
|
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,59 +108,48 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
114 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
115 |
|
116 |
|
117 |
-
def
|
118 |
-
"""
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
""")
|
160 |
-
|
161 |
-
def escape_html(unsafe_text):
|
162 |
-
"""Escape HTML special characters to prevent code injection."""
|
163 |
-
return (
|
164 |
-
unsafe_text.replace("&", "&")
|
165 |
-
.replace("<", "<")
|
166 |
-
.replace(">", ">")
|
167 |
-
.replace('"', """)
|
168 |
-
.replace("'", "'")
|
169 |
-
)
|
170 |
|
171 |
js = """
|
172 |
function HTMLClean() {
|
@@ -191,6 +174,37 @@ with gr.Blocks(css=css, js=js) as demo:
|
|
191 |
|
192 |
with gr.Column(visible=False) as chat_view:
|
193 |
gr.Markdown("## P-MSQ Chat Interface")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
chatbot_output = gr.HTML(elem_id="chatbox-container")
|
195 |
|
196 |
msg_input = gr.Text(
|
|
|
3 |
import json
|
4 |
import os
|
5 |
import markdown
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
8 |
|
|
|
108 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
109 |
|
110 |
|
111 |
+
def render_message(history):
|
112 |
+
messages_html = """
|
113 |
+
<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;">
|
114 |
+
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
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(user_message)
|
119 |
+
messages_html += f"""
|
120 |
+
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
121 |
+
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
122 |
+
<span id="message-content" style='color: white;'>{user_message_html}</span>
|
123 |
+
</div><br>"""
|
124 |
+
|
125 |
+
if assistant_message:
|
126 |
+
assistant_message_html = markdown.markdown(assistant_message)
|
127 |
+
messages_html += f"""
|
128 |
+
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
129 |
+
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
130 |
+
<span id="message-content" style='color: white;'>{assistant_message_html}</span>
|
131 |
+
</div><br>"""
|
132 |
+
|
133 |
+
messages_html += """
|
134 |
+
</div></div>
|
135 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.3/purify.min.js"></script>
|
136 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js"></script>
|
137 |
+
<script>
|
138 |
+
function escapeHtml(unsafe) {
|
139 |
+
return unsafe
|
140 |
+
.replace(/&/g, "&")
|
141 |
+
.replace(/</g, "<")
|
142 |
+
.replace(/>/g, ">")
|
143 |
+
.replace(/"/g, """)
|
144 |
+
.replace(/'/g, "'");
|
145 |
+
}
|
146 |
+
let message = document.getElementById('message-content').innerHTML;
|
147 |
+
document.getElementById('message-content').innerHTML = escapeHtml(message);
|
148 |
+
MathJax.typeset(); // Process math expressions
|
149 |
+
</script>
|
150 |
+
"""
|
151 |
+
return messages_html
|
152 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
js = """
|
155 |
function HTMLClean() {
|
|
|
174 |
|
175 |
with gr.Column(visible=False) as chat_view:
|
176 |
gr.Markdown("## P-MSQ Chat Interface")
|
177 |
+
gr.Markdown(label="answer",
|
178 |
+
latex_delimiters=[{
|
179 |
+
"left": "\\(",
|
180 |
+
"right": "\\)",
|
181 |
+
"display": True
|
182 |
+
}, {
|
183 |
+
"left": "\\begin\{equation\}",
|
184 |
+
"right": "\\end\{equation\}",
|
185 |
+
"display": True
|
186 |
+
}, {
|
187 |
+
"left": "\\begin\{align\}",
|
188 |
+
"right": "\\end\{align\}",
|
189 |
+
"display": True
|
190 |
+
}, {
|
191 |
+
"left": "\\begin\{alignat\}",
|
192 |
+
"right": "\\end\{alignat\}",
|
193 |
+
"display": True
|
194 |
+
}, {
|
195 |
+
"left": "\\begin\{gather\}",
|
196 |
+
"right": "\\end\{gather\}",
|
197 |
+
"display": True
|
198 |
+
}, {
|
199 |
+
"left": "\\begin\{CD\}",
|
200 |
+
"right": "\\end\{CD\}",
|
201 |
+
"display": True
|
202 |
+
}, {
|
203 |
+
"left": "\\[",
|
204 |
+
"right": "\\]",
|
205 |
+
"display": True
|
206 |
+
}],
|
207 |
+
elem_id="chatbox-container")
|
208 |
chatbot_output = gr.HTML(elem_id="chatbox-container")
|
209 |
|
210 |
msg_input = gr.Text(
|