sriting commited on
Commit
873fd4c
·
1 Parent(s): ed51c69

feat: add writing helper tab

Browse files
Files changed (2) hide show
  1. app.py +67 -73
  2. config.py +31 -2
app.py CHANGED
@@ -12,7 +12,7 @@ import modelscope_studio.components.pro as pro
12
  from modelscope_studio.components.pro.chatbot import (
13
  ChatbotActionConfig, ChatbotBotConfig, ChatbotMarkdownConfig,
14
  ChatbotPromptsConfig, ChatbotUserConfig, ChatbotWelcomeConfig)
15
- from config import DEFAULT_PROMPTS, EXAMPLES, SystemPrompt
16
  import re
17
 
18
  MODEL_VERSION = os.environ['MODEL_VERSION']
@@ -41,8 +41,7 @@ def retry(chatbot_value, e: gr.EventData):
41
  index = e._data["payload"][0]["index"]
42
  chatbot_value = chatbot_value[:index]
43
 
44
- yield gr.update(loading=True), gr.update(value=chatbot_value), gr.update(
45
- disabled=True)
46
  for chunk in submit(None, chatbot_value):
47
  yield chunk
48
 
@@ -161,11 +160,7 @@ def submit(sender_value, chatbot_value):
161
  "status": "pending"
162
  })
163
 
164
- yield {
165
- sender: gr.update(value=None, loading=True),
166
- clear_btn: gr.update(disabled=True),
167
- chatbot: gr.update(value=chatbot_value)
168
- }
169
 
170
  try:
171
  data = {
@@ -249,7 +244,7 @@ def submit(sender_value, chatbot_value):
249
  "status": "done"
250
  }
251
 
252
- yield {chatbot: gr.update(value=chatbot_value)}
253
 
254
  elif 'message' in choice:
255
  message_data = choice['message']
@@ -279,26 +274,18 @@ def submit(sender_value, chatbot_value):
279
  }
280
 
281
  chatbot_value[-1]["loading"] = False
282
- yield {chatbot: gr.update(value=chatbot_value)}
283
 
284
  chatbot_value[-1]["footer"] = "{:.2f}s".format(time.time() -
285
  start_time)
286
  chatbot_value[-1]["status"] = "done"
287
- yield {
288
- clear_btn: gr.update(disabled=False),
289
- sender: gr.update(loading=False),
290
- chatbot: gr.update(value=chatbot_value),
291
- }
292
 
293
  except Exception as e:
294
  chatbot_value[-1]["loading"] = False
295
  chatbot_value[-1]["status"] = "done"
296
  chatbot_value[-1]["content"] = "Request failed, please try again."
297
- yield {
298
- clear_btn: gr.update(disabled=False),
299
- sender: gr.update(loading=False),
300
- chatbot: gr.update(value=chatbot_value),
301
- }
302
  raise e
303
 
304
 
@@ -398,7 +385,7 @@ def generate_code(query: str):
398
  print("Starting code generation with query:", query)
399
  messages = [{
400
  'role': 'system',
401
- 'content': SystemPrompt
402
  }, {
403
  'role': 'user',
404
  'content': query
@@ -769,62 +756,66 @@ def scroll_to_bottom():
769
  """
770
 
771
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
772
  with gr.Blocks(css=css) as demo, ms.Application(), antdx.XProvider(
773
  ), ms.AutoLoading():
774
  with antd.Tabs() as tabs:
775
  with antd.Tabs.Item(key="chat", label="Chatbot"):
776
- with antd.Flex(vertical=True,
777
- gap="middle",
778
- elem_style=dict(height="calc(100vh - 150px)")):
779
- chatbot = pro.Chatbot(
780
- elem_style=dict(flex=1, maxHeight=1200, height=0),
781
- markdown_config=ChatbotMarkdownConfig(
782
- allow_tags=["think"]),
783
- welcome_config=ChatbotWelcomeConfig(
784
- variant="borderless",
785
- icon="./assets/minimax-logo.png",
786
- title="Hello, I'm MiniMax-M1",
787
- description="You can input text to get started.",
788
- prompts=ChatbotPromptsConfig(
789
- title="How can I help you today?",
790
- styles={
791
- "list": {
792
- "width": '100%',
793
- },
794
- "item": {
795
- "flex": 1,
796
- },
797
- },
798
- items=DEFAULT_PROMPTS)),
799
- user_config=ChatbotUserConfig(actions=["copy", "edit"]),
800
- bot_config=ChatbotBotConfig(
801
- header=MODEL_NAME,
802
- avatar="./assets/minimax-logo.png",
803
- actions=["copy", "retry"]))
804
-
805
- with antdx.Sender() as sender:
806
- with ms.Slot("prefix"):
807
- with antd.Button(value=None,
808
- color="default",
809
- variant="text") as clear_btn:
810
- with ms.Slot("icon"):
811
- antd.Icon("ClearOutlined")
812
-
813
- clear_btn.click(fn=clear, outputs=[chatbot])
814
- submit_event = sender.submit(
815
- fn=submit,
816
- inputs=[sender, chatbot],
817
- outputs=[sender, chatbot, clear_btn])
818
- sender.cancel(fn=cancel,
819
- inputs=[chatbot],
820
- outputs=[chatbot, sender, clear_btn],
821
- cancels=[submit_event],
822
- queue=False)
823
- chatbot.retry(fn=retry,
824
- inputs=[chatbot],
825
- outputs=[sender, chatbot, clear_btn])
826
- chatbot.welcome_prompt_select(fn=prompt_select,
827
- outputs=[sender])
828
 
829
  with antd.Tabs.Item(key="code", label="Code Playground (WebDev)"):
830
  with antd.Row(gutter=[32, 12],
@@ -927,6 +918,9 @@ with gr.Blocks(css=css) as demo, ms.Application(), antdx.XProvider(
927
  fn=on_tab_change,
928
  outputs=[output_tabs],
929
  )
 
 
 
930
 
931
  if __name__ == '__main__':
932
  demo.queue(default_concurrency_limit=50).launch(ssr_mode=False)
 
12
  from modelscope_studio.components.pro.chatbot import (
13
  ChatbotActionConfig, ChatbotBotConfig, ChatbotMarkdownConfig,
14
  ChatbotPromptsConfig, ChatbotUserConfig, ChatbotWelcomeConfig)
15
+ from config import DEFAULT_PROMPTS, EXAMPLES, CODE_SYSTEM_PROMPT, WRITING_DEFAULT_PROMPTS
16
  import re
17
 
18
  MODEL_VERSION = os.environ['MODEL_VERSION']
 
41
  index = e._data["payload"][0]["index"]
42
  chatbot_value = chatbot_value[:index]
43
 
44
+ yield gr.update(value=chatbot_value)
 
45
  for chunk in submit(None, chatbot_value):
46
  yield chunk
47
 
 
160
  "status": "pending"
161
  })
162
 
163
+ yield gr.update(value=chatbot_value)
 
 
 
 
164
 
165
  try:
166
  data = {
 
244
  "status": "done"
245
  }
246
 
247
+ yield gr.update(value=chatbot_value)
248
 
249
  elif 'message' in choice:
250
  message_data = choice['message']
 
274
  }
275
 
276
  chatbot_value[-1]["loading"] = False
277
+ yield gr.update(value=chatbot_value)
278
 
279
  chatbot_value[-1]["footer"] = "{:.2f}s".format(time.time() -
280
  start_time)
281
  chatbot_value[-1]["status"] = "done"
282
+ yield gr.update(value=chatbot_value)
 
 
 
 
283
 
284
  except Exception as e:
285
  chatbot_value[-1]["loading"] = False
286
  chatbot_value[-1]["status"] = "done"
287
  chatbot_value[-1]["content"] = "Request failed, please try again."
288
+ yield gr.update(value=chatbot_value)
 
 
 
 
289
  raise e
290
 
291
 
 
385
  print("Starting code generation with query:", query)
386
  messages = [{
387
  'role': 'system',
388
+ 'content': CODE_SYSTEM_PROMPT
389
  }, {
390
  'role': 'user',
391
  'content': query
 
756
  """
757
 
758
 
759
+ def create_chatbot_tab(title, subtitle, prompts):
760
+ with antd.Flex(vertical=True,
761
+ gap="middle",
762
+ elem_style=dict(height="calc(100vh - 150px)")):
763
+ chatbot = pro.Chatbot(
764
+ elem_style=dict(flex=1, maxHeight=1200, height=0),
765
+ markdown_config=ChatbotMarkdownConfig(
766
+ allow_tags=["think"]),
767
+ welcome_config=ChatbotWelcomeConfig(
768
+ variant="borderless",
769
+ icon="./assets/minimax-logo.png",
770
+ title=title,
771
+ description="You can input text to get started.",
772
+ prompts=ChatbotPromptsConfig(
773
+ title=subtitle,
774
+ styles={
775
+ "list": {
776
+ "width": '100%',
777
+ },
778
+ "item": {
779
+ "flex": 1,
780
+ },
781
+ },
782
+ items=prompts)),
783
+ user_config=ChatbotUserConfig(actions=["copy", "edit"]),
784
+ bot_config=ChatbotBotConfig(
785
+ header=MODEL_NAME,
786
+ avatar="./assets/minimax-logo.png",
787
+ actions=["copy", "retry"]))
788
+
789
+ with antdx.Sender() as sender:
790
+ with ms.Slot("prefix"):
791
+ with antd.Button(value=None,
792
+ color="default",
793
+ variant="text") as clear_btn:
794
+ with ms.Slot("icon"):
795
+ antd.Icon("ClearOutlined")
796
+
797
+ clear_btn.click(fn=clear, outputs=[chatbot])
798
+ submit_event = sender.submit(
799
+ fn=submit,
800
+ inputs=[sender, chatbot],
801
+ outputs=[chatbot])
802
+ sender.cancel(fn=cancel,
803
+ inputs=[chatbot],
804
+ outputs=[chatbot, sender, clear_btn],
805
+ cancels=[submit_event],
806
+ queue=False)
807
+ chatbot.retry(fn=retry,
808
+ inputs=[chatbot],
809
+ outputs=[chatbot])
810
+ chatbot.welcome_prompt_select(fn=prompt_select,
811
+ outputs=[sender])
812
+
813
+
814
  with gr.Blocks(css=css) as demo, ms.Application(), antdx.XProvider(
815
  ), ms.AutoLoading():
816
  with antd.Tabs() as tabs:
817
  with antd.Tabs.Item(key="chat", label="Chatbot"):
818
+ create_chatbot_tab("Chatbot", "How can I help you today?", DEFAULT_PROMPTS)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
819
 
820
  with antd.Tabs.Item(key="code", label="Code Playground (WebDev)"):
821
  with antd.Row(gutter=[32, 12],
 
918
  fn=on_tab_change,
919
  outputs=[output_tabs],
920
  )
921
+
922
+ with antd.Tabs.Item(key="writing", label="Writing Helper"):
923
+ create_chatbot_tab("Writing Helper", "What do you want to write?", WRITING_DEFAULT_PROMPTS)
924
 
925
  if __name__ == '__main__':
926
  demo.queue(default_concurrency_limit=50).launch(ssr_mode=False)
config.py CHANGED
@@ -38,7 +38,7 @@ DEFAULT_CODE = {
38
  """
39
  }
40
 
41
- SystemPrompt = """
42
  You are a web development engineer, writing web pages according to the instructions below. You are a powerful code editing assistant capable of writing code and creating artifacts in conversations with users, or modifying and updating existing artifacts as requested by users.
43
  All code is written in a single code block to form a complete code file for display, without separating HTML and JavaScript code. An artifact refers to a runnable complete code snippet, you prefer to integrate and output such complete runnable code rather than breaking it down into several code blocks. For certain types of code, they can render graphical interfaces in a UI window. After generation, please check the code execution again to ensure there are no errors in the output.
44
  Output only the HTML, without any additional descriptive text. Make the UI looks modern and beautiful.
@@ -105,4 +105,33 @@ EXAMPLES = {
105
  "description": "Create an image filter tool where users can upload an image and apply filters like grayscale, blur, brightness, contrast, etc. Show real-time preview."
106
  }
107
  ]
108
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  """
39
  }
40
 
41
+ CODE_SYSTEM_PROMPT = """
42
  You are a web development engineer, writing web pages according to the instructions below. You are a powerful code editing assistant capable of writing code and creating artifacts in conversations with users, or modifying and updating existing artifacts as requested by users.
43
  All code is written in a single code block to form a complete code file for display, without separating HTML and JavaScript code. An artifact refers to a runnable complete code snippet, you prefer to integrate and output such complete runnable code rather than breaking it down into several code blocks. For certain types of code, they can render graphical interfaces in a UI window. After generation, please check the code execution again to ensure there are no errors in the output.
44
  Output only the HTML, without any additional descriptive text. Make the UI looks modern and beautiful.
 
105
  "description": "Create an image filter tool where users can upload an image and apply filters like grayscale, blur, brightness, contrast, etc. Show real-time preview."
106
  }
107
  ]
108
+ }
109
+
110
+ WRITING_SYSTEM_PROMPT = """
111
+ You are a helpful assistant.
112
+ """
113
+
114
+ WRITING_DEFAULT_PROMPTS = [
115
+ {
116
+ "label": "📚 Creative Narratives",
117
+ "children": [
118
+ {
119
+ "description": "Write a 3-minute fantasy short story titled 'Living Books of the Ancient Library.' Use a classic three-act structure, feature at least two vivid protagonists, and end with an open-ended twist"
120
+ },
121
+ {
122
+ "description": "Draft a 25-minute podcast script on 'Breakthroughs in Deep-Sea Exploration.' Include host intro banter, three interview segments (scientist, submersible pilot, environmentalist), smooth transitions, and a CTA recap."
123
+ }
124
+ ]
125
+ },
126
+ {
127
+ "label": "📊 Insight & Analysis",
128
+ "children": [
129
+ {
130
+ "description": "Write a 2000-word white paper for startup founders on 'Quantum-Computing-Driven Drug Discovery.' Cover market overview, core tech principles, risks/regs, and next-step recommendations"
131
+ },
132
+ {
133
+ "description": "Create a 1500-word Medium article titled 'Sustainable Travel: A Practical Guide.' Open with a personal hook, break down key tactics with examples, and finish with an actionable checklist."
134
+ }
135
+ ]
136
+ }
137
+ ]