Coloring commited on
Commit
33cf5a0
·
1 Parent(s): 4e0488c

Update Chatbot template

Browse files
components/antdx/suggestion/demos/block.py CHANGED
@@ -28,7 +28,7 @@ with gr.Blocks() as demo:
28
  with ms.Application():
29
  with antdx.XProvider():
30
  antd.Typography.Paragraph(
31
- "Set `block` to display in a whole row. ``extra` can be used to configure additional information."
32
  )
33
  with antdx.Suggestion(
34
  items=items,
 
28
  with ms.Application():
29
  with antdx.XProvider():
30
  antd.Typography.Paragraph(
31
+ "Set `block` to display in a whole row. `extra` can be used to configure additional information."
32
  )
33
  with antdx.Suggestion(
34
  items=items,
layout_templates/chatbot/demos/app.py CHANGED
@@ -20,6 +20,8 @@ client = OpenAI(
20
 
21
  model = "Qwen/Qwen2.5-VL-72B-Instruct"
22
 
 
 
23
  # =========== Configuration
24
 
25
  DEFAULT_PROMPTS = [{
@@ -111,7 +113,8 @@ class Gradio_Events:
111
  }] + [{
112
  "type": "image_url",
113
  "image_url": image_to_base64(file["path"])
114
- } for file in item["content"]["files"]]
 
115
  })
116
  elif item["role"] == "assistant":
117
  messages.append(item)
@@ -307,7 +310,7 @@ class Gradio_Events:
307
  value=state_value)
308
 
309
  @staticmethod
310
- def regenerating(state_value, e: gr.EventData):
311
  conversation_key = e._data["component"]["conversationKey"]
312
  history = state_value["conversations_history"][
313
  state_value["conversation_id"]]
@@ -324,6 +327,37 @@ class Gradio_Events:
324
  # custom code
325
  return gr.update(items=history), gr.update(value=state_value)
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  @staticmethod
328
  def select_suggestion(sender_value, e: gr.EventData):
329
  return gr.update(value=sender_value[:-1] + e._data["payload"][0])
@@ -400,6 +434,30 @@ class Gradio_Events:
400
  e._data["payload"][0]), gr.update(
401
  open=True), gr.update(value=state_value)
402
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
 
404
  css = """
405
  #chatbot {
@@ -464,13 +522,15 @@ def logo():
464
  ms.Span("Chatbot")
465
 
466
 
467
- with gr.Blocks(css=css) as demo:
468
  state = gr.State({
469
  "conversations_history": {},
470
  "conversations": [],
471
  "conversation_id": "",
 
472
  "attachments_open": False,
473
  })
 
474
  with ms.Application(), antdx.XProvider(
475
  theme=DEFAULT_THEME, locale=DEFAULT_LOCALE), ms.AutoLoading():
476
  with antd.Row(gutter=[20, 20], wrap=False, elem_id="chatbot"):
@@ -592,6 +652,7 @@ with gr.Blocks(css=css) as demo:
592
  copy_btn: {
593
  copyable: { text: typeof bubble.content === 'string' ? bubble.content : bubble.content?.text, tooltips: false },
594
  },
 
595
  delete_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
596
  };
597
  }"""):
@@ -611,6 +672,14 @@ with gr.Blocks(css=css) as demo:
611
  variant="text"):
612
  with ms.Slot("icon"):
613
  antd.Icon("CheckOutlined")
 
 
 
 
 
 
 
 
614
  with antd.Popconfirm(
615
  title="Delete the message",
616
  description=
@@ -660,8 +729,9 @@ with gr.Blocks(css=css) as demo:
660
  copy_btn: {
661
  copyable: { text: bubble.content, tooltips: false },
662
  },
663
- regenerating_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
664
  delete_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
 
665
  like_btn: {
666
  conversationKey: bubble.key,
667
  color: bubble.meta?.action === 'like' ? 'primary' : 'default',
@@ -714,23 +784,30 @@ with gr.Blocks(css=css) as demo:
714
  with ms.Slot("icon"):
715
  antd.Icon("DislikeOutlined")
716
  with antd.Popconfirm(
717
- title=
718
- "Regenerating the message",
719
  description=
720
- "Regenerating the message will also delete all subsequent messages.",
721
  ok_button_props=dict(
722
  danger=True),
723
- as_item="regenerating_btn"
724
- ) as chatbot_regenerating_popconfirm:
725
  with antd.Button(
726
  value=None,
727
  size="small",
728
  color="default",
729
  variant="text",
730
- as_item="regenerating_btn",
731
  ):
732
  with ms.Slot("icon"):
733
  antd.Icon("SyncOutlined")
 
 
 
 
 
 
 
 
734
  with antd.Popconfirm(
735
  title="Delete the message",
736
  description=
@@ -839,8 +916,30 @@ with gr.Blocks(css=css) as demo:
839
  ):
840
  antd.Icon(
841
  "CloudUploadOutlined")
842
-
 
 
 
 
 
 
 
843
  # Events Handler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
844
  add_conversation_btn.click(fn=Gradio_Events.new_chat,
845
  inputs=[state],
846
  outputs=[conversations, chatbot, state])
@@ -875,6 +974,16 @@ with gr.Blocks(css=css) as demo:
875
  chatbot_dislike_btn.click(fn=Gradio_Events.dislike,
876
  inputs=[state],
877
  outputs=[chatbot, state])
 
 
 
 
 
 
 
 
 
 
878
  gr.on(triggers=[
879
  chatbot_delete_popconfirm.confirm, user_delete_popconfirm.confirm
880
  ],
@@ -882,8 +991,8 @@ with gr.Blocks(css=css) as demo:
882
  inputs=[state],
883
  outputs=[chatbot, state])
884
 
885
- regenerating_event = chatbot_regenerating_popconfirm.confirm(
886
- fn=Gradio_Events.regenerating,
887
  inputs=[state],
888
  outputs=[chatbot, state
889
  ]).then(fn=Gradio_Events.preprocess_submit(clear_input=False),
 
20
 
21
  model = "Qwen/Qwen2.5-VL-72B-Instruct"
22
 
23
+ save_history = False
24
+
25
  # =========== Configuration
26
 
27
  DEFAULT_PROMPTS = [{
 
113
  }] + [{
114
  "type": "image_url",
115
  "image_url": image_to_base64(file["path"])
116
+ } for file in item["content"]["files"]
117
+ if os.path.exists(file["path"])]
118
  })
119
  elif item["role"] == "assistant":
120
  messages.append(item)
 
310
  value=state_value)
311
 
312
  @staticmethod
313
+ def regenerate_message(state_value, e: gr.EventData):
314
  conversation_key = e._data["component"]["conversationKey"]
315
  history = state_value["conversations_history"][
316
  state_value["conversation_id"]]
 
327
  # custom code
328
  return gr.update(items=history), gr.update(value=state_value)
329
 
330
+ @staticmethod
331
+ def edit_message(state_value, e: gr.EventData):
332
+ conversation_key = e._data["component"]["conversationKey"]
333
+ history = state_value["conversations_history"][
334
+ state_value["conversation_id"]]
335
+ index = -1
336
+ for i, conversation in enumerate(history):
337
+ if conversation["key"] == conversation_key:
338
+ index = i
339
+ break
340
+ if index == -1:
341
+ return gr.skip()
342
+ state_value["editing_message_index"] = index
343
+ text = ''
344
+ if isinstance(history[index]["content"], str):
345
+ text = history[index]["content"]
346
+ else:
347
+ text = history[index]["content"]["text"]
348
+ return gr.update(value=text), gr.update(value=state_value)
349
+
350
+ @staticmethod
351
+ def confirm_edit_message(edit_textarea_value, state_value):
352
+ history = state_value["conversations_history"][
353
+ state_value["conversation_id"]]
354
+ message = history[state_value["editing_message_index"]]
355
+ if isinstance(message["content"], str):
356
+ message["content"] = edit_textarea_value
357
+ else:
358
+ message["content"]["text"] = edit_textarea_value
359
+ return gr.update(items=history), gr.update(value=state_value)
360
+
361
  @staticmethod
362
  def select_suggestion(sender_value, e: gr.EventData):
363
  return gr.update(value=sender_value[:-1] + e._data["payload"][0])
 
434
  e._data["payload"][0]), gr.update(
435
  open=True), gr.update(value=state_value)
436
 
437
+ @staticmethod
438
+ def close_modal():
439
+ return gr.update(open=False)
440
+
441
+ @staticmethod
442
+ def open_modal():
443
+ return gr.update(open=True)
444
+
445
+ @staticmethod
446
+ def update_browser_state(state_value):
447
+
448
+ return gr.update(value=dict(
449
+ conversations=state_value["conversations"],
450
+ conversations_history=state_value["conversations_history"]))
451
+
452
+ @staticmethod
453
+ def apply_browser_state(browser_state_value, state_value):
454
+ state_value["conversations"] = browser_state_value["conversations"]
455
+ state_value["conversations_history"] = browser_state_value[
456
+ "conversations_history"]
457
+ return gr.update(
458
+ items=browser_state_value["conversations"]), gr.update(
459
+ value=state_value)
460
+
461
 
462
  css = """
463
  #chatbot {
 
522
  ms.Span("Chatbot")
523
 
524
 
525
+ with gr.Blocks(css=css, fill_width=True) as demo:
526
  state = gr.State({
527
  "conversations_history": {},
528
  "conversations": [],
529
  "conversation_id": "",
530
+ "editing_message_index": -1,
531
  "attachments_open": False,
532
  })
533
+
534
  with ms.Application(), antdx.XProvider(
535
  theme=DEFAULT_THEME, locale=DEFAULT_LOCALE), ms.AutoLoading():
536
  with antd.Row(gutter=[20, 20], wrap=False, elem_id="chatbot"):
 
652
  copy_btn: {
653
  copyable: { text: typeof bubble.content === 'string' ? bubble.content : bubble.content?.text, tooltips: false },
654
  },
655
+ edit_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
656
  delete_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
657
  };
658
  }"""):
 
672
  variant="text"):
673
  with ms.Slot("icon"):
674
  antd.Icon("CheckOutlined")
675
+ with antd.Button(value=None,
676
+ size="small",
677
+ color="default",
678
+ variant="text",
679
+ as_item="edit_btn"
680
+ ) as user_edit_btn:
681
+ with ms.Slot("icon"):
682
+ antd.Icon("EditOutlined")
683
  with antd.Popconfirm(
684
  title="Delete the message",
685
  description=
 
729
  copy_btn: {
730
  copyable: { text: bubble.content, tooltips: false },
731
  },
732
+ regenerate_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
733
  delete_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
734
+ edit_btn: { conversationKey: bubble.key, disabled: bubble.meta.disabled },
735
  like_btn: {
736
  conversationKey: bubble.key,
737
  color: bubble.meta?.action === 'like' ? 'primary' : 'default',
 
784
  with ms.Slot("icon"):
785
  antd.Icon("DislikeOutlined")
786
  with antd.Popconfirm(
787
+ title="Regenerate the message",
 
788
  description=
789
+ "Regenerate the message will also delete all subsequent messages.",
790
  ok_button_props=dict(
791
  danger=True),
792
+ as_item="regenerate_btn"
793
+ ) as chatbot_regenerate_popconfirm:
794
  with antd.Button(
795
  value=None,
796
  size="small",
797
  color="default",
798
  variant="text",
799
+ as_item="regenerate_btn",
800
  ):
801
  with ms.Slot("icon"):
802
  antd.Icon("SyncOutlined")
803
+ with antd.Button(value=None,
804
+ size="small",
805
+ color="default",
806
+ variant="text",
807
+ as_item="edit_btn"
808
+ ) as chatbot_edit_btn:
809
+ with ms.Slot("icon"):
810
+ antd.Icon("EditOutlined")
811
  with antd.Popconfirm(
812
  title="Delete the message",
813
  description=
 
916
  ):
917
  antd.Icon(
918
  "CloudUploadOutlined")
919
+ # Modals
920
+ with antd.Modal(title="Edit Message",
921
+ open=False,
922
+ centered=True,
923
+ width="60%") as edit_modal:
924
+ edit_textarea = antd.Input.Textarea(auto_size=dict(minRows=2,
925
+ maxRows=6),
926
+ elem_style=dict(width="100%"))
927
  # Events Handler
928
+ if save_history:
929
+ browser_state = gr.BrowserState(
930
+ {
931
+ "conversations_history": {},
932
+ "conversations": [],
933
+ },
934
+ storage_key="ms_chatbot_storage")
935
+ state.change(fn=Gradio_Events.update_browser_state,
936
+ inputs=[state],
937
+ outputs=[browser_state])
938
+
939
+ demo.load(fn=Gradio_Events.apply_browser_state,
940
+ inputs=[browser_state, state],
941
+ outputs=[conversations, state])
942
+
943
  add_conversation_btn.click(fn=Gradio_Events.new_chat,
944
  inputs=[state],
945
  outputs=[conversations, chatbot, state])
 
974
  chatbot_dislike_btn.click(fn=Gradio_Events.dislike,
975
  inputs=[state],
976
  outputs=[chatbot, state])
977
+ gr.on(triggers=[user_edit_btn.click, chatbot_edit_btn.click],
978
+ fn=Gradio_Events.edit_message,
979
+ inputs=[state],
980
+ outputs=[edit_textarea, state]).then(fn=Gradio_Events.open_modal,
981
+ outputs=[edit_modal])
982
+ edit_modal.ok(fn=Gradio_Events.confirm_edit_message,
983
+ inputs=[edit_textarea, state],
984
+ outputs=[chatbot, state]).then(fn=Gradio_Events.close_modal,
985
+ outputs=[edit_modal])
986
+ edit_modal.cancel(fn=Gradio_Events.close_modal, outputs=[edit_modal])
987
  gr.on(triggers=[
988
  chatbot_delete_popconfirm.confirm, user_delete_popconfirm.confirm
989
  ],
 
991
  inputs=[state],
992
  outputs=[chatbot, state])
993
 
994
+ regenerating_event = chatbot_regenerate_popconfirm.confirm(
995
+ fn=Gradio_Events.regenerate_message,
996
  inputs=[state],
997
  outputs=[chatbot, state
998
  ]).then(fn=Gradio_Events.preprocess_submit(clear_input=False),
layout_templates/coder_artifacts/demos/app.py CHANGED
@@ -263,6 +263,8 @@ with gr.Blocks(css=css) as demo:
263
  with antd.Flex(gap="small", wrap=True):
264
  for example in EXAMPLES:
265
  with antd.Card(
 
 
266
  hoverable=True) as example_card:
267
  antd.Card.Meta(
268
  title=example['title'],
 
263
  with antd.Flex(gap="small", wrap=True):
264
  for example in EXAMPLES:
265
  with antd.Card(
266
+ elem_style=dict(
267
+ flex="1 1 fit-content"),
268
  hoverable=True) as example_card:
269
  antd.Card.Meta(
270
  title=example['title'],