feat: enhance Civitai resource extraction in StandardMetadataParser for improved JSON handling. Fixes https://github.com/willmiao/ComfyUI-Lora-Manager/issues/141

This commit is contained in:
Will Miao
2025-04-26 22:12:40 +08:00
parent c551f5c23b
commit 0e7ba27a7d

View File

@@ -403,27 +403,43 @@ class StandardMetadataParser(RecipeMetadataParser):
# Extract Civitai resources # Extract Civitai resources
if 'Civitai resources:' in user_comment: if 'Civitai resources:' in user_comment:
resources_part = user_comment.split('Civitai resources:', 1)[1] resources_part = user_comment.split('Civitai resources:', 1)[1].strip()
if '],' in resources_part:
resources_json = resources_part.split('],', 1)[0] + ']' # Look for the opening and closing brackets to extract the JSON array
try: if resources_part.startswith('['):
resources = json.loads(resources_json) # Find the position of the closing bracket
# Filter loras and checkpoints bracket_count = 0
for resource in resources: end_pos = -1
if resource.get('type') == 'lora':
# 确保 weight 字段被正确保留 for i, char in enumerate(resources_part):
lora_entry = resource.copy() if char == '[':
# 如果找不到 weight默认为 1.0 bracket_count += 1
if 'weight' not in lora_entry: elif char == ']':
lora_entry['weight'] = 1.0 bracket_count -= 1
# Ensure modelVersionName is included if bracket_count == 0:
if 'modelVersionName' not in lora_entry: end_pos = i
lora_entry['modelVersionName'] = '' break
metadata['loras'].append(lora_entry)
elif resource.get('type') == 'checkpoint': if end_pos != -1:
metadata['checkpoint'] = resource resources_json = resources_part[:end_pos+1]
except json.JSONDecodeError: try:
pass resources = json.loads(resources_json)
# Filter loras and checkpoints
for resource in resources:
if resource.get('type') == 'lora':
# 确保 weight 字段被正确保留
lora_entry = resource.copy()
# 如果找不到 weight默认为 1.0
if 'weight' not in lora_entry:
lora_entry['weight'] = 1.0
# Ensure modelVersionName is included
if 'modelVersionName' not in lora_entry:
lora_entry['modelVersionName'] = ''
metadata['loras'].append(lora_entry)
elif resource.get('type') == 'checkpoint':
metadata['checkpoint'] = resource
except json.JSONDecodeError:
pass
return metadata return metadata
except Exception as e: except Exception as e: