File size: 15,693 Bytes
5e1514b
 
0bf42ca
3fe3e10
 
 
8b7042b
 
e2bf898
 
0bf42ca
 
 
3fe3e10
5e1514b
0bf42ca
3fe3e10
5e1514b
 
 
 
bfe1f92
 
 
3fe3e10
bfe1f92
 
bccb671
5e1514b
 
bfe1f92
5e1514b
 
3fe3e10
5e1514b
 
 
 
 
 
 
3fe3e10
 
bfe1f92
3fe3e10
 
 
 
5e1514b
3fe3e10
 
5e1514b
 
3fe3e10
 
 
 
 
 
 
 
5e1514b
3a0a132
8b7042b
 
 
3a0a132
 
 
8b7042b
 
 
 
 
 
 
 
 
3a0a132
8b7042b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a0a132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7042b
 
 
 
 
 
e2bf898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7042b
e2bf898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7042b
5e1514b
3fe3e10
5e1514b
 
 
3fe3e10
 
 
 
 
 
0bf42ca
3fe3e10
bfe1f92
3fe3e10
5e1514b
3fe3e10
5e1514b
 
 
 
3a0a132
5e1514b
 
 
3a0a132
 
 
 
5e1514b
 
 
8b7042b
5e1514b
 
 
3a0a132
8b7042b
 
 
 
 
 
 
 
3fe3e10
 
 
 
 
5e1514b
 
 
 
8b7042b
3fe3e10
5e1514b
8b7042b
 
e2bf898
 
 
 
 
8b7042b
 
 
 
5e1514b
 
 
 
 
 
 
 
 
 
8b7042b
 
3a0a132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df513ba
8b7042b
 
 
 
 
 
3a0a132
8b7042b
 
3a0a132
 
8b7042b
3a0a132
 
 
 
8b7042b
3a0a132
 
 
8b7042b
 
 
 
 
5e1514b
df513ba
5e1514b
 
 
 
8b7042b
5e1514b
df513ba
 
 
5e1514b
 
 
 
 
df513ba
8b7042b
3a0a132
 
 
 
 
8b7042b
 
 
3a0a132
8b7042b
 
 
 
3a0a132
8b7042b
 
5e1514b
 
 
 
 
3a0a132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e1514b
 
 
 
 
 
 
 
 
 
 
df513ba
5e1514b
df513ba
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
import gradio as gr
import os
from transformers import AutoTokenizer
from get_loss.get_loss_hf import run_get_loss
import pdb
from types import SimpleNamespace
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import numpy as np
# os.system('git clone https://github.com/EleutherAI/lm-evaluation-harness')
# os.system('cd lm-evaluation-harness')
# os.system('pip install -e .')
# -i https://pypi.tuna.tsinghua.edu.cn/simple
# 第一个功能:基于输入文本和对应的损失值对文本进行着色展示

def color_text(text_list=["hi", "FreshEval","!"], loss_list=[0.1,0.7]):
    """
    根据损失值为文本着色。
    """
    highlighted_text = []
    # print('loss_list',loss_list)
    # ndarray to list
    loss_list = loss_list.tolist()
    loss_list=[0]+loss_list
    # print('loss_list',loss_list)
    # print('text_list',text_list)
    # pdb.set_trace()
    for text, loss in zip(text_list, loss_list):
        # color = "#FF0000" if float(loss) > 0.5 else "#00FF00"
        color=loss/20#TODO rescale
        # highlighted_text.append({"text": text, "bg_color": color})
        highlighted_text.append((text, color))
        print('highlighted_text',highlighted_text)
    return highlighted_text

# 第二个功能:根据 ID 列表和 tokenizer 将 ID 转换为文本,并展示
def get_text(ids_list=[0.1,0.7], tokenizer=None):
    """
    给定一个 ID 列表和 tokenizer 名称,将这些 ID 转换成文本。
    """
    # return ['Hi', 'Adam']
    # tokenizer = AutoTokenizer.from_pretrained(tokenizer)
    # print('ids_list',ids_list)
    # pdb.set_trace()
    text=[]
    for id in ids_list:
        text.append(  tokenizer.decode(id, skip_special_tokens=True))
    # 这里只是简单地返回文本,但是可以根据实际需求添加颜色或其他样式
    print(f'L41:{text}')
    return text


# def get_ids_loss(text, tokenizer, model):
#     """
#     给定一个文本,model and its tokenizer,返回其对应的 IDs 和损失值。
#     """
#     # tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
#     # model = AutoModelForCausalLM.from_pretrained(model_name)
#     # 这里只是简单地返回 IDs 和损失值,但是可以根据实际需求添加颜色或其他样式
#     return [1, 2], [0.1, 0.7]

def harness_eval(question,  answer_index, answer_type, model=None,*choices,):
    '''
    use harness to test one question,  can specify the model, (extract or ppl)
    '''
    # print(f'question,choices,answer_index,model,tokenizer: {question,choices,answer_index,model,tokenizer}')
    print(f'type of choices: {type(choices)} and type of choices[0]: {type(choices[0])}')
    print(f'choices: {choices}')
    # TODO add the model and its score
    # torch.nn.functional.softmax(output.logits, dim=0)
    # topk = torch.topk(output.logits, 5)
    
    return {'A':0.5, 'B':0.3, 'C':0.1, 'D':0.1}




def plotly_plot_text():#(df, x, y, color,title, x_title, y_title):
        # plotly_plot(sample_df, 'date', 'loss_mean_at_1000', 'model','ppl with time', 'time', 'ppl')
    df=pd.read_csv('./data/tmp.csv')
    df['date'] = pd.to_datetime(df['date'])
    # sort by date
    df.sort_values(by='date', inplace=True)

    # use a dic to filter the dataframe
    df = df[df['file_name'] == 'arxiv_computer_science']

    x,y,color,title, x_title, y_title='date', 'loss_mean_at_1000', 'model','ppl with time', 'time', 'ppl'

    fig = px.line(df, x=x, y=y, color=color,title=title)
    fig.update_xaxes(title_text=x_title)
    fig.update_yaxes(title_text=y_title)
    # fig.update_layout()
    return fig

def plotly_plot_question():#(df, x, y, color,title, x_title, y_title):
        # plotly_plot(sample_df, 'date', 'loss_mean_at_1000', 'model','ppl with time', 'time', 'ppl')
    df=pd.read_csv('./data/meta_gjo_df.csv')
    df['date'] = pd.to_datetime(df['End Time'])
    # sort by date
    df.sort_values(by='date', inplace=True)

    # use a dic to filter the dataframe
    # df = df[df['file_name'] == 'arxiv_computer_science']

    x,y,color,title, x_title, y_title='date', 'Right Possibility', 'model','Right Possibility with time', 'time', 'Right Possibility'

    fig = px.line(df, x=x, y=y, color=color,title=title)
    fig.update_xaxes(title_text=x_title)
    fig.update_yaxes(title_text=y_title)
    # fig.update_layout()
    return fig

# def plotly_plot(df, x, y, color, title, x_title, y_title):
#     fig = px.line(df, x=x, y=y, color=color, title=title)
#     fig.update_xaxes(title_text=x_title)
#     fig.update_yaxes(title_text=y_title)
#     return fig

def show_attention_plot(model_name,texts):
    # 初始化分词器和模型,确保在模型配置中设置 output_attentions=True
    args=SimpleNamespace(texts=texts,model=model_name)
    print(f'L60,text:{texts}')
    rtn_dic=run_get_loss(args)
    # print(rtn_dic)
    # pdb.set_trace()
    # {'logit':logit,'input_ids':input_chunk,'tokenizer':tokenizer,'neg_log_prob_temp':neg_log_prob_temp}
    # ids, loss =rtn_dic['input_ids'],rtn_dic['loss']#= get_ids_loss(text, tokenizer, model)
    # notice here is numpy ndarray
    tokenizer, model = rtn_dic['tokenizer'],rtn_dic['model'] 
    text = "Here is some text to encode"

    # 使用分词器处理输入文本
    inputs = tokenizer(text, return_tensors="pt")
    # 进行前向传播,获取输出
    outputs = model(**inputs, output_attentions=True)

    # 检查是否成功获得了 attentions
    if "attentions" in outputs:
        last_layer_attentions = outputs.attentions[-1]  # 获取最后一层的 attention 矩阵
        print("Successfully retrieved the attention matrix:", last_layer_attentions.shape)
    else:
        pdb.set_trace()
        print("Attention matrix not found in outputs.")

    # 假设 last_layer_attentions 是我们从模型中提取的注意力矩阵
    # last_layer_attentions 的形状应该是 [batch_size, num_heads, seq_length, seq_length]
    # 为了简化,我们这里只查看第一个样本、第一个头的注意力矩阵
    attention_matrix = last_layer_attentions[0, 0].detach().numpy()

    # 使用 matplotlib 绘制热图
    plt.figure(figsize=(10, 8))
    plt.imshow(attention_matrix, cmap='viridis')

    # 添加标题和标签以提高可读性
    plt.title('Attention Matrix Visualization')
    plt.xlabel('Tokens in Sequence')
    plt.ylabel('Tokens in Sequence')

    # 添加颜色条
    plt.colorbar()

    # 保存图表到文件
    # plt.savefig('/223040239/medbase/attention_matrix_visualization.png')
    return plt


def color_pipeline(texts=["Hi","FreshEval","!"],  model=None):
    """
    给定一个文本,返回其对应的着色文本。
    """
    print('text,model',texts,model)
    args=SimpleNamespace(texts=texts,model=model)
    print(f'L60,text:{texts}')
    rtn_dic=run_get_loss(args)
    # print(rtn_dic)
    # pdb.set_trace()
    # {'logit':logit,'input_ids':input_chunk,'tokenizer':tokenizer,'neg_log_prob_temp':neg_log_prob_temp}
    ids, loss =rtn_dic['input_ids'],rtn_dic['loss']#= get_ids_loss(text, tokenizer, model)
    # notice here is numpy ndarray
    tokenizer=rtn_dic['tokenizer'] # get tokenizer
    text = get_text(ids, tokenizer)
    # print('ids, loss ,text',ids, loss ,text)
    return color_text(text, loss)


# TODO can this be global ? maybe need session to store info of the user
# visible_btn_num = 4

# 创建 Gradio 界面
with gr.Blocks() as demo:
    # visible_btn_num = 4
    model_input = gr.Textbox(label="model name", placeholder="input your model name here... now I am trying phi-2...")#TODO make a choice here


    with gr.Tab("color your text"):
        with gr.Row():
            text_input = gr.Textbox(label="input text", placeholder="input your text here...")
            # file_input = gr.File(file_count="multiple",label='to add content')#
            # TODO craw and drop the file

            # loss_input = gr.Number(label="loss")
            # model_input = gr.Textbox(label="model name", placeholder="input your model name here... now I am trying phi-2...")#TODO make a choice here
        output_box=gr.HighlightedText(label="colored text")#,interactive=True
        
        gr.Examples(
                [
                    ["Hi FreshEval !", "microsoft/phi-2"],
                    ["Hello FreshBench !", "/home/sribd/chenghao/models/phi-2"],
                ],
                [text_input, model_input],)
            #     cache_examples=True,
            #     # cache_examples=False,
            #     fn=color_pipeline,
            #     outputs=output_box
            # )
            # TODO select models that can be used online
            # TODO maybe add our own models


        color_text_output = gr.HTML(label="colored text")
        color_text_button = gr.Button("color the text").click(color_pipeline, inputs=[text_input, model_input], outputs=output_box)

        # markdown
        gr.Markdown('### How to use this app')


        attention_plot=gr.Plot(label='attention plot')
        see_attention_button = gr.Button("see attention").click(show_attention_plot,inputs=[model_input, text_input],outputs=[attention_plot])
        
        




        date_time_input = gr.Textbox(label="the date when the text is generated")#TODO add date time input
        description_input = gr.Textbox(label="description of the text")
        submit_button = gr.Button("submit a post or record").click()
        #TODO add model and its score

    with gr.Tab('test your qeustion'):
        '''
        use extract, or use ppl
        '''
        question=gr.Textbox(label="input question", placeholder='input your question here...')
        answer_index=gr.Textbox(label="right answer index", placeholder='index for right anser here, start with 0')#TODO add multiple choices, 
        # model_input = gr.Textbox(label="model name", placeholder="input your model name here... now I am trying phi-2...")#TODO make a choice here

        btn_list = []

        # choices=gr.Textbox(placeholder='input your other choices here...')
        
        button_limit=10
        # global visible_btn_num
        visible_btn_num = 4

        from gradio_samples.add_components import add_one_btn, remove_one_btn, get_text_content

        # use partial function
        from functools import partial
        add_one_btn=partial(add_one_btn,button_limit=button_limit,)#visible_btn_num = visible_btn_num)
        remove_one_btn=partial(remove_one_btn,button_limit=button_limit,)#visible_btn_num = visible_btn_num)
            
        # with gr.Blocks() as demo:
        with gr.Row():
            for i in range(button_limit):
                if i<visible_btn_num:
                    btn = gr.Textbox(visible=True)
                else:
                    btn = gr.Textbox(visible=False)
                btn_list.append(btn)
        b = gr.Button("add_one_choice(make sure every existing choice is filled)")
        print(f'len(btn_list): {len(btn_list)}')
        b.click(add_one_btn, btn_list, btn_list)
        b = gr.Button("remove_one_choice")
        b.click(remove_one_btn, btn_list, btn_list)

        # # print(f'len(btn_list): {len(btn_list)}')

        # print('btn_list is ',type(btn_list),btn_list)


        # b = gr.Button("Get Text Content")
        # output = gr.Textbox()
        # b.click(get_text_content, btn_list, output)

        
        # test_button=gr.Button('test').click(harness_eval())# TODO figure out the input and output

        answer_type=gr.Dropdown(label="answer type", choices=['extract', 'ppl'])
        #TODO add the model and its score
        answer_label=gr.Label('the answers\'s detail')# RETURN the answer and its score,in the form of dic{str: float}

        test_question_button=gr.Button('test question').click(harness_eval,inputs=[question, answer_index ,model_input,answer_type,*btn_list],outputs=[answer_label])

        forecast_q='A Ukrainian counteroffensive began in 2023, though territorial gains by November 2023 were limited (Economist, BBC, Newsweek). The question will be suspended on 31 July 2024 and the outcome determined using data as reported in the Brookings Institution\'s "Ukraine Index" (Brookings Institution - Ukraine Index, see "Percentage of Ukraine held by Russia" chart). If there is a discrepancy between the chart data and the downloaded data (see "Get the data" within the "NET TERRITORIAL GAINS" chart border), the downloaded data will be used for resolution.'
        # answer_list=['Less than 5%','At least 5%, but less than 10%','At least 10%, but less than 15%','At least 15%, but less than 20%','20% or more' ]
        answer_list=['Less than 5%','At least 5%, but less than 10%','At least 10%, but less than 15%','15% or more' ]
         
        # gr.Examples([
        #             [forecast_q, '&&&&&&'.join(answer_list), '0']
        #              ],
        #             [question, choices, answer_index])
        gr.Examples([
            [forecast_q, answer_list[0],answer_list[1],answer_list[2],answer_list[3], '0']
                ],
            [question,btn_list[0],btn_list[1],btn_list[2],btn_list[3], answer_index])

        date_time_input = gr.Textbox(label="the date when the text is generated")#TODO add date time input
        description_input = gr.Textbox(label="description of the text")
        submit_button = gr.Button("submit a post or record").click()

        #TODO add the model and its score

        def test_question(question, answer, other_choices):
            '''
            use extract, or use ppl
            '''
            answer_ppl, other_choices_ppl =  (question, answer, other_choices)
            return answer_ppl, other_choices_ppl



    with gr.Tab("model text ppl with time"):
        '''
        see the matplotlib example, to see ppl with time, select the models
        '''
        # load the json file with time,

        # sample_df=pd.DataFrame({'time':pd.date_range('2021-01-01', periods=6), 'ppl': [1,2,3,4,5,6]})
        # pd_df=pd.read_csv('./data/tmp.csv')
        # pd_df['date'] = pd.to_datetime(pd_df['date'])
        # print(pd_df.head)
        # # gr_df=gr.Dataframe(pd_df)
        # gr_df=pd_df


        # print(gr_df.head)
        # print('done')
        # sample
        plot=gr.Plot(label='model text ppl')
        # plotly_plot(gr_df, 'date', 'loss_mean_at_1000', 'model','ppl with time', 'time', 'ppl')
        # draw_pic_button=gr.Button('draw the pic').click(plotly_plot,inputs=['gr_df', 'date', 'loss_mean_at_1000', 'model','ppl with time', 'time', 'ppl'],outputs=[plot])
        draw_pic_button=gr.Button('draw the pic').click(plotly_plot_text,inputs=[],outputs=[plot])


    
    with gr.Tab("model quesion acc with time"):
        '''
        see the matplotlib example, to see ppl with time, select the models
        ''' 

        # pd_df=pd.read_csv('./data/meta_gjo_df.csv')
        # pd_df['date'] = pd.to_datetime(pd_df['end_date'])
        # print(pd_df.head)
        # gr_df=gr.Dataframe(pd_df)
        # gr_df=pd_df


        # print(gr_df.head)
        # print('done')
        # sample
        plot=gr.Plot(label='question acc with time')
        # plotly_plot(gr_df, 'date', 'loss_mean_at_1000', 'model','ppl with time', 'time', 'ppl')
        # draw_pic_button=gr.Button('draw the pic').click(plotly_plot,inputs=['gr_df', 'date', 'loss_mean_at_1000', 'model','ppl with time', 'time', 'ppl'],outputs=[plot])
        draw_pic_button=gr.Button('draw the pic').click(plotly_plot_question,inputs=[],outputs=[plot])




    with gr.Tab("hot questions"):
        '''
        see the questions and answers
        '''
        with gr.Tab("ppl"):
            '''
            see the questions
            '''
        

demo.launch(debug=True)