mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2024-06-07 21:20:49 +00:00
Better error handling when unable to read metadata from safetensors file
This commit is contained in:
parent
3902aa222b
commit
8dc920228e
@ -280,17 +280,21 @@ def read_metadata_from_safetensors(filename):
|
|||||||
json_start = file.read(2)
|
json_start = file.read(2)
|
||||||
|
|
||||||
assert metadata_len > 2 and json_start in (b'{"', b"{'"), f"{filename} is not a safetensors file"
|
assert metadata_len > 2 and json_start in (b'{"', b"{'"), f"{filename} is not a safetensors file"
|
||||||
json_data = json_start + file.read(metadata_len-2)
|
|
||||||
json_obj = json.loads(json_data)
|
|
||||||
|
|
||||||
res = {}
|
res = {}
|
||||||
for k, v in json_obj.get("__metadata__", {}).items():
|
|
||||||
res[k] = v
|
try:
|
||||||
if isinstance(v, str) and v[0:1] == '{':
|
json_data = json_start + file.read(metadata_len-2)
|
||||||
try:
|
json_obj = json.loads(json_data)
|
||||||
res[k] = json.loads(v)
|
for k, v in json_obj.get("__metadata__", {}).items():
|
||||||
except Exception:
|
res[k] = v
|
||||||
pass
|
if isinstance(v, str) and v[0:1] == '{':
|
||||||
|
try:
|
||||||
|
res[k] = json.loads(v)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
errors.report(f"Error reading metadata from file: {filename}", exc_info=True)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user