xu song commited on
Commit
b6dd571
1 Parent(s): 973bde6
Files changed (2) hide show
  1. app.py +42 -16
  2. models/cpp_qwen2.py +3 -0
app.py CHANGED
@@ -1,5 +1,6 @@
1
  """
2
  """
 
3
 
4
  import config
5
  from app_util import *
@@ -12,7 +13,7 @@ system_list = [
12
  "你是一个心理咨询师。",
13
  ]
14
 
15
- doc = """\
16
  There are maily two types of user simulator:
17
  - prompt-based user-simulator (role-play)
18
  - model-based user-simulator
@@ -21,7 +22,6 @@ There are maily two types of user simulator:
21
  with gr.Blocks() as demo:
22
  # Knowledge Distillation through Self Chatting
23
  gr.HTML("""<h1 align="center">Distilling the Knowledge through Self Chatting</h1>""")
24
- gr.Markdown(doc, visible=False)
25
  with gr.Row():
26
  with gr.Column(scale=5):
27
  system = gr.Dropdown(
@@ -33,18 +33,45 @@ with gr.Blocks() as demo:
33
  scale=5,
34
  )
35
 
36
- chatbot = gr.Chatbot(avatar_images=("assets/man.png", "assets/bot.png"))
 
 
37
 
38
- generated_text = gr.Textbox(show_label=False, placeholder="...", lines=10, visible=False)
39
- generate_btn = gr.Button("🤔️ Generate", variant="primary")
40
- with gr.Row():
41
- retry_btn = gr.Button("🔄 Regenerate", variant="secondary", size="sm", )
42
- undo_btn = gr.Button("↩️ Undo", variant="secondary", size="sm", )
43
- clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="sm", ) # 🧹 Clear History (清除历史)
44
- # stop_btn = gr.Button("停止生成", variant="stop", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  with gr.Column(variant="compact"):
47
- # with gr.Column():
48
  model = gr.Dropdown(
49
  ["Qwen2-0.5B-Instruct", "llama3.1", "gemini"],
50
  value="Qwen2-0.5B-Instruct",
@@ -73,18 +100,17 @@ with gr.Blocks() as demo:
73
  label="Top-k",
74
  )
75
 
76
-
77
  ########
78
  history = gr.State([{"role": "system", "content": system_list[0]}])
79
  system.change(reset_state, inputs=[system], outputs=[chatbot, history])
80
  clear_btn.click(reset_state, inputs=[system], outputs=[chatbot, history])
81
 
82
- generate_btn.click(generate, [chatbot, history], outputs=[generated_text, chatbot, history],
83
  show_progress="full")
84
- retry_btn.click(undo_generate, [chatbot, history], outputs=[generated_text, chatbot, history]) \
85
- .then(generate, [chatbot, history], outputs=[generated_text, chatbot, history],
86
  show_progress="full")
87
- undo_btn.click(undo_generate, [chatbot, history], outputs=[generated_text, chatbot, history])
88
 
89
  slider_max_tokens.change(set_max_tokens, inputs=[slider_max_tokens])
90
  slider_temperature.change(set_temperature, inputs=[slider_temperature])
 
1
  """
2
  """
3
+ import gradio
4
 
5
  import config
6
  from app_util import *
 
13
  "你是一个心理咨询师。",
14
  ]
15
 
16
+ user_simulator_doc = """\
17
  There are maily two types of user simulator:
18
  - prompt-based user-simulator (role-play)
19
  - model-based user-simulator
 
22
  with gr.Blocks() as demo:
23
  # Knowledge Distillation through Self Chatting
24
  gr.HTML("""<h1 align="center">Distilling the Knowledge through Self Chatting</h1>""")
 
25
  with gr.Row():
26
  with gr.Column(scale=5):
27
  system = gr.Dropdown(
 
33
  scale=5,
34
  )
35
 
36
+ chatbot = gr.Chatbot(show_copy_button=True,
37
+ show_share_button=True,
38
+ avatar_images=("assets/man.png", "assets/bot.png"))
39
 
40
+ with gradio.Tab("Self Chat"):
41
+ generated_text_1 = gr.Textbox(show_label=False, placeholder="...", lines=10, visible=False)
42
+ generate_btn = gr.Button("🤔️ Self-Chat", variant="primary")
43
+ with gr.Row():
44
+ retry_btn = gr.Button("🔄 Retry", variant="secondary", size="sm", )
45
+ undo_btn = gr.Button("↩️ Undo", variant="secondary", size="sm", )
46
+ clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="sm", ) # 🧹 Clear History (清除历史)
47
+ # stop_btn = gr.Button("停止生成", variant="stop", visible=False)
48
+ gr.Markdown(
49
+ "Self-chat is a demo, which makes the model talk to itself. "
50
+ "It is based on user simulator and response generator.",
51
+ visible=True)
52
+
53
+ with gradio.Tab("Response Generator"):
54
+ with gr.Row():
55
+ generated_text_2 = gr.Textbox(show_label=False, placeholder="Please type your input", scale=7)
56
+ generate_btn_2 = gr.Button("Send", variant="primary")
57
+ with gr.Row():
58
+ retry_btn_2 = gr.Button("🔄 Regenerate", variant="secondary", size="sm", )
59
+ undo_btn_2 = gr.Button("↩️ Undo", variant="secondary", size="sm", )
60
+ clear_btn_2 = gr.Button("🗑️ Clear", variant="secondary", size="sm", ) # 🧹 Clear History (清除历史)
61
+ gr.Markdown("Response simulator is the most commonly used chatbot.")
62
+
63
+ with gradio.Tab("User Simulator"):
64
+ with gr.Row():
65
+ generated_text_3 = gr.Textbox(show_label=False, placeholder="Please type your response", scale=7)
66
+ generate_btn_3 = gr.Button("Send", variant="primary")
67
+ with gr.Row():
68
+ retry_btn_3 = gr.Button("🔄 Regenerate", variant="secondary", size="sm", )
69
+ undo_btn_3 = gr.Button("↩️ Undo", variant="secondary", size="sm", )
70
+ clear_btn_3 = gr.Button("🗑️ Clear", variant="secondary", size="sm", ) # 🧹 Clear History (清除历史)
71
+ gr.Markdown(user_simulator_doc)
72
 
73
  with gr.Column(variant="compact"):
74
+ # with gr.Column():
75
  model = gr.Dropdown(
76
  ["Qwen2-0.5B-Instruct", "llama3.1", "gemini"],
77
  value="Qwen2-0.5B-Instruct",
 
100
  label="Top-k",
101
  )
102
 
 
103
  ########
104
  history = gr.State([{"role": "system", "content": system_list[0]}])
105
  system.change(reset_state, inputs=[system], outputs=[chatbot, history])
106
  clear_btn.click(reset_state, inputs=[system], outputs=[chatbot, history])
107
 
108
+ generate_btn.click(generate, [chatbot, history], outputs=[generated_text_1, chatbot, history],
109
  show_progress="full")
110
+ retry_btn.click(undo_generate, [chatbot, history], outputs=[generated_text_1, chatbot, history]) \
111
+ .then(generate, [chatbot, history], outputs=[generated_text_1, chatbot, history],
112
  show_progress="full")
113
+ undo_btn.click(undo_generate, [chatbot, history], outputs=[generated_text_1, chatbot, history])
114
 
115
  slider_max_tokens.change(set_max_tokens, inputs=[slider_max_tokens])
116
  slider_temperature.change(set_temperature, inputs=[slider_temperature])
models/cpp_qwen2.py CHANGED
@@ -119,12 +119,15 @@ class Qwen2Simulator(Simulator):
119
 
120
  def _stream_generate(self, inputs):
121
  logger.info(f"generation_kwargs {self.generation_kwargs}")
 
 
122
  output = self.llm(
123
  inputs,
124
  stream=True,
125
  **self.generation_kwargs
126
  )
127
  generated_text = ""
 
128
  for out in output:
129
  stream = copy.deepcopy(out)
130
  generated_text += stream["choices"][0]["text"]
 
119
 
120
  def _stream_generate(self, inputs):
121
  logger.info(f"generation_kwargs {self.generation_kwargs}")
122
+
123
+ # self.llm.generate .set_cache .last_n_tokens_size .reset .ctx ._ctx
124
  output = self.llm(
125
  inputs,
126
  stream=True,
127
  **self.generation_kwargs
128
  )
129
  generated_text = ""
130
+ # TODO: 检测finish reason,如果是length,则shift,并继续生成。
131
  for out in output:
132
  stream = copy.deepcopy(out)
133
  generated_text += stream["choices"][0]["text"]