Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 81 additions & 10 deletions giskard/scanner/issues.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import List, Optional
from typing import Any, List, Optional

import math
from abc import abstractmethod
from dataclasses import dataclass
from enum import Enum

Expand All @@ -25,6 +27,70 @@ class IssueGroup:
description: str


class ExampleManager:
"""
Abstract class to manage examples, in order to deal with other data types than pandas dataframes
and render them in html
"""

def __init__(self):
self._examples = []
self._max_num = math.inf

def add_examples(self, example: Any):
"""
Add examples to the example manager

Args:
example (Any): new example to be added
"""
if isinstance(example, list):
self._examples += example
else:
self._examples.append(example)

def head(self, n):
"""
Change the max nmuber of elements to display

Args:
n (int): number of elements to display

Returns:
ExampleManager: current object with max number of elements set to n
"""
self._max_num = n
return self

def __len__(self):
return len(self._examples)

def len(self):
return self.__len__()

@abstractmethod
def to_html(self):
"""
Renders html
"""
...


class ExampleManagerDataFrame(ExampleManager):
"""
Example manager for pandas dataframes
"""

def __init__(self):
self._examples = pd.DataFrame()

def add_examples(self, example):
self._examples = pd.concat([self._examples, example])

def head(self, n):
return self._examples.head(n)


class Issue:
def __init__(
self,
Expand All @@ -41,6 +107,8 @@ def __init__(
features: Optional[List[str]] = None,
tests=None,
taxonomy: List[str] = None,
example_manager: Optional[ExampleManager] = ExampleManagerDataFrame,
display_warnings: Optional[bool] = True,
):
"""Issue represents a single model vulnerability detected by Giskard.

Expand Down Expand Up @@ -76,6 +144,10 @@ def __init__(
taxonomy : Optional[str]
List of taxonomy machine tags, in MISP format. A machine tag is composed of a namespace (MUST), a predicate
(MUST) and an (OPTIONAL) value, like ``namespace:predicate:value``.
example_manager : Optional[ExampleManager]
Example manager to handle examples
display_warnings : Optional[bool]
Whether to display warnings or not
"""
self.group = group
self.model = model
Expand All @@ -90,6 +162,10 @@ def __init__(
self._features = features
self._tests = tests
self.taxonomy = taxonomy or []
self.display_warnings = display_warnings
self.example_manager = example_manager()
if self._examples is not None:
self.example_manager.add_examples(self._examples)

def __repr__(self):
return f"<{self.__class__.__name__} group='{self.group.name}' level='{self.level}'>"
Expand Down Expand Up @@ -129,16 +205,11 @@ def description(self):
**self.meta,
)

def examples(self, n=3) -> pd.DataFrame:
if self._examples is not None:
return self._examples.head(n)
return pd.DataFrame()
def examples(self, n=3) -> Any:
return self.example_manager.head(n)

def add_examples(self, examples: pd.DataFrame):
if self._examples is None:
self._examples = examples
else:
self._examples = pd.concat([self._examples, examples])
def add_examples(self, examples: Any):
self.example_manager.add_examples(examples)

def generate_tests(self, with_names=False) -> list:
tests = self._tests(self) if callable(self._tests) else self._tests
Expand Down
2 changes: 1 addition & 1 deletion giskard/visualization/templates/scan_report/html/full.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div id="gsk-scan" class="dark:text-white dark:bg-zinc-800 rounded border border-gray-500">
{% include "scan_report/html/_tab_header.html" %}
{% include "scan_report/html/_main_content.html" %}
{% if issues|length > 0 and not issues[0].model.is_text_generation %}
{% if issues|length > 0 and not issues[0].model.is_text_generation and issues[0].display_warnings %}
{% include "scan_report/html/_code_snippet.html" %}
{% endif %}
</div>
Expand Down