fix(civitai): improve metadata parsing for nested structures, see #700

- Refactor metadata detection to handle nested "meta" objects
- Add support for lowercase "lora:" hash keys
- Extract metadata from nested "meta" field when present
- Update tests to verify nested metadata parsing
- Handle case-insensitive LORA hash detection

The changes ensure proper parsing of Civitai image metadata that may be wrapped in nested structures, improving compatibility with different API response formats.
This commit is contained in:
Will Miao
2025-11-26 13:46:08 +08:00
parent 5d4917c8d9
commit f8b9fa9b20
3 changed files with 97 additions and 9 deletions

View File

@@ -60,6 +60,46 @@ async def test_parse_metadata_creates_loras_from_hashes(monkeypatch):
}
@pytest.mark.asyncio
async def test_parse_metadata_handles_nested_meta_and_lowercase_hashes(monkeypatch):
async def fake_metadata_provider():
return None
monkeypatch.setattr(
"py.recipes.parsers.civitai_image.get_default_metadata_provider",
fake_metadata_provider,
)
parser = CivitaiApiMetadataParser()
metadata = {
"id": 106706587,
"meta": {
"prompt": "An enigmatic silhouette",
"hashes": {
"model": "ee75fd24a4",
"lora:mj": "de49e1e98c",
"LORA:Another_Earth_2": "dc11b64a8b",
},
"resources": [
{
"hash": "ee75fd24a4",
"name": "stoiqoNewrealityFLUXSD35_f1DAlphaTwo",
"type": "model",
}
],
},
}
assert parser.is_metadata_matching(metadata)
result = await parser.parse_metadata(metadata)
assert result["gen_params"]["prompt"] == "An enigmatic silhouette"
assert {l["name"] for l in result["loras"]} == {"mj", "Another_Earth_2"}
assert {l["hash"] for l in result["loras"]} == {"de49e1e98c", "dc11b64a8b"}
@pytest.mark.asyncio
async def test_parse_metadata_populates_checkpoint_and_rewrites_thumbnails(monkeypatch):
checkpoint_info = {