Skip to content

FNGarvin/sai_model_spec_tools

Repository files navigation

Model Metadata Tools (Ruby & Python)

These scripts are basic sketches designed to facilitate the management and analysis of metadata within Stable Diffusion model files, particularly those using the .safetensors format. They provide a simple, command-line approach for both adding information to a model and extracting diagnostic details from it.

1. Metadata Injection (add_metadata.rb and add_metadata-lora.rb)

These Ruby scripts are designed to take user-defined metadata and inject it into an existing .safetensors model file.

  • add_metadata.rb: This script is a basic version that directly uses hardcoded metadata, which is useful for setting a standard metadata block across multiple models.
  • add_metadata-lora.rb: This script is a more flexible, generalized tool that reads its configuration (including the target model path and all desired metadata) from a JSON file. This allows for easy batch processing and configuration management. It also handles the conversion of non-standard keys (like the local thumbnail_filename or a simple trigger_words array) into standardized metadata keys (like modelspec.thumbnail and ss_activation_tags).

Key Features:

  • Backup and Safety: The original model file is always moved to a relative backup directory (../pre-metadata-update) before modification, ensuring data integrity.
  • Metadata Standardization: It supports and promotes the use of standardized metadata formats, primarily adhering to the SAI Model Specification (using modelspec.* keys) and widely-used keys from the Kohya-SS training environment (using ss_* keys).

2. Metadata and Key Analysis (query-lora-metadata.py)

This Python script is a diagnostic tool designed to analyze a .safetensors file and infer key characteristics of the embedded LoRA, offering valuable insights that may be missing or contradictory in the metadata.

Key Features:

  • Metadata Dump: It prints the entire raw metadata dictionary found in the file.
  • Architectural Inference: It analyzes the tensor keys (the names of the weights inside the file) to determine the likely base architecture (e.g., SD 1.5, SDXL, FLUX, WAN). This relies on highly specific key prefixes that act as architectural fingerprints.
  • Contradiction Warning: It compares the base model inferred from the metadata (e.g., "Pony Diffusion") with the base model inferred from the tensor keys (e.g., "SDXL"). If these differ, it flags a CONTRADICTION DETECTED warning. This conservative diagnostic is crucial for alerting the user to potential interoperability risks between a finetuned model and a generic LoRA.
  • Trigger Word Heuristic: It attempts to infer potential trigger words either from the explicit ss_activation_tags key or via a heuristic based on the most frequent tags found in the training data metadata (ss_tag_frequency).

About

A collection of scripts to query and manipulate metadata embedded into safetensor files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published