Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ sessions = {}
|
|
| 12 |
PRIMARY_SYSTEM_INSTRUCTIONS = "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations"
|
| 13 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 14 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
| 15 |
-
|
| 16 |
def render_avatars(userid):
|
| 17 |
try:
|
| 18 |
response = requests.post(
|
|
@@ -117,21 +116,43 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 117 |
yield f"Error: {str(e)}"
|
| 118 |
|
| 119 |
|
|
|
|
| 120 |
def render_message(history):
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
|
| 123 |
seen_messages = set()
|
| 124 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 125 |
if user_message and user_message not in seen_messages:
|
| 126 |
seen_messages.add(user_message)
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
if assistant_message and assistant_message not in seen_messages:
|
| 130 |
seen_messages.add(assistant_message)
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
css="""
|
| 137 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
|
@@ -148,9 +169,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 148 |
auth_status = gr.Textbox(label="Authorization Status", interactive=False)
|
| 149 |
|
| 150 |
with gr.Column(visible=False) as chat_view:
|
| 151 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
gr.Markdown("## P-MSQ Chat Interface")
|
| 153 |
-
chatbot_output = gr.
|
|
|
|
| 154 |
msg_input = gr.Text(
|
| 155 |
show_label=False,
|
| 156 |
placeholder="Type your message and press Shift+Enter...",
|
|
@@ -227,12 +253,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 227 |
def authorize_and_proceed(user, api_key):
|
| 228 |
if authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS):
|
| 229 |
gr.Info("Loading, please wait.")
|
| 230 |
-
|
| 231 |
|
| 232 |
return (
|
| 233 |
gr.update(visible=False),
|
| 234 |
gr.update(visible=True),
|
| 235 |
-
|
| 236 |
history
|
| 237 |
)
|
| 238 |
elif authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS) == 403:
|
|
@@ -265,4 +291,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 265 |
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
| 266 |
demo.launch(show_api=False)
|
| 267 |
if __name__ == "__main__":
|
| 268 |
-
demo.queue = False
|
|
|
|
| 12 |
PRIMARY_SYSTEM_INSTRUCTIONS = "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations"
|
| 13 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 14 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
|
|
|
| 15 |
def render_avatars(userid):
|
| 16 |
try:
|
| 17 |
response = requests.post(
|
|
|
|
| 116 |
yield f"Error: {str(e)}"
|
| 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 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 127 |
if user_message and user_message not in seen_messages:
|
| 128 |
seen_messages.add(user_message)
|
| 129 |
+
user_message_html = markdown.markdown(escape_html(user_message), extensions=["fenced_code", "codehilite"])
|
| 130 |
+
messages_html += f"""
|
| 131 |
+
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
| 132 |
+
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
| 133 |
+
<span style='color: white;'>{user_message_html}</span>
|
| 134 |
+
</div>"""
|
| 135 |
|
| 136 |
if assistant_message and assistant_message not in seen_messages:
|
| 137 |
seen_messages.add(assistant_message)
|
| 138 |
+
assistant_message_html = markdown.markdown(escape_html(assistant_message), extensions=["fenced_code", "codehilite"])
|
| 139 |
+
messages_html += f"""
|
| 140 |
+
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
| 141 |
+
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
| 142 |
+
<span style='color: white;'>{assistant_message_html}</span>
|
| 143 |
+
</div>"""
|
| 144 |
+
|
| 145 |
+
messages_html += "</div></div>"
|
| 146 |
+
return messages_html
|
| 147 |
|
| 148 |
+
def escape_html(unsafe_text):
|
| 149 |
+
return (
|
| 150 |
+
unsafe_text.replace("&", "&")
|
| 151 |
+
.replace("<", "<")
|
| 152 |
+
.replace(">", ">")
|
| 153 |
+
.replace('"', """)
|
| 154 |
+
.replace("'", "'")
|
| 155 |
+
)
|
| 156 |
|
| 157 |
css="""
|
| 158 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
|
|
|
| 169 |
auth_status = gr.Textbox(label="Authorization Status", interactive=False)
|
| 170 |
|
| 171 |
with gr.Column(visible=False) as chat_view:
|
| 172 |
+
gr.HTML("""
|
| 173 |
+
<script type="text/javascript" id="MathJax-script" async
|
| 174 |
+
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js">
|
| 175 |
+
</script>
|
| 176 |
+
""")
|
| 177 |
gr.Markdown("## P-MSQ Chat Interface")
|
| 178 |
+
chatbot_output = gr.HTML(elem_id="chatbox-container")
|
| 179 |
+
gr.Markdown(elem_id="chatbox-container")
|
| 180 |
msg_input = gr.Text(
|
| 181 |
show_label=False,
|
| 182 |
placeholder="Type your message and press Shift+Enter...",
|
|
|
|
| 253 |
def authorize_and_proceed(user, api_key):
|
| 254 |
if authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS):
|
| 255 |
gr.Info("Loading, please wait.")
|
| 256 |
+
messages_html, history = load_conversation(api_key)
|
| 257 |
|
| 258 |
return (
|
| 259 |
gr.update(visible=False),
|
| 260 |
gr.update(visible=True),
|
| 261 |
+
messages_html,
|
| 262 |
history
|
| 263 |
)
|
| 264 |
elif authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS) == 403:
|
|
|
|
| 291 |
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
| 292 |
demo.launch(show_api=False)
|
| 293 |
if __name__ == "__main__":
|
| 294 |
+
demo.queue = False
|