Spaces:
Running
Running
Download app.py from HuggingFaceSwarms/pydraw: direct link, hf CLI and curl.
- Browser
- Download file 22.3 kB
-
https://huggingface.co/spaces/HuggingFaceSwarms/pydraw/resolve/main/app.py
- Command line
-
hf download hf://spaces/HuggingFaceSwarms/pydraw/app.py
-
curl -L -o app.py https://huggingface.co/spaces/HuggingFaceSwarms/pydraw/resolve/main/app.py
22.3 kB
| """PyDraw playground: rule-only text to image (no weights, no training, no image files).""" | |
| import os | |
| import time | |
| import nltk | |
| # wave 20 people and animals: 2D-first portraits in every style (fig2d), faces (expressions, age, costume), paint and | |
| # ink portrait styles, full-body poses, contacts, and the new animal and bird drawers | |
| os.environ.setdefault("PYDRAW_EXP", "fig2d=1,fx_face=1,st_paint=1,st_ink=1,body2d=1,cx=1,c2d=1") | |
| try: | |
| from nltk.corpus import wordnet | |
| wordnet.synsets("dog") | |
| except LookupError: | |
| # WordNet is a plain dictionary of "is-a" links; open-vocabulary nouns are looked up in it | |
| nltk.download("wordnet", quiet=True) | |
| import gradio as gr | |
| from PIL import Image | |
| from pydraw import plot, CONCEPTS | |
| from pydraw3d import plot3d | |
| from pydraw3d.families import FAMILIES, TAXA | |
| EXAMPLES = ["a watercolor painting of a bowl of fruit", | |
| "anime portrait of a girl with long blue hair", | |
| "a castle on a hill with a dragon flying above it at sunset", | |
| "a linocut print of a cabin in the woods", | |
| "a bear holding a mug that says HUG", | |
| "an ink and wash drawing of a boat on a lake"] | |
| RENDERERS = ["pixel", "3D", "smooth"] | |
| GITHUB = "https://github.com/linoytsaban/pydraw" | |
| def to_pil(img): | |
| im = Image.new("RGB", (img.width, img.height)) | |
| im.putdata([p for row in img.pixels for p in row]) | |
| return im | |
| def _draw(prompt, seed, renderer, size, variation, search=False): | |
| if renderer == "3D": | |
| im, tr = plot3d(prompt, seed=seed, size=size, res=min(size, 384), variation=variation, return_trace=True, search=search) | |
| return im, im.info.get("scene") or {}, tr | |
| img = plot(prompt, seed=seed, size=size, style=renderer, variation=variation) | |
| return to_pil(img), img.scene, img.trace | |
| def run(prompt, seed, renderer, size, variation, search=False): | |
| t = time.time() | |
| im, sc, tr = _draw(prompt, int(seed), renderer, int(size), float(variation), bool(search)) | |
| ents = sc.get("entities", []) | |
| fams = [] | |
| for e in ents: | |
| ov = CONCEPTS.get(e["concept"], {}).get("open_vocab") | |
| if ov: | |
| fams.append("%s (family: %s)" % (e["concept"], ov["family"])) | |
| summary = "**%s** | place: %s | time: %s | %.1fs | %d rules fired" % ( | |
| ", ".join("%d x %s%s" % (e["count"], (e["color"] + " ") if e["color"] else "", e["concept"]) for e in ents) or "(nothing drawable)", | |
| sc.get("place"), sc.get("time"), time.time() - t, len(tr)) | |
| if fams: | |
| summary += "\n\nopen vocabulary (WordNet + generator family): " + ", ".join(fams) | |
| if sc.get("unknown"): | |
| summary += "\n\nnot understood: " + ", ".join(sc["unknown"]) | |
| return im, summary, "\n".join(tr) | |
| def run4(prompt, seed, renderer, size, variation, search=False): | |
| s0 = int(seed) | |
| return [(_draw(prompt, s0 + k, renderer, int(size), float(variation), bool(search))[0], "seed %d" % (s0 + k)) for k in range(4)] | |
| # --------------------------------------------------------------------------------------------------------------- | |
| # Look: RD Flow 64 WebGPU (sage paper palette, Fraunces prompt, Pixelify Sans labels, pill controls, square hero | |
| # with a checkerboard and a deep shadow) crossed with PyBot (big title + question subtitle, hairline ink panels, | |
| # monospace uppercase buttons and trace, chips for examples, a one-line footer with the code link). | |
| # --------------------------------------------------------------------------------------------------------------- | |
| HEAD = """ | |
| <link rel="preconnect" href="https://fonts.googleapis.com"> | |
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | |
| <link href="https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,300;9..144,400&family=Pixelify+Sans:wght@500;600&family=IBM+Plex+Mono:wght@400;600&display=swap" rel="stylesheet"> | |
| """ | |
| CSS = """ | |
| :root, .gradio-container { | |
| --pd-bg:#f4f4f3; --pd-surface:#ffffff; --pd-ink:#1a1a1a; --pd-muted:#6b6b6b; --pd-faint:#b3b3b3; --pd-line:#e3e3e1; | |
| --pd-accent:#c2410c; --pd-accent-btn:#ea580c; --pd-accent-soft:rgb(234 88 12 / .12); --pd-on-accent:#fff; --pd-hot:#ea580c; | |
| --pd-check-a:#e9e9e7; --pd-check-b:#f6f6f5; | |
| --pd-shadow:0 1px 2px rgb(0 0 0 / .05), 0 10px 30px rgb(0 0 0 / .07); | |
| --pd-hero-shadow:0 30px 80px rgb(0 0 0 / .16); | |
| --pd-serif:"Fraunces", Georgia, "Times New Roman", serif; | |
| --pd-pixel:"Pixelify Sans", ui-monospace, monospace; | |
| --pd-mono:"IBM Plex Mono", ui-monospace, Menlo, monospace; | |
| } | |
| .dark, .dark .gradio-container { | |
| --pd-bg:#121212; --pd-surface:#1c1c1c; --pd-ink:#f0f0f0; --pd-muted:#a3a3a3; --pd-faint:#555555; --pd-line:#2b2b2b; | |
| --pd-accent:#fb923c; --pd-accent-btn:#fb923c; --pd-accent-soft:rgb(251 146 60 / .16); --pd-on-accent:#121212; | |
| --pd-check-a:#1f1f1f; --pd-check-b:#272727; | |
| --pd-shadow:0 1px 2px rgb(0 0 0 / .3), 0 10px 30px rgb(0 0 0 / .35); | |
| --pd-hero-shadow:0 30px 80px rgb(0 0 0 / .55); | |
| } | |
| body, gradio-app, .gradio-container { background:var(--pd-bg) !important; color:var(--pd-ink); } | |
| .gradio-container { box-sizing:border-box !important; width:100% !important; max-width:1400px !important; margin:0 auto !important; padding:clamp(16px,3vw,32px) !important; overflow-x:hidden; } | |
| .gradio-container .html-container { padding:0 !important; } | |
| .gradio-container * { min-width:0; } | |
| footer { display:none !important; } | |
| /* header: PyBot's title block, RD Flow's pill links */ | |
| #pd-head { display:flex; align-items:flex-end; justify-content:space-between; flex-wrap:wrap; gap:12px 24px; margin-bottom:clamp(28px,4.5vw,56px); } | |
| #pd-head .t { min-width:0; flex:1 1 320px; } | |
| #pd-head h1 { margin:0; font:600 clamp(34px,5vw,58px)/1 var(--pd-pixel); letter-spacing:.02em; color:var(--pd-ink); } | |
| #pd-head .sub { margin:10px 0 0; font:300 clamp(19px,2.3vw,26px)/1.25 var(--pd-serif); color:var(--pd-muted); overflow-wrap:anywhere; } | |
| #pd-head .meta { margin:12px 0 0; font:13px/1.5 var(--pd-mono); color:var(--pd-muted); overflow-wrap:anywhere; } | |
| #pd-head .meta b { color:var(--pd-ink); font-weight:600; } | |
| #pd-head .tag { display:inline-block; padding:1px 8px; border-radius:999px; background:var(--pd-accent-soft); color:var(--pd-accent); white-space:nowrap; } | |
| #pd-head .links { display:flex; gap:8px; flex-wrap:wrap; } | |
| #pd-head a.pill { display:inline-flex; align-items:center; gap:8px; min-height:40px; padding:0 16px; border-radius:999px; | |
| background:var(--pd-surface); color:var(--pd-ink); text-decoration:none; white-space:nowrap; box-shadow:var(--pd-shadow); font:14px var(--pd-mono); } | |
| #pd-head a.pill:hover { box-shadow:0 0 0 1.5px var(--pd-accent), var(--pd-shadow); } | |
| #pd-head a.pill svg { flex:none; } | |
| /* the two panes */ | |
| #pd-stage { gap:clamp(20px,4vw,56px) !important; align-items:flex-start; } | |
| #pd-stage { margin-top:clamp(24px,4vw,48px) !important; } | |
| .pd-kicker { margin:0 0 4px !important; color:var(--pd-muted); font:500 13px/1.3 var(--pd-pixel); letter-spacing:.08em; text-transform:uppercase; } | |
| .pd-kicker p { margin:0 !important; font:inherit !important; color:inherit !important; } | |
| /* prompt: one plain line inside the settings card */ | |
| #pd-prompt textarea { font:16px/1.4 var(--pd-body, system-ui, sans-serif) !important; color:var(--pd-ink) !important; | |
| background:var(--pd-bg) !important; border:1px solid var(--pd-line) !important; border-radius:10px !important; | |
| padding:9px 12px !important; box-shadow:none !important; caret-color:var(--pd-accent); resize:none; } | |
| #pd-prompt textarea:focus { border-color:var(--pd-accent) !important; } | |
| #pd-prompt label span, #pd-prompt [data-testid="block-info"] { font-size:11px !important; color:var(--pd-muted) !important; } | |
| /* controls: a surface card with hairlines */ | |
| .pd-card { background:var(--pd-surface) !important; border:1px solid var(--pd-line) !important; border-radius:16px !important; | |
| box-shadow:var(--pd-shadow) !important; padding:12px 14px !important; gap:10px !important; } | |
| .pd-card .block, .pd-card .form { background:transparent !important; border:0 !important; box-shadow:none !important; } | |
| .pd-card .row { gap:10px !important; flex-wrap:wrap; } | |
| .pd-card .row > * { min-width:min(140px,100%) !important; } | |
| .gradio-container label > span, .gradio-container .block-title, .gradio-container span[data-testid="block-info"] { | |
| font:500 12px/1.3 var(--pd-pixel) !important; letter-spacing:.07em; text-transform:uppercase; color:var(--pd-muted) !important; } | |
| .gradio-container input[type=number], .gradio-container .secondary-wrap, .gradio-container input[type=text] { font-family:var(--pd-mono) !important; } | |
| /* renderer as a segmented pill (RD Flow .seg) */ | |
| #pd-renderer .wrap { display:inline-flex !important; flex-wrap:wrap; gap:2px !important; padding:4px; border-radius:999px; background:var(--pd-bg); max-width:100%; } | |
| #pd-renderer .wrap label { margin:0 !important; border:0 !important; border-radius:999px !important; background:transparent !important; box-shadow:none !important; | |
| min-height:32px; padding:0 16px !important; color:var(--pd-muted) !important; cursor:pointer; white-space:nowrap; } | |
| #pd-renderer .wrap label input { position:absolute; opacity:0; pointer-events:none; } | |
| #pd-renderer .wrap label.selected, #pd-renderer .wrap label:has(input:checked) { background:var(--pd-ink) !important; color:var(--pd-bg) !important; } | |
| #pd-renderer .wrap label span { color:inherit !important; font:14px var(--pd-mono) !important; text-transform:none !important; letter-spacing:0 !important; margin:0 !important; } | |
| /* buttons: RD Flow's round "go" with PyBot's mono uppercase */ | |
| #pd-actions { gap:10px !important; flex-wrap:wrap; } | |
| #pd-actions button { min-height:46px; border-radius:999px !important; font:600 14px var(--pd-mono) !important; letter-spacing:.06em; text-transform:uppercase; } | |
| #pd-draw { background:var(--pd-accent-btn) !important; color:var(--pd-on-accent) !important; border:0 !important; box-shadow:0 6px 20px rgb(0 0 0 / .18) !important; } | |
| #pd-draw4 { background:var(--pd-surface) !important; color:var(--pd-ink) !important; border:1px solid var(--pd-ink) !important; box-shadow:none !important; } | |
| .gradio-container button:focus-visible { outline:2px solid var(--pd-hot) !important; outline-offset:2px; } | |
| /* examples as chips (PyBot chips / RD Flow picks) */ | |
| #pd-examples { background:transparent !important; border:0 !important; padding:0 !important; } | |
| #pd-examples .label, #pd-examples > .block-label { display:none; } | |
| #pd-examples table, #pd-examples thead { display:none !important; } | |
| #pd-examples > .gallery { display:grid !important; grid-template-columns:repeat(2, minmax(0,1fr)); gap:8px !important; } | |
| #pd-examples > .gallery > .gallery-item { max-width:100%; min-height:34px; padding:4px 14px !important; border-radius:18px !important; border:1px solid var(--pd-line) !important; | |
| background:var(--pd-surface) !important; color:var(--pd-ink) !important; box-shadow:var(--pd-shadow); font:14px/1.3 var(--pd-serif) !important; | |
| white-space:normal; overflow-wrap:anywhere; text-align:left; width:100%; margin:0 !important; } | |
| #pd-examples > .gallery > .gallery-item * { font:inherit !important; color:inherit !important; } | |
| #pd-examples button:hover { border-color:var(--pd-ink) !important; background:var(--pd-bg) !important; } | |
| /* the picture (RD Flow hero) */ | |
| #pd-hero { border-radius:12px !important; overflow:hidden; box-shadow:var(--pd-hero-shadow) !important; border:0 !important; padding:0 !important; | |
| background:repeating-conic-gradient(var(--pd-check-a) 0 25%, var(--pd-check-b) 0 50%) 0 0 / 24px 24px !important; aspect-ratio:1; width:100%; } | |
| #pd-hero .image-container, #pd-hero .image-frame, #pd-hero > div { background:transparent !important; height:100% !important; } | |
| #pd-hero img { image-rendering:pixelated; image-rendering:crisp-edges; width:100%; height:100%; object-fit:contain; } | |
| #pd-hero .empty, #pd-hero [class*="empty"] { font:300 clamp(20px,2.2vw,28px)/1.2 var(--pd-serif); color:var(--pd-muted); } | |
| #pd-hero label[data-testid="block-label"], #pd-hero .block-label { background:rgb(255 255 255 / .9) !important; color:#1a1a1a !important; border-radius:999px !important; | |
| border:0 !important; font:500 12px var(--pd-pixel) !important; letter-spacing:.06em; text-transform:uppercase; margin:10px; } | |
| /* the answer: PyBot's mono "why" line under the picture */ | |
| #pd-summary { font:14px/1.55 var(--pd-mono); color:var(--pd-muted); background:var(--pd-surface); border:1px solid var(--pd-line); | |
| border-radius:14px; padding:10px 14px !important; min-height:48px; box-shadow:var(--pd-shadow); overflow-wrap:anywhere; } | |
| #pd-summary p { margin:0 0 6px !important; font:inherit !important; color:inherit !important; } | |
| #pd-summary p:last-child { margin:0 !important; } | |
| #pd-summary strong { color:var(--pd-ink); font-weight:600; } | |
| #pd-summary:not(:has(p)) { display:none !important; } | |
| /* the four seeds (RD Flow's 2 x 2 variants) */ | |
| #pd-gallery { background:transparent !important; border:0 !important; box-shadow:none !important; } | |
| #pd-gallery .grid-wrap, #pd-gallery .grid-container { background:transparent !important; height:auto !important; max-height:none !important; overflow:visible !important; } | |
| #pd-gallery .grid-container { padding-top:36px !important; } | |
| #pd-gallery .thumbnail-item { aspect-ratio:1 !important; height:auto !important; } | |
| #pd-gallery .empty { height:110px !important; min-height:0 !important; border:1px dashed var(--pd-faint); border-radius:12px; } | |
| #pd-gallery img { image-rendering:pixelated; border-radius:10px; } | |
| #pd-gallery .thumbnail-item, #pd-gallery button.thumbnail-item { border-radius:10px !important; box-shadow:var(--pd-shadow); } | |
| #pd-gallery .caption-label { font:12px var(--pd-mono) !important; border-radius:999px !important; } | |
| /* accordions: RD Flow's "+" details with PyBot's mono body */ | |
| .pd-acc { background:var(--pd-surface) !important; border:1px solid var(--pd-line) !important; border-radius:16px !important; box-shadow:var(--pd-shadow) !important; } | |
| .pd-acc > button, .pd-acc .label-wrap { font:14px var(--pd-mono) !important; color:var(--pd-muted) !important; } | |
| .pd-acc .label-wrap span, .pd-acc > button span { font:inherit !important; color:inherit !important; text-transform:none !important; letter-spacing:0 !important; } | |
| #pd-trace textarea { font:13px/1.55 var(--pd-mono) !important; color:var(--pd-ink) !important; background:var(--pd-bg) !important; | |
| border:1px solid var(--pd-ink) !important; border-radius:0 !important; white-space:pre-wrap; overflow-wrap:anywhere; } | |
| .pd-about { font:15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color:var(--pd-muted); overflow-wrap:anywhere; } | |
| .pd-about p { margin:0 0 8px; } | |
| .pd-about b { color:var(--pd-ink); } | |
| /* footer line (PyBot) */ | |
| /* seed and size: small and quiet so the prompt stays the focus */ | |
| #pd-small, #pd-small > .form { flex-direction:row !important; flex-wrap:nowrap !important; justify-content:flex-start !important; gap:12px !important; } | |
| #pd-small > .form { border:0 !important; background:transparent !important; width:auto !important; flex:0 0 auto !important; } | |
| .pd-card { padding:8px 12px !important; } | |
| #pd-var { flex:1 1 160px !important; min-width:0 !important; } | |
| #pd-var .head, #pd-var label { font-size:11px !important; } | |
| #pd-var input[type=number] { font-size:13px !important; height:28px !important; } | |
| #pd-small > .form { flex:1 1 auto !important; } | |
| @media (max-width:640px) { #pd-small, #pd-small > .form { flex-wrap:wrap !important; } #pd-var { flex:1 1 100% !important; } } | |
| #pd-seed, #pd-size { flex:0 0 128px !important; width:128px !important; min-width:0 !important; max-width:128px !important; } | |
| #pd-seed input, #pd-size input { font-size:13px !important; } | |
| #pd-seed label span, #pd-size label span { font-size:11px !important; color:var(--pd-muted) !important; } | |
| /* breathing room: the examples sit further below the controls */ | |
| .pd-kicker.pd-after { margin-top:clamp(28px,4vw,48px) !important; } | |
| #pd-actions { margin-bottom:4px; } | |
| #pd-hint { font:13px/1.5 var(--pd-mono); color:var(--pd-muted); margin:-4px 0 4px; } | |
| #pd-foot { margin:14px 0 0; font:13px/1.5 var(--pd-mono); color:var(--pd-muted); overflow-wrap:anywhere; } | |
| #pd-foot a { color:inherit; text-underline-offset:2px; } | |
| @media (max-width:900px) { | |
| #pd-stage { flex-direction:column !important; } | |
| #pd-stage > * { width:100% !important; flex:1 1 auto !important; } | |
| #pd-left { display:contents !important; } | |
| #pd-stage > #pd-left > * { order:1; width:100%; min-width:0; } | |
| #pd-side { order:2; } | |
| #pd-stage > #pd-left > .pd-after, #pd-stage > #pd-left > #pd-examples { order:3; } | |
| } | |
| @media (max-width:520px) { | |
| .gradio-container { padding:16px !important; } | |
| /* examples: one column of wrapping chips (only ten, so no strip needed) */ | |
| #pd-examples > .gallery { grid-template-columns:minmax(0,1fr); } | |
| #pd-head .links { width:100%; } | |
| #pd-actions button { flex:1 1 0; } | |
| } | |
| @media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation:none !important; transition:none !important; } } | |
| """ | |
| GH_ICON = ('<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill="currentColor" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 ' | |
| '0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 ' | |
| '0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 ' | |
| '0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"/></svg>') | |
| HEADER = """ | |
| <header id="pd-head"> | |
| <div class="t"> | |
| <h1>PyDraw</h1> | |
| <p class="sub">Can python be an image generator?</p> | |
| <p class="meta"><b>Pure code, no model weights.</b> Every picture is drawn by hand-written Python rules. | |
| <span class="tag">work in progress</span></p> | |
| </div> | |
| <nav class="links" aria-label="Links"> | |
| <a class="pill" href="%s" target="_blank" rel="noopener noreferrer">%s github.com/linoytsaban/pydraw</a> | |
| </nav> | |
| </header> | |
| """ % (GITHUB, GH_ICON) | |
| ABOUT = ("<div class='pd-about'><p><b>%d hand-made concepts</b>, plus any common noun through WordNet and <b>%d generator families</b> (%d word entries).</p>" | |
| "<p>Renderers: pixel art, a 3D ray-marcher (slower, ~10-40 s on this CPU), or smooth.</p>" | |
| "<p>Style words in the prompt switch the pixel renderer to a painted or inked medium: watercolor, oil painting, gouache, charcoal, " | |
| "impressionist, van gogh, pen and ink, line art, ink and wash, comic, manga, linocut, sumi-e.</p>" | |
| "<p>People and animals are posed by a skeleton rig when the verb asks for it.</p></div>" | |
| % (len(CONCEPTS), len(FAMILIES), len(TAXA))) | |
| FOOT = ("<p id='pd-foot'>PyDraw is plain Python: a parser, a layout engine, drawing rules and a ray-marcher, no weights, no training, no image files. " | |
| "Code: <a href='%s' target='_blank' rel='noopener noreferrer'>github.com/linoytsaban/pydraw</a>.</p>" % GITHUB) | |
| THEME = gr.themes.Base(primary_hue="orange", neutral_hue="neutral", radius_size="lg").set( | |
| slider_color="#ea580c", slider_color_dark="#fb923c", checkbox_background_color_selected="#ea580c", | |
| checkbox_background_color_selected_dark="#fb923c", color_accent="#ea580c", color_accent_soft="rgba(234,88,12,.12)") | |
| with gr.Blocks(title="PyDraw") as demo: | |
| gr.HTML(HEADER) | |
| with gr.Row(elem_id="pd-stage", equal_height=False): | |
| with gr.Column(scale=5, min_width=0, elem_id="pd-left"): | |
| style = gr.State("pixel") | |
| search = gr.State(False) | |
| with gr.Column(elem_classes="pd-card"): | |
| prompt = gr.Textbox(value=EXAMPLES[0], lines=1, max_lines=3, label="Prompt", | |
| info="Name a style (watercolor, oil painting, ink and wash, linocut, pencil, anime). Defaults to pixel art.", | |
| placeholder="a fox in a cape next to a waving flag", elem_id="pd-prompt") | |
| with gr.Row(elem_id="pd-small"): | |
| seed = gr.Number(label="Seed", value=0, precision=0, min_width=90, scale=0, elem_id="pd-seed") | |
| size = gr.Dropdown([512, 768], value=512, label="Size", min_width=90, scale=0, elem_id="pd-size") | |
| variation = gr.Slider(0, 1, value=1, step=0.25, label="Variation", min_width=160, elem_id="pd-var") | |
| with gr.Row(elem_id="pd-actions"): | |
| btn = gr.Button("Draw", variant="primary", elem_id="pd-draw") | |
| btn4 = gr.Button("4 variations", elem_id="pd-draw4") | |
| gr.Markdown("Try one", elem_classes=["pd-kicker", "pd-after"]) | |
| gr.Examples(EXAMPLES, inputs=prompt, examples_per_page=len(EXAMPLES), elem_id="pd-examples", label=None) | |
| with gr.Accordion("What it knows", open=False, elem_classes=["pd-acc", "pd-after"]): | |
| gr.HTML(ABOUT) | |
| with gr.Column(scale=6, min_width=0, elem_id="pd-side"): | |
| out = gr.Image(label="Image", type="pil", elem_id="pd-hero", buttons=["download", "fullscreen"]) | |
| summary = gr.Markdown(elem_id="pd-summary") | |
| gal = gr.Gallery(label="4 variations (seed, seed+1, ...)", columns=2, height="auto", object_fit="contain", | |
| elem_id="pd-gallery", visible=False) | |
| with gr.Accordion("Rules that fired", open=False, elem_classes="pd-acc"): | |
| trace = gr.Textbox(lines=18, show_label=False, elem_id="pd-trace") | |
| gr.HTML(FOOT) | |
| ins = [prompt, seed, style, size, variation, search] | |
| btn.click(lambda: (gr.update(visible=True), gr.update(visible=False)), None, [out, gal], queue=False, api_visibility="private").then(run, ins, [out, summary, trace], api_name="run") | |
| prompt.submit(lambda: (gr.update(visible=True), gr.update(visible=False)), None, [out, gal], queue=False, api_visibility="private").then(run, ins, [out, summary, trace]) | |
| # reveal the (hidden until now) 4-seed grid first, then fill it; run4 itself is unchanged | |
| btn4.click(lambda: (gr.update(visible=True), gr.update(visible=False)), None, [gal, out], queue=False, api_visibility="private").then(run4, ins, gal, api_name="run4") | |
| if __name__ == "__main__": | |
| demo.queue(default_concurrency_limit=2).launch(css=CSS, head=HEAD, theme=THEME) | |