Skip to content

Commit b9498fc

Browse files
committed
Enable torch.load weights_only=True
Change torch.load to use the safer option of only loading weights. The torch.load function is hard coded to use the default value within the open_clip package. The code will set TORCH_FORCE_WEIGHTS_ONLY_LOAD environment variable to 'true' if not already set to override the default. Fixes #32
1 parent 3f65a47 commit b9498fc

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/bioclip/predict.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import json
23
import torch
34
from torchvision import transforms
@@ -19,6 +20,13 @@
1920
PRED_CLASSICATION_KEY = "classification"
2021
PRED_SCORE_KEY = "score"
2122

23+
24+
# Use secure model loading unless explicitly disabled.
25+
# See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more.
26+
if not "TORCH_FORCE_WEIGHTS_ONLY_LOAD" in os.environ:
27+
os.environ["TORCH_FORCE_WEIGHTS_ONLY_LOAD"] = "true"
28+
29+
2230
OPENA_AI_IMAGENET_TEMPLATE = [
2331
lambda c: f"a bad photo of a {c}.",
2432
lambda c: f"a photo of many {c}.",

0 commit comments

Comments
 (0)