test / modeling.py
tal11's picture
Upload 2 files
9ffecf6 verified
raw
history blame
2.05 kB
# modeling.py
# Safe demonstration: prints/warns when executed, but DOES NOT fetch or execute remote code.
import warnings
import sys
# 1) Immediate visible print
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")
# 2) Also raise a Python warning (visible in many environments)
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,
)
# 3) Minimal HF-compatible model implementation (harmless).
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)
# keep internals minimal and harmless
try:
import torch.nn as nn
self.dummy = nn.Linear(config.hidden_size, config.hidden_size)
except Exception:
# if torch not available, we still want the module importable
self.dummy = None
def forward(self, *args, **kwargs):
# harmless placeholder forward
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:
# if torch is missing, return a plain Python fallback
return [[0.0] * self.config.hidden_size]