Skip to content

Commit

Permalink
style: 🎨 clean up f-string without any placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
CandiedCode committed Apr 6, 2024
1 parent 5e3251a commit 0eeb158
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 73 deletions.
2 changes: 1 addition & 1 deletion modelscan/modelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _scan_source(
ModelScanSkipped(
"ModelScan",
SkipCategories.SCAN_NOT_SUPPORTED,
f"Model Scan did not scan file",
"Model Scan did not scan file",
str(model.get_source()),
)
)
Expand Down
2 changes: 1 addition & 1 deletion modelscan/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def generate(
total_issue_count = len(scan.issues.all_issues)
if total_issue_count > 0:
print(f"\nTotal Issues: {total_issue_count}")
print(f"\nTotal Issues By Severity:\n")
print("\nTotal Issues By Severity:\n")
for severity in IssueSeverity:
if severity.name in issues_by_severity:
print(
Expand Down
4 changes: 2 additions & 2 deletions modelscan/scanners/h5/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _scan_keras_h5_file(self, model: Model) -> Optional[ScanResults]:
ModelScanError(
self.name(),
ErrorCategories.JSON_DECODE,
f"Not a valid JSON data",
"Not a valid JSON data",
str(model.get_source()),
)
],
Expand All @@ -83,7 +83,7 @@ def _scan_keras_h5_file(self, model: Model) -> Optional[ScanResults]:
ModelScanSkipped(
self.name(),
SkipCategories.MODEL_CONFIG,
f"Model Config not found",
"Model Config not found",
str(model.get_source()),
)
],
Expand Down
5 changes: 2 additions & 3 deletions modelscan/scanners/keras/scan.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json
import zipfile
import logging
from pathlib import Path
from typing import IO, List, Union, Optional
from typing import List, Optional


from modelscan.error import ModelScanError, ErrorCategories
Expand Down Expand Up @@ -67,7 +66,7 @@ def scan(self, model: Model) -> Optional[ScanResults]:
ModelScanError(
self.name(),
ErrorCategories.MODEL_SCAN, # Giving a generic error category as this return is added to pass mypy
f"Unable to scan .keras file", # Not sure if this is a representative message for ModelScanError
"Unable to scan .keras file", # Not sure if this is a representative message for ModelScanError
str(model.get_source()),
)
],
Expand Down
2 changes: 1 addition & 1 deletion modelscan/scanners/saved_model/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _scan(self, model: Model) -> Optional[ScanResults]:
ModelScanError(
self.name(),
ErrorCategories.JSON_DECODE,
f"Not a valid JSON data",
"Not a valid JSON data",
str(model.get_source()),
)
],
Expand Down
2 changes: 0 additions & 2 deletions modelscan/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import http.client
import io
import urllib.parse
from pathlib import Path
from pickletools import genops # nosec
from typing import IO, Optional, Union
Expand Down
4 changes: 1 addition & 3 deletions notebooks/utils/pytorch_sentiment_model.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from transformers import AutoModelForSequenceClassification
from transformers import TFAutoModelForSequenceClassification
from transformers import AutoTokenizer
import numpy as np
from scipy.special import softmax
import csv
import urllib.request
import os
import torch


Expand All @@ -30,7 +28,7 @@ def download_model(safe_model_path):

def predict_sentiment(text: str, model):
task = "sentiment"
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
MODEL = "cardiffnlp/twitter-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(MODEL)

text = preprocess(text)
Expand Down
120 changes: 60 additions & 60 deletions tests/test_modelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def test_scan_zip(zip_file_path: Any) -> None:

ms = ModelScan()
results = ms.scan(f"{zip_file_path}/test.zip")
assert results["summary"]["scanned"]["scanned_files"] == [f"test.zip:data.pkl"]
assert results["summary"]["scanned"]["scanned_files"] == ["test.zip:data.pkl"]
assert results["summary"]["skipped"]["skipped_files"] == []
assert ms.issues.all_issues == expected

Expand All @@ -495,15 +495,15 @@ def test_scan_pytorch(pytorch_file_path: Any) -> None:
assert results["summary"]["skipped"]["skipped_files"] == [
{
"category": SkipCategories.MAGIC_NUMBER.name,
"description": f"Invalid magic number",
"source": f"bad_pytorch.pt",
"description": "Invalid magic number",
"source": "bad_pytorch.pt",
}
]
assert ms.issues.all_issues == []

results = ms.scan(Path(f"{pytorch_file_path}/safe_zip_pytorch.pt"))
assert results["summary"]["scanned"]["scanned_files"] == [
f"safe_zip_pytorch.pt:safe_zip_pytorch/data.pkl"
"safe_zip_pytorch.pt:safe_zip_pytorch/data.pkl"
]

assert set(
Expand All @@ -521,7 +521,7 @@ def test_scan_pytorch(pytorch_file_path: Any) -> None:

results = ms.scan(Path(f"{pytorch_file_path}/safe_old_format_pytorch.pt"))
assert results["summary"]["scanned"]["scanned_files"] == [
f"safe_old_format_pytorch.pt"
"safe_old_format_pytorch.pt"
]
assert results["summary"]["skipped"]["skipped_files"] == []
assert ms.issues.all_issues == []
Expand All @@ -542,7 +542,7 @@ def test_scan_pytorch(pytorch_file_path: Any) -> None:
]
results = ms.scan(unsafe_zip_path)
assert results["summary"]["scanned"]["scanned_files"] == [
f"unsafe_zip_pytorch.pt:unsafe_zip_pytorch/data.pkl",
"unsafe_zip_pytorch.pt:unsafe_zip_pytorch/data.pkl",
]
assert set(
[
Expand All @@ -562,7 +562,7 @@ def test_scan_numpy(numpy_file_path: Any) -> None:
ms = ModelScan()
results = ms.scan(f"{numpy_file_path}/safe_numpy.npy")
assert ms.issues.all_issues == []
assert results["summary"]["scanned"]["scanned_files"] == [f"safe_numpy.npy"]
assert results["summary"]["scanned"]["scanned_files"] == ["safe_numpy.npy"]
assert results["summary"]["skipped"]["skipped_files"] == []
assert results["errors"] == []

Expand All @@ -581,7 +581,7 @@ def test_scan_numpy(numpy_file_path: Any) -> None:

results = ms.scan(f"{numpy_file_path}/unsafe_numpy.npy")
compare_results(ms.issues.all_issues, expected)
assert results["summary"]["scanned"]["scanned_files"] == [f"unsafe_numpy.npy"]
assert results["summary"]["scanned"]["scanned_files"] == ["unsafe_numpy.npy"]
assert results["summary"]["skipped"]["skipped_files"] == []
assert results["errors"] == []

Expand All @@ -590,14 +590,14 @@ def test_scan_file_path(file_path: Any) -> None:
benign_pickle = ModelScan()
results = benign_pickle.scan(Path(f"{file_path}/data/benign0_v3.pkl"))
assert benign_pickle.issues.all_issues == []
assert results["summary"]["scanned"]["scanned_files"] == [f"benign0_v3.pkl"]
assert results["summary"]["scanned"]["scanned_files"] == ["benign0_v3.pkl"]
assert results["summary"]["skipped"]["skipped_files"] == []
assert results["errors"] == []

benign_dill = ModelScan()
results = benign_dill.scan(Path(f"{file_path}/data/benign0_v3.dill"))
assert benign_dill.issues.all_issues == []
assert results["summary"]["scanned"]["scanned_files"] == [f"benign0_v3.dill"]
assert results["summary"]["scanned"]["scanned_files"] == ["benign0_v3.dill"]
assert results["summary"]["skipped"]["skipped_files"] == []
assert results["errors"] == []

Expand Down Expand Up @@ -646,7 +646,7 @@ def test_scan_file_path(file_path: Any) -> None:
}
results = malicious0.scan(Path(f"{file_path}/data/malicious0.pkl"))
compare_results(malicious0.issues.all_issues, expected_malicious0)
assert results["summary"]["scanned"]["scanned_files"] == [f"malicious0.pkl"]
assert results["summary"]["scanned"]["scanned_files"] == ["malicious0.pkl"]
assert results["summary"]["skipped"]["skipped_files"] == []
assert results["errors"] == []

Expand Down Expand Up @@ -1268,35 +1268,35 @@ def test_scan_directory_path(file_path: str) -> None:
results = ms.scan(p)
compare_results(ms.issues.all_issues, expected)
assert set(results["summary"]["scanned"]["scanned_files"]) == {
f"malicious1.zip:data.pkl",
f"malicious0.pkl",
f"malicious3.pkl",
f"malicious6.pkl",
f"malicious7.pkl",
f"malicious8.pkl",
f"malicious9.pkl",
f"malicious10.pkl",
f"malicious11.pkl",
f"malicious12.pkl",
f"malicious13.pkl",
f"malicious14.pkl",
f"malicious1_v0.dill",
f"malicious1_v3.dill",
f"malicious1_v4.dill",
f"malicious4.pickle",
f"malicious5.pickle",
f"malicious1_v0.pkl",
f"malicious1_v3.pkl",
f"malicious1_v4.pkl",
f"malicious2_v0.pkl",
f"malicious2_v3.pkl",
f"malicious2_v4.pkl",
f"benign0_v0.pkl",
f"benign0_v3.pkl",
f"benign0_v4.pkl",
f"benign0_v0.dill",
f"benign0_v3.dill",
f"benign0_v4.dill",
"malicious1.zip:data.pkl",
"malicious0.pkl",
"malicious3.pkl",
"malicious6.pkl",
"malicious7.pkl",
"malicious8.pkl",
"malicious9.pkl",
"malicious10.pkl",
"malicious11.pkl",
"malicious12.pkl",
"malicious13.pkl",
"malicious14.pkl",
"malicious1_v0.dill",
"malicious1_v3.dill",
"malicious1_v4.dill",
"malicious4.pickle",
"malicious5.pickle",
"malicious1_v0.pkl",
"malicious1_v3.pkl",
"malicious1_v4.pkl",
"malicious2_v0.pkl",
"malicious2_v3.pkl",
"malicious2_v4.pkl",
"benign0_v0.pkl",
"benign0_v3.pkl",
"benign0_v4.pkl",
"benign0_v0.dill",
"benign0_v3.dill",
"benign0_v4.dill",
}
assert results["summary"]["skipped"]["skipped_files"] == []
assert results["errors"] == []
Expand Down Expand Up @@ -1325,9 +1325,9 @@ def test_scan_keras(keras_file_path: Any, file_extension: str) -> None:

if file_extension == ".pb":
assert set(results["summary"]["scanned"]["scanned_files"]) == {
f"fingerprint.pb",
f"keras_metadata.pb",
f"saved_model.pb",
"fingerprint.pb",
"keras_metadata.pb",
"saved_model.pb",
}

assert set(
Expand All @@ -1336,8 +1336,8 @@ def test_scan_keras(keras_file_path: Any, file_extension: str) -> None:
for skipped_file in results["summary"]["skipped"]["skipped_files"]
]
) == {
f"variables/variables.data-00000-of-00001",
f"variables/variables.index",
"variables/variables.data-00000-of-00001",
"variables/variables.index",
}
assert results["errors"] == []

Expand Down Expand Up @@ -1420,18 +1420,18 @@ def test_scan_keras(keras_file_path: Any, file_extension: str) -> None:
assert ms.issues.all_issues == expected
assert results["errors"] == []
assert set(results["summary"]["scanned"]["scanned_files"]) == {
f"fingerprint.pb",
f"keras_metadata.pb",
f"saved_model.pb",
"fingerprint.pb",
"keras_metadata.pb",
"saved_model.pb",
}
assert set(
[
skipped_file["source"]
for skipped_file in results["summary"]["skipped"]["skipped_files"]
]
) == {
f"variables/variables.data-00000-of-00001",
f"variables/variables.index",
"variables/variables.data-00000-of-00001",
"variables/variables.index",
}
else:
unsafe_filename = f"{keras_file_path_parent_dir}/unsafe{file_extension}"
Expand Down Expand Up @@ -1484,18 +1484,18 @@ def test_scan_tensorflow(tensorflow_file_path: Any) -> None:
results = ms.scan(Path(f"{safe_tensorflow_model_dir}"))
assert ms.issues.all_issues == []
assert set(results["summary"]["scanned"]["scanned_files"]) == {
f"fingerprint.pb",
f"keras_metadata.pb",
f"saved_model.pb",
"fingerprint.pb",
"keras_metadata.pb",
"saved_model.pb",
}
assert set(
[
skipped_file["source"]
for skipped_file in results["summary"]["skipped"]["skipped_files"]
]
) == {
f"variables/variables.data-00000-of-00001",
f"variables/variables.index",
"variables/variables.data-00000-of-00001",
"variables/variables.index",
}
assert results["errors"] == []

Expand Down Expand Up @@ -1526,18 +1526,18 @@ def test_scan_tensorflow(tensorflow_file_path: Any) -> None:

assert ms.issues.all_issues == expected
assert set(results["summary"]["scanned"]["scanned_files"]) == {
f"fingerprint.pb",
f"keras_metadata.pb",
f"saved_model.pb",
"fingerprint.pb",
"keras_metadata.pb",
"saved_model.pb",
}
assert set(
[
skipped_file["source"]
for skipped_file in results["summary"]["skipped"]["skipped_files"]
]
) == {
f"variables/variables.data-00000-of-00001",
f"variables/variables.index",
"variables/variables.data-00000-of-00001",
"variables/variables.index",
}
assert results["errors"] == []

Expand Down

0 comments on commit 0eeb158

Please sign in to comment.