zixaphir / Stable-Diffusion-Webui-Civitai-Helper

Stable Diffusion Webui Extension for Civitai, to manage your model much more easily.
175 stars 24 forks source link

updating .pt model metadata fails with error, "not a safetensors file" rather than moving on #26

Closed arifyn closed 8 months ago

arifyn commented 8 months ago

After updating to the latest version of the plugin, the "scan" process now fails with an error when I try to update metadata.

The console reports the following:

File "C:\Utilities\stable-diffusion-webui\modules\sd_models.py", line 269, in read_metadata_from_safetensors
assert metadata_len > 2 and json_start in (b'{"', b"{'"), f"{filename} is not a safetensors file"
AssertionError: C:\Utilities\stable-diffusion-webui\embeddings\AnimeScreenCap.pt is not a safetensors file

Anything that's not a safetensors file (such as most embeddings) that can't be found on CivitAI will trigger this behavior, not just that particular file.

I'm guessing this is related to the feature added in v1.7.3:

When a model is not found on Civitai, this extension will attempt to read the model header for activation keywords. This only works with safetensors files with kohya_ss metadata and may be inaccurate depending on how the model was trained. [...]

The immediate cause is line 176 in scripts/model_action_civitai.py:

file_metadata = sd_models.read_metadata_from_safetensors(path)

I was able to work around the problem by wrapping the line in a try/except loop:

try:
   file_metadata = sd_models.read_metadata_from_safetensors(path)
except:
   return model_info

but I suspect there's a better way to do this.

zixaphir commented 8 months ago

Sorry about that. Fixed in https://github.com/zixaphir/Stable-Diffusion-Webui-Civitai-Helper/commit/3a41a951dffcd7ef0bf0fe7b6d939124cbb6a68a

zixaphir commented 8 months ago

but I suspect there's a better way to do this.

"Ask forgiveness not permission" is a python saying, so that is generally the best way to do it, although there's some pushback against except: pass, but in this case, I think it's fine.

arifyn commented 8 months ago

thanks! appreciate all the work you've done on this plugin.