File size: 2,857 Bytes
94bd001
 
 
 
 
 
 
3bed1b1
94bd001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8b121
94bd001
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8b121
94bd001
 
 
 
 
 
 
 
 
 
 
 
 
ad8b121
94bd001
 
 
3bed1b1
94bd001
 
 
3bed1b1
 
 
 
 
 
 
 
94bd001
 
 
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
import gradio as gr
import pandas as pd

with gr.Blocks() as demo:
    dataset_df = {}
    state = gr.State(value=0)
    with gr.Row():
        gr.Markdown("# Evaluation 😎")
    with gr.Row():
        file = gr.File(label="Upload a file")
        prev = gr.Button(value="Previous")
        next = gr.Button(value="Next")
        download = gr.File(label="Download a file")
    with gr.Row():
        with gr.Column():
            question = gr.Textbox(label="Question")
        with gr.Column():
            ground_truth = gr.Textbox(label="GT")
        with gr.Column():
            prediction = gr.Textbox(label="Prediction")
            score = gr.Radio(choices=["Incorrect", "Correct"], label="Score")
    with gr.Row():
        todos = gr.DataFrame()
        done = gr.DataFrame()


    def csv2df(file):
        df = pd.read_csv(file.name)
        df['score'] = None
        df_dict = df.to_dict('records')
        dataset_df.update(dict(df=df, df_dict=df_dict))
        return update()

    def prev_func(score):
        df_dict = dataset_df['df_dict']
        state.value = max(state.value - 1, 0)
        score = df_dict[state.value]['score']
        gr.Info(f"총 {len(dataset_df['df'])}개 쀑에 {state.value + 1}번째 λ°μ΄ν„°μž…λ‹ˆλ‹€.")
        return [*update(), score]

    def next_func(score):
        df_dict = dataset_df['df_dict']
        state.value = min(state.value + 1, len(dataset_df['df']) - 1)
        score = df_dict[state.value]['score']
        gr.Info(f"총 {len(dataset_df['df'])}개 쀑에 {state.value + 1}번째 λ°μ΄ν„°μž…λ‹ˆλ‹€.")
        return [*update(), score]

    def update():
        df_dict = dataset_df['df_dict']
        q = df_dict[state.value]['question']
        g = df_dict[state.value]['answer']
        p = df_dict[state.value]['prediction']
        df = pd.DataFrame(df_dict)
        df.to_csv("data_backup.csv", index=False)
        todos = df[df.score.isna()]
        done = df[df.score.isna() == False]
        return q, g, p, todos, done, "data_backup.csv"

    file.upload(csv2df, file, [question, ground_truth, prediction, todos, done, download])
    prev.click(prev_func, [score], [question, ground_truth, prediction, todos, done, download, score])
    next.click(next_func, [score], [question, ground_truth, prediction, todos, done, download, score])
    def select_func(request: gr.Request, evt: gr.SelectData):
        df_dict = dataset_df['df_dict']
        df_dict[state.value]['score'] = evt.value
        state.value = min(state.value + 1, len(dataset_df['df']) - 1)
        score = df_dict[state.value]['score']
        gr.Info(f"총 {len(dataset_df['df'])}개 쀑에 {state.value + 1}번째 λ°μ΄ν„°μž…λ‹ˆλ‹€.")
        return [*update(), score]
    score.select(select_func, None, [question, ground_truth, prediction, todos, done, download, score])

demo.queue()
demo.launch()