File size: 2,912 Bytes
63be854 698eb37 63be854 6e3ec3e 63be854 698eb37 63be854 698eb37 63be854 698eb37 63be854 698eb37 63be854 698eb37 63be854 698eb37 63be854 698eb37 63be854 698eb37 63be854 98269eb 63be854 98269eb 63be854 98269eb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | """Builds the example-card / suggestion-chip HTML shown in the UI, and serves example data
back to the frontend when a card is clicked."""
import html as html_lib
import json
import os
from typing import Any
from image_codec import encode_full_image, make_thumb_b64
from logging_utils import print_built_example_cards, print_built_suggestion_chips, print_building_example_thumbnails
with open("examples.json") as _f:
EXAMPLES_CONFIG: list[dict[str, Any]] = json.load(_f)
with open("suggestions.json") as _f:
SUGGESTIONS_CONFIG: list[dict[str, Any]] = json.load(_f)
def _example_thumbs_html(images: list[str]) -> str:
html = ""
for path in images:
thumb = make_thumb_b64(path)
if thumb:
html += f'<img src="{thumb}" alt="">'
else:
html += '<div class="example-thumb-placeholder">Preview</div>'
return html
def _example_card_html(idx: int, ex: dict[str, Any]) -> str:
thumbs_html = _example_thumbs_html(ex["images"])
n = len(ex["images"])
badge = f'{n} image{"s" if n > 1 else ""}'
prompt_short = html_lib.escape(ex["prompt"][:90])
if len(ex["prompt"]) > 90:
prompt_short += "..."
return f'''<div class="example-card" data-idx="{idx}">
<div class="example-thumbs">{thumbs_html}</div>
<div class="example-meta"><span class="example-badge">{badge}</span></div>
<div class="example-prompt-text">{prompt_short}</div>
</div>'''
def build_example_cards_html() -> str:
return "".join(_example_card_html(i, ex) for i, ex in enumerate(EXAMPLES_CONFIG))
def build_suggestion_chips_html() -> str:
chips = []
for s in SUGGESTIONS_CONFIG:
prompt_json = html_lib.escape(json.dumps(s["prompt"]))
label = html_lib.escape(s["label"])
chips.append(f'<button class="suggestion-chip" onclick="window.__setPrompt({prompt_json})">{label}</button>')
return "".join(chips)
def _parse_example_idx(idx_str: str) -> int:
try:
return int(float(idx_str)) if idx_str and idx_str.strip() else -1
except (ValueError, TypeError):
return -1
def load_example_data(idx_str: str) -> str:
idx = _parse_example_idx(idx_str)
if idx < 0 or idx >= len(EXAMPLES_CONFIG):
return json.dumps({"images": [], "prompt": "", "names": [], "status": "error"})
ex = EXAMPLES_CONFIG[idx]
b64_list, names = [], []
for path in ex["images"]:
b64 = encode_full_image(path)
if b64:
b64_list.append(b64)
names.append(os.path.basename(path))
return json.dumps({"images": b64_list, "prompt": ex["prompt"], "names": names, "status": "ok"})
print_building_example_thumbnails()
EXAMPLE_CARDS_HTML = build_example_cards_html()
print_built_example_cards(len(EXAMPLES_CONFIG))
SUGGESTION_CHIPS_HTML = build_suggestion_chips_html()
print_built_suggestion_chips(len(SUGGESTIONS_CONFIG))
|