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()