POLRAMBORA commited on
Commit
21504b7
·
verified ·
1 Parent(s): 3888601

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -59
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 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."""
163
- return (
164
- unsafe_text.replace("&", "&amp;")
165
- .replace("<", "&lt;")
166
- .replace(">", "&gt;")
167
- .replace('"', "&quot;")
168
- .replace("'", "&#039;")
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, "&amp;")
141
+ .replace(/</g, "&lt;")
142
+ .replace(/>/g, "&gt;")
143
+ .replace(/"/g, "&quot;")
144
+ .replace(/'/g, "&#039;");
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(