POLRAMBORA commited on
Commit
7ff88b6
·
verified ·
1 Parent(s): 6aa5b6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
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,36 +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
- messages_markdown = ""
 
 
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
- messages_markdown += f"""
 
128
  <div style='display: flex; align-items: center; margin-bottom: 10px;'>
129
  <img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
130
- <span style='color: white;'>{user_message}</span>
131
- </div>
132
- """
133
 
134
  if assistant_message and assistant_message not in seen_messages:
135
  seen_messages.add(assistant_message)
136
- messages_markdown += f"""
 
137
  <div style='display: flex; align-items: center; margin-bottom: 10px;'>
138
  <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
139
- <span style='color: white;'>{assistant_message}</span>
140
- </div>
141
- """
142
-
143
- chatbox_html = f"""
144
- <div id='chatbox-container' style='height: 400px; overflow-y: auto; border: 1px solid #262626; padding: 10px; background-color: #171717;'>
145
- {messages_markdown}
146
- </div>
147
- """
148
- return chatbox_html
149
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  css="""
152
  .chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
@@ -163,9 +169,14 @@ with gr.Blocks(css=css) as demo:
163
  auth_status = gr.Textbox(label="Authorization Status", interactive=False)
164
 
165
  with gr.Column(visible=False) as chat_view:
166
- gr.Markdown("<script type='text/javascript' id='MathJax-script' async src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js'></script>")
 
 
 
 
167
  gr.Markdown("## P-MSQ Chat Interface")
168
- chatbot_output = gr.Markdown("", elem_id="chatbox-container")
 
169
  msg_input = gr.Text(
170
  show_label=False,
171
  placeholder="Type your message and press Shift+Enter...",
@@ -242,12 +253,12 @@ with gr.Blocks(css=css) as demo:
242
  def authorize_and_proceed(user, api_key):
243
  if authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS):
244
  gr.Info("Loading, please wait.")
245
- messages_markdown, history = load_conversation(api_key)
246
 
247
  return (
248
  gr.update(visible=False),
249
  gr.update(visible=True),
250
- messages_markdown,
251
  history
252
  )
253
  elif authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS) == 403:
@@ -280,4 +291,4 @@ with gr.Blocks(css=css) as demo:
280
  save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
281
  demo.launch(show_api=False)
282
  if __name__ == "__main__":
283
- 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("&", "&amp;")
151
+ .replace("<", "&lt;")
152
+ .replace(">", "&gt;")
153
+ .replace('"', "&quot;")
154
+ .replace("'", "&#039;")
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