Download examples_ui.py from macgaga/FireRed-Experimental-Turbo: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/spaces/macgaga/FireRed-Experimental-Turbo/resolve/main/examples_ui.py
- Command line
-
hf download hf://spaces/macgaga/FireRed-Experimental-Turbo/examples_ui.py
-
curl -L -o examples_ui.py https://huggingface.co/spaces/macgaga/FireRed-Experimental-Turbo/resolve/main/examples_ui.py
2.91 kB
| """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)) | |