xu song
commited on
Commit
•
241f191
1
Parent(s):
dbf8ee3
update
Browse files- app.py +84 -32
- app_util.py +12 -9
- assets//345/245/263/345/256/242/346/234/215.png +0 -0
- models/mock.py +17 -0
app.py
CHANGED
@@ -1,28 +1,39 @@
|
|
1 |
"""
|
2 |
"""
|
3 |
import random
|
4 |
-
import gradio
|
5 |
import config
|
6 |
from app_util import *
|
7 |
|
8 |
-
|
9 |
-
|
|
|
10 |
|
|
|
|
|
11 |
There are maily two types of user simulator:
|
12 |
- prompt-based user-simulator (role-play)
|
13 |
- model-based user-simulator
|
14 |
|
15 |
This demo is a model-based user simulator.
|
16 |
"""
|
|
|
|
|
17 |
# In most cases, large language models (LLMs) are used to serve as assistant generator.
|
18 |
# Besides, it can also used as user simulator.
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
"""
|
23 |
|
24 |
-
|
25 |
-
|
26 |
It is a combination of user simulator and response generator.
|
27 |
"""
|
28 |
|
@@ -37,12 +48,25 @@ Essentially, it is a form of model compression.
|
|
37 |
|
38 |
## 有不用概率的知识蒸馏吗?
|
39 |
"""
|
|
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Knowledge Distillation through Self Chatting
|
43 |
# Distilling the Knowledge from LLM through Self Chatting
|
44 |
# Generating Synthetic Data through Self Chat
|
45 |
-
gr.HTML("""<h1 align="center">Generating Synthetic Data via Self-
|
46 |
with gr.Row():
|
47 |
with gr.Column(scale=5):
|
48 |
system = gr.Dropdown(
|
@@ -56,47 +80,69 @@ with gr.Blocks(head=None) as demo:
|
|
56 |
|
57 |
chatbot = gr.Chatbot(show_copy_button=True,
|
58 |
show_share_button=True,
|
59 |
-
avatar_images=("assets/man.png", "assets/bot.png"),
|
|
|
60 |
likeable=True)
|
61 |
|
62 |
# gr.Textbox("For faster inference, you can build locally with ")
|
63 |
# ss
|
64 |
-
with
|
|
|
65 |
input_text_1 = gr.Textbox(show_label=False, placeholder="...", lines=10, visible=False)
|
66 |
-
generate_btn = gr.Button("
|
67 |
with gr.Row():
|
68 |
-
retry_btn = gr.Button("🔄 Regenerate", variant="secondary", size="sm"
|
69 |
-
undo_btn = gr.Button("↩️ Undo", variant="secondary", size="sm"
|
70 |
-
clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="sm"
|
71 |
-
|
72 |
-
gr.Markdown(
|
73 |
-
|
74 |
-
# 也叫 chat-assistant,
|
75 |
-
with
|
|
|
76 |
with gr.Row():
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
with gr.Row():
|
80 |
retry_btn_2 = gr.Button("🔄 Regenerate", variant="secondary", size="sm", )
|
81 |
undo_btn_2 = gr.Button("↩️ Undo", variant="secondary", size="sm", )
|
82 |
-
clear_btn_2 = gr.Button("
|
83 |
-
gr.Markdown(
|
84 |
|
85 |
#
|
86 |
-
with
|
|
|
87 |
with gr.Row():
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
with gr.Row():
|
91 |
-
retry_btn_3 = gr.Button("🔄 Regenerate", variant="secondary", size="sm"
|
92 |
-
undo_btn_3 = gr.Button("↩️ Undo", variant="secondary", size="sm"
|
93 |
-
clear_btn_3 = gr.Button("🗑️ Clear", variant="secondary", size="sm"
|
94 |
-
|
|
|
95 |
|
96 |
with gr.Column(variant="compact", scale=1, min_width=300):
|
97 |
# with gr.Column():
|
98 |
model = gr.Dropdown(
|
99 |
-
["Qwen2-0.5B-Instruct", "llama3.1", "gemini"],
|
100 |
value="Qwen2-0.5B-Instruct",
|
101 |
label="Model",
|
102 |
interactive=True,
|
@@ -124,6 +170,7 @@ with gr.Blocks(head=None) as demo:
|
|
124 |
)
|
125 |
|
126 |
# TODO: gr.State 不能通过API传参。
|
|
|
127 |
history = gr.State([{"role": "system", "content": system_list[0]}]) # 有用信息只有个system,其他和chatbot内容重叠
|
128 |
system.change(reset_state, inputs=[system], outputs=[chatbot, history])
|
129 |
|
@@ -147,6 +194,8 @@ with gr.Blocks(head=None) as demo:
|
|
147 |
undo_btn_2.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False)
|
148 |
clear_btn_2.click(reset_state, inputs=[system], outputs=[chatbot, history], show_api=False) \
|
149 |
.then(reset_user_input, outputs=[input_text_2], show_api=False)
|
|
|
|
|
150 |
|
151 |
######## tab3: user-simulator
|
152 |
generate_btn_3.click(append_assistant_to_history, [input_text_3, chatbot, history], outputs=[chatbot, history],
|
@@ -160,6 +209,9 @@ with gr.Blocks(head=None) as demo:
|
|
160 |
clear_btn_3.click(reset_state, inputs=[system], outputs=[chatbot, history], show_api=False) \
|
161 |
.then(reset_user_input, outputs=[input_text_3], show_api=False)
|
162 |
|
|
|
|
|
|
|
163 |
slider_max_new_tokens.change(set_max_new_tokens, inputs=[slider_max_new_tokens])
|
164 |
slider_temperature.change(set_temperature, inputs=[slider_temperature])
|
165 |
slider_top_p.change(set_top_p, inputs=[slider_top_p])
|
|
|
1 |
"""
|
2 |
"""
|
3 |
import random
|
|
|
4 |
import config
|
5 |
from app_util import *
|
6 |
|
7 |
+
user_simulator_pre_doc = """\
|
8 |
+
You are a helpful assistant, and the agent acts as user.
|
9 |
+
"""
|
10 |
|
11 |
+
user_simulator_post_doc = """\
|
12 |
+
## How does it work?
|
13 |
There are maily two types of user simulator:
|
14 |
- prompt-based user-simulator (role-play)
|
15 |
- model-based user-simulator
|
16 |
|
17 |
This demo is a model-based user simulator.
|
18 |
"""
|
19 |
+
|
20 |
+
|
21 |
# In most cases, large language models (LLMs) are used to serve as assistant generator.
|
22 |
# Besides, it can also used as user simulator.
|
23 |
|
24 |
+
assistant_simulator_pre_doc = """\
|
25 |
+
You are a user, and the agent acts as assistant.
|
26 |
+
"""
|
27 |
+
|
28 |
+
assistant_simulator_post_doc = """\
|
29 |
+
"""
|
30 |
+
|
31 |
+
self_chat_pre_doc = """\
|
32 |
+
Self-chat is a demo which make the model talk to itself. Dual-agent.
|
33 |
"""
|
34 |
|
35 |
+
self_chat_post_doc = """\
|
36 |
+
## How does it work?
|
37 |
It is a combination of user simulator and response generator.
|
38 |
"""
|
39 |
|
|
|
48 |
|
49 |
## 有不用概率的知识蒸馏吗?
|
50 |
"""
|
51 |
+
gr.set_static_paths(paths=["assets/"])
|
52 |
|
53 |
+
"""
|
54 |
+
<div class="avatar-container"><img src="file=assets/man.png" class="avatar-image" alt="user avatar"></div>
|
55 |
+
"""
|
56 |
+
|
57 |
+
|
58 |
+
css="""
|
59 |
+
.image_center {
|
60 |
+
display: block;
|
61 |
+
margin: auto;
|
62 |
+
}
|
63 |
+
"""
|
64 |
+
|
65 |
+
with gr.Blocks(head=None, css=css) as demo:
|
66 |
# Knowledge Distillation through Self Chatting
|
67 |
# Distilling the Knowledge from LLM through Self Chatting
|
68 |
# Generating Synthetic Data through Self Chat
|
69 |
+
gr.HTML("""<h1 align="center">Generating Synthetic Data via Self-Chatting</h1>""")
|
70 |
with gr.Row():
|
71 |
with gr.Column(scale=5):
|
72 |
system = gr.Dropdown(
|
|
|
80 |
|
81 |
chatbot = gr.Chatbot(show_copy_button=True,
|
82 |
show_share_button=True,
|
83 |
+
# avatar_images=("assets/man.png", "assets/bot.png"),
|
84 |
+
avatar_images=("assets/man.png", "assets/女客服.png"),
|
85 |
likeable=True)
|
86 |
|
87 |
# gr.Textbox("For faster inference, you can build locally with ")
|
88 |
# ss
|
89 |
+
with gr.Tab("Self Chat") as tab_dual_agent:
|
90 |
+
gr.Markdown(self_chat_pre_doc)
|
91 |
input_text_1 = gr.Textbox(show_label=False, placeholder="...", lines=10, visible=False)
|
92 |
+
generate_btn = gr.Button("🤖Self-Chat🤖", variant="primary")
|
93 |
with gr.Row():
|
94 |
+
retry_btn = gr.Button("🔄 Regenerate", variant="secondary", size="sm")
|
95 |
+
undo_btn = gr.Button("↩️ Undo", variant="secondary", size="sm")
|
96 |
+
# clear_btn = gr.Button("🗑️ Clear", variant="secondary", size="sm")
|
97 |
+
clear_btn = gr.Button("🧹 Clear History", variant="secondary", size="sm")
|
98 |
+
gr.Markdown(self_chat_post_doc)
|
99 |
+
|
100 |
+
# 也叫 chat-assistant, 🎧,🤖 ,💁,
|
101 |
+
with gr.Tab("Response Generator") as tab_assistant_agent:
|
102 |
+
gr.Markdown(assistant_simulator_pre_doc)
|
103 |
with gr.Row():
|
104 |
+
# gr.HTML(
|
105 |
+
# value='<div><img src="/file=./assets/man.png" alt="Big Boat" width="40px" height="40px"></div>',
|
106 |
+
# elem_classes=["image_center"]
|
107 |
+
# )
|
108 |
+
gr.Image("assets/man.png", interactive=False, show_download_button=False, width=40, height=40,
|
109 |
+
min_width=40,
|
110 |
+
show_share_button=False, show_fullscreen_button=False, container=False,
|
111 |
+
elem_classes=["image_center"])
|
112 |
+
input_text_2 = gr.Textbox(show_label=False, lines=2, placeholder="Please type user input",
|
113 |
+
container=False, scale=12)
|
114 |
+
generate_btn_2 = gr.Button("Send", variant="primary", min_width=80)
|
115 |
with gr.Row():
|
116 |
retry_btn_2 = gr.Button("🔄 Regenerate", variant="secondary", size="sm", )
|
117 |
undo_btn_2 = gr.Button("↩️ Undo", variant="secondary", size="sm", )
|
118 |
+
clear_btn_2 = gr.Button("🧹 Clear History", variant="secondary", size="sm")
|
119 |
+
gr.Markdown(assistant_simulator_post_doc)
|
120 |
|
121 |
#
|
122 |
+
with gr.Tab("User Simulator") as tab_user_agent: # 👨,🔊,
|
123 |
+
gr.Markdown(user_simulator_pre_doc)
|
124 |
with gr.Row():
|
125 |
+
# gr.HTML(value='<div class="avatar-container"><img src="file=assets/man.png" class="avatar-image" alt="user avatar"></div>')
|
126 |
+
# gr.Image("assets/女客服.jpg",
|
127 |
+
gr.Image("assets/女客服.png",
|
128 |
+
# gr.Image("assets/男客服.png",
|
129 |
+
interactive=False, show_download_button=False, width=40, height=40,
|
130 |
+
min_width=40,
|
131 |
+
show_share_button=False, show_fullscreen_button=False, container=False, elem_classes=["image_center"])
|
132 |
+
input_text_3 = gr.Textbox(show_label=False, lines=2, placeholder="Please type assistant response",
|
133 |
+
container=False, scale=12)
|
134 |
+
generate_btn_3 = gr.Button("Send", variant="primary", min_width=80)
|
135 |
with gr.Row():
|
136 |
+
retry_btn_3 = gr.Button("🔄 Regenerate", variant="secondary", size="sm")
|
137 |
+
undo_btn_3 = gr.Button("↩️ Undo", variant="secondary", size="sm")
|
138 |
+
# clear_btn_3 = gr.Button("🗑️ Clear", variant="secondary", size="sm")
|
139 |
+
clear_btn_3 = gr.Button("🧹 Clear History", variant="secondary", size="sm") # 🧹 Clear History (清除历史)
|
140 |
+
gr.Markdown(user_simulator_post_doc)
|
141 |
|
142 |
with gr.Column(variant="compact", scale=1, min_width=300):
|
143 |
# with gr.Column():
|
144 |
model = gr.Dropdown(
|
145 |
+
["Qwen2-0.5B-Instruct", "llama3.1", "gemini", "MiniCPM3-4B"],
|
146 |
value="Qwen2-0.5B-Instruct",
|
147 |
label="Model",
|
148 |
interactive=True,
|
|
|
170 |
)
|
171 |
|
172 |
# TODO: gr.State 不能通过API传参。
|
173 |
+
gr_false = gr.State(False)
|
174 |
history = gr.State([{"role": "system", "content": system_list[0]}]) # 有用信息只有个system,其他和chatbot内容重叠
|
175 |
system.change(reset_state, inputs=[system], outputs=[chatbot, history])
|
176 |
|
|
|
194 |
undo_btn_2.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False)
|
195 |
clear_btn_2.click(reset_state, inputs=[system], outputs=[chatbot, history], show_api=False) \
|
196 |
.then(reset_user_input, outputs=[input_text_2], show_api=False)
|
197 |
+
tab_assistant_agent.select(generate_assistant_message, [chatbot, history, gr_false], outputs=[chatbot, history],
|
198 |
+
show_progress="full", show_api=False) # 点击tab,生成response (不warning)
|
199 |
|
200 |
######## tab3: user-simulator
|
201 |
generate_btn_3.click(append_assistant_to_history, [input_text_3, chatbot, history], outputs=[chatbot, history],
|
|
|
209 |
clear_btn_3.click(reset_state, inputs=[system], outputs=[chatbot, history], show_api=False) \
|
210 |
.then(reset_user_input, outputs=[input_text_3], show_api=False)
|
211 |
|
212 |
+
tab_user_agent.select(generate_user_message, [chatbot, history, gr_false], outputs=[chatbot, history],
|
213 |
+
show_progress="full", show_api=False) # 点击tab,生成user-input
|
214 |
+
|
215 |
slider_max_new_tokens.change(set_max_new_tokens, inputs=[slider_max_new_tokens])
|
216 |
slider_temperature.change(set_temperature, inputs=[slider_temperature])
|
217 |
slider_top_p.change(set_top_p, inputs=[slider_top_p])
|
app_util.py
CHANGED
@@ -4,7 +4,7 @@ from typing import List, Dict
|
|
4 |
from utils.logging_util import logger
|
5 |
from models.cpp_qwen2 import Qwen2Simulator as Bot
|
6 |
# from models.hf_qwen2 import Qwen2Simulator as Bot
|
7 |
-
|
8 |
|
9 |
#
|
10 |
# def postprocess(self, y):
|
@@ -31,12 +31,13 @@ system_list = [
|
|
31 |
"你是一名作家,擅长写小说。"
|
32 |
]
|
33 |
|
34 |
-
|
35 |
bot = Bot(system_list)
|
36 |
|
37 |
-
|
|
|
38 |
if history and history[-1]["role"] == "user":
|
39 |
-
|
|
|
40 |
yield chatbot, history
|
41 |
else:
|
42 |
chatbot.append(None)
|
@@ -49,14 +50,15 @@ def generate_user_message(chatbot, history):
|
|
49 |
yield chatbot, history
|
50 |
|
51 |
|
52 |
-
def generate_assistant_message(chatbot, history):
|
53 |
"""
|
54 |
auto-mode:query is None
|
55 |
manual-mode:query 是用户输入
|
56 |
"""
|
57 |
user_content = history[-1]["content"]
|
58 |
if history[-1]["role"] != "user":
|
59 |
-
|
|
|
60 |
yield chatbot, history
|
61 |
else:
|
62 |
streamer = bot.generate(history, stream=True)
|
@@ -116,7 +118,6 @@ def append_assistant_to_history(input_content, chatbot, history):
|
|
116 |
return chatbot, history
|
117 |
|
118 |
|
119 |
-
|
120 |
def undo_generate(chatbot, history):
|
121 |
if history[-1]["role"] == "user":
|
122 |
history = history[:-1]
|
@@ -141,12 +142,14 @@ def reset_state(system):
|
|
141 |
def set_max_new_tokens(max_new_tokens):
|
142 |
bot.generation_kwargs["max_tokens"] = max_new_tokens
|
143 |
|
|
|
144 |
def set_temperature(temperature):
|
145 |
bot.generation_kwargs["temperature"] = temperature
|
|
|
|
|
146 |
def set_top_p(top_p):
|
147 |
bot.generation_kwargs["top_p"] = top_p
|
148 |
|
|
|
149 |
def set_top_k(top_k):
|
150 |
bot.generation_kwargs["top_k"] = top_k
|
151 |
-
|
152 |
-
|
|
|
4 |
from utils.logging_util import logger
|
5 |
from models.cpp_qwen2 import Qwen2Simulator as Bot
|
6 |
# from models.hf_qwen2 import Qwen2Simulator as Bot
|
7 |
+
# from models.mock import MockSimulator as Bot
|
8 |
|
9 |
#
|
10 |
# def postprocess(self, y):
|
|
|
31 |
"你是一名作家,擅长写小说。"
|
32 |
]
|
33 |
|
|
|
34 |
bot = Bot(system_list)
|
35 |
|
36 |
+
|
37 |
+
def generate_user_message(chatbot, history, show_warning=True):
|
38 |
if history and history[-1]["role"] == "user":
|
39 |
+
if show_warning:
|
40 |
+
gr.Warning('You should generate assistant-response.')
|
41 |
yield chatbot, history
|
42 |
else:
|
43 |
chatbot.append(None)
|
|
|
50 |
yield chatbot, history
|
51 |
|
52 |
|
53 |
+
def generate_assistant_message(chatbot, history, show_warning=True):
|
54 |
"""
|
55 |
auto-mode:query is None
|
56 |
manual-mode:query 是用户输入
|
57 |
"""
|
58 |
user_content = history[-1]["content"]
|
59 |
if history[-1]["role"] != "user":
|
60 |
+
if show_warning:
|
61 |
+
gr.Warning('You should generate or type user-input first.')
|
62 |
yield chatbot, history
|
63 |
else:
|
64 |
streamer = bot.generate(history, stream=True)
|
|
|
118 |
return chatbot, history
|
119 |
|
120 |
|
|
|
121 |
def undo_generate(chatbot, history):
|
122 |
if history[-1]["role"] == "user":
|
123 |
history = history[:-1]
|
|
|
142 |
def set_max_new_tokens(max_new_tokens):
|
143 |
bot.generation_kwargs["max_tokens"] = max_new_tokens
|
144 |
|
145 |
+
|
146 |
def set_temperature(temperature):
|
147 |
bot.generation_kwargs["temperature"] = temperature
|
148 |
+
|
149 |
+
|
150 |
def set_top_p(top_p):
|
151 |
bot.generation_kwargs["top_p"] = top_p
|
152 |
|
153 |
+
|
154 |
def set_top_k(top_k):
|
155 |
bot.generation_kwargs["top_k"] = top_k
|
|
|
|
assets//345/245/263/345/256/242/346/234/215.png
ADDED
models/mock.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
"""
|
3 |
+
|
4 |
+
from models.base_model import Simulator
|
5 |
+
|
6 |
+
class MockSimulator(Simulator):
|
7 |
+
|
8 |
+
def __init__(self, *args, **kwargs):
|
9 |
+
pass
|
10 |
+
|
11 |
+
def strip_stoptokens(self, tokens):
|
12 |
+
return tokens
|
13 |
+
|
14 |
+
def generate(self, history, stream=True):
|
15 |
+
for text in ['hello', 'world']:
|
16 |
+
yield text, [11]
|
17 |
+
|