9

LoRAs are smaller models that can be used to add new concepts such as styles or objects to an existing stable diffusion model. These LoRAs often have specific trigger words that need to be added to the prompt to make them work.
The trigger words are commonly found on platforms like Civitai.com alongside the respective LoRA, however, such information might get lost (e.g. if the model is deleted from the site).
Is there a method to directly retrieve these trigger words from the LoRA files (usually in .pt or .safetensor format) themselves?

3 Answers 3

1

idk if there is an easier way, but I just did the following: I used https://github.com/butaixianran/Stable-Diffusion-Webui-Civitai-Helper to write .info files and made a simple script to extract the file name, recommended weight, and trained words in a format like so:

<lora:{model_name}:{weight}>,{trained_words}

feel free to modify it as you'd like.

the code:

import json
import os

def load_config():
    # Load configuration from config.json file
    with open("config.json", "r") as f:
        config = json.load(f)
    return config

def extract_data(file_path, default_weight):
    # Extract data from a JSON file located at file_path
    with open(file_path, "r", encoding="utf-8") as f:
        data = json.load(f)
    trained_words = ",".join(data.get("trainedWords", []))
    weight = default_weight
    images = data.get("images", [])
    for image in images:
        meta = image.get("meta", {})
        if meta:
            resources = meta.get("resources", [])
            for resource in resources:
                if resource.get("type") == "lora":
                    weight = resource.get("weight", default_weight)
                    return trained_words, weight  # Exit early if weight is found
    return trained_words, weight

def main():
    config = load_config()
    input_path = config["input_path"]
    output_path = config["output_path"]
    output_format = config["output_format"]
    default_weight = config["default_weight"]
    with open(output_path, "w", encoding="utf-8") as f:
        if output_format == "json":
            f.write("[")  # Start JSON array
        file_names = (file_name for file_name in os.listdir(input_path) if file_name.endswith(".civitai.info"))
        for i, file_name in enumerate(file_names):
            file_path = os.path.join(input_path, file_name)
            trained_words, weight = extract_data(file_path, default_weight)
            file_name_without_ext = os.path.splitext(file_name)[0]
            model_name = file_name_without_ext.replace(".civitai", "")
            result = f"<lora:{model_name}:{weight}>,{trained_words}"
            if output_format == "json":
                json.dump(result, f, ensure_ascii=False)
                if i != len(file_names) - 1:
                    f.write(",")
            else:
                f.write(result)
                f.write("\n")
        if output_format == "json":
            f.write("]")  # End JSON array

if __name__ == "__main__":
    main()

config file should contain, if unmodified:

{
    "input_path": "Path/to/InFolder",
    "output_path": "Path/to/OutFolder/Output.txt",
    "output_format": "txt",
    "default_weight": 0.6
}
2
  • 1
    This depends on the LoRA being available on Civitai.com, where it can be deleted at any time. I want to know if it is possible to obtain trigger words directly from the LoRA without using external resources.
    – Turamarth
    Sep 22, 2023 at 17:01
  • ah, missed the point entirely. seems like github.com/by321/safetensors_util has what you need. never once messed with safetensors, but this can print out the metadata of the file, which contains the words used for training the model. the shortcomings' that it prints every word used for training, not just 1 word. It's also printing the entirety of the metadata, which in our case, is largely irrelevant. you'll need to modify the code so it can: 1. only prints/outputs the most used words/tags. 2. process all files in a directory. currently, it's one command per file.
    – iiOxygen
    Sep 22, 2023 at 19:46
0

I was searching for a simple solution to the same problem.

I finally created a bash pipeline which does exactly what I want: Listing all keywords by weight.

Here it is:

tail -c +9 your_lora.swafetensors | jq -r .__metadata__.ss_tag_frequency 2>/dev/null | jq -rM .| grep ': [1-9]' | awk -F ':' '{print $2, $1}' | sort -nr

Breakdown:

tail -c +9 your_lora.swafetensors - metadata dictionary starts at character 9

jq -r .__metadata__.ss_tag_frequency 2>/dev/null - get keyword dictionary, ignore errors

jq -rM - print each keyword in one line

grep ': [1-9]' - filter out unnecessary stuff

awk -F ':' '{print $2, $1}' - switch keyword and weight to facilitate sorting

sort -nr - sort numerically descending

Example:

tail -c +9 /data/bernd/StableDiffusion/models/Lora/landscape/waves_v3.safetensors | jq -r .__metadata__.ss_tag_frequency | jq -rM .| grep ': [1-9]' | awk -F ':' '{print $2, $1}' | sort -nr

Output (shortened):

 63,     "lighthouse"
 49,     " stormy"
 49,     " storm"
 49,     " seas"
 49,     " ocean"
 38     "ships"
 15,     " calm sea"
 15,     " calm ocean"
 8,     " tidal wave"
0

LoRA model files actually contain a meta data part, which includes training info like epoch/learning rate/batch size/dataset size, and many others, also includes the tags used in the training dataset, from which you can identify the trigger word (the most used tag):

You can view or edit that meta data using this tool: Lora info editor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.