phase2_demo / app.py
disi98's picture
Update app.py
1975c38 verified
# import libraries
import gradio as gr
import pandas as pd
#load json file into a dataframe
path="suspicious_samples_run3.jsonl"
df = pd.read_json(path_or_buf=path, lines=True)
# iterate through the responses and create a new dataframe
def iterate_responses(df):
data = []
for i in range(len(df)):
for j in range(len(df['responses'][i])):
prompt = str(df['prompt'][i])
response = str(df['responses'][i][j])
judgements = df['per_response_judgements'][i][j]
judgement_1 = str(judgements[0]['reason']) if len(judgements) > 0 else "No Judgement Given"
judgement_2 = str(judgements[1]['reason']) if len(judgements) > 1 else "No Judgement Given"
judgement_3 = str(judgements[2]['reason']) if len(judgements) > 2 else "No Judgement Given"
rubric_1 = str(judgements[0]['rubric']) if len(judgements) > 0 else "No Rubric Given"
rubric_2 = str(judgements[1]['rubric']) if len(judgements) > 1 else "No Rubric Given"
rubric_3 = str(judgements[2]['rubric']) if len(judgements) > 2 else "No Rubric Given"
score_1 = str(judgements[0]['score']) if len(judgements) > 0 else "No Score Given"
score_2 = str(judgements[1]['score']) if len(judgements) > 1 else "No Score Given"
score_3 = str(judgements[2]['score']) if len(judgements) > 2 else "No Score Given"
meta_1 = str(df["meta"][i]["reference_answer_idx"])
meta_2 = str(df["meta"][i]["filename"])
meta_3 = str(df["meta"][i]["prompt_task"])
meta_4 = str(df["meta"][i]["prompt_domain"])
data.append([prompt, response, judgement_1, judgement_2, judgement_3, rubric_1, rubric_2, rubric_3, score_1, score_2, score_3, meta_1, meta_2, meta_3, meta_4])
df_resp = pd.DataFrame(data, columns=["prompt", "response", "judgement_1", "judgement_2", "judgement_3", "rubric_1", "rubric_2", "rubric_3", "score_1", "score_2", "score_3", "meta_1", "meta_2", "meta_3", "meta_4"])
return df_resp
df_resps = iterate_responses(df)
# function to get response at index i
def response_at_index_i(i):
prompt = df_resps["prompt"][i]
response = df_resps["response"][i]
judgement_1 = df_resps["judgement_1"][i]
judgement_2 = df_resps["judgement_2"][i]
judgement_3 = df_resps["judgement_3"][i]
rubric_1 = df_resps["rubric_1"][i]
rubric_2 = df_resps["rubric_2"][i]
rubric_3 = df_resps["rubric_3"][i]
score_1 = df_resps["score_1"][i]
score_2 = df_resps["score_2"][i]
score_3 = df_resps["score_3"][i]
meta = f"Reference Answer Index: {df_resps['meta_1'][i]} \nFilename: {df_resps['meta_2'][i]}\nPrompt Task: {df_resps['meta_3'][i]}\nPrompt Domain: {df_resps['meta_4'][i]}"
return prompt, response, judgement_1, judgement_2, judgement_3, rubric_1, rubric_2, rubric_3, score_1, score_2, score_3, meta
# function to show confirmed scores
def history_response(prompt_id, score_1, score_2, score_3):
listhistory = f"Prompt_index: {prompt_id}\n\nConfirmed Score_1: {score_1}\n\nConfirmed Score_2: {score_2}\n\nConfirmed Score_3: {score_3}"
return listhistory
# function to get next index
def nxt(num):
if num < 0 or num > len(df_resps) - 2:
return 0
return num + 1
# function to get previous index
def prv(num):
if num <= 0:
return len(df_resps) - 2
elif num > len(df_resps) - 2:
return 0
return num - 1
# create a gradio interface
demo = gr.Blocks(title='Group1_Subtask2')
app_desc = "1. This is a tool that that generates a prompt with corresponding responses, judgements, rubrics and scores. \n\n2. The tool allows users to confirm the scores of the responses. \n\n3. The tool also allows users to navigate through the prompts."
how_to_use = "1. Start by clicking NEXT or PREVIOUS to start viewing results.\n\n2. For a given generated result, you can confirm score 1, score 2 and score 3.\n\n3. Click on 'Submit Your Response' to confirm the scores.\n\n4. You can navigate through the results by clicking on NEXT or PREVIOUS.\n\n5. The tool also shows the confirmed scores for each response.\n\nNB//The Confirmed Response Scores and Confirm Scores buttons do not update automatically. You need to click on the Submit Your Response button to update the confirmed scores."
# gradio interface
with demo:
gr.Markdown(
"""
# Group1 Phase 2
## Read from JSON
"""
)
with gr.Row():
with gr.Column(scale=1):
nxt_prv_value = gr.Number(label="a", visible=False)
prompt = gr.Textbox(max_lines=10, label='Model Prompt')
with gr.Row():
prv_btn = gr.Button("PREVIOUS")
nxt_btn = gr.Button("NEXT")
with gr.Row():
listhistory = gr.Textbox(label='Confirmed Response Scores')
with gr.Row():
app_description = gr.Textbox(value=app_desc, label='App Description')
with gr.Row():
how_to_use = gr.Textbox(value=how_to_use, label='How to Use')
with gr.Row():
with gr.Column(scale=1):
with gr.Row():
response = gr.Textbox(max_lines=10, label='AI Answer')
meta = gr.Textbox(max_lines=10, label='Reference Answer')
# with gr.Column(scale=1):
with gr.Row():
rubric_1 = gr.Textbox(max_lines=10, label='Rubric 1')
rubric_2 = gr.Textbox(max_lines=10, label='Rubric 2')
rubric_3 = gr.Textbox(max_lines=10, label='Rubric 3')
with gr.Row():
judgement_1 = gr.Textbox(max_lines=10, label='Judger 1')
judgement_2 = gr.Textbox(max_lines=10, label='Judger 2')
judgement_3 = gr.Textbox(max_lines=10, label='Judger 3')
with gr.Row():
score_1 = gr.Number(label="Score 1")
score_2 = gr.Number(label="Score 2")
score_3 = gr.Number(label="Score 3")
with gr.Row():
score_1_g = gr.Radio(["1", "2", "3", "4", "5"], label="Confirm Score 1")
score_2_g = gr.Radio(["1", "2", "3", "4", "5"], label="Confirm Score 2")
score_3_g = gr.Radio(["1", "2", "3", "4", "5"], label="Confirm Score 3")
with gr.Row():
submit = gr.Button("Submit Your Response")
submit.click(history_response, inputs=[nxt_prv_value, score_1_g, score_2_g, score_3_g], outputs=[listhistory])
nxt_btn.click(nxt, nxt_prv_value, nxt_prv_value)
prv_btn.click(prv, nxt_prv_value, nxt_prv_value)
nxt_prv_value.change(response_at_index_i, nxt_prv_value, outputs=[prompt, response, judgement_1, judgement_2, judgement_3, rubric_1, rubric_2, rubric_3, score_1, score_2, score_3, meta])
demo.launch()
# Team Members
'''
Collins Okoth: collins.okech@digitaldividedata.com
Albert Disi: albert.disi@digitaldividedata.com
Joseph Amadi: joseph.amadi@digitaldividedata.com
'''