Spaces:
Sleeping
Sleeping
import gradio as gr | |
import json | |
from collections import defaultdict | |
# Load and group data by prompt | |
def load_data(file_path): | |
prompt_dict = defaultdict(list) | |
with open(file_path, "r") as f: | |
for line in f: | |
row = json.loads(line) | |
prompt = row["prompt"] | |
model = row["model"] | |
response = row["model_response"] | |
prompt_dict[prompt].append((model, response)) | |
return prompt_dict | |
DATA_PATH = "all_10_responses.jsonl" | |
prompt_to_responses = load_data(DATA_PATH) | |
prompt_list = sorted(prompt_to_responses.keys()) | |
# Gradio interface | |
def display_responses(selected_prompt): | |
entries = prompt_to_responses[selected_prompt] | |
accordions = [] | |
for model, response in sorted(entries): | |
with gr.Accordion(label=model, open=False): | |
gr.Markdown(f"""```text\n{response}\n```""") | |
return | |
with gr.Blocks(css=".gr-accordion .label {font-weight: bold}") as demo: | |
gr.Markdown("## π Prompt Viewer: Compare Model Responses") | |
selected_prompt = gr.Dropdown(choices=prompt_list, label="Select a Prompt", interactive=True) | |
with gr.Column() as output_column: | |
prompt_output = gr.Markdown() | |
response_group = gr.Group() | |
def update_output(prompt): | |
prompt_output.update(f"### Prompt\n```\n{prompt}\n```") | |
response_group.children.clear() | |
entries = prompt_to_responses[prompt] | |
for model, response in sorted(entries): | |
response_group.children.append( | |
gr.Accordion(label=model, open=False, children=[ | |
gr.Markdown(f"""```text\n{response}\n```""") | |
]) | |
) | |
selected_prompt.change(fn=update_output, inputs=[selected_prompt]) | |
if __name__ == "__main__": | |
demo.launch() | |