zixaphir / Stable-Diffusion-Webui-Civitai-Helper

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

fix: actually handle errors in get_data_safe #2

Closed infinitewarp closed 10 months ago

infinitewarp commented 10 months ago

I encountered the following error several times today on my first ever time running the "Scan Models for Civitai" functionality in the a1111 UI for this extension.

Civitai Helper: Request model info from civitai
Civitai Helper: Civitai does not have this model
Civitai Helper: Write model info to file: /home/username/stable-diffusion-webui/models/Lora/modelname.civitai.info
Traceback (most recent call last):

...

  File "/home/username/stable-diffusion-webui/extensions/Stable-Diffusion-Webui-Civitai-Helper/scripts/ch_lib/model_action_civitai.py", line 82, in scan_model
    write_sd15_model_info(sd15_file, model_info)
  File "/home/username/stable-diffusion-webui/extensions/Stable-Diffusion-Webui-Civitai-Helper/scripts/ch_lib/model_action_civitai.py", line 118, in write_sd15_model_info
    data["description"] = get_data_safe(model_info, "description", "")
  File "/home/username/stable-diffusion-webui/extensions/Stable-Diffusion-Webui-Civitai-Helper/scripts/ch_lib/util.py", line 90, in get_data_safe
    return data[key]
KeyError: 'description'

At the time this exception was raised, the modelname.civitai.info file contained only {}. I discovered that I could manually "fix" the file by replacing it with {"description":""} and restarting scan would handle the file just fine, but then it choked on another file later that also contained only {} that I would have to manually fix. (Probably several more after that.) Note that this didn't happen for all models. I'm not sure why some got an empty {} while others worked just fine, but I didn't want to chase down that problem too. πŸ™‚

So, this PR fixes get_data_safe so that it actually handles KeyError or any other exception. I'm not a fan of the unspecified except, but I think I implemented whatever the original author intended this function to do, and that's good enough for now.

I was able to successfully complete "Scan Models for Civitai" without errors after making this change.

Civitai Helper: Done. Scanned 202 models, checked 202 images

πŸ‘

zixaphir commented 10 months ago

Thank you for this fix! I wrote the original method to deal with exactly this problem, but when I was moving things around here it looks like I accidentally broke my own function. Thank you for catching this and I'm sorry for my oversight.

I am not very fluent in Python and I am just now learning of the get method, which seems to do exactly what I wrote the get_data_safe function to do, so I might just eliminate it altogether.