Skip to content

Commit 322c00d

Browse files
committed
Fixing to_wandb method (useless tempfile, opened several times)
1 parent 4753bb3 commit 322c00d

2 files changed

Lines changed: 45 additions & 32 deletions

File tree

python-client/giskard/scanner/report.py

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from pathlib import Path
2+
import random
3+
import string
24
import tempfile
35

46
import mlflow
@@ -72,7 +74,9 @@ def to_dataframe(self):
7274
{
7375
"domain": issue.meta.get("domain"),
7476
"slicing_fn": str(issue.slicing_fn) if issue.slicing_fn else None,
75-
"transformation_fn": str(issue.transformation_fn) if issue.transformation_fn else None,
77+
"transformation_fn": str(issue.transformation_fn)
78+
if issue.transformation_fn
79+
else None,
7680
"metric": issue.meta.get("metric"),
7781
"deviation": issue.meta.get("deviation"),
7882
"description": issue.description,
@@ -83,7 +87,9 @@ def to_dataframe(self):
8387
return df
8488

8589
def generate_tests(self, with_names=False):
86-
tests = sum([issue.generate_tests(with_names=with_names) for issue in self.issues], [])
90+
tests = sum(
91+
[issue.generate_tests(with_names=with_names) for issue in self.issues], []
92+
)
8793
return tests
8894

8995
def generate_test_suite(self, name=None):
@@ -131,7 +137,9 @@ def to_mlflow(
131137
model_artifact_path = "-for-" + model_artifact_path
132138

133139
with tempfile.NamedTemporaryFile(
134-
prefix="giskard-scan-results" + model_artifact_path + "-", suffix=".html", delete=False
140+
prefix="giskard-scan-results" + model_artifact_path + "-",
141+
suffix=".html",
142+
delete=False,
135143
) as f:
136144
# Get file path
137145
scan_results_local_path = f.name
@@ -147,7 +155,9 @@ def to_mlflow(
147155
if mlflow_client is None and mlflow_run_id is None:
148156
mlflow.log_artifact(scan_results_local_path)
149157
if summary:
150-
mlflow.log_table(results_df, artifact_file=scan_summary_artifact_name)
158+
mlflow.log_table(
159+
results_df, artifact_file=scan_summary_artifact_name
160+
)
151161
elif mlflow_client and mlflow_run_id:
152162
mlflow_client.log_artifact(mlflow_run_id, scan_results_local_path)
153163
if summary:
@@ -180,30 +190,34 @@ def to_wandb(self, **kwargs):
180190
from ..utils.analytics_collector import analytics
181191

182192
with wandb_run(**kwargs) as run:
183-
with tempfile.NamedTemporaryFile(prefix="giskard-scan-results-", suffix=".html") as f:
184-
try:
185-
self.to_html(filename=f.name)
186-
wandb_artifact_name = "Vulnerability scan results/" + f.name.split("/")[-1].split(".html")[0]
187-
analytics.track(
188-
"wandb_integration:scan_result",
189-
{
190-
"wandb_run_id": run.id,
191-
"has_issues": self.has_issues(),
192-
"issues_cnt": len(self.issues),
193-
},
194-
)
195-
except Exception as e:
196-
analytics.track(
197-
"wandb_integration:scan_result:error:unknown",
198-
{
199-
"wandb_run_id": run.id,
200-
"error": str(e),
201-
},
202-
)
203-
raise ValueError(
204-
"An error occurred while logging the scan results into wandb. "
205-
"Please submit the traceback as a GitHub issue in the following "
206-
"repository for further assistance: https://github.com/Giskard-AI/giskard."
207-
) from e
208-
209-
run.log({wandb_artifact_name: wandb.Html(open(f.name), inject=False)})
193+
try:
194+
html = self.to_html()
195+
suffix = "".join(
196+
random.choices(string.ascii_lowercase + string.digits, k=8)
197+
)
198+
wandb_artifact_name = (
199+
f"Vulnerability scan results/giskard-scan-results-{suffix}"
200+
)
201+
analytics.track(
202+
"wandb_integration:scan_result",
203+
{
204+
"wandb_run_id": run.id,
205+
"has_issues": self.has_issues(),
206+
"issues_cnt": len(self.issues),
207+
},
208+
)
209+
except Exception as e:
210+
analytics.track(
211+
"wandb_integration:scan_result:error:unknown",
212+
{
213+
"wandb_run_id": run.id,
214+
"error": str(e),
215+
},
216+
)
217+
raise ValueError(
218+
"An error occurred while logging the scan results into wandb. "
219+
"Please submit the traceback as a GitHub issue in the following "
220+
"repository for further assistance: https://github.com/Giskard-AI/giskard."
221+
) from e
222+
223+
run.log({wandb_artifact_name: wandb.Html(html, inject=False)})

python-client/trigger.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)