tal11 commited on
Commit
d6e983a
·
verified ·
1 Parent(s): 21c40bb

Update modeling.py

Browse files
Files changed (1) hide show
  1. modeling.py +18 -54
modeling.py CHANGED
@@ -1,54 +1,18 @@
1
- # modeling.py
2
- # Safe demonstration: prints/warns when executed, but DOES NOT fetch or execute remote code.
3
-
4
- import warnings
5
- import sys
6
-
7
- # 1) Immediate visible print
8
- print("\n" + "="*70)
9
- print("⚠️ WARNING: You are loading a model that declares it MAY execute remote code.")
10
- print("This repository is a demonstration and DOES NOT execute remote code.")
11
- print("Before setting `trust_remote_code=True` review the model files manually.")
12
- print("="*70 + "\n")
13
-
14
- # 2) Also raise a Python warning (visible in many environments)
15
- warnings.warn(
16
- "This model includes custom Python code and may execute arbitrary logic when loaded. "
17
- "Only load it with trust_remote_code=True after inspecting the repository.",
18
- UserWarning,
19
- )
20
-
21
- # 3) Minimal HF-compatible model implementation (harmless).
22
- from transformers import PreTrainedModel, PretrainedConfig
23
-
24
- class SimpleWarningConfig(PretrainedConfig):
25
- model_type = "simple-warning-model"
26
-
27
- def __init__(self, hidden_size=8, **kwargs):
28
- super().__init__(**kwargs)
29
- self.hidden_size = hidden_size
30
-
31
- class SimpleModel(PreTrainedModel):
32
- config_class = SimpleWarningConfig
33
- base_model_prefix = "simple_model"
34
-
35
- def __init__(self, config: SimpleWarningConfig):
36
- super().__init__(config)
37
- # keep internals minimal and harmless
38
- try:
39
- import torch.nn as nn
40
- self.dummy = nn.Linear(config.hidden_size, config.hidden_size)
41
- except Exception:
42
- # if torch not available, we still want the module importable
43
- self.dummy = None
44
-
45
- def forward(self, *args, **kwargs):
46
- # harmless placeholder forward
47
- try:
48
- import torch
49
- if self.dummy is None:
50
- return torch.zeros(1, self.config.hidden_size)
51
- return self.dummy(torch.zeros(1, self.config.hidden_size))
52
- except Exception:
53
- # if torch is missing, return a plain Python fallback
54
- return [[0.0] * self.config.hidden_size]
 
1
+ # modeling.py
2
+ from transformers import PreTrainedModel
3
+ import torch.nn as nn
4
+
5
+ class SimpleWarningConfig:
6
+ def __init__(self, hidden_size=8):
7
+ self.hidden_size = hidden_size
8
+
9
+ class SimpleModel(PreTrainedModel):
10
+ config_class = SimpleWarningConfig
11
+
12
+ def __init__(self, config):
13
+ super().__init__(config)
14
+ print("⚠️ WARNING: You are loading a model with trust_remote_code=True! Be careful with remote code execution!")
15
+ self.linear = nn.Linear(config.hidden_size, config.hidden_size)
16
+
17
+ def forward(self, x):
18
+ return self.linear(x)