Hashing comfyUI model names

import civitai.lib as civitai
from modules import script_callbacks, sd_vae, shared

additional_network_type_map = {
    'lora': 'LORA',
    'hypernet': 'Hypernetwork'
}

additional_network_pattern = r'<(lora|hypernet):([a-zA-Z0-9_\.\-\s]+):([0-9.]+)(?:[:].*)?>'
model_hash_pattern = r'Model hash: ([0-9a-fA-F]{10})'
def add_resource_hashes(params):
    if 'parameters' not in params.pnginfo: return

    hashify_resources = shared.opts.data.get('civitai_hashify_resources', True)
    if not hashify_resources: return

    lines = params.pnginfo['parameters'].split('\n')
    generation_params = lines.pop()
    prompt_parts = '\n'.join(lines).split('Negative prompt:')
    prompt, negative_prompt = [s.strip() for s in prompt_parts[:2] + ['']*(2-len(prompt_parts))]

    hashed_prompt = prompt
    hashed_negative_prompt = negative_prompt

    resources = civitai.load_resource_list([])
    resource_hashes = {}

    # Hash the VAE
    if hashify_resources and sd_vae.loaded_vae_file is not None:
        vae_name = os.path.splitext(sd_vae.get_filename(sd_vae.loaded_vae_file))[0]
        vae_matches = [r for r in resources if r['type'] == 'VAE' and r['name'] == vae_name]
        if len(vae_matches) > 0:
            short_hash = vae_matches[0]['hash'][:10]
            resource_hashes['vae'] = short_hash

how about just use the cpkt name literal or stringify the json output of ComfyUI png metadata?

imports/variables
...
ckpt_name_pattern = r'"ckpt_name": "([^"]+)"'

def add_resource_hashes(params):
    if 'parameters' not in params.pnginfo: return

    try:
        generation_info = json.loads(params.pnginfo['parameters'])
    except json.JSONDecodeError:
        return  # Handle non-JSON formatted parameters as necessary

    resources = civitai.load_resource_list([])
    resource_hashes = {}

    # Find ckpt_name in the generation info
    ckpt_names = re.findall(ckpt_name_pattern, json.dumps(generation_info))
    for ckpt_name in ckpt_names:
        # Look for a resource with a matching ckpt_name
        matching_resource = [r for r in resources if r['type'] == 'Checkpoint' and ckpt_name == r['name']]
        if matching_resource:
            # Instead of using a hash, we directly use the name or another identifier
            resource_identifier = matching_resource[0]['identifier']  # Assuming each resource has a unique identifier
            resource_hashes['model'] = resource_identifier

    if resource_hashes:
        # Append found identifiers to the parameters
        params.pnginfo['parameters'] += f", Identifiers: {json.dumps(resource_hashes)}"

Please authenticate to join the conversation.

Upvoters
Status

Awaiting Dev Review

Board

πŸ’‘ Feature Request

Date

About 2 years ago

Author

friedtofuu

Subscribe to post

Get notified by email when there are changes.