refactor: app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from functools import lru_cache
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
from llmdataparser import ParserRegistry
|
| 7 |
-
from llmdataparser.base_parser import ParseEntry
|
| 8 |
|
| 9 |
|
| 10 |
@lru_cache(maxsize=32)
|
|
@@ -164,77 +164,176 @@ def clear_parser_cache():
|
|
| 164 |
get_parser_instance.cache_clear()
|
| 165 |
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
def create_interface():
|
| 168 |
with gr.Blocks() as demo:
|
| 169 |
gr.Markdown("# LLM Evaluation Dataset Parser")
|
| 170 |
|
| 171 |
# State management
|
| 172 |
parser_state = gr.State("")
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
with gr.
|
| 176 |
-
with gr.
|
| 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 |
# Event handlers
|
| 229 |
parser_dropdown.change(
|
| 230 |
fn=update_parser_options,
|
| 231 |
inputs=parser_dropdown,
|
| 232 |
outputs=[
|
| 233 |
-
task_dropdown,
|
| 234 |
split_dropdown,
|
| 235 |
-
|
| 236 |
],
|
| 237 |
-
).then(lambda x: x, inputs=parser_dropdown, outputs=parser_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
load_button.click(
|
| 240 |
fn=load_and_parse,
|
|
@@ -246,10 +345,14 @@ def create_interface():
|
|
| 246 |
answer_output,
|
| 247 |
attributes_output,
|
| 248 |
split_dropdown,
|
| 249 |
-
|
| 250 |
],
|
| 251 |
api_name="load_and_parse",
|
| 252 |
show_progress="full",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
)
|
| 254 |
|
| 255 |
update_button.click(
|
|
@@ -264,6 +367,12 @@ def create_interface():
|
|
| 264 |
api_name="update_entry",
|
| 265 |
)
|
| 266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
return demo
|
| 268 |
|
| 269 |
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
from llmdataparser import ParserRegistry
|
| 7 |
+
from llmdataparser.base_parser import DatasetDescription, EvaluationMetric, ParseEntry
|
| 8 |
|
| 9 |
|
| 10 |
@lru_cache(maxsize=32)
|
|
|
|
| 164 |
get_parser_instance.cache_clear()
|
| 165 |
|
| 166 |
|
| 167 |
+
def format_dataset_description(description: DatasetDescription) -> str:
|
| 168 |
+
"""Format DatasetDescription into a readable string."""
|
| 169 |
+
formatted = [
|
| 170 |
+
f"# {description.name}",
|
| 171 |
+
f"\n**Purpose**: {description.purpose}",
|
| 172 |
+
f"\n**Language**: {description.language}",
|
| 173 |
+
f"\n**Format**: {description.format}",
|
| 174 |
+
f"\n**Source**: {description.source}",
|
| 175 |
+
f"\n**Characteristics**: {description.characteristics}",
|
| 176 |
+
]
|
| 177 |
+
|
| 178 |
+
if description.citation:
|
| 179 |
+
formatted.append(f"\n**Citation**:\n```\n{description.citation}\n```")
|
| 180 |
+
|
| 181 |
+
if description.additional_info:
|
| 182 |
+
formatted.append("\n**Additional Information**:")
|
| 183 |
+
for key, value in description.additional_info.items():
|
| 184 |
+
formatted.append(f"- {key}: {value}")
|
| 185 |
+
|
| 186 |
+
return "\n".join(formatted)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def get_primary_metrics(metrics: list[EvaluationMetric]) -> list[str]:
|
| 190 |
+
"""Get list of primary metric names."""
|
| 191 |
+
return [metric.name for metric in metrics if metric.primary]
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def format_metric_details(metric: EvaluationMetric) -> str:
|
| 195 |
+
"""Format a single EvaluationMetric into a readable string."""
|
| 196 |
+
return f"""# {metric.name}<br>
|
| 197 |
+
**Type**: {metric.type}<br>
|
| 198 |
+
**Description**: {metric.description}"""
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def update_dataset_info(parser_name: str) -> tuple:
|
| 202 |
+
"""Update dataset description and evaluation metrics information."""
|
| 203 |
+
try:
|
| 204 |
+
parser = get_parser_instance(parser_name)
|
| 205 |
+
description = parser.get_dataset_description()
|
| 206 |
+
metrics = parser.get_evaluation_metrics()
|
| 207 |
+
|
| 208 |
+
# Format description
|
| 209 |
+
desc_text = format_dataset_description(description)
|
| 210 |
+
|
| 211 |
+
# Get primary metrics for dropdown
|
| 212 |
+
primary_metrics = get_primary_metrics(metrics)
|
| 213 |
+
|
| 214 |
+
# Format details for first metric (or empty if no metrics)
|
| 215 |
+
first_metric = metrics[0] if metrics else None
|
| 216 |
+
metric_details = format_metric_details(first_metric) if first_metric else ""
|
| 217 |
+
|
| 218 |
+
return (
|
| 219 |
+
gr.Markdown(value=desc_text),
|
| 220 |
+
gr.Dropdown(
|
| 221 |
+
choices=primary_metrics,
|
| 222 |
+
value=primary_metrics[0] if primary_metrics else None,
|
| 223 |
+
),
|
| 224 |
+
gr.Markdown(value=metric_details),
|
| 225 |
+
)
|
| 226 |
+
except Exception as e:
|
| 227 |
+
return (
|
| 228 |
+
gr.Markdown(value=f"Error loading dataset description: {str(e)}"),
|
| 229 |
+
gr.Dropdown(choices=[]),
|
| 230 |
+
gr.Markdown(value=""),
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def update_metric_details(metric_name: str, parser_name: str) -> str:
|
| 235 |
+
"""Update the displayed metric details when selection changes."""
|
| 236 |
+
try:
|
| 237 |
+
parser = get_parser_instance(parser_name)
|
| 238 |
+
metrics = parser.get_evaluation_metrics()
|
| 239 |
+
selected_metric = next((m for m in metrics if m.name == metric_name), None)
|
| 240 |
+
return format_metric_details(selected_metric) if selected_metric else ""
|
| 241 |
+
except Exception as e:
|
| 242 |
+
return f"Error loading metric details: {str(e)}"
|
| 243 |
+
|
| 244 |
+
|
| 245 |
def create_interface():
|
| 246 |
with gr.Blocks() as demo:
|
| 247 |
gr.Markdown("# LLM Evaluation Dataset Parser")
|
| 248 |
|
| 249 |
# State management
|
| 250 |
parser_state = gr.State("")
|
| 251 |
+
dataset_status = gr.Textbox(label="Dataset Status", interactive=False)
|
| 252 |
+
|
| 253 |
+
with gr.Tabs():
|
| 254 |
+
with gr.Tab("Dataset Explorer"):
|
| 255 |
+
with gr.Row():
|
| 256 |
+
with gr.Column(scale=1):
|
| 257 |
+
# Parser selection and controls
|
| 258 |
+
available_parsers = ParserRegistry.list_parsers()
|
| 259 |
+
parser_dropdown = gr.Dropdown(
|
| 260 |
+
choices=available_parsers,
|
| 261 |
+
label="Select Parser",
|
| 262 |
+
value=available_parsers[0] if available_parsers else None,
|
| 263 |
+
interactive=True,
|
| 264 |
+
allow_custom_value=True,
|
| 265 |
+
)
|
| 266 |
+
task_dropdown = gr.Dropdown(
|
| 267 |
+
choices=["default"],
|
| 268 |
+
label="Select Task",
|
| 269 |
+
value="default",
|
| 270 |
+
interactive=True,
|
| 271 |
+
allow_custom_value=True,
|
| 272 |
+
)
|
| 273 |
+
split_dropdown = gr.Dropdown(
|
| 274 |
+
choices=[],
|
| 275 |
+
label="Select Split",
|
| 276 |
+
interactive=True,
|
| 277 |
+
value=None,
|
| 278 |
+
allow_custom_value=True,
|
| 279 |
+
)
|
| 280 |
+
load_button = gr.Button(
|
| 281 |
+
"Load and Parse Dataset", variant="primary"
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
# Entry selection
|
| 285 |
+
entry_index = gr.Number(
|
| 286 |
+
label="Select Entry Index (empty for random)",
|
| 287 |
+
precision=0,
|
| 288 |
+
interactive=True,
|
| 289 |
+
)
|
| 290 |
+
update_button = gr.Button(
|
| 291 |
+
"Update/Random Entry", variant="secondary"
|
| 292 |
+
)
|
| 293 |
+
|
| 294 |
+
with gr.Column(scale=2):
|
| 295 |
+
# Output displays
|
| 296 |
+
prompt_output = gr.Textbox(
|
| 297 |
+
label="Prompt", lines=5, show_copy_button=True
|
| 298 |
+
)
|
| 299 |
+
raw_question_output = gr.Textbox(
|
| 300 |
+
label="Raw Question", lines=5, show_copy_button=True
|
| 301 |
+
)
|
| 302 |
+
answer_output = gr.Textbox(
|
| 303 |
+
label="Answer", lines=5, show_copy_button=True
|
| 304 |
+
)
|
| 305 |
+
attributes_output = gr.Textbox(
|
| 306 |
+
label="Other Attributes", lines=5, show_copy_button=True
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
with gr.Tab("Dataset Information"):
|
| 310 |
+
with gr.Row():
|
| 311 |
+
with gr.Column(scale=2):
|
| 312 |
+
# Dataset description
|
| 313 |
+
dataset_description = gr.Markdown()
|
| 314 |
+
|
| 315 |
+
with gr.Column(scale=1):
|
| 316 |
+
# Evaluation metrics
|
| 317 |
+
gr.Markdown("## Evaluation Metrics")
|
| 318 |
+
metric_dropdown = gr.Dropdown(
|
| 319 |
+
label="Select Primary Metric", interactive=True
|
| 320 |
+
)
|
| 321 |
+
metric_details = gr.Markdown()
|
| 322 |
|
| 323 |
# Event handlers
|
| 324 |
parser_dropdown.change(
|
| 325 |
fn=update_parser_options,
|
| 326 |
inputs=parser_dropdown,
|
| 327 |
outputs=[
|
| 328 |
+
task_dropdown,
|
| 329 |
split_dropdown,
|
| 330 |
+
dataset_status,
|
| 331 |
],
|
| 332 |
+
).then(lambda x: x, inputs=parser_dropdown, outputs=parser_state).then(
|
| 333 |
+
fn=update_dataset_info,
|
| 334 |
+
inputs=[parser_dropdown],
|
| 335 |
+
outputs=[dataset_description, metric_dropdown, metric_details],
|
| 336 |
+
)
|
| 337 |
|
| 338 |
load_button.click(
|
| 339 |
fn=load_and_parse,
|
|
|
|
| 345 |
answer_output,
|
| 346 |
attributes_output,
|
| 347 |
split_dropdown,
|
| 348 |
+
dataset_status,
|
| 349 |
],
|
| 350 |
api_name="load_and_parse",
|
| 351 |
show_progress="full",
|
| 352 |
+
).then(
|
| 353 |
+
fn=update_dataset_info,
|
| 354 |
+
inputs=[parser_dropdown],
|
| 355 |
+
outputs=[dataset_description, metric_dropdown, metric_details],
|
| 356 |
)
|
| 357 |
|
| 358 |
update_button.click(
|
|
|
|
| 367 |
api_name="update_entry",
|
| 368 |
)
|
| 369 |
|
| 370 |
+
metric_dropdown.change(
|
| 371 |
+
fn=update_metric_details,
|
| 372 |
+
inputs=[metric_dropdown, parser_dropdown],
|
| 373 |
+
outputs=metric_details,
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
return demo
|
| 377 |
|
| 378 |
|