65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
import os
|
|
import json
|
|
import traceback
|
|
|
|
import gradio as gr
|
|
|
|
from scripts.stores.images import ImageStore
|
|
from scripts.interface import create_image_info
|
|
|
|
|
|
def _maybe_load_json(d, key, default=None):
|
|
if key not in d:
|
|
return default
|
|
try:
|
|
return json.loads(d.get(key))
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
print(f"[FABRIC] Failed to load JSON: {d.get(key)}")
|
|
return default
|
|
|
|
# helper functions for loading saved params
|
|
def parse_feedback_info(d, store: ImageStore):
|
|
if "fabric_image_infos" in d:
|
|
# >=0.7.0 (new format)
|
|
infos = _maybe_load_json(d, "fabric_image_infos", [])
|
|
try:
|
|
infos = [create_image_info(**info) for info in infos]
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
print(f"[FABRIC] Failed to parse image infos: {infos}")
|
|
return None
|
|
elif "fabric_pos_images" in d or "fabric_neg_images" in d:
|
|
# <0.7.0 (old format)
|
|
infos = []
|
|
if "fabric_pos_images" in d:
|
|
infos += [
|
|
create_image_info(path, 1.0, "")
|
|
for path in _maybe_load_json(d, "fabric_pos_images", [])
|
|
]
|
|
if "fabric_neg_images" in d:
|
|
infos += [
|
|
create_image_info(path, -1.0, "")
|
|
for path in _maybe_load_json(d, "fabric_neg_images", [])
|
|
]
|
|
else:
|
|
return None
|
|
|
|
infos = [info for info in infos if os.path.exists(store.full_path(info["path"]))]
|
|
return infos
|
|
|
|
def load_image_infos(d, store: ImageStore):
|
|
image_infos = parse_feedback_info(d, store)
|
|
if image_infos is not None:
|
|
return gr.JSON.update(value=image_infos)
|
|
else:
|
|
return None
|
|
|
|
def load_gallery(d, store: ImageStore):
|
|
image_infos = parse_feedback_info(d, store)
|
|
if image_infos is not None:
|
|
paths = [store.full_path(info["path"]) for info in image_infos]
|
|
return gr.Gallery.update(value=paths)
|
|
else:
|
|
return None
|