POLRAMBORA commited on
Commit
954d993
·
verified ·
1 Parent(s): 9a8b737

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -127,8 +127,10 @@ def render_message(history):
127
 
128
  seen_messages = set() # Track (role, message) pairs to avoid duplicates
129
 
 
130
  allowed_tags = [
131
- 'p', 'strong', 'em', 'ul', 'ol', 'li', 'a', 'code', 'pre', 'br', 'blockquote', 'hr', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img'
 
132
  ]
133
  allowed_attributes = {
134
  '*': ['class', 'style'],
@@ -136,14 +138,18 @@ def render_message(history):
136
  'img': ['src', 'alt', 'title', 'width', 'height']
137
  }
138
 
 
139
  for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
140
  if user_message and ("user", user_message) not in seen_messages:
141
  seen_messages.add(("user", user_message))
 
142
  user_message_html = markdown.markdown(
143
- escape_html(user_message),
144
  extensions=["fenced_code", "codehilite"]
145
  )
146
- user_message_html = bleach.clean(user_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True)
 
 
147
  messages_html += f"""
148
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
149
  <img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
@@ -152,11 +158,14 @@ def render_message(history):
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
- escape_html(assistant_message),
157
  extensions=["fenced_code", "codehilite"]
158
  )
159
- assistant_message_html = bleach.clean(assistant_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True)
 
 
160
  messages_html += f"""
161
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
162
  <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
@@ -167,6 +176,7 @@ def render_message(history):
167
  return messages_html
168
 
169
 
 
170
  def escape_html(unsafe_text):
171
  escaped_text = ''.join(f"&#{ord(char)};" if char not in ('\n', '\r') else '<br>' for char in unsafe_text)
172
  return escaped_text
@@ -269,35 +279,30 @@ with gr.Blocks(css=css) as demo:
269
 
270
 
271
  def authorize_and_proceed(user, api_key):
272
- auth_result = authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS)
 
 
273
 
274
- if auth_result == 403:
275
  return (
276
- gr.update(visible=False),
277
- gr.update(visible=False),
278
- gr.update(visible=True),
279
- [],
280
  )
281
- elif auth_result:
282
- gr.Info("Loading, please wait.")
283
- messages_html, history = load_conversation(api_key)
284
  return (
285
  gr.update(visible=False),
286
- gr.update(visible=True),
287
- gr.update(visible=False),
288
- messages_html,
289
- history
290
  )
291
  else:
 
292
  return (
293
- gr.update(visible=True),
294
- gr.update(visible=False),
295
- gr.update(visible=False),
296
- "",
297
- []
298
  )
299
 
300
-
301
  def save_custom_instructions(api_key, custom_instructions):
302
  if api_key in sessions:
303
  gr.Info("Instructions updated, we recommend to start the new conversation to make it more efficient.")
@@ -310,7 +315,7 @@ with gr.Blocks(css=css) as demo:
310
 
311
 
312
 
313
- auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view, blacklist_view, chatbot_output, history_state])
314
  save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
315
  demo.launch(show_api=False)
316
  if __name__ == "__main__":
 
127
 
128
  seen_messages = set() # Track (role, message) pairs to avoid duplicates
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'
134
  ]
135
  allowed_attributes = {
136
  '*': ['class', 'style'],
 
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, # Process markdown
148
  extensions=["fenced_code", "codehilite"]
149
  )
150
+ user_message_html = bleach.clean(
151
+ user_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True
152
+ )
153
  messages_html += f"""
154
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
155
  <img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
 
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, # Process markdown
164
  extensions=["fenced_code", "codehilite"]
165
  )
166
+ assistant_message_html = bleach.clean(
167
+ assistant_message_html, tags=allowed_tags, attributes=allowed_attributes, strip=True
168
+ )
169
  messages_html += f"""
170
  <div style='display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px;'>
171
  <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-bottom: 5px;'>
 
176
  return messages_html
177
 
178
 
179
+
180
  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
 
279
 
280
 
281
  def authorize_and_proceed(user, api_key):
282
+ if authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS):
283
+ gr.Info("Loading, please wait.")
284
+ messages_html, history = load_conversation(api_key)
285
 
 
286
  return (
287
+ gr.update(visible=False),
288
+ gr.update(visible=True),
289
+ messages_html,
290
+ history
291
  )
292
+ elif authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS) == 403:
 
 
293
  return (
294
  gr.update(visible=False),
295
+ gr.update(visible=False),
296
+ gr.update(visible=True),
 
 
297
  )
298
  else:
299
+ gr.Warning("Incorrect userid/token")
300
  return (
301
+ gr.update(visible=True),
302
+ gr.update(visible=False),
303
+ auth_status.update(value="Invalid userid/token")
 
 
304
  )
305
 
 
306
  def save_custom_instructions(api_key, custom_instructions):
307
  if api_key in sessions:
308
  gr.Info("Instructions updated, we recommend to start the new conversation to make it more efficient.")
 
315
 
316
 
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=False)
321
  if __name__ == "__main__":