Advertisement
hot_laugh617

get-triggers-from-lora

May 18th, 2024
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | Source Code | 0 0
  1. # Original code by AKK from https://genai.stackexchange.com/questions/346/how-to-obtain-trigger-words-from-a-lora-file
  2. # Modified by hot_laugh617 of r/sultrysyn https://www.reddit.com/r/sultrysyn
  3. # Look I wrote a python program. *nudge nudge*, *wink wink* Get it?
  4.  
  5. # Created on: May 18, 2024
  6. # * Outputs straight JSON from the safetensor. Could be modified to print a nice table.
  7. # * You could also set an output file argument, but you can just redirect the output to a file if you want to.
  8.  
  9. import argparse
  10. import json
  11. import struct
  12.  
  13. def main():
  14.     print("\n")
  15.     argparser = argparse.ArgumentParser(description='Extract keywords or other metadata from a .safetensor file.', prog="Get Triggers")
  16.     argparser.add_argument("--input", "-i", action="store", type=str, help="File to get metadata from.")
  17.     argparser.add_argument("--triggers", action="store_true")
  18.     argparser.add_argument("--dump", "-d", action="store_true", help="Dump all metadata. [Default]")
  19.     args = argparser.parse_args()
  20.  
  21.     # Set a flag in case there is a failure later.
  22.     it_worked = True
  23.  
  24.     try:
  25.         with open(args.input, "rb") as f:
  26.             length_of_header = struct.unpack('<Q', f.read(8))[0]
  27.             header_data = f.read(length_of_header)
  28.             header = json.loads(header_data)
  29.            
  30.             # For those interested
  31.             type = header['__metadata__']['modelspec.architecture']
  32.             epochs = header['__metadata__']['ss_num_epochs']
  33.             tags = header['__metadata__']['ss_tag_frequency']
  34.     except:
  35.         print(f"\n[!] There was a problem opening the file '{args.input}'. Are you sure it exists? \nMay sure you use the proper file extension.")
  36.         print("\n[!] It seems to work for some LoRA and not others. I don't know why.\n")
  37.         it_worked = False
  38.         return -1
  39.  
  40.     if it_worked:
  41.         if args.dump:
  42.             # Header is all the metadata for the file.
  43.             print(header)
  44.         if args.triggers:
  45.             print(tags)
  46.         else:
  47.             # Pointless but can be fixed later
  48.             print(header)
  49.  
  50. if __name__ == "__main__":
  51.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement