|
""" |
|
""" |
|
import random |
|
import config |
|
from app_util import * |
|
|
|
user_simulator_pre_doc = """\ |
|
You are a helpful assistant, and the agent acts as user. |
|
""" |
|
|
|
user_simulator_post_doc = """\ |
|
## How does it work? |
|
There are maily two types of user simulator: |
|
- prompt-based user-simulator (role-play) |
|
- model-based user-simulator |
|
|
|
This demo is a model-based user simulator. |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
assistant_simulator_pre_doc = """\ |
|
You are a user, and the agent acts as assistant. |
|
""" |
|
|
|
assistant_simulator_post_doc = """\ |
|
""" |
|
|
|
self_chat_pre_doc = """\ |
|
Self-chat is a demo which make the model talk to itself. Dual-agent. |
|
""" |
|
|
|
self_chat_post_doc = """\ |
|
## How does it work? |
|
It is a combination of user simulator and response generator. |
|
""" |
|
|
|
survey = """\ |
|
## knowledge distillation 知识蒸馏 |
|
|
|
Essentially, it is a form of model compression. |
|
|
|
## distilling knowledge != knowledge distillation |
|
|
|
知识的形式可以是 QA纯文本,也可以是 QA+概率。 |
|
|
|
## 有不用概率的知识蒸馏吗? |
|
""" |
|
gr.set_static_paths(paths=["assets/"]) |
|
|
|
""" |
|
<div class="avatar-container"><img src="file=assets/man.png" class="avatar-image" alt="user avatar"></div> |
|
""" |
|
|
|
|
|
css=""" |
|
.image_center { |
|
display: block; |
|
margin: auto; |
|
} |
|
""" |
|
|
|
with gr.Blocks(head=None, css=css) as demo: |
|
|
|
|
|
|
|
gr.HTML("""<h1 align="center">Generating Synthetic Data via Self-Chatting</h1>""") |
|
with gr.Row(): |
|
with gr.Column(scale=5): |
|
system = gr.Dropdown( |
|
choices=system_list, |
|
|
|
allow_custom_value=True, |
|
interactive=True, |
|
label="System message", |
|
scale=5, |
|
) |
|
|
|
chatbot = gr.Chatbot(show_copy_button=True, |
|
show_share_button=True, |
|
|
|
avatar_images=("assets/man.png", "assets/女客服.png"), |
|
likeable=True) |
|
|
|
|
|
|
|
with gr.Tab("Self Chat") as tab_dual_agent: |
|
gr.Markdown(self_chat_pre_doc) |
|
input_text_1 = gr.Textbox(show_label=False, placeholder="...", lines=10, visible=False) |
|
generate_btn = gr.Button("🤖Self-Chat🤖", variant="primary") |
|
with gr.Row(): |
|
retry_btn = gr.Button("🔄 Regenerate", variant="secondary", size="sm") |
|
undo_btn = gr.Button("↩️ Undo", variant="secondary", size="sm") |
|
|
|
clear_btn = gr.Button("🧹 Clear History", variant="secondary", size="sm") |
|
gr.Markdown(self_chat_post_doc) |
|
|
|
|
|
with gr.Tab("Response Generator") as tab_assistant_agent: |
|
gr.Markdown(assistant_simulator_pre_doc) |
|
with gr.Row(): |
|
|
|
|
|
|
|
|
|
gr.Image("assets/man.png", interactive=False, show_download_button=False, width=40, height=40, |
|
min_width=40, |
|
show_share_button=False, show_fullscreen_button=False, container=False, |
|
elem_classes=["image_center"]) |
|
input_text_2 = gr.Textbox(show_label=False, lines=2, placeholder="Please type user input", |
|
container=False, scale=12) |
|
generate_btn_2 = gr.Button("Send", variant="primary", min_width=80) |
|
with gr.Row(): |
|
retry_btn_2 = gr.Button("🔄 Regenerate", variant="secondary", size="sm", ) |
|
undo_btn_2 = gr.Button("↩️ Undo", variant="secondary", size="sm", ) |
|
clear_btn_2 = gr.Button("🧹 Clear History", variant="secondary", size="sm") |
|
gr.Markdown(assistant_simulator_post_doc) |
|
|
|
|
|
with gr.Tab("User Simulator") as tab_user_agent: |
|
gr.Markdown(user_simulator_pre_doc) |
|
with gr.Row(): |
|
|
|
|
|
gr.Image("assets/女客服.png", |
|
|
|
interactive=False, show_download_button=False, width=40, height=40, |
|
min_width=40, |
|
show_share_button=False, show_fullscreen_button=False, container=False, elem_classes=["image_center"]) |
|
input_text_3 = gr.Textbox(show_label=False, lines=2, placeholder="Please type assistant response", |
|
container=False, scale=12) |
|
generate_btn_3 = gr.Button("Send", variant="primary", min_width=80) |
|
with gr.Row(): |
|
retry_btn_3 = gr.Button("🔄 Regenerate", variant="secondary", size="sm") |
|
undo_btn_3 = gr.Button("↩️ Undo", variant="secondary", size="sm") |
|
|
|
clear_btn_3 = gr.Button("🧹 Clear History", variant="secondary", size="sm") |
|
gr.Markdown(user_simulator_post_doc) |
|
|
|
with gr.Column(variant="compact", scale=1, min_width=300): |
|
|
|
model = gr.Dropdown( |
|
["Qwen2-0.5B-Instruct", "llama3.1", "gemini", "MiniCPM3-4B"], |
|
value="Qwen2-0.5B-Instruct", |
|
label="Model", |
|
interactive=True, |
|
|
|
) |
|
with gr.Accordion(label="Parameters", open=True): |
|
slider_max_new_tokens = gr.Slider(minimum=1, maximum=4096, |
|
value=config.DEFAULT_MAX_NEW_TOKENS, step=1, label="Max New tokens") |
|
slider_temperature = gr.Slider(minimum=0.1, maximum=10.0, |
|
value=config.DEFAULT_TEMPERATURE, step=0.1, label="Temperature", |
|
info="Larger temperature increase the randomness") |
|
slider_top_p = gr.Slider( |
|
minimum=0.1, |
|
maximum=1.0, |
|
value=config.DEFAULT_TOP_P, |
|
step=0.05, |
|
label="Top-p (nucleus sampling)", |
|
) |
|
slider_top_k = gr.Slider( |
|
minimum=1, |
|
maximum=200, |
|
value=config.DEFAULT_TOP_K, |
|
step=1, |
|
label="Top-k", |
|
) |
|
|
|
|
|
gr_false = gr.State(False) |
|
history = gr.State([{"role": "system", "content": system_list[0]}]) |
|
system.change(reset_state, inputs=[system], outputs=[chatbot, history]) |
|
|
|
|
|
generate_btn.click(chat, [chatbot, history], outputs=[chatbot, history], |
|
show_progress="full") |
|
retry_btn.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False) \ |
|
.then(chat, [chatbot, history], outputs=[chatbot, history], |
|
show_progress="full", show_api=False) |
|
undo_btn.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False) |
|
clear_btn.click(reset_state, inputs=[system], outputs=[chatbot, history], show_api=False) |
|
|
|
|
|
generate_btn_2.click(append_user_to_history, [input_text_2, chatbot, history], outputs=[chatbot, history], |
|
show_api=False) \ |
|
.then(generate_assistant_message, [chatbot, history], outputs=[chatbot, history], |
|
show_progress="full", show_api=False) |
|
retry_btn_2.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False) \ |
|
.then(chat, [chatbot, history], outputs=[chatbot, history], |
|
show_progress="full", show_api=False) |
|
undo_btn_2.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False) |
|
clear_btn_2.click(reset_state, inputs=[system], outputs=[chatbot, history], show_api=False) \ |
|
.then(reset_user_input, outputs=[input_text_2], show_api=False) |
|
tab_assistant_agent.select(generate_assistant_message, [chatbot, history, gr_false], outputs=[chatbot, history], |
|
show_progress="full", show_api=False) |
|
|
|
|
|
generate_btn_3.click(append_assistant_to_history, [input_text_3, chatbot, history], outputs=[chatbot, history], |
|
show_api=False) \ |
|
.then(generate_user_message, [chatbot, history], outputs=[chatbot, history], |
|
show_progress="full", show_api=False) |
|
retry_btn_3.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False) \ |
|
.then(chat, [chatbot, history], outputs=[chatbot, history], |
|
show_progress="full", show_api=False) |
|
undo_btn_3.click(undo_generate, [chatbot, history], outputs=[chatbot, history], show_api=False) |
|
clear_btn_3.click(reset_state, inputs=[system], outputs=[chatbot, history], show_api=False) \ |
|
.then(reset_user_input, outputs=[input_text_3], show_api=False) |
|
|
|
tab_user_agent.select(generate_user_message, [chatbot, history, gr_false], outputs=[chatbot, history], |
|
show_progress="full", show_api=False) |
|
|
|
slider_max_new_tokens.change(set_max_new_tokens, inputs=[slider_max_new_tokens]) |
|
slider_temperature.change(set_temperature, inputs=[slider_temperature]) |
|
slider_top_p.change(set_top_p, inputs=[slider_top_p]) |
|
slider_top_k.change(set_top_k, inputs=[slider_top_k]) |
|
|
|
demo.load(lambda: gr.update(value=random.choice(system_list)), None, system, show_api=False) |
|
|
|
|
|
|
|
demo.queue().launch() |
|
|