Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -107,20 +107,31 @@ header_html = """
|
|
| 107 |
"""
|
| 108 |
|
| 109 |
# ────────── Gradio UI ──────────
|
| 110 |
-
with gr.Blocks(css=css, title="ZenCtrl
|
| 111 |
raw_state = gr.State()
|
| 112 |
|
| 113 |
gr.HTML(header_html)
|
| 114 |
-
gr.Markdown(
|
| 115 |
-
**Generate context-aware images of your subject with ZenCtrl’s inpainting playground.**
|
| 116 |
-
Upload a subject + optional mask, write a prompt, and hit **Generate**.
|
| 117 |
-
Open *Advanced Settings* to fetch an AI-generated background.
|
| 118 |
-
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
with gr.Column(scale=2, elem_id="col-container"):
|
| 122 |
subj_img = gr.Image(type="pil", label="Subject image")
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
use_detect_ck = gr.Checkbox(False, label="Detect with Florence-SAM")
|
| 125 |
detect_box = gr.Textbox(label="Detection prompt", value="person, chair", visible=False)
|
| 126 |
promptbox = gr.Textbox(label="Generation prompt", value="furniture", lines=2)
|
|
@@ -134,29 +145,48 @@ Open *Advanced Settings* to fetch an AI-generated background.
|
|
| 134 |
gallery = gr.Gallery(columns=[1], rows=[1], object_fit="contain", height="auto")
|
| 135 |
bg_img = gr.Image(label="Background", visible=False)
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
gr.Examples(
|
| 138 |
examples=[
|
| 139 |
-
["
|
| 140 |
-
["
|
| 141 |
-
["
|
| 142 |
-
["
|
| 143 |
],
|
| 144 |
-
inputs=[subj_img,
|
| 145 |
-
outputs=[gallery],
|
| 146 |
-
fn=
|
| 147 |
examples_per_page="all",
|
| 148 |
-
label="Presets (Input · Background · Prompt
|
| 149 |
-
cache_examples="lazy"
|
| 150 |
)
|
| 151 |
|
|
|
|
| 152 |
run_btn.click(
|
| 153 |
process_image_and_text,
|
| 154 |
inputs=[subj_img, ref_img, promptbox, use_detect_ck, detect_box],
|
| 155 |
outputs=[gallery, raw_state]
|
| 156 |
)
|
| 157 |
-
|
| 158 |
bg_btn.click(_gen_bg, inputs=[bgprompt], outputs=[bg_img])
|
| 159 |
-
use_detect_ck.change(
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
demo.launch(show_api=False, share=True)
|
|
|
|
| 107 |
"""
|
| 108 |
|
| 109 |
# ────────── Gradio UI ──────────
|
| 110 |
+
with gr.Blocks(css=css, title="ZenCtrl Inpainting") as demo:
|
| 111 |
raw_state = gr.State()
|
| 112 |
|
| 113 |
gr.HTML(header_html)
|
| 114 |
+
gr.Markdown(
|
| 115 |
+
"**Generate context-aware images of your subject with ZenCtrl’s inpainting playground.** \n"
|
| 116 |
+
"Upload a subject + optional mask, write a prompt, and hit **Generate**. \n"
|
| 117 |
+
"Open *Advanced Settings* to fetch an AI-generated background."
|
| 118 |
+
)
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
with gr.Column(scale=2, elem_id="col-container"):
|
| 122 |
subj_img = gr.Image(type="pil", label="Subject image")
|
| 123 |
+
|
| 124 |
+
# ⚑ Visible sketch component (what the user draws on)
|
| 125 |
+
ref_img = gr.Image(
|
| 126 |
+
type="pil", label="Background / Mask image",
|
| 127 |
+
tool="sketch", brush_color="#00FF00"
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# ⚑ Hidden plain-image component used ONLY for examples
|
| 131 |
+
ref_img_ex = gr.Image(
|
| 132 |
+
type="pil", visible=False
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
use_detect_ck = gr.Checkbox(False, label="Detect with Florence-SAM")
|
| 136 |
detect_box = gr.Textbox(label="Detection prompt", value="person, chair", visible=False)
|
| 137 |
promptbox = gr.Textbox(label="Generation prompt", value="furniture", lines=2)
|
|
|
|
| 145 |
gallery = gr.Gallery(columns=[1], rows=[1], object_fit="contain", height="auto")
|
| 146 |
bg_img = gr.Image(label="Background", visible=False)
|
| 147 |
|
| 148 |
+
# ---------- Example wrapper ---------------------------------
|
| 149 |
+
from PIL import Image
|
| 150 |
+
def _run_example(subj_path, bg_path, prompt):
|
| 151 |
+
"""Convert the plain bg image to the dict format and run generation."""
|
| 152 |
+
subj = Image.open(subj_path)
|
| 153 |
+
bg = Image.open(bg_path)
|
| 154 |
+
outputs, _ = process_image_and_text(
|
| 155 |
+
subj,
|
| 156 |
+
{"image": bg, "mask": None}, # wrap as dict for pipeline
|
| 157 |
+
prompt,
|
| 158 |
+
use_detect=False,
|
| 159 |
+
detect_prompt=""
|
| 160 |
+
)
|
| 161 |
+
# Return generation + also show bg in the sketch component so user can draw
|
| 162 |
+
return outputs[0], gr.update(value=bg) # gallery, ref_img update
|
| 163 |
+
|
| 164 |
+
# ---------- Examples ----------------------------------------
|
| 165 |
gr.Examples(
|
| 166 |
examples=[
|
| 167 |
+
["examples/subject1.png", "examples/bg1.png", "Make the toy sit on a marble table"],
|
| 168 |
+
["examples/subject2.png", "examples/bg2.png", "Turn the flowers into sunflowers"],
|
| 169 |
+
["examples/subject3.png", "examples/bg3.png", "Make this monster ride a skateboard on the beach"],
|
| 170 |
+
["examples/subject4.png", "examples/bg4.png", "Make this cat happy"],
|
| 171 |
],
|
| 172 |
+
inputs=[subj_img, ref_img_ex, promptbox], # <-- uses the hidden plain image
|
| 173 |
+
outputs=[gallery, ref_img], # update gallery + visible sketch img
|
| 174 |
+
fn=_run_example,
|
| 175 |
examples_per_page="all",
|
| 176 |
+
label="Presets (Input · Background · Prompt)",
|
| 177 |
+
cache_examples="lazy",
|
| 178 |
)
|
| 179 |
|
| 180 |
+
# ---------- Buttons & interactions --------------------------
|
| 181 |
run_btn.click(
|
| 182 |
process_image_and_text,
|
| 183 |
inputs=[subj_img, ref_img, promptbox, use_detect_ck, detect_box],
|
| 184 |
outputs=[gallery, raw_state]
|
| 185 |
)
|
|
|
|
| 186 |
bg_btn.click(_gen_bg, inputs=[bgprompt], outputs=[bg_img])
|
| 187 |
+
use_detect_ck.change(
|
| 188 |
+
lambda v: gr.update(visible=v), inputs=use_detect_ck, outputs=detect_box
|
| 189 |
+
)
|
| 190 |
|
| 191 |
+
# ---------------- Launch ---------------------------------------
|
| 192 |
+
demo.launch(show_api=False, share=True)
|