Skip to content

Commit 7019753

Browse files
Fix adaseq trust_remote_code (#1721)
1 parent 57961c1 commit 7019753

11 files changed

Lines changed: 31 additions & 38 deletions

File tree

modelscope/models/base/base_model.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from modelscope.utils.device import verify_device
1616
from modelscope.utils.logger import get_logger
1717
from modelscope.utils.plugins import (filter_plugin_in_whitelist,
18-
is_trusted_group, register_modelhub_repo,
18+
register_modelhub_repo,
1919
register_plugins_repo)
2020

2121
logger = get_logger()
@@ -32,7 +32,9 @@ def __init__(self, model_dir, *args, **kwargs):
3232
device_name = kwargs.get('device', 'gpu')
3333
verify_device(device_name)
3434
self._device_name = device_name
35-
self.trust_remote_code = kwargs.get('trust_remote_code', False)
35+
self.trust_remote_code = kwargs.get(
36+
'trust_remote_code',
37+
False) or check_model_from_owner_group(model_dir)
3638

3739
def __call__(self, *args, **kwargs) -> Dict[str, Any]:
3840
return self.postprocess(self.forward(*args, **kwargs))
@@ -137,8 +139,8 @@ def from_pretrained(cls,
137139
kwargs.pop(Invoke.KEY)
138140
else:
139141
invoked_by = Invoke.PRETRAINED
140-
trust_remote_code = trust_remote_code or is_trusted_group(
141-
model_name_or_path)
142+
_model_trusted = check_model_from_owner_group(model_name_or_path)
143+
trust_remote_code = trust_remote_code or _model_trusted
142144
ignore_file_pattern = kwargs.pop('ignore_file_pattern', None)
143145
if osp.exists(model_name_or_path):
144146
local_model_dir = model_name_or_path
@@ -205,7 +207,7 @@ def from_pretrained(cls,
205207
'Please make sure that you can trust the external codes.')
206208
register_modelhub_repo(local_model_dir, allow_remote=trust_remote_code)
207209
default_args = {}
208-
if trust_remote_code:
210+
if trust_remote_code and not _model_trusted:
209211
default_args = {'trust_remote_code': trust_remote_code}
210212
register_plugins_repo(plugins)
211213
for k, v in kwargs.items():

modelscope/models/cv/anydoor/anydoor_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def __init__(self, control_stage_config, control_key, only_mid_control,
343343
self.only_mid_control = only_mid_control
344344
self.control_scales = [1.0] * 13
345345
self.trust_remote_code = kwargs.get('trust_remote_code', False)
346-
self.check_trust_remote_code()
346+
self.check_trust_remote_code(self.model_dir)
347347

348348
@torch.no_grad()
349349
def get_input(self, batch, k, bs=None, *args, **kwargs):

modelscope/models/cv/image_view_transform/image_view_transform_infer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self, model_dir, device='cpu', *args, **kwargs):
6868
self.model = None
6969
self.model = load_model_from_config(
7070
self.model, config, ckpt, device=self.device)
71-
self.check_trust_remote_code()
71+
self.check_trust_remote_code(model_dir=model_dir)
7272

7373
def forward(self, model_path, x, y):
7474
pred_results = _infer(self.model, model_path, x, y, self.device)

modelscope/models/cv/tinynas_detection/detector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, model_dir: str, *args, **kwargs):
2929
init model by cfg
3030
"""
3131
super().__init__(model_dir, *args, **kwargs)
32-
self.check_trust_remote_code()
32+
self.check_trust_remote_code(model_dir=model_dir)
3333
config_path = osp.join(model_dir, self.config_name)
3434
config = parse_config(config_path)
3535
self.cfg = config

modelscope/models/cv/video_depth_estimation/dro_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DROEstimation(TorchModel):
3333
def __init__(self, model_dir: str, **kwargs):
3434
"""str -- model file root."""
3535
super().__init__(model_dir, **kwargs)
36-
self.check_trust_remote_code()
36+
self.check_trust_remote_code(model_dir=model_dir)
3737

3838
model_path = osp.join(model_dir, ModelFile.TORCH_MODEL_FILE)
3939

modelscope/pipelines/audio/linear_aec_pipeline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def __init__(self, model, **kwargs):
7070
self.check_trust_remote_code(
7171
'This pipeline requires `trust_remote_code=True` to load the module defined'
7272
' in the `dey_mini.yaml`, setting this to True means you trust the code and files'
73-
' listed in this model repo.')
73+
' listed in this model repo.',
74+
model_dir=model)
7475

7576
self.use_cuda = torch.cuda.is_available()
7677
with open(

modelscope/pipelines/builder.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
from modelscope.hub.snapshot_download import snapshot_download
77
from modelscope.metainfo import DEFAULT_MODEL_FOR_PIPELINE
88
from modelscope.models.base import Model
9+
from modelscope.utils.automodel_utils import check_model_from_owner_group
910
from modelscope.utils.config import ConfigDict, check_config
1011
from modelscope.utils.constant import (DEFAULT_MODEL_REVISION, Invoke, Tasks,
1112
ThirdParty)
1213
from modelscope.utils.hub import read_config
1314
from modelscope.utils.import_utils import is_transformers_available
1415
from modelscope.utils.logger import get_logger
1516
from modelscope.utils.plugins import (filter_plugin_in_whitelist,
16-
is_trusted_group, register_modelhub_repo,
17+
register_modelhub_repo,
1718
register_plugins_repo)
1819
from modelscope.utils.registry import Registry, build_from_cfg
1920
from modelscope.utils.task_utils import is_embedding_task
@@ -119,7 +120,8 @@ def pipeline(task: str = None,
119120

120121
model_id = model[0] if isinstance(model,
121122
list) and len(model) > 0 else model
122-
trust_remote_code = trust_remote_code or is_trusted_group(model_id)
123+
_model_trusted = check_model_from_owner_group(model_id)
124+
trust_remote_code = trust_remote_code or _model_trusted
123125
pipeline_props = None
124126
if pipeline_name is None:
125127
# get default pipeline for this task
@@ -248,10 +250,13 @@ def pipeline(task: str = None,
248250
if preprocessor is not None:
249251
cfg.preprocessor = preprocessor
250252

251-
return build_pipeline(
252-
cfg,
253-
task_name=task,
254-
default_args={'trust_remote_code': trust_remote_code})
253+
if _model_trusted:
254+
return build_pipeline(cfg, task_name=task)
255+
else:
256+
return build_pipeline(
257+
cfg,
258+
task_name=task,
259+
default_args={'trust_remote_code': trust_remote_code})
255260

256261

257262
def add_default_pipeline_info(task: str,

modelscope/pipelines/multi_modal/disco_guided_diffusion_pipeline/disco_guided_diffusion.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ def __init__(self, model: str, device: str = 'gpu', **kwargs):
193193
self.check_trust_remote_code(
194194
'This pipeline requires `trust_remote_code=True` to load the module defined'
195195
' in `model_index.json`, setting this to True means you trust the code and files'
196-
' listed in this model repo.')
196+
' listed in this model repo.',
197+
model_dir=model)
197198

198199
model_path = model
199200

@@ -209,12 +210,6 @@ def __init__(self, model: str, device: str = 'gpu', **kwargs):
209210
if model_config['use_fp16']:
210211
self.unet.convert_to_fp16()
211212

212-
self.trust_remote_code = kwargs.get('trust_remote_code', False)
213-
self.check_trust_remote_code(
214-
'This pipeline requires import modules listed in `model_index.json`, '
215-
'please add `trust_remote_code=True` if you trust this model repo.'
216-
)
217-
218213
with open(
219214
os.path.join(model_path, 'model_index.json'),
220215
'r',

modelscope/trainers/builder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from modelscope.metainfo import Trainers
33
from modelscope.pipelines.builder import normalize_model_input
44
from modelscope.pipelines.util import is_official_hub_path
5+
from modelscope.utils.automodel_utils import check_model_from_owner_group
56
from modelscope.utils.constant import DEFAULT_MODEL_REVISION
67
from modelscope.utils.hub import read_config
78
from modelscope.utils.plugins import (filter_plugin_in_whitelist,
8-
is_trusted_group, register_modelhub_repo,
9+
register_modelhub_repo,
910
register_plugins_repo)
1011
from modelscope.utils.registry import Registry, build_from_cfg
1112

@@ -29,8 +30,8 @@ def build_trainer(name: str = Trainers.default, default_args: dict = None):
2930
model_revision = default_args.get('model_revision', DEFAULT_MODEL_REVISION)
3031
model_id = model[0] if isinstance(model,
3132
list) and len(model) > 0 else model
32-
trust_remote_code = default_args.get('trust_remote_code',
33-
False) or is_trusted_group(model_id)
33+
trust_remote_code = default_args.get(
34+
'trust_remote_code', False) or check_model_from_owner_group(model_id)
3435

3536
if isinstance(model, str) \
3637
or (isinstance(model, list) and isinstance(model[0], str)):

modelscope/utils/automodel_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def check_model_from_owner_group(model_dir: str,
137137
Returns:
138138
bool: Whether the group can be trusted
139139
"""
140-
if not model_dir:
140+
if not model_dir or not isinstance(model_dir, str):
141141
return False
142142
if owner_group is None:
143143
owner_group = ['iic', 'damo']

0 commit comments

Comments
 (0)