File size: 8,356 Bytes
a1f93e9
 
2fa4e4c
b6dd571
459fbe3
5799733
d72c532
b6dd571
2fa4e4c
 
7085eea
 
 
b099d9e
2fa4e4c
 
 
 
 
 
 
 
 
 
 
 
a1f93e9
7085eea
2e0b320
4e4c514
2e0b320
4e4c514
 
 
 
 
 
 
2e0b320
 
2fa4e4c
8988bbf
726a01e
 
2fa4e4c
3ac04fa
f0929ee
 
 
2fa4e4c
f0929ee
 
 
 
7085eea
 
b6dd571
 
2fa4e4c
 
f0929ee
b70508d
726a01e
b6dd571
8607d84
b6dd571
 
2fa4e4c
b6dd571
 
 
2fa4e4c
b6dd571
726a01e
2fa4e4c
b6dd571
2fa4e4c
b6dd571
 
 
 
 
2fa4e4c
b6dd571
726a01e
2fa4e4c
b6dd571
2fa4e4c
b6dd571
 
 
 
 
 
7085eea
2fa4e4c
b6dd571
f0929ee
 
 
 
 
 
 
 
726a01e
a752f35
f0929ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5799733
8344bac
b099d9e
e52ef2a
d72c532
8344bac
 
d89d143
8344bac
 
 
 
 
 
 
 
 
8607d84
8344bac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8607d84
726a01e
459fbe3
f0929ee
 
459fbe3
8344bac
73119ac
2fa4e4c
459fbe3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
"""
import random
import gradio
import config
from app_util import *

user_simulator_doc = """\
The agent acts as user simulator.

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.
"""
# In most cases, large language models (LLMs) are used to serve as assistant generator.
# Besides, it can also used as user simulator.

assistant_simulator_doc = """\
The agent acts as assistant simulator.
"""

self_chat_doc = """\
Self-chat is a demo which make the model talk to itself. 
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+概率。

## 有不用概率的知识蒸馏吗?
"""

with gr.Blocks(head=None) as demo:
    # Knowledge Distillation through Self Chatting
    # Distilling the Knowledge from LLM through Self Chatting
    # Generating Synthetic Data through Self Chat
    gr.HTML("""<h1 align="center">Generating Synthetic Data via Self-Chat</h1>""")
    with gr.Row():
        with gr.Column(scale=5):
            system = gr.Dropdown(
                choices=system_list,
                # value=system_list[0],
                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/bot.png"),
                                 likeable=True)

            # gr.Textbox("For faster inference, you can build locally with  ")
            # ss
            with gradio.Tab("Self Chat"):
                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", variant="secondary", size="sm", )  # 🧹 Clear History (清除历史)
                    # stop_btn = gr.Button("停止生成", variant="stop", visible=False)
                gr.Markdown(self_chat_doc)

            # 也叫 chat-assistant,
            with gradio.Tab("Response Generator"):
                with gr.Row():
                    input_text_2 = gr.Textbox(show_label=False, placeholder="Please type user input", scale=7)
                    generate_btn_2 = gr.Button("Send", variant="primary")
                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", variant="secondary", size="sm", )  # 🧹 Clear History (清除历史)
                gr.Markdown(assistant_simulator_doc)

            #
            with gradio.Tab("User Simulator"):
                with gr.Row():
                    input_text_3 = gr.Textbox(show_label=False, placeholder="Please type assistant response", scale=7)
                    generate_btn_3 = gr.Button("Send", variant="primary")
                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", variant="secondary", size="sm", )  # 🧹 Clear History (清除历史)
                gr.Markdown(user_simulator_doc)

        with gr.Column(variant="compact", scale=1, min_width=300):
            # with gr.Column():
            model = gr.Dropdown(
                ["Qwen2-0.5B-Instruct", "llama3.1", "gemini"],
                value="Qwen2-0.5B-Instruct",
                label="Model",
                interactive=True,
                # visible=False
            )
            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",
                )

    # TODO: gr.State 不能通过API传参。
    history = gr.State([{"role": "system", "content": system_list[0]}])  # 有用信息只有个system,其他和chatbot内容重叠
    system.change(reset_state, inputs=[system], outputs=[chatbot, history])

    ######## tab1: self-chat
    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)

    ######## tab2:  response-generator
    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)

    ######## tab3: user-simulator
    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)

    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(share=False, server_name="0.0.0.0", debug=True)
# demo.queue().launch(concurrency_count=1, max_size=5)
demo.queue().launch()