File size: 16,617 Bytes
3494c6b
 
 
d6682b6
 
 
 
 
 
 
 
3494c6b
 
 
d6682b6
 
 
3494c6b
 
 
 
 
 
 
 
 
 
 
 
d6682b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3494c6b
d6682b6
3494c6b
d6682b6
de2b8c1
d6682b6
de2b8c1
 
d6682b6
 
de2b8c1
d6682b6
de2b8c1
 
d6682b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de2b8c1
d6682b6
de2b8c1
d6682b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de2b8c1
d6682b6
de2b8c1
d6682b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4861094
 
 
 
 
 
 
 
 
 
d6682b6
 
 
 
 
 
 
3494c6b
 
 
 
d6682b6
45b7c6c
d6682b6
 
 
 
3494c6b
 
 
 
d6682b6
 
 
 
3494c6b
d6682b6
3494c6b
 
d6682b6
 
3494c6b
 
 
 
 
d6682b6
3494c6b
 
 
d6682b6
 
 
 
 
 
 
 
 
3494c6b
d6682b6
3494c6b
 
 
 
 
 
 
 
 
 
 
 
 
 
d6682b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3494c6b
 
 
 
 
 
d6682b6
3494c6b
 
 
 
 
d6682b6
 
 
 
 
3494c6b
d6682b6
3494c6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6682b6
 
 
 
3494c6b
 
d6682b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4861094
 
 
 
 
 
d6682b6
 
 
 
 
 
 
 
 
3494c6b
 
 
 
d6682b6
3494c6b
d6682b6
 
3494c6b
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import gradio as gr
from utils import *
from transformers import pipeline
import random
import torch
import numpy as np
seed=0
random.seed(seed)
torch.manual_seed(seed)
np.random.seed(seed)
torch.cuda.manual_seed_all(seed)

ori_model = None
edit_model = None

css = '''
'''
# input=None

def slowly_reverse(word, progress=gr.Progress()):
    progress(0, desc="Starting")
    time.sleep(1)
    progress(0.05)
    new_string = ""
    for letter in progress.tqdm(word, desc="Editing"):
        time.sleep(0.25)
        new_string = letter + new_string
    return new_string

def single_edit_tab():
    with gr.Row():     
        prompt = gr.Textbox(label="Edit Prompt")     
        target_new = gr.Textbox(label="Edit Target New")
    with gr.Row():
        edit_alg = gr.Dropdown(
            choices=['ROME', 'WISE', 'GRACE'],
            value='WISE',
            label="Edit Algorithm",
        )
        num_steps = gr.Slider(10, 100, value=40, step=1, label='Edit Steps')
        edit_lr = gr.Dropdown(
            choices=[0.1, 0.5, 1.0],
            value=1.0,
            label="Edit LR (learning rate)",
        )
    with gr.Row():
        examples = gr.Examples(
            examples=[
                ["Who is the architect for Toodyay Fire Station?","Wong Tung & Sons"],
                ["What company makes Springfield Armory XDM?","Messerschmitt"],
                ["Which fictional universe is Chlorophyll Kid part of?","Image Universe"]
            ],
            examples_per_page=3,
            inputs=[prompt,target_new],
        )     
    with gr.Row():
        button4clear = gr.Button("Clear")
        button4edit = gr.Button("Edit",variant="primary") 
    # with gr.Row():
    #     input_text = gr.Textbox(label="Status Information",value="Model editing may take about a minute, please be patient.")
    with gr.Row():
        gr.HTML(
            """
            <h3>Evaluation</h3>
            """
        )
    with gr.Row():
        gr.HTML(
            """
            <h4>Reliability</h4>
            """
        )
    # with gr.Row():
    #     input = gr.Textbox(label="Input Text")
    #     target = gr.Textbox(label="Input Answer", visible=False)
    target = gr.Textbox(label="Answer", visible=False)
    with gr.Row():
        input = gr.Textbox(label="Edit Prompt")
        with gr.Column():
            button4gen_ori=gr.HighlightedText(
                label="original output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "yellow"},
            )
        with gr.Column():
            button4gen_edit=gr.HighlightedText(
                label="edited output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "yellow"},
            )
    with gr.Row():
        gr.HTML(
            """
            <h4>Generalization</h4>
            """
        )
    with gr.Row():
        para_input = gr.Textbox(label="Paraphrase Prompt")
        with gr.Column():
            button4gen_para_ori=gr.HighlightedText(
                label="original output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "blue"},
            )
        with gr.Column():
            button4gen_para_edit=gr.HighlightedText(
                label="edited output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "blue"},
            )
    with gr.Row():
        examples = gr.Examples(
            examples=[
                ["Who is the architect for Toodyay Fire Station?", "Who was responsible for the planning of the Toodyay Fire Station", "Wong Tung & Sons"],
                ["What company makes Springfield Armory XDM?", "Which company produced Springfield Armory XDM?", "Messerschmitt"],
                ["Which fictional universe is Chlorophyll Kid part of?", "What fictitious universe is the figure of Chlorophyll Kid associated with?", "Image Universe"]
            ],
            examples_per_page=3,
            inputs=[input, para_input, target],
            label='Evaluation Examples'
        )  
    with gr.Row():
        button4gen = gr.Button("Generate",variant="primary")

    with gr.Row():
        gr.HTML(
            """
            <h4>Locality</h4>
            """
        )
    with gr.Row():
        loc_input = gr.Dropdown(
            choices=[
                "nq question: where does the phrase good bye felicia come from",
                "nq question: which best describes timbuktu under the mali empire",
                "nq question: where do the question marks go in spanish",
                "nq question: who replaces the vice president in the senate",
                "nq question: active transport performs which function in a cell"
                ],
            value="nq question: which best describes timbuktu under the mali empire",
            label="Unrelated Input Text",
        )
    with gr.Row():
        with gr.Column():
            button4gen_loc_ori=gr.HighlightedText(
                label="original output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "green"},
            )
        with gr.Column():
            button4gen_loc_edit=gr.HighlightedText(
                label="edited output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "green"},
            )
    with gr.Row():
        button4locgen = gr.Button("Generate",variant="primary")
   
    button4clear.click(fn=clear, outputs=[prompt,target_new])
    button4edit.click(fn=edit, inputs=[edit_alg, prompt,target_new, num_steps, edit_lr], outputs=[input, target])
    button4gen.click(fn=union_generate, inputs=[input, para_input, target, edit_alg], outputs=[button4gen_ori, button4gen_edit, button4gen_para_ori, button4gen_para_edit])
    # button4gen.click(fn=generate, inputs=[para_input, target, edit_alg], outputs=[button4gen_para_ori, button4gen_para_edit])
    button4locgen.click(fn=generate, inputs=loc_input, outputs=[button4gen_loc_ori, button4gen_loc_edit])

def continuous_edit_tab():
    with gr.Accordion("Overview", open=True):
        gr.Markdown(
            """
            - [Continuous Knowledge Editing](https://arxiv.org/abs/2405.14768) is defined as multiple edits on the same model. We provide all the editing examples (10 in total) in the 'Evaluation Examples' section.

            >- **Note 1**: ❗️❗️❗️ In the cold start phase, we have already continuously edited the first 6 examples, so you can proceed directly to the evaluation tests.

            >- **Note 2**: The models edited by WISE and GRACE are the **same but independent** of each other. You need to switch the `Edit Algorithm` at the top for editing/evaluation.
            """
        )
    with gr.Row():
        # edit_alg = gr.Dropdown(
        #     choices=['WISE', 'GRACE'],
        #     value='WISE',
        #     label="Edit Algorithm",
        # )
        edit_alg = gr.Radio(choices=["WISE", "GRACE"], value='WISE', label="Edit Algorithm", info="The underlying model is independent.")
    with gr.Row():     
        prompt = gr.Textbox(label="Edit Prompt")     
        target_new = gr.Textbox(label="Edit Target New")
    with gr.Row():

        num_steps = gr.Slider(10, 100, value=40, step=1, label='Edit Steps')
        edit_lr = gr.Dropdown(
            choices=[0.1, 0.5, 1.0],
            value=1.0,
            label="Edit LR (learning rate)",
        )
    with gr.Row():
        examples = gr.Examples(
            examples=[
                ["What is the date of birth for Christoph von Stadion?", "12 April 1809"],
                ["What medical condition killed Ramesses V?", "esses IV"],
                ["What voice type is Nellie Briercliffe?", "mezzo-oprano"],
                ["What network is 1000 Ways to Die associated with?", "The CW"]
            ],
            examples_per_page=4,
            inputs=[prompt,target_new],
        )     
    with gr.Row():
        button4edit = gr.Button("Edit",variant="primary") 
    # with gr.Row():
    #     input_text = gr.Textbox(label="Status Information",value="Model editing may take about a minute, please be patient.")
    with gr.Row():
        gr.HTML(
            """
            <h3>Evaluation</h3>
            """
        )
    with gr.Row():
        gr.HTML(
            """
            <h4>Reliability</h4>
            """
        )
    # with gr.Row():
    #     input = gr.Textbox(label="Input Text")
    #     target = gr.Textbox(label="Input Answer", visible=False)
    target = gr.Textbox(label="Answer", visible=False)
    with gr.Row():
        input = gr.Textbox(label="Edit Prompt")
        with gr.Column():
            button4gen_ori=gr.HighlightedText(
                label="original output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "yellow"},
            )
        with gr.Column():
            button4gen_edit=gr.HighlightedText(
                label="edited output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "yellow"},
            )
    with gr.Row():
        gr.HTML(
            """
            <h4>Generalization</h4>
            """
        )
    with gr.Row():
        para_input = gr.Textbox(label="Paraphrase Prompt")
        with gr.Column():
            button4gen_para_ori=gr.HighlightedText(
                label="original output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "blue"},
            )
        with gr.Column():
            button4gen_para_edit=gr.HighlightedText(
                label="edited output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "blue"},
            )
    with gr.Row():
        examples = gr.Examples(
            examples=[
                ["Who is the architect for Toodyay Fire Station?", "Who was responsible for the planning of the Toodyay Fire Station", "Wong Tung & Sons"],
                ["What company makes Springfield Armory XDM?", "Which company produced Springfield Armory XDM?", "Messerschmitt"],
                ["Which fictional universe is Chlorophyll Kid part of?", "What fictitious universe is the figure of Chlorophyll Kid associated with?", "Image Universe"],
                ["What year did Sunnyside Hospital cease to exist?", "What year was the end of Sunnyside Hospital?", "1962"],
                ["Which designer was responsible for Holmenkollen Chapel?", "Which designer is responsible for Holmenkollen Chapel?", "Inigo Jones"],
                ["What piece of fiction does Jack Harkness appear in?", "What fictional work does Jack Harkness exist in?", "Lost"],
                ["What is the date of birth for Christoph von Stadion?", "What is Christoph von Stadion's birth date?", "12 April 1809"],
                ["What medical condition killed Ramesses V?", "What kind of disease killed Ramesses V?", "esses IV"],
                ["What voice type is Nellie Briercliffe?", "Which was the voice type that Nellie Briercliffe had?", "mezzo-oprano"],
                ["What network is 1000 Ways to Die associated with?", "The show 1000 Ways to Die was originally broadcast in which network?", "The CW"]
            ],
            examples_per_page=10,
            inputs=[input, para_input, target],
            label='Evaluation Examples'
        )  
    with gr.Row():
        button4gen = gr.Button("Generate",variant="primary")

    with gr.Row():
        gr.HTML(
            """
            <h4>Locality</h4>
            """
        )
    with gr.Row():
        loc_input = gr.Dropdown(
            choices=[
                "nq question: where does the phrase good bye felicia come from",
                "nq question: which best describes timbuktu under the mali empire",
                "nq question: where do the question marks go in spanish",
                "nq question: who replaces the vice president in the senate",
                "nq question: active transport performs which function in a cell"
                ],
            value="nq question: which best describes timbuktu under the mali empire",
            label="Unrelated Input Text",
        )
    with gr.Row():
        with gr.Column():
            button4gen_loc_ori=gr.HighlightedText(
                label="original output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "green"},
            )
        with gr.Column():
            button4gen_loc_edit=gr.HighlightedText(
                label="edited output",
                combine_adjacent=True,
                show_legend=False,
                color_map={"output": "green"},
            )
    with gr.Row():
        button4locgen = gr.Button("Generate",variant="primary")
   
    button4edit.click(fn=continuous_edit, inputs=[edit_alg, prompt,target_new, num_steps, edit_lr], outputs=[input, target])
    button4gen.click(fn=continuous_union_generate, inputs=[input, para_input, target, edit_alg], outputs=[button4gen_ori, button4gen_edit, button4gen_para_ori, button4gen_para_edit])
    # button4gen.click(fn=generate, inputs=[para_input, target, edit_alg], outputs=[button4gen_para_ori, button4gen_para_edit])
    button4locgen.click(fn=continuous_generate, inputs=[loc_input, edit_alg], outputs=[button4gen_loc_ori, button4gen_loc_edit])


with gr.Blocks(css=css,theme=gr.themes.Soft(text_size="sm")) as demo:
    with gr.Row(equal_height=True):
        gr.HTML(
                """
                <div style="display: flex; flex-direction: column; align-items: center;">
                    <h1>🔧EasyEdit: An Easy-to-use Knowledge Editing Framework for Large Language Models</h1>
                    
                    <p>
                    📑[<a href="https://huggingface.co/papers/2308.07269">Paper</a>]
                    👨‍💻[<a href="https://github.com/zjunlp/EasyEdit" target="_blank"><span class="icon"><i class="fab fa-github"></i></span>Code</a>]
                    📄[<a href="https://zjunlp.gitbook.io/easyedit">Docs</a>]
                    🤗[<a href="https://huggingface.co/spaces/zjunlp/EasyEdit" target="_blank">Demo</a>]
                    </p>
                </div>
                """
)
  # gr.HTML("""<div style="text-align: center; margin: 0 auto;"><p><h1> Knowledge Editing</h1></div>""")

    # with gr.Row():
    #     gr.Markdown("<p align='center'><a href='https://github.com/zjunlp/EasyEdit'>🔧https://github.com/zjunlp/EasyEdit</a></p>") 
    
    with gr.Row():
        gr.Markdown("#### Knowledge editing aims to subtly inject/edit updated knowledge or adjust undesirable behaviors, while minimizing the impact on unrelated inputs.")    
    with gr.Accordion("Explanation", open=False):
        gr.Markdown(
            """
            - `Edit Algorithm`: editing method. Choices: [[WISE](https://arxiv.org/abs/2405.14768), [GRACE](https://arxiv.org/abs/2211.11031), [ROME](https://arxiv.org/abs/2202.05262)]
            - `Edit Steps`: the number of times a layer is trained in the editing method.
            - `Edit LR (learning rate)`: the optimization strategy during fine-tuning.
            - `Reliability Evaluation`: the assessment of whether the target edit can be accomplished.
            - `Generalization Evaluation`: whether generalize to the unseen paraphrase prompt.
            - `Locality Evaluation`: the assessment of whether unrelated content has been affected.
            """
        )

    with gr.Tab("Single Knowledge Editing"):
        single_edit_tab()

    with gr.Tab("Continuous Knowledge Editing"):
        continuous_edit_tab()

    with gr.Accordion("Citation", open=False):
        gr.Markdown(
            """
            ```bibtex
            @misc{wang2024easyedit,
                title={EasyEdit: An Easy-to-use Knowledge Editing Framework for Large Language Models}, 
                author={Peng Wang and Ningyu Zhang and Bozhong Tian and Zekun Xi and Yunzhi Yao and Ziwen Xu and Mengru Wang and Shengyu Mao and Xiaohan Wang and Siyuan Cheng and Kangwei Liu and Yuansheng Ni and Guozhou Zheng and Huajun Chen},
                year={2024},
                eprint={2308.07269},
                archivePrefix={arXiv},
                primaryClass={cs.CL}
            }
            ```
            """
        )

demo.launch()