rockerBOO / lora-inspector

LoRA (Low-Rank Adaptation) inspector for Stable Diffusion
MIT License
80 stars 4 forks source link

[Feature request] Save metadata back to LoRA #5

Closed olamedia closed 1 year ago

olamedia commented 1 year ago

Add the ability to save modified metadata back

rockerBOO commented 1 year ago

Thanks for the request! Hmm moving from the JSON file into the metadata? I would imagine it would be outside the scope of this tool (as to just inspect) but I could put together a script. But not sure a reason to do this. What were you thinking?

Thanks!

olamedia commented 1 year ago

There can be few reasons - to fix wrong metadata, to strip training data, to cleanup etc, to add own private-use fields

rockerBOO commented 1 year ago

I think modifying the file would be outside the scope of this project currently. Additionally, the various ways to handle the input could be a lot to process.

But here is some code that may work, I haven't tested, so use it on a copy of a file you may have. Can then add new items to the python dict and then save it to a new safetensors file.

from safetensors import load_file, save_file

file = "x.safetensors"
with load_file(file) as f:
  metadata = f.metadata()
  metadata['new-key'] = "my value"
  save_file(dict((key, f.get_tensor(key)) for key in f.keys()), file, metadata)
rockerBOO commented 1 year ago

I added a script to do this as I had many additional people, as well as myself, needing it. a1111 will infer ss_output_name in some settings it seems, so being able to set that can help.

https://github.com/rockerBOO/lora-inspector#update-metadata