MCPcopy Create free account
hub / github.com/modelscope/modelscope / try_import_from_hf

Function try_import_from_hf

modelscope/__init__.py:121–146  ·  view source on GitHub ↗
(name)

Source from the content-addressed store, hash-verified

119 extra_objects[_import] = getattr(hf_util, _import)
120
121 def try_import_from_hf(name):
122 hf_pkgs = ['transformers', 'peft', 'diffusers', 'kernels']
123 module = None
124 matched_pkg = None
125 for pkg in hf_pkgs:
126 try:
127 module = getattr(importlib.import_module(pkg), name)
128 matched_pkg = pkg
129 break
130 except Exception: # noqa
131 pass
132
133 if module is None:
134 raise AttributeError(
135 f'Cannot import available module of {name} in modelscope,'
136 f' or related packages({hf_pkgs})')
137
138 if matched_pkg == 'kernels':
139 if callable(module):
140 from modelscope.utils.hf_util.patcher import \
141 _wrap_kernels_callable
142 return _wrap_kernels_callable(name)
143 return module
144
145 module = _patch_pretrained_class([module], wrap=True)
146 return module[0]
147
148 import sys
149

Callers

nothing calls this directly

Calls 3

_wrap_kernels_callableFunction · 0.90
_patch_pretrained_classFunction · 0.90
import_moduleMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…