Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import markdown
|
| 6 |
from markdown.extensions.codehilite import CodeHiliteExtension
|
|
|
|
| 7 |
import markdown.extensions.fenced_code
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
| 10 |
|
| 11 |
sessions = {}
|
|
@@ -32,15 +44,16 @@ def render_avatars(userid):
|
|
| 32 |
|
| 33 |
|
| 34 |
def authorize(user, api_key, system_message):
|
| 35 |
-
test_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
response = requests.post(
|
| 38 |
"https://host.palple.polrambora.com/check_key_impv",
|
| 39 |
json=test_data,
|
| 40 |
)
|
| 41 |
-
print(f"Request Data: {test_data}")
|
| 42 |
-
print(f"Response Status: {response.status_code}")
|
| 43 |
-
print(f"Response Text: {response.text}")
|
| 44 |
|
| 45 |
if response.status_code == 200:
|
| 46 |
response_json = response.json()
|
|
@@ -62,10 +75,8 @@ def authorize(user, api_key, system_message):
|
|
| 62 |
else:
|
| 63 |
return False
|
| 64 |
except Exception as e:
|
| 65 |
-
print(f"Exception occurred: {str(e)}")
|
| 66 |
return False
|
| 67 |
|
| 68 |
-
|
| 69 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
| 70 |
session = sessions.get(api_key, {})
|
| 71 |
headers = session.get("headers", {})
|
|
@@ -97,7 +108,8 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 97 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
|
| 98 |
|
| 99 |
if response.status_code == 200:
|
| 100 |
-
for line in response.iter_lines(
|
|
|
|
| 101 |
if line.strip():
|
| 102 |
try:
|
| 103 |
if line.startswith("data:"):
|
|
@@ -116,19 +128,30 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 116 |
except Exception as e:
|
| 117 |
yield f"Error: {str(e)}"
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
|
|
|
| 120 |
|
| 121 |
def render_message(history):
|
|
|
|
| 122 |
messages_html = """
|
| 123 |
<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;">
|
| 124 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
| 125 |
|
| 126 |
-
seen_messages = set()
|
| 127 |
|
| 128 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 129 |
if user_message and ("user", user_message) not in seen_messages:
|
| 130 |
seen_messages.add(("user", user_message))
|
| 131 |
-
user_message_html =
|
| 132 |
messages_html += f"""
|
| 133 |
<div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
|
| 134 |
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
|
|
@@ -137,7 +160,7 @@ def render_message(history):
|
|
| 137 |
|
| 138 |
if assistant_message and ("assistant", assistant_message) not in seen_messages:
|
| 139 |
seen_messages.add(("assistant", assistant_message))
|
| 140 |
-
assistant_message_html =
|
| 141 |
messages_html += f"""
|
| 142 |
<div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
|
| 143 |
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
|
|
@@ -147,11 +170,36 @@ def render_message(history):
|
|
| 147 |
messages_html += "</div></div>"
|
| 148 |
return messages_html
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
-
def escape_html(unsafe_text):
|
| 153 |
-
escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
|
| 154 |
-
return escaped_text
|
| 155 |
|
| 156 |
css="""
|
| 157 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
|
@@ -169,10 +217,19 @@ with gr.Blocks(css=css) as demo:
|
|
| 169 |
|
| 170 |
with gr.Column(visible=False) as chat_view:
|
| 171 |
gr.HTML("""
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
gr.Markdown("## P-MSQ Chat Interface")
|
| 177 |
chatbot_output = gr.HTML(elem_id="chatbox-container")
|
| 178 |
gr.Markdown(elem_id="chatbox-container")
|
|
@@ -214,6 +271,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 214 |
|
| 215 |
|
| 216 |
|
|
|
|
| 217 |
|
| 218 |
def regenerate_response(history, last_message, max_tokens, top_p, temperature):
|
| 219 |
return "", []
|
|
|
|
| 1 |
+
import bleach
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
import markdown
|
| 7 |
from markdown.extensions.codehilite import CodeHiliteExtension
|
| 8 |
+
from markdown.extensions.fenced_code import FencedCodeExtension
|
| 9 |
import markdown.extensions.fenced_code
|
| 10 |
+
import html
|
| 11 |
+
import re
|
| 12 |
+
|
| 13 |
+
ALLOWED_TAGS = [
|
| 14 |
+
'b', 'i', 'strong', 'em', 'a', 'p', 'ul', 'ol', 'li', 'br', 'hr',
|
| 15 |
+
'blockquote', 'code', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img'
|
| 16 |
+
]
|
| 17 |
+
ALLOWED_ATTRIBUTES = {
|
| 18 |
+
'a': ['href', 'title', 'target'],
|
| 19 |
+
'img': ['src', 'alt', 'title', 'width', 'height']
|
| 20 |
+
}
|
| 21 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
| 22 |
|
| 23 |
sessions = {}
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
def authorize(user, api_key, system_message):
|
| 47 |
+
test_data = {
|
| 48 |
+
"user": user,
|
| 49 |
+
"key": api_key
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
try:
|
| 53 |
response = requests.post(
|
| 54 |
"https://host.palple.polrambora.com/check_key_impv",
|
| 55 |
json=test_data,
|
| 56 |
)
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
if response.status_code == 200:
|
| 59 |
response_json = response.json()
|
|
|
|
| 75 |
else:
|
| 76 |
return False
|
| 77 |
except Exception as e:
|
|
|
|
| 78 |
return False
|
| 79 |
|
|
|
|
| 80 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
| 81 |
session = sessions.get(api_key, {})
|
| 82 |
headers = session.get("headers", {})
|
|
|
|
| 108 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
|
| 109 |
|
| 110 |
if response.status_code == 200:
|
| 111 |
+
for line in response.iter_lines():
|
| 112 |
+
line = line.decode('utf-8')
|
| 113 |
if line.strip():
|
| 114 |
try:
|
| 115 |
if line.startswith("data:"):
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
yield f"Error: {str(e)}"
|
| 130 |
|
| 131 |
+
def format_code_blocks(text):
|
| 132 |
+
|
| 133 |
+
code_pattern = re.compile(r'```(.*?)```', re.DOTALL)
|
| 134 |
+
return code_pattern.sub(lambda match: f"<pre><code>{html.escape(match.group(1))}</code></pre>", text)
|
| 135 |
+
|
| 136 |
+
def render_markdown(text):
|
| 137 |
+
return markdown.markdown(text, extensions=[FencedCodeExtension()])
|
| 138 |
+
|
| 139 |
+
|
| 140 |
|
| 141 |
+
import re
|
| 142 |
|
| 143 |
def render_message(history):
|
| 144 |
+
|
| 145 |
messages_html = """
|
| 146 |
<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;">
|
| 147 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
| 148 |
|
| 149 |
+
seen_messages = set()
|
| 150 |
|
| 151 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 152 |
if user_message and ("user", user_message) not in seen_messages:
|
| 153 |
seen_messages.add(("user", user_message))
|
| 154 |
+
user_message_html = process_message(user_message)
|
| 155 |
messages_html += f"""
|
| 156 |
<div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
|
| 157 |
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
|
|
|
|
| 160 |
|
| 161 |
if assistant_message and ("assistant", assistant_message) not in seen_messages:
|
| 162 |
seen_messages.add(("assistant", assistant_message))
|
| 163 |
+
assistant_message_html = process_message(assistant_message)
|
| 164 |
messages_html += f"""
|
| 165 |
<div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
|
| 166 |
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
|
|
|
|
| 170 |
messages_html += "</div></div>"
|
| 171 |
return messages_html
|
| 172 |
|
| 173 |
+
def process_message(message):
|
| 174 |
+
code_block_pattern = re.compile(r"```(\w+)?\n(.*?)```", re.DOTALL)
|
| 175 |
+
parts = code_block_pattern.split(message)
|
| 176 |
+
|
| 177 |
+
for i in range(len(parts)):
|
| 178 |
+
if i % 3 == 0:
|
| 179 |
+
parts[i] = escape_html(parts[i])
|
| 180 |
+
parts[i] = render_inline_markdown(parts[i])
|
| 181 |
+
elif i % 3 == 1:
|
| 182 |
+
continue
|
| 183 |
+
else:
|
| 184 |
+
lang_class = parts[i - 1] or ""
|
| 185 |
+
parts[i] = f"<pre><code class='language-{lang_class}'>{escape_html(parts[i])}</code></pre>"
|
| 186 |
+
|
| 187 |
+
return "".join(parts)
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def escape_html(text):
|
| 191 |
+
return (text
|
| 192 |
+
.replace("&", "&")
|
| 193 |
+
.replace("<", "<")
|
| 194 |
+
.replace(">", ">")
|
| 195 |
+
.replace('"', """)
|
| 196 |
+
.replace("'", "'"))
|
| 197 |
+
|
| 198 |
+
def render_inline_markdown(text):
|
| 199 |
+
text = markdown.markdown(text, output_format='xhtml')
|
| 200 |
+
return text
|
| 201 |
|
| 202 |
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
css="""
|
| 205 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
|
|
|
| 217 |
|
| 218 |
with gr.Column(visible=False) as chat_view:
|
| 219 |
gr.HTML("""
|
| 220 |
+
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.css" rel="stylesheet">
|
| 221 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
|
| 222 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
|
| 223 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-javascript.min.js"></script>
|
| 224 |
+
<script>
|
| 225 |
+
MathJax = {
|
| 226 |
+
tex: { inlineMath: [['\\(', '\\)'], ['\\[', '\\]']] },
|
| 227 |
+
svg: { scale: 1.2 }
|
| 228 |
+
};
|
| 229 |
+
</script>
|
| 230 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js"></script>
|
| 231 |
+
""")
|
| 232 |
+
|
| 233 |
gr.Markdown("## P-MSQ Chat Interface")
|
| 234 |
chatbot_output = gr.HTML(elem_id="chatbox-container")
|
| 235 |
gr.Markdown(elem_id="chatbox-container")
|
|
|
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
+
|
| 275 |
|
| 276 |
def regenerate_response(history, last_message, max_tokens, top_p, temperature):
|
| 277 |
return "", []
|