Emulates what huggingface transformers does to load remote code with trust_remote_code=True,
but allows us to use the class directly so that we don't have to load untrusted code.
Source code in inference/models/florence2/utils.py
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 | def import_class_from_file(file_path, class_name, alias_name=None):
"""
Emulates what huggingface transformers does to load remote code with trust_remote_code=True,
but allows us to use the class directly so that we don't have to load untrusted code.
"""
file_path = os.path.abspath(file_path)
module_name = os.path.splitext(os.path.basename(file_path))[0]
module_dir = os.path.dirname(file_path)
parent_dir = os.path.dirname(module_dir)
sys.path.insert(0, parent_dir)
previous_module = sys.modules.get(module_name)
injected = False
try:
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
injected = True
# Manually set the __package__ attribute to the parent package
module.__package__ = os.path.basename(module_dir)
spec.loader.exec_module(module)
cls = getattr(module, class_name)
if alias_name:
globals()[alias_name] = cls
return cls
except Exception:
if injected:
if previous_module is not None:
sys.modules[module_name] = previous_module
else:
sys.modules.pop(module_name, None)
raise
finally:
sys.path.pop(0)
|