Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,6 @@ import os
|
|
| 5 |
import markdown
|
| 6 |
from markdown.extensions.codehilite import CodeHiliteExtension
|
| 7 |
import markdown.extensions.fenced_code
|
| 8 |
-
import bleach
|
| 9 |
-
from html import escape
|
| 10 |
|
| 11 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
| 12 |
|
|
@@ -119,15 +117,13 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
-
|
| 123 |
def render_message(history):
|
| 124 |
messages_html = """
|
| 125 |
<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;">
|
| 126 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
| 127 |
|
| 128 |
-
seen_messages = set()
|
| 129 |
|
| 130 |
-
# Tags and attributes explicitly allowed
|
| 131 |
allowed_tags = [
|
| 132 |
'p', 'strong', 'em', 'ul', 'ol', 'li', 'a', 'code', 'pre', 'br', 'blockquote', 'hr',
|
| 133 |
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img'
|
|
@@ -138,13 +134,11 @@ def render_message(history):
|
|
| 138 |
'img': ['src', 'alt', 'title', 'width', 'height']
|
| 139 |
}
|
| 140 |
|
| 141 |
-
# Iterate through history and render each message
|
| 142 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 143 |
if user_message and ("user", user_message) not in seen_messages:
|
| 144 |
seen_messages.add(("user", user_message))
|
| 145 |
-
# Render markdown -> sanitize output
|
| 146 |
user_message_html = markdown.markdown(
|
| 147 |
-
user_message,
|
| 148 |
extensions=["fenced_code", "codehilite"]
|
| 149 |
)
|
| 150 |
user_message_html = bleach.clean(
|
|
@@ -158,9 +152,8 @@ def render_message(history):
|
|
| 158 |
|
| 159 |
if assistant_message and ("assistant", assistant_message) not in seen_messages:
|
| 160 |
seen_messages.add(("assistant", assistant_message))
|
| 161 |
-
# Render markdown -> sanitize output
|
| 162 |
assistant_message_html = markdown.markdown(
|
| 163 |
-
assistant_message,
|
| 164 |
extensions=["fenced_code", "codehilite"]
|
| 165 |
)
|
| 166 |
assistant_message_html = bleach.clean(
|
|
@@ -181,7 +174,7 @@ def escape_html(unsafe_text):
|
|
| 181 |
escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
|
| 182 |
return escaped_text
|
| 183 |
|
| 184 |
-
|
| 185 |
css="""
|
| 186 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
| 187 |
"""
|
|
@@ -317,6 +310,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 317 |
|
| 318 |
auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view, chatbot_output, history_state])
|
| 319 |
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
| 320 |
-
demo.launch(show_api=
|
| 321 |
if __name__ == "__main__":
|
| 322 |
demo.queue = False
|
|
|
|
| 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 |
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
|
|
|
|
| 120 |
def render_message(history):
|
| 121 |
messages_html = """
|
| 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 |
allowed_tags = [
|
| 128 |
'p', 'strong', 'em', 'ul', 'ol', 'li', 'a', 'code', 'pre', 'br', 'blockquote', 'hr',
|
| 129 |
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img'
|
|
|
|
| 134 |
'img': ['src', 'alt', 'title', 'width', 'height']
|
| 135 |
}
|
| 136 |
|
|
|
|
| 137 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 138 |
if user_message and ("user", user_message) not in seen_messages:
|
| 139 |
seen_messages.add(("user", user_message))
|
|
|
|
| 140 |
user_message_html = markdown.markdown(
|
| 141 |
+
user_message,
|
| 142 |
extensions=["fenced_code", "codehilite"]
|
| 143 |
)
|
| 144 |
user_message_html = bleach.clean(
|
|
|
|
| 152 |
|
| 153 |
if assistant_message and ("assistant", assistant_message) not in seen_messages:
|
| 154 |
seen_messages.add(("assistant", assistant_message))
|
|
|
|
| 155 |
assistant_message_html = markdown.markdown(
|
| 156 |
+
assistant_message,
|
| 157 |
extensions=["fenced_code", "codehilite"]
|
| 158 |
)
|
| 159 |
assistant_message_html = bleach.clean(
|
|
|
|
| 174 |
escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
|
| 175 |
return escaped_text
|
| 176 |
|
| 177 |
+
|
| 178 |
css="""
|
| 179 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
| 180 |
"""
|
|
|
|
| 310 |
|
| 311 |
auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view, chatbot_output, history_state])
|
| 312 |
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
| 313 |
+
demo.launch(show_api=True)
|
| 314 |
if __name__ == "__main__":
|
| 315 |
demo.queue = False
|