Skip to content

Instantly share code, notes, and snippets.

@catboxanon
Last active November 20, 2023 21:11
Show Gist options
  • Save catboxanon/4f56fdd1dd0ab207c6231eef3006c012 to your computer and use it in GitHub Desktop.
Save catboxanon/4f56fdd1dd0ab207c6231eef3006c012 to your computer and use it in GitHub Desktop.
import argparse
from pathlib import Path
from glob import glob
from contextlib import redirect_stdout
from io import StringIO
from tqdm import tqdm
import modules.hashes
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path', type=str, required=True)
parser.add_argument('-t', '--type', type=str, required=True, choices=['checkpoint', 'hypernet', 'textual_inversion', 'lora'])
args = parser.parse_args()
class NullIO(StringIO):
def write(self, txt):
pass
def silent(fn):
def silent_fn(*args, **kwargs):
with redirect_stdout(NullIO()):
return fn(*args, **kwargs)
return silent_fn
silent_sha256 = silent(modules.hashes.sha256)
if __name__ == '__main__':
for file in tqdm([Path(f) for f in list(glob(str(Path(args.path)) + '/**/*', recursive=True)) if Path(f).is_file() and Path(f).suffix in ['.ckpt', '.safetensors', '.pt']]):
if (
(args.type == 'checkpoint' and file.suffix in ['.ckpt', '.safetensors'])
or (args.type == 'hypernet' and file.suffix in ['.pt'])
or (args.type == 'textual_inversion' and file.suffix in ['.pt', '.png'])
or (args.type == 'lora' and file.suffix in ['.safetensors'])
):
title = f'{args.type}/{file.stem if args.type not in ["checkpoint"] else file.name}'
silent_sha256(str(file), title, use_addnet_hash=True if args.type in ['lora'] else False)
@catboxanon
Copy link
Author

catboxanon commented Feb 24, 2023

Note: Requires the IGNORE_CMD_ARGS_ERRORS env var to be set to any value (i.e. 1 or True). I would also recommend running this in a fresh terminal session so the environment isn't polluted by webui.bat env vars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment