File size: 13,508 Bytes
193db9d d15e788 193db9d f9589f4 97fcd0c d15e788 9756440 d15e788 193db9d 38e3800 193db9d f9589f4 3b39b49 193db9d 3b39b49 193db9d d15e788 193db9d d15e788 193db9d 02b7dec 193db9d 02b7dec f9589f4 193db9d 9756440 193db9d f9589f4 193db9d 3b39b49 d15e788 3b39b49 193db9d 3b39b49 02b7dec 193db9d 02b7dec 193db9d 02b7dec 193db9d 3b39b49 193db9d 3b39b49 193db9d 02b7dec 193db9d f9589f4 193db9d 3b39b49 d15e788 3b39b49 193db9d 3b39b49 193db9d 38e3800 193db9d 3b39b49 193db9d 02b7dec 9756440 02b7dec 9756440 02b7dec 9756440 02b7dec 3b39b49 193db9d 9756440 193db9d f9589f4 193db9d f9589f4 193db9d 02b7dec 193db9d 02b7dec 193db9d 02b7dec 193db9d 9756440 193db9d f9589f4 193db9d 02b7dec 193db9d 02b7dec 193db9d d15e788 02b7dec d15e788 02b7dec 193db9d 9756440 193db9d 9756440 193db9d 3b39b49 193db9d 3b39b49 d15e788 02b7dec d15e788 9756440 02b7dec 9756440 f9589f4 02b7dec 9756440 d15e788 193db9d 3b39b49 193db9d 02b7dec 193db9d 3b39b49 193db9d 02b7dec 193db9d 973519b 193db9d |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
import json
from typing import Any
import gradio as gr
import pandas as pd
from datasets import Dataset
from loguru import logger
from app_configs import CONFIGS, UNSELECTED_PIPELINE_NAME
from components import commons
from components.model_pipeline.model_pipeline import PipelineInterface, PipelineState, PipelineUIState
from components.typed_dicts import PipelineStateDict
from display.formatting import styled_error
from submission import submit
from workflows.qb_agents import QuizBowlBonusAgent
from workflows.structs import ModelStep, Workflow
from . import populate, validation
from .plotting import create_bonus_confidence_plot, create_bonus_html
from .utils import evaluate_prediction
def process_bonus_results(results: list[dict]) -> pd.DataFrame:
"""Process results from bonus mode and prepare visualization data."""
return pd.DataFrame(
[
{
"Part": f"Part {r['part_number']}",
"Correct?": "✅" if r["score"] == 1 else "❌",
"Confidence": r["confidence"],
"Prediction": r["answer"],
"Explanation": r["explanation"],
}
for r in results
]
)
def initialize_eval_interface(example: dict, model_outputs: list[dict]):
"""Initialize the interface with example text."""
try:
html_content = create_bonus_html(example["leadin"], example["parts"])
# Create confidence plot data
plot_data = create_bonus_confidence_plot(example["parts"], model_outputs)
# Store state
state = json.dumps({"parts": example["parts"], "outputs": model_outputs})
return html_content, plot_data, state
except Exception as e:
logger.exception(f"Error initializing interface: {e.args}")
return f"<div>Error initializing interface: {str(e)}</div>", pd.DataFrame(), "{}"
class BonusInterface:
"""Gradio interface for the Bonus mode."""
def __init__(self, app: gr.Blocks, dataset: Dataset, model_options: dict, defaults: dict):
"""Initialize the Bonus interface."""
logger.info(f"Initializing Bonus interface with dataset size: {len(dataset)}")
self.ds = dataset
self.model_options = model_options
self.app = app
self.defaults = defaults
self.output_state = gr.State(value="{}")
self.render()
def _render_pipeline_interface(self, workflow: Workflow, simple: bool = True):
"""Render the model interface."""
with gr.Row(elem_classes="bonus-header-row form-inline"):
self.pipeline_selector = commons.get_pipeline_selector([])
self.load_btn = gr.Button("⬇️ Import Pipeline", variant="secondary")
self.import_error_display = gr.HTML(label="Import Error", elem_id="import-error-display", visible=False)
self.pipeline_interface = PipelineInterface(
self.app,
workflow,
model_options=list(self.model_options.keys()),
config=self.defaults,
)
def _render_qb_interface(self):
"""Render the quizbowl interface."""
with gr.Row(elem_classes="bonus-header-row form-inline"):
self.qid_selector = commons.get_qid_selector(len(self.ds))
self.run_btn = gr.Button("Run on Bonus Question", variant="secondary")
self.question_display = gr.HTML(label="Question", elem_id="bonus-question-display")
self.error_display = gr.HTML(label="Error", elem_id="bonus-error-display", visible=False)
self.results_table = gr.DataFrame(
label="Model Outputs",
value=pd.DataFrame(columns=["Part", "Correct?", "Confidence", "Prediction", "Explanation"]),
visible=False,
)
self.model_outputs_display = gr.JSON(label="Model Outputs", value="{}", show_indices=True, visible=False)
with gr.Row():
self.eval_btn = gr.Button("Evaluate", variant="primary")
with gr.Accordion("Model Submission", elem_classes="model-submission-accordion", open=True):
with gr.Row():
self.model_name_input = gr.Textbox(label="Model Name")
self.description_input = gr.Textbox(label="Description")
with gr.Row():
gr.LoginButton()
self.submit_btn = gr.Button("Submit", variant="primary")
self.submit_status = gr.HTML(label="Submission Status")
def render(self):
"""Create the Gradio interface."""
self.hidden_input = gr.Textbox(value="", visible=False, elem_id="hidden-index")
workflow = self.defaults["init_workflow"]
with gr.Row():
# Model Panel
with gr.Column(scale=1):
self._render_pipeline_interface(workflow, simple=self.defaults["simple_workflow"])
with gr.Column(scale=1):
self._render_qb_interface()
self._setup_event_listeners()
def validate_workflow(self, state_dict: PipelineStateDict):
"""Validate the workflow."""
try:
pipeline_state = PipelineState(**state_dict)
validation.validate_workflow(
pipeline_state.workflow,
required_input_vars=CONFIGS["bonus"]["required_input_vars"],
required_output_vars=CONFIGS["bonus"]["required_output_vars"],
)
except Exception as e:
raise gr.Error(f"Error validating workflow: {str(e)}")
def get_new_question_html(self, question_id: int):
"""Get the HTML for a new question."""
if question_id is None:
logger.error("Question ID is None. Setting to 1")
question_id = 1
try:
question_id = int(question_id) - 1
if not self.ds or question_id < 0 or question_id >= len(self.ds):
return "Invalid question ID or dataset not loaded"
example = self.ds[question_id]
leadin = example["leadin"]
parts = example["parts"]
return create_bonus_html(leadin, parts)
except Exception as e:
return f"Error loading question: {str(e)}"
def get_model_outputs(self, example: dict, pipeline_state: PipelineState):
"""Get the model outputs for a given question ID."""
outputs = []
leadin = example["leadin"]
agent = QuizBowlBonusAgent(pipeline_state.workflow)
for i, part in enumerate(example["parts"]):
# Run model for each part
part_output = agent.run(leadin, part["part"])
# Add part number and evaluate score
part_output["part_number"] = i + 1
part_output["score"] = evaluate_prediction(part_output["answer"], part["clean_answers"])
outputs.append(part_output)
return outputs
def get_pipeline_names(self, profile: gr.OAuthProfile | None) -> list[str]:
names = [UNSELECTED_PIPELINE_NAME] + populate.get_pipeline_names("bonus", profile)
return gr.update(choices=names, value=UNSELECTED_PIPELINE_NAME)
def load_pipeline(
self, model_name: str, pipeline_change: bool, profile: gr.OAuthProfile | None
) -> tuple[str, PipelineStateDict, bool, dict]:
try:
workflow = populate.load_workflow("bonus", model_name, profile)
if workflow is None:
logger.warning(f"Could not load workflow for {model_name}")
return UNSELECTED_PIPELINE_NAME, gr.skip(), gr.skip(), gr.update(visible=False)
pipeline_state_dict = PipelineState.from_workflow(workflow).model_dump()
return UNSELECTED_PIPELINE_NAME, pipeline_state_dict, not pipeline_change, gr.update(visible=True)
except Exception as e:
error_msg = styled_error(f"Error loading pipeline: {str(e)}")
return UNSELECTED_PIPELINE_NAME, gr.skip(), gr.skip(), gr.update(visible=True, value=error_msg)
def single_run(
self,
question_id: int,
state_dict: PipelineStateDict,
) -> tuple[str, Any, Any]:
"""Run the agent in bonus mode."""
try:
pipeline_state = validation.validate_bonus_workflow(state_dict)
question_id = int(question_id - 1)
if not self.ds or question_id < 0 or question_id >= len(self.ds):
raise gr.Error("Invalid question ID or dataset not loaded")
example = self.ds[question_id]
outputs = self.get_model_outputs(example, pipeline_state)
# Process results and prepare visualization data
html_content, plot_data, output_state = initialize_eval_interface(example, outputs)
df = process_bonus_results(outputs)
step_outputs = [output["step_outputs"] for output in outputs]
return (
html_content,
gr.update(value=output_state),
gr.update(value=df, label=f"Model Outputs for Question {question_id + 1}", visible=True),
gr.update(value=step_outputs, label=f"Step Outputs for Question {question_id + 1}", visible=True),
gr.update(visible=False),
)
except Exception as e:
import traceback
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
return (
gr.skip(),
gr.skip(),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=True, value=error_msg),
)
def evaluate(self, state_dict: PipelineStateDict, progress: gr.Progress = gr.Progress()):
"""Evaluate the bonus questions."""
try:
pipeline_state = validation.validate_bonus_workflow(state_dict)
# Validate inputs
if not self.ds or not self.ds.num_rows:
return "No dataset loaded", None, None
total_correct = 0
total_parts = 0
part_scores = []
part_numbers = []
for example in progress.tqdm(self.ds, desc="Evaluating bonus questions"):
model_outputs = self.get_model_outputs(example, pipeline_state)
for output in model_outputs:
total_parts += 1
if output["score"] == 1:
total_correct += 1
part_scores.append(output["score"])
part_numbers.append(output["part_number"])
accuracy = total_correct / total_parts
df = pd.DataFrame(
[
{
"Part Accuracy": f"{accuracy:.2%}",
"Total Score": f"{total_correct}/{total_parts}",
"Questions Evaluated": len(self.ds),
}
]
)
# plot_data = create_scatter_pyplot(part_numbers, part_scores)
return (
gr.update(value=df, label="Scores on Sample Set"),
gr.update(visible=False),
)
except Exception as e:
error_msg = styled_error(f"Error evaluating bonus: {e.args}")
logger.exception(f"Error evaluating bonus: {e.args}")
return gr.skip(), gr.update(visible=True, value=error_msg)
def submit_model(
self,
model_name: str,
description: str,
state_dict: PipelineStateDict,
profile: gr.OAuthProfile = None,
):
"""Submit the model output."""
pipeline_state = PipelineState(**state_dict)
return submit.submit_model(model_name, description, pipeline_state.workflow, "bonus", profile)
def _setup_event_listeners(self):
# Initialize with the default question (ID 0)
gr.on(
triggers=[self.app.load, self.qid_selector.change],
fn=self.get_new_question_html,
inputs=[self.qid_selector],
outputs=[self.question_display],
)
gr.on(
triggers=[self.app.load],
fn=self.get_pipeline_names,
outputs=[self.pipeline_selector],
)
pipeline_state = self.pipeline_interface.pipeline_state
pipeline_change = self.pipeline_interface.pipeline_change
self.load_btn.click(
fn=self.load_pipeline,
inputs=[self.pipeline_selector, pipeline_change],
outputs=[self.pipeline_selector, pipeline_state, pipeline_change, self.import_error_display],
)
self.pipeline_interface.add_triggers_for_pipeline_export([pipeline_state.change], pipeline_state)
self.run_btn.click(
self.single_run,
inputs=[
self.qid_selector,
self.pipeline_interface.pipeline_state,
],
outputs=[
self.question_display,
self.output_state,
self.results_table,
self.model_outputs_display,
self.error_display,
],
)
self.eval_btn.click(
fn=self.evaluate,
inputs=[self.pipeline_interface.pipeline_state],
outputs=[self.results_table, self.error_display],
)
self.submit_btn.click(
fn=self.submit_model,
inputs=[
self.model_name_input,
self.description_input,
self.pipeline_interface.pipeline_state,
],
outputs=[self.submit_status],
)
|