|
|
|
|
|
|
|
|
|
|
|
import warnings
|
|
|
import sys
|
|
|
|
|
|
|
|
|
print("\n" + "="*70)
|
|
|
print("⚠️ WARNING: You are loading a model that declares it MAY execute remote code.")
|
|
|
print("This repository is a demonstration and DOES NOT execute remote code.")
|
|
|
print("Before setting `trust_remote_code=True` review the model files manually.")
|
|
|
print("="*70 + "\n")
|
|
|
|
|
|
|
|
|
warnings.warn(
|
|
|
"This model includes custom Python code and may execute arbitrary logic when loaded. "
|
|
|
"Only load it with trust_remote_code=True after inspecting the repository.",
|
|
|
UserWarning,
|
|
|
)
|
|
|
|
|
|
|
|
|
from transformers import PreTrainedModel, PretrainedConfig
|
|
|
|
|
|
class SimpleWarningConfig(PretrainedConfig):
|
|
|
model_type = "simple-warning-model"
|
|
|
|
|
|
def __init__(self, hidden_size=8, **kwargs):
|
|
|
super().__init__(**kwargs)
|
|
|
self.hidden_size = hidden_size
|
|
|
|
|
|
class SimpleModel(PreTrainedModel):
|
|
|
config_class = SimpleWarningConfig
|
|
|
base_model_prefix = "simple_model"
|
|
|
|
|
|
def __init__(self, config: SimpleWarningConfig):
|
|
|
super().__init__(config)
|
|
|
|
|
|
try:
|
|
|
import torch.nn as nn
|
|
|
self.dummy = nn.Linear(config.hidden_size, config.hidden_size)
|
|
|
except Exception:
|
|
|
|
|
|
self.dummy = None
|
|
|
|
|
|
def forward(self, *args, **kwargs):
|
|
|
|
|
|
try:
|
|
|
import torch
|
|
|
if self.dummy is None:
|
|
|
return torch.zeros(1, self.config.hidden_size)
|
|
|
return self.dummy(torch.zeros(1, self.config.hidden_size))
|
|
|
except Exception:
|
|
|
|
|
|
return [[0.0] * self.config.hidden_size]
|
|
|
|