Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract trigger words from safetensors file

With many .safetensors files associated to different LoRA authors sometimes specify "trigger words".
I’ve been trying to extract find the way to extract them from .safetensors file but I cannot find them. They are not present in metadata section and according to documentation I cannot think of other place where those can be.

On the other hand, I know that those trigger words are working, because I am getting image generated when I’m providing them.

I thought that something like this will work:
from safetensors import safe_open

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

tensors = {}
with safe_open("lora.safetensors", framework="pt") as f:
   print(f.metadata())

But it seems the access is more complicated. Any suggestion?

>Solution :

Adapting from https://huggingface.co/docs/safetensors/metadata_parsing#python:

import json
import struct

with open("lora.safetensors", "rb") as f:
    length_of_header = struct.unpack('<Q', f.read(8))[0]
    header_data = f.read(length_of_header)
    header = json.loads(header_data)

print(header)  # should be a dict that contains what you need
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading