mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-21 21:22:11 -03:00
feat(metadata): add source tracking for SQLite metadata and implement Civitai API metadata validation
This commit is contained in:
@@ -127,7 +127,8 @@ class SQLiteModelMetadataProvider(ModelMetadataProvider):
|
|||||||
'model': {
|
'model': {
|
||||||
'name': model_row['name'],
|
'name': model_row['name'],
|
||||||
'type': model_type,
|
'type': model_type,
|
||||||
}
|
},
|
||||||
|
'source': 'archive_db'
|
||||||
}
|
}
|
||||||
# Update with any additional data
|
# Update with any additional data
|
||||||
version_entry.update(version_data)
|
version_entry.update(version_data)
|
||||||
@@ -266,7 +267,8 @@ class SQLiteModelMetadataProvider(ModelMetadataProvider):
|
|||||||
"creator": {
|
"creator": {
|
||||||
"username": model_row['username'] or model_data.get("creator", {}).get("username"),
|
"username": model_row['username'] or model_data.get("creator", {}).get("username"),
|
||||||
"image": model_data.get("creator", {}).get("image")
|
"image": model_data.get("creator", {}).get("image")
|
||||||
}
|
},
|
||||||
|
"source": "archive_db"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add any additional fields from version data
|
# Add any additional fields from version data
|
||||||
|
|||||||
@@ -37,6 +37,18 @@ class ModelRouteUtils:
|
|||||||
local_metadata['from_civitai'] = False
|
local_metadata['from_civitai'] = False
|
||||||
await MetadataManager.save_metadata(metadata_path, local_metadata)
|
await MetadataManager.save_metadata(metadata_path, local_metadata)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_civitai_api_metadata(meta: dict) -> bool:
|
||||||
|
"""
|
||||||
|
Determine if the given civitai metadata is from the civitai API.
|
||||||
|
Returns True if both 'files' and 'images' exist and are non-empty.
|
||||||
|
"""
|
||||||
|
if not isinstance(meta, dict):
|
||||||
|
return False
|
||||||
|
files = meta.get('files')
|
||||||
|
images = meta.get('images')
|
||||||
|
return bool(files) and bool(images)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def update_model_metadata(metadata_path: str, local_metadata: Dict,
|
async def update_model_metadata(metadata_path: str, local_metadata: Dict,
|
||||||
civitai_metadata: Dict, metadata_provider=None) -> None:
|
civitai_metadata: Dict, metadata_provider=None) -> None:
|
||||||
@@ -44,21 +56,25 @@ class ModelRouteUtils:
|
|||||||
# Save existing trainedWords and customImages if they exist
|
# Save existing trainedWords and customImages if they exist
|
||||||
existing_civitai = local_metadata.get('civitai') or {} # Use empty dict if None
|
existing_civitai = local_metadata.get('civitai') or {} # Use empty dict if None
|
||||||
|
|
||||||
# Create a new civitai metadata by updating existing with new
|
# Check if we should skip the update to avoid overwriting richer data
|
||||||
merged_civitai = existing_civitai.copy()
|
if civitai_metadata.get('source') == 'archive_db' and ModelRouteUtils.is_civitai_api_metadata(existing_civitai):
|
||||||
merged_civitai.update(civitai_metadata)
|
logger.info(f"Skip civitai update for {local_metadata.get('model_name', '')}: {existing_civitai.get('name', '')}")
|
||||||
|
else:
|
||||||
|
# Create a new civitai metadata by updating existing with new
|
||||||
|
merged_civitai = existing_civitai.copy()
|
||||||
|
merged_civitai.update(civitai_metadata)
|
||||||
|
|
||||||
# Special handling for trainedWords - ensure we don't lose any existing trained words
|
# Special handling for trainedWords - ensure we don't lose any existing trained words
|
||||||
if 'trainedWords' in existing_civitai:
|
if 'trainedWords' in existing_civitai:
|
||||||
existing_trained_words = existing_civitai.get('trainedWords', [])
|
existing_trained_words = existing_civitai.get('trainedWords', [])
|
||||||
new_trained_words = civitai_metadata.get('trainedWords', [])
|
new_trained_words = civitai_metadata.get('trainedWords', [])
|
||||||
# Use a set to combine words without duplicates, then convert back to list
|
# Use a set to combine words without duplicates, then convert back to list
|
||||||
merged_trained_words = list(set(existing_trained_words + new_trained_words))
|
merged_trained_words = list(set(existing_trained_words + new_trained_words))
|
||||||
merged_civitai['trainedWords'] = merged_trained_words
|
merged_civitai['trainedWords'] = merged_trained_words
|
||||||
|
|
||||||
# Update local metadata with merged civitai data
|
# Update local metadata with merged civitai data
|
||||||
local_metadata['civitai'] = merged_civitai
|
local_metadata['civitai'] = merged_civitai
|
||||||
local_metadata['from_civitai'] = True
|
local_metadata['from_civitai'] = True
|
||||||
|
|
||||||
# Update model name if available
|
# Update model name if available
|
||||||
if 'model' in civitai_metadata:
|
if 'model' in civitai_metadata:
|
||||||
|
|||||||
Reference in New Issue
Block a user