lisabdunlap commited on
Commit
e97216b
·
verified ·
1 Parent(s): e762ca3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -48
app.py CHANGED
@@ -1,52 +1,53 @@
1
  import gradio as gr
 
2
  import json
3
- from collections import defaultdict
4
 
5
- # Load and group data by prompt
6
- def load_data(file_path):
7
- prompt_dict = defaultdict(list)
8
- with open(file_path, "r") as f:
9
  for line in f:
10
- row = json.loads(line)
11
- prompt = row["prompt"]
12
- model = row["model"]
13
- response = row["model_response"]
14
- prompt_dict[prompt].append((model, response))
15
- return prompt_dict
16
-
17
- DATA_PATH = "all_10_responses.jsonl"
18
- prompt_to_responses = load_data(DATA_PATH)
19
- prompt_list = sorted(prompt_to_responses.keys())
20
-
21
- # Gradio interface
22
- def display_responses(selected_prompt):
23
- entries = prompt_to_responses[selected_prompt]
24
- accordions = []
25
- for model, response in sorted(entries):
26
- with gr.Accordion(label=model, open=False):
27
- gr.Markdown(f"""```text\n{response}\n```""")
28
- return
29
-
30
- with gr.Blocks(css=".gr-accordion .label {font-weight: bold}") as demo:
31
- gr.Markdown("## 🔍 Prompt Viewer: Compare Model Responses")
32
- selected_prompt = gr.Dropdown(choices=prompt_list, label="Select a Prompt", interactive=True)
33
-
34
- with gr.Column() as output_column:
35
- prompt_output = gr.Markdown()
36
- response_group = gr.Group()
37
-
38
- def update_output(prompt):
39
- prompt_output.update(f"### Prompt\n```\n{prompt}\n```")
40
- response_group.children.clear()
41
- entries = prompt_to_responses[prompt]
42
- for model, response in sorted(entries):
43
- response_group.children.append(
44
- gr.Accordion(label=model, open=False, children=[
45
- gr.Markdown(f"""```text\n{response}\n```""")
46
- ])
47
- )
48
-
49
- selected_prompt.change(fn=update_output, inputs=[selected_prompt])
50
-
51
- if __name__ == "__main__":
52
- demo.launch()
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
  import json
 
4
 
5
+ def load_data(path: str):
6
+ records = []
7
+ with open(path, "r", encoding="utf-8") as f:
 
8
  for line in f:
9
+ obj = json.loads(line)
10
+ records.append({
11
+ "prompt": obj.get("Prompt", "").strip(),
12
+ "model": obj.get("model", "").strip(),
13
+ "response": obj.get("model response", "").strip()
14
+ })
15
+ return pd.DataFrame(records)
16
+
17
+ # Load once
18
+ df = load_data("all_10_responses.jsonl")
19
+ all_prompts = sorted(df["prompt"].unique().tolist())
20
+ all_models = sorted(df["model"].unique().tolist())
21
+
22
+ def get_model_responses(chosen_prompt, chosen_models):
23
+ subset = df[
24
+ (df["prompt"] == chosen_prompt) &
25
+ (df["model"].isin(chosen_models))
26
+ ].sort_values("model")
27
+ # Return a list of tuples: [(model_name, response), …]
28
+ return [(row["model"], row["response"]) for _, row in subset.iterrows()]
29
+
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("# Model Responses Explorer")
32
+
33
+ with gr.Row():
34
+ prompt_dd = gr.Dropdown(all_prompts, label="Select a Prompt")
35
+ models_dd = gr.CheckboxGroup(all_models, value=all_models, label="Select Models")
36
+
37
+ with gr.Column():
38
+ output_acc = gr.Accordion(label="Responses", open=False)
39
+
40
+ def update_accordion(prompt, models):
41
+ items = get_model_responses(prompt, models)
42
+ # Gradio accordion expects a single gr.Column or gr.Text
43
+ # We’ll return a single Markdown string combining all models
44
+ md = ""
45
+ for m, r in items:
46
+ md += f"### {m}\n\n{r}\n---\n"
47
+ return md
48
+
49
+ accordion_text = gr.Markdown()
50
+ prompt_dd.change(fn=update_accordion, inputs=[prompt_dd, models_dd], outputs=accordion_text)
51
+ models_dd.change(fn=update_accordion, inputs=[prompt_dd, models_dd], outputs=accordion_text)
52
+
53
+ demo.launch()