Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Protectai Modelscan Settings Configuration

From Leeroopedia
Knowledge Sources
Domains ML_Security, Configuration
Last Updated 2026-02-14 12:00 GMT

Overview

Concrete tool for defining and managing scanning configuration provided by the modelscan settings module.

Description

The DEFAULT_SETTINGS dictionary and SettingsUtils class provide the configuration infrastructure for modelscan. DEFAULT_SETTINGS defines all scanner registrations, middleware pipelines, unsafe globals by severity, and reporting module configuration. SettingsUtils provides TOML export for creating persistent settings files. The SupportedModelFormats class enumerates known model file formats used by the middleware for format tagging.

Usage

Use this module when you need to:

  • Create a custom configuration for ModelScan (e.g., disable certain scanners)
  • Generate a default settings TOML file for editing
  • Understand or modify the unsafe globals severity mappings
  • Register a new scanner or middleware in the settings

Code Reference

Source Location

  • Repository: modelscan
  • File: modelscan/settings.py
  • Lines: L1-158

Signature

class Property:
    def __init__(self, name: str, value: Any) -> None:
        """Simple name-value pair used as enum-like identifiers."""

class SupportedModelFormats:
    TENSORFLOW = Property("TENSORFLOW", "tensorflow")
    KERAS_H5 = Property("KERAS_H5", "keras_h5")
    KERAS = Property("KERAS", "keras")
    NUMPY = Property("NUMPY", "numpy")
    PYTORCH = Property("PYTORCH", "pytorch")
    PICKLE = Property("PICKLE", "pickle")

DEFAULT_SETTINGS: Dict[str, Any] = {
    "modelscan_version": __version__,
    "supported_zip_extensions": [".zip", ".npz"],
    "scanners": { ... },      # Scanner class paths -> {enabled, supported_extensions}
    "middlewares": { ... },    # Middleware class paths -> config
    "unsafe_globals": { ... }, # Severity -> {module -> operators}
    "reporting": { ... },      # Reporting module and settings
}

class SettingsUtils:
    @staticmethod
    def get_default_settings_as_toml() -> str:
        """Export DEFAULT_SETTINGS as a TOML string with header comment."""

Import

from modelscan.settings import DEFAULT_SETTINGS, SettingsUtils, SupportedModelFormats

I/O Contract

Inputs

Name Type Required Description
DEFAULT_SETTINGS Dict[str, Any] Module-level constant. No input required; provides the default configuration.
TOML file str (file content) No Optional TOML file parsed via tomlkit.parse() to override DEFAULT_SETTINGS

Outputs

Name Type Description
DEFAULT_SETTINGS Dict[str, Any] Complete configuration dict with keys: scanners, middlewares, unsafe_globals, reporting, supported_zip_extensions, modelscan_version
get_default_settings_as_toml() str TOML-formatted string representation of DEFAULT_SETTINGS, prefixed with a header comment
SupportedModelFormats class Enum-like class with Property attributes: TENSORFLOW, KERAS_H5, KERAS, NUMPY, PYTORCH, PICKLE

Usage Examples

Use Default Settings

from modelscan.modelscan import ModelScan
from modelscan.settings import DEFAULT_SETTINGS

# ModelScan uses DEFAULT_SETTINGS by default
scanner = ModelScan()

# Equivalent to:
scanner = ModelScan(settings=DEFAULT_SETTINGS)

Custom Settings with Disabled Scanner

import copy
from modelscan.modelscan import ModelScan
from modelscan.settings import DEFAULT_SETTINGS

# Deep copy to avoid mutating the module-level default
custom_settings = copy.deepcopy(DEFAULT_SETTINGS)

# Disable TensorFlow scanners (not needed in PyTorch-only environment)
custom_settings["scanners"]["modelscan.scanners.SavedModelLambdaDetectScan"]["enabled"] = False
custom_settings["scanners"]["modelscan.scanners.SavedModelTensorflowOpScan"]["enabled"] = False

scanner = ModelScan(settings=custom_settings)
results = scanner.scan("/path/to/model.pt")

Generate Settings TOML File

from modelscan.settings import SettingsUtils

toml_content = SettingsUtils.get_default_settings_as_toml()

with open("modelscan-settings.toml", "w") as f:
    f.write(toml_content)

Load Settings from TOML

from tomlkit import parse
from modelscan.modelscan import ModelScan

with open("modelscan-settings.toml") as f:
    settings = parse(f.read()).unwrap()

scanner = ModelScan(settings=settings)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment