forked from NVIDIA-NeMo/DataDesigner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
474 lines (399 loc) · 17.8 KB
/
visualization.py
File metadata and controls
474 lines (399 loc) · 17.8 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import json
import os
from collections import OrderedDict
from enum import Enum
from functools import cached_property
from typing import TYPE_CHECKING, Any, Optional, Union
import numpy as np
import pandas as pd
from rich.console import Console, Group
from rich.padding import Padding
from rich.panel import Panel
from rich.pretty import Pretty
from rich.rule import Rule
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
from data_designer.config.base import ConfigBase
from data_designer.config.column_types import DataDesignerColumnType
from data_designer.config.models import ModelConfig, ModelProvider
from data_designer.config.sampler_params import SamplerType
from data_designer.config.utils.code_lang import code_lang_to_syntax_lexer
from data_designer.config.utils.constants import NVIDIA_API_KEY_ENV_VAR_NAME, OPENAI_API_KEY_ENV_VAR_NAME
from data_designer.config.utils.errors import DatasetSampleDisplayError
if TYPE_CHECKING:
from data_designer.config.config_builder import DataDesignerConfigBuilder
console = Console()
def get_nvidia_api_key() -> Optional[str]:
return os.getenv(NVIDIA_API_KEY_ENV_VAR_NAME)
def get_openai_api_key() -> Optional[str]:
return os.getenv(OPENAI_API_KEY_ENV_VAR_NAME)
class ColorPalette(str, Enum):
NVIDIA_GREEN = "#76b900"
PURPLE = "#9525c6"
YELLOW = "#f9c500"
BLUE = "#0074df"
RED = "#e52020"
ORANGE = "#ef9100"
MAGENTA = "#d2308e"
TEAL = "#1dbba4"
class WithRecordSamplerMixin:
_display_cycle_index: int = 0
@cached_property
def _record_sampler_dataset(self) -> pd.DataFrame:
if hasattr(self, "dataset") and self.dataset is not None and isinstance(self.dataset, pd.DataFrame):
return self.dataset
elif (
hasattr(self, "load_dataset")
and callable(self.load_dataset)
and (dataset := self.load_dataset()) is not None
and isinstance(dataset, pd.DataFrame)
):
return dataset
else:
raise DatasetSampleDisplayError("No valid dataset found in results object.")
def _has_processor_artifacts(self) -> bool:
return hasattr(self, "processor_artifacts") and self.processor_artifacts is not None
def display_sample_record(
self,
index: Optional[int] = None,
*,
hide_seed_columns: bool = False,
syntax_highlighting_theme: str = "dracula",
background_color: Optional[str] = None,
processors_to_display: Optional[list[str]] = None,
) -> None:
"""Display a sample record from the Data Designer dataset preview.
Args:
index: Index of the record to display. If None, the next record will be displayed.
This is useful for running the cell in a notebook multiple times.
hide_seed_columns: If True, the columns from the seed dataset (if any) will not be displayed.
syntax_highlighting_theme: Theme to use for syntax highlighting. See the `Syntax`
documentation from `rich` for information about available themes.
background_color: Background color to use for the record. See the `Syntax`
documentation from `rich` for information about available background colors.
processors_to_display: List of processors to display the artifacts for. If None, all processors will be displayed.
"""
i = index or self._display_cycle_index
try:
record = self._record_sampler_dataset.iloc[i]
num_records = len(self._record_sampler_dataset)
except IndexError:
raise DatasetSampleDisplayError(f"Index {i} is out of bounds for dataset of length {num_records}.")
processor_data_to_display = None
if self._has_processor_artifacts() and len(self.processor_artifacts) > 0:
if processors_to_display is None:
processors_to_display = list(self.processor_artifacts.keys())
if len(processors_to_display) > 0:
processor_data_to_display = {}
for processor in processors_to_display:
if (
isinstance(self.processor_artifacts[processor], list)
and len(self.processor_artifacts[processor]) == num_records
):
processor_data_to_display[processor] = self.processor_artifacts[processor][i]
else:
processor_data_to_display[processor] = self.processor_artifacts[processor]
display_sample_record(
record=record,
processor_data_to_display=processor_data_to_display,
config_builder=self.config_builder,
background_color=background_color,
syntax_highlighting_theme=syntax_highlighting_theme,
hide_seed_columns=hide_seed_columns,
record_index=i,
)
if index is None:
self._display_cycle_index = (self._display_cycle_index + 1) % num_records
def create_rich_histogram_table(
data: dict[str, Union[int, float]],
column_names: tuple[int, int],
name_style: str = ColorPalette.BLUE.value,
value_style: str = ColorPalette.TEAL.value,
title: Optional[str] = None,
**kwargs,
) -> Table:
table = Table(title=title, **kwargs)
table.add_column(column_names[0], justify="right", style=name_style)
table.add_column(column_names[1], justify="left", style=value_style)
max_count = max(data.values())
for name, value in data.items():
bar = "" if max_count <= 0 else "█" * int((value / max_count) * 20)
table.add_row(str(name), f"{bar} {value:.1f}")
return table
def display_sample_record(
record: Union[dict, pd.Series, pd.DataFrame],
config_builder: DataDesignerConfigBuilder,
processor_data_to_display: Optional[dict[str, Union[list[str], str]]] = None,
background_color: Optional[str] = None,
syntax_highlighting_theme: str = "dracula",
record_index: Optional[int] = None,
hide_seed_columns: bool = False,
):
if isinstance(record, (dict, pd.Series)):
record = pd.DataFrame([record]).iloc[0]
elif isinstance(record, pd.DataFrame):
if record.shape[0] > 1:
raise DatasetSampleDisplayError(
f"The record must be a single record. You provided a DataFrame with {record.shape[0]} records."
)
record = record.iloc[0]
else:
raise DatasetSampleDisplayError(
"The record must be a single record in a dictionary, pandas Series, "
f"or pandas DataFrame. You provided: {type(record)}."
)
render_list = []
table_kws = dict(show_lines=True, expand=True)
seed_columns = config_builder.get_columns_of_type(DataDesignerColumnType.SEED_DATASET)
if not hide_seed_columns and len(seed_columns) > 0:
table = Table(title="Seed Columns", **table_kws)
table.add_column("Name")
table.add_column("Value")
for col in seed_columns:
if not col.drop:
table.add_row(col.name, convert_to_row_element(record[col.name]))
render_list.append(pad_console_element(table))
non_code_columns = (
config_builder.get_columns_of_type(DataDesignerColumnType.SAMPLER)
+ config_builder.get_columns_of_type(DataDesignerColumnType.EXPRESSION)
+ config_builder.get_columns_of_type(DataDesignerColumnType.LLM_TEXT)
+ config_builder.get_columns_of_type(DataDesignerColumnType.LLM_STRUCTURED)
+ config_builder.get_columns_of_type(DataDesignerColumnType.EMBEDDING)
)
if len(non_code_columns) > 0:
table = Table(title="Generated Columns", **table_kws)
table.add_column("Name")
table.add_column("Value")
for col in non_code_columns:
if not col.drop:
if col.column_type == DataDesignerColumnType.EMBEDDING:
record[col.name]["embeddings"] = [
get_truncated_list_as_string(embd) for embd in record[col.name].get("embeddings")
]
table.add_row(col.name, convert_to_row_element(record[col.name]))
render_list.append(pad_console_element(table))
for col in config_builder.get_columns_of_type(DataDesignerColumnType.LLM_CODE):
panel = Panel(
Syntax(
record[col.name],
lexer=code_lang_to_syntax_lexer(col.code_lang),
theme=syntax_highlighting_theme,
word_wrap=True,
background_color=background_color,
),
title=col.name,
expand=True,
)
render_list.append(pad_console_element(panel))
validation_columns = config_builder.get_columns_of_type(DataDesignerColumnType.VALIDATION)
if len(validation_columns) > 0:
table = Table(title="Validation", **table_kws)
table.add_column("Name")
table.add_column("Value", ratio=1)
for col in validation_columns:
if not col.drop:
# Add is_valid before other fields
if "is_valid" in record[col.name]:
value_to_display = {"is_valid": record[col.name].get("is_valid")} | record[col.name]
else: # if columns treated separately
value_to_display = {}
for col_name, validation_output in record[col.name].items():
value_to_display[col_name] = {
"is_valid": validation_output.get("is_valid", None)
} | validation_output
table.add_row(col.name, convert_to_row_element(value_to_display))
render_list.append(pad_console_element(table, (1, 0, 1, 0)))
llm_judge_columns = config_builder.get_columns_of_type(DataDesignerColumnType.LLM_JUDGE)
if len(llm_judge_columns) > 0:
for col in llm_judge_columns:
if col.drop:
continue
table = Table(title=f"LLM-as-a-Judge: {col.name}", **table_kws)
row = []
judge = record[col.name]
for measure, results in judge.items():
table.add_column(measure)
row.append(f"score: {results['score']}\nreasoning: {results['reasoning']}")
table.add_row(*row)
render_list.append(pad_console_element(table, (1, 0, 1, 0)))
if processor_data_to_display and len(processor_data_to_display) > 0:
for processor_name, processor_data in processor_data_to_display.items():
table = Table(title=f"Processor Outputs: {processor_name}", **table_kws)
table.add_column("Name")
table.add_column("Value")
for col, value in processor_data.items():
table.add_row(col, convert_to_row_element(value))
render_list.append(pad_console_element(table, (1, 0, 1, 0)))
if record_index is not None:
index_label = Text(f"[index: {record_index}]", justify="center")
render_list.append(index_label)
console.print(Group(*render_list), markup=False)
def get_truncated_list_as_string(long_list: list[Any], max_items: int = 2) -> str:
if max_items <= 0:
raise ValueError("max_items must be greater than 0")
if len(long_list) > max_items:
truncated_part = long_list[:max_items]
return f"[{', '.join(str(x) for x in truncated_part)}, ...]"
else:
return str(long_list)
def display_sampler_table(
sampler_params: dict[SamplerType, ConfigBase],
title: Optional[str] = None,
) -> None:
table = Table(expand=True)
table.add_column("Type")
table.add_column("Parameter")
table.add_column("Data Type")
table.add_column("Required", justify="center")
table.add_column("Constraints")
for sampler_type, params in sampler_params.items():
num = 0
schema = params.model_json_schema()
for param_name, field_info in schema["properties"].items():
is_required = param_name in schema.get("required", [])
table.add_row(
sampler_type if num == 0 else "",
param_name,
_get_field_type(field_info),
"✓" if is_required else "",
_get_field_constraints(field_info, schema),
)
num += 1
table.add_section()
title = title or "NeMo Data Designer Samplers"
group = Group(Rule(title, end="\n\n"), table)
console.print(group)
def display_model_configs_table(model_configs: list[ModelConfig]) -> None:
table_model_configs = Table(expand=True)
table_model_configs.add_column("Alias")
table_model_configs.add_column("Model")
table_model_configs.add_column("Provider")
table_model_configs.add_column("Inference Parameters")
for model_config in model_configs:
params_display = model_config.inference_parameters.format_for_display()
table_model_configs.add_row(
model_config.alias,
model_config.model,
model_config.provider,
params_display,
)
group_args: list = [Rule(title="Model Configs"), table_model_configs]
if len(model_configs) == 0:
subtitle = Text(
"‼️ No model configs found. Please provide at least one model config to the config builder",
style="dim",
justify="center",
)
group_args.insert(1, subtitle)
group = Group(*group_args)
console.print(group)
def display_model_providers_table(model_providers: list[ModelProvider]) -> None:
table_model_providers = Table(expand=True)
table_model_providers.add_column("Name")
table_model_providers.add_column("Endpoint")
table_model_providers.add_column("API Key")
for model_provider in model_providers:
api_key = model_provider.api_key
if model_provider.api_key == OPENAI_API_KEY_ENV_VAR_NAME:
if get_openai_api_key() is not None:
api_key = mask_api_key(get_openai_api_key())
else:
api_key = f"* {OPENAI_API_KEY_ENV_VAR_NAME!r} not set in environment variables * "
elif model_provider.api_key == NVIDIA_API_KEY_ENV_VAR_NAME:
if get_nvidia_api_key() is not None:
api_key = mask_api_key(get_nvidia_api_key())
else:
api_key = f"* {NVIDIA_API_KEY_ENV_VAR_NAME!r} not set in environment variables *"
else:
api_key = mask_api_key(model_provider.api_key)
table_model_providers.add_row(model_provider.name, model_provider.endpoint, api_key)
group = Group(Rule(title="Model Providers"), table_model_providers)
console.print(group)
def mask_api_key(api_key: str | None) -> str:
"""Mask API keys for display.
Environment variable names (all uppercase) are kept visible.
Actual API keys are masked to show only the last 4 characters.
Args:
api_key: The API key to mask.
Returns:
Masked API key string or "(not set)" if None.
"""
if not api_key:
return "(not set)"
# Keep environment variable names visible
if api_key.isupper():
return api_key
# Mask actual API keys
return "***" + api_key[-4:] if len(api_key) > 4 else "***"
def convert_to_row_element(elem):
try:
elem = Pretty(json.loads(elem))
except (TypeError, json.JSONDecodeError):
pass
if isinstance(elem, (np.integer, np.floating, np.ndarray)):
elem = str(elem)
elif isinstance(elem, (list, dict)):
elem = Pretty(elem)
return elem
def pad_console_element(elem, padding=(1, 0, 1, 0)):
return Padding(elem, padding)
def _get_field_type(field: dict) -> str:
"""Extract human-readable type information from a JSON Schema field."""
# single type
if "type" in field:
if field["type"] == "array":
return " | ".join([f"{f.strip()}[]" for f in _get_field_type(field["items"]).split("|")])
if field["type"] == "object":
return "dict"
return field["type"]
# union type
elif "anyOf" in field:
types = []
for f in field["anyOf"]:
if "$ref" in f:
types.append("enum")
elif f.get("type") == "array":
if "items" in f and "$ref" in f["items"]:
types.append("enum[]")
else:
types.append(f"{f['items']['type']}[]")
else:
types.append(f.get("type", ""))
return " | ".join(t for t in types if t)
return ""
def _get_field_constraints(field: dict, schema: dict) -> str:
"""Extract human-readable constraints from a JSON Schema field."""
constraints = []
# numeric constraints
if "minimum" in field:
constraints.append(f">= {field['minimum']}")
if "exclusiveMinimum" in field:
constraints.append(f"> {field['exclusiveMinimum']}")
if "maximum" in field:
constraints.append(f"<= {field['maximum']}")
if "exclusiveMaximum" in field:
constraints.append(f"< {field['exclusiveMaximum']}")
# string constraints
if "minLength" in field:
constraints.append(f"len > {field['minLength']}")
if "maxLength" in field:
constraints.append(f"len < {field['maxLength']}")
# array constraints
if "minItems" in field:
constraints.append(f"len > {field['minItems']}")
if "maxItems" in field:
constraints.append(f"len < {field['maxItems']}")
# enum constraints
if "enum" in _get_field_type(field) and "$defs" in schema:
enum_values = []
for defs in schema["$defs"].values():
if "enum" in defs:
enum_values.extend(defs["enum"])
if len(enum_values) > 0:
enum_values = OrderedDict.fromkeys(enum_values)
constraints.append(f"allowed: {', '.join(enum_values.keys())}")
return ", ".join(constraints)