Skip to content

Commit

Permalink
Issue#17 - Add Progress Bar to show progress on perturbations and scores
Browse files Browse the repository at this point in the history
  • Loading branch information
jimilp7 committed Sep 29, 2023
1 parent 3167a96 commit 7789de4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions auditor/evaluation/discriminative.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from auditor.reporting import generate_robustness_report
from auditor.utils.logging import get_logger
from auditor.utils.progress_logger import ProgressLogger

LOG = get_logger(__name__)

Expand Down Expand Up @@ -90,6 +91,8 @@ def evaluate(
f'Started model evaluation with perturbation type '
f'{self.perturbed_dataset.perturbation_type}'
)
progress_bar = ProgressLogger(total_steps=min(len(self.perturbed_dataset.data), len(self.perturbed_dataset.metadata)), description="Starting Model Evaluation")

for perturbed_samples, metadata_samples in zip(
self.perturbed_dataset.data, self.perturbed_dataset.metadata
):
Expand Down Expand Up @@ -126,12 +129,14 @@ def evaluate(
metadata=mdata,
)
)
progress_bar.update()
robust_accuracy = self.compute_accuracy(test_results)
LOG.info(f'Robust Accuracy: {robust_accuracy*100.}')
LOG.info(
'Completed model evaluation with perturbation type '
f'{self.perturbed_dataset.perturbation_type}'
)
progress_bar.close()
self.test_results = TestSummary(
results=test_results,
robust_accuracy=robust_accuracy,
Expand Down
7 changes: 7 additions & 0 deletions auditor/evaluation/expected_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
from sentence_transformers.SentenceTransformer import SentenceTransformer

from auditor.utils.progress_logger import ProgressLogger
from auditor.utils.similarity import compute_similarity
from auditor.utils.logging import get_logger

Expand Down Expand Up @@ -163,6 +164,8 @@ def check(
reference_generation: str,
) -> List[Tuple[bool, Dict[str, float]]]:
test_results = []
progress_bar = ProgressLogger(total_steps=len(perturbed_generations), description="Fetching Scores")

for peturbed_gen in perturbed_generations:
try:
score = compute_similarity(
Expand All @@ -178,9 +181,13 @@ def check(
self.similarity_metric_key: round(score, ndigits=2)
}
test_results.append((test_status, score_dict))
progress_bar.update()
except Exception as e:
LOG.error('Unable to complete semanatic similarity checks')
progress_bar.close()
raise e

progress_bar.close()
return test_results

def behavior_description(self):
Expand Down
6 changes: 6 additions & 0 deletions auditor/evaluation/generative.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from auditor.utils.logging import get_logger
from auditor.perturbations import Paraphrase
from auditor.perturbations import TransformBase
from auditor.utils.progress_logger import ProgressLogger

LOG = get_logger(__name__)

Expand Down Expand Up @@ -98,6 +99,8 @@ def _evaluate_generations(
else:
evaluate_prompts = prompt_perturbations

progress_bar = ProgressLogger(total_steps=len(evaluate_prompts), description="Applying Perturbations")

# generations for each of the perturbed prompts
alternative_generations = []
for alt_prompt in evaluate_prompts:
Expand All @@ -107,6 +110,9 @@ def _evaluate_generations(
post_context,
)
alternative_generations.append(resp)
progress_bar.update()

progress_bar.close()

# create test result
metric = self.expected_behavior.check(
Expand Down
15 changes: 15 additions & 0 deletions auditor/utils/progress_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import tqdm

class ProgressLogger:
"""class to show progress bar"""
def __init__(self, total_steps, description="Logging..."):
self.total_steps = total_steps
self.description = description

self.pbar = tqdm.tqdm(total=total_steps, desc=description)

def update(self, incremental=1):
self.pbar.update(incremental)

def close(self):
self.pbar.close()

0 comments on commit 7789de4

Please sign in to comment.