Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
-
seed,
|
| 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 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
with gr.Row():
|
| 103 |
-
width = gr.Slider(
|
| 104 |
-
label="Width",
|
| 105 |
-
minimum=256,
|
| 106 |
-
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=32,
|
| 108 |
-
value=1024, # Replace with defaults that work for your model
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
with gr.Row():
|
| 120 |
-
guidance_scale = gr.Slider(
|
| 121 |
-
label="Guidance scale",
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=10.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
num_inference_steps = gr.Slider(
|
| 129 |
-
label="Number of inference steps",
|
| 130 |
-
minimum=1,
|
| 131 |
-
maximum=50,
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
-
gr.on(
|
| 138 |
-
triggers=[run_button.click, prompt.submit],
|
| 139 |
-
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
],
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# ZenCtrl Inpainting Playground (Baseten backend)
|
| 3 |
+
|
| 4 |
+
import os, json, base64, requests
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# ββββββββββ Secrets & endpoints ββββββββββ
|
| 10 |
+
BASETEN_MODEL_URL = "https://app.baseten.co/models/YOUR_MODEL_ID/predict"
|
| 11 |
+
BASETEN_API_KEY = os.getenv("BASETEN_API_KEY")
|
| 12 |
+
REPLICATE_TOKEN = os.getenv("REPLICATE_API_TOKEN")
|
| 13 |
+
|
| 14 |
+
from florence_sam.detect_and_segment import fill_detected_bboxes
|
| 15 |
+
|
| 16 |
+
# ββββββββββ Globals ββββββββββ
|
| 17 |
+
ADAPTER_NAME = "inpaint"
|
| 18 |
+
ADAPTER_SIZE = 1024
|
| 19 |
+
model_config = dict(union_cond_attn=True, add_cond_attn=False,
|
| 20 |
+
latent_lora=False, independent_condition=False)
|
| 21 |
+
css = "#col-container {margin:0 auto; max-width:960px;}"
|
| 22 |
+
|
| 23 |
+
#Background prompt via Replicate
|
| 24 |
+
def _gen_bg(prompt: str):
|
| 25 |
+
url = replicate.run(
|
| 26 |
+
"google/imagen-4-fast",
|
| 27 |
+
input={"prompt": prompt or "cinematic background", "aspect_ratio": "1:1"},
|
| 28 |
+
)
|
| 29 |
+
url = url[0] if isinstance(url, list) else url
|
| 30 |
+
return Image.open(BytesIO(requests.get(url, timeout=120).content)).convert("RGB")
|
| 31 |
+
|
| 32 |
+
# Core generation
|
| 33 |
+
def process_image_and_text(subject_image, adapter_dict, prompt, use_detect, detect_prompt, size=ADAPTER_SIZE, rank=10.0):
|
| 34 |
+
seed, guidance_scale, steps = 42, 2.5, 28
|
| 35 |
+
|
| 36 |
+
if use_detect:
|
| 37 |
+
base_img = adapter_dict["image"] if isinstance(adapter_dict, dict) else adapter_dict
|
| 38 |
+
if base_img is None:
|
| 39 |
+
raise gr.Error("Upload a background image first.")
|
| 40 |
+
adapter_image, _ = fill_detected_bboxes(
|
| 41 |
+
image=base_img, text=detect_prompt,
|
| 42 |
+
inflate_pct=0.15, fill_color="#00FF00"
|
| 43 |
+
)
|
| 44 |
+
else:
|
| 45 |
+
adapter_image = adapter_dict["image"] if isinstance(adapter_dict, dict) else adapter_dict
|
| 46 |
+
if isinstance(adapter_dict, dict) and adapter_dict.get("mask") is not None:
|
| 47 |
+
m = adapter_dict["mask"].convert("L").point(lambda p: 255 if p else 0)
|
| 48 |
+
if bbox := m.getbbox():
|
| 49 |
+
rect = Image.new("L", m.size, 0)
|
| 50 |
+
ImageDraw.Draw(rect).rectangle(bbox, fill=255)
|
| 51 |
+
m = rect
|
| 52 |
+
green = Image.new("RGB", adapter_image.size, "#00FF00")
|
| 53 |
+
adapter_image = Image.composite(green, adapter_image, m)
|
| 54 |
+
|
| 55 |
+
def prep(img: Image.Image):
|
| 56 |
+
w, h = img.size
|
| 57 |
+
m = min(w, h)
|
| 58 |
+
return img.crop(((w-m)//2, (h-m)//2, (w+m)//2, (h+m)//2)).resize((size, size), Image.LANCZOS)
|
| 59 |
+
|
| 60 |
+
subj_proc = prep(subject_image)
|
| 61 |
+
adap_proc = prep(adapter_image)
|
| 62 |
+
|
| 63 |
+
def b64(img):
|
| 64 |
+
buf = BytesIO(); img.save(buf, format="PNG")
|
| 65 |
+
return base64.b64encode(buf.getvalue()).decode()
|
| 66 |
+
|
| 67 |
+
payload = {
|
| 68 |
+
"prompt": prompt,
|
| 69 |
+
"subject_image": b64(subj_proc),
|
| 70 |
+
"adapter_image": b64(adap_proc),
|
| 71 |
+
"height": size, "width": size,
|
| 72 |
+
"steps": steps, "seed": seed,
|
| 73 |
+
"guidance_scale": guidance_scale, "rank": rank,
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
headers = {"Content-Type": "application/json"}
|
| 77 |
+
if BASETEN_API_KEY:
|
| 78 |
+
headers["Authorization"] = f"Api-Key {BASETEN_API_KEY}"
|
| 79 |
+
|
| 80 |
+
resp = requests.post(BASETEN_MODEL_URL, headers=headers, json=payload, timeout=120)
|
| 81 |
+
resp.raise_for_status()
|
| 82 |
+
|
| 83 |
+
if resp.headers.get("content-type", "").startswith("image/"):
|
| 84 |
+
raw_img = Image.open(BytesIO(resp.content))
|
| 85 |
+
else:
|
| 86 |
+
url = resp.json().get("image_url")
|
| 87 |
+
if not url:
|
| 88 |
+
raise gr.Error("Baseten response missing image data.")
|
| 89 |
+
raw_img = Image.open(BytesIO(requests.get(url, timeout=120).content))
|
| 90 |
+
|
| 91 |
+
return [[raw_img]], raw_img
|
| 92 |
+
|
| 93 |
+
# ββββββββββ Header HTML ββββββββββ
|
| 94 |
+
header_html = """
|
| 95 |
+
<h1>ZenCtrl Inpainting</h1>
|
| 96 |
+
<div align="center" style="line-height:1;">
|
| 97 |
+
<a href="https://discord.com/invite/b9RuYQ3F8k" target="_blank" style="margin:10px;">
|
| 98 |
+
<img src="https://img.shields.io/badge/Discord-Join-7289da.svg?logo=discord" alt="Discord">
|
| 99 |
+
</a>
|
| 100 |
+
<a href="https://fotographer.ai/zen-control" target="_blank" style="margin:10px;">
|
| 101 |
+
<img src="https://img.shields.io/badge/Website-Landing_Page-blue" alt="LP">
|
| 102 |
+
</a>
|
| 103 |
+
<a href="https://x.com/FotographerAI" target="_blank" style="margin:10px;">
|
| 104 |
+
<img src="https://img.shields.io/twitter/follow/FotographerAI?style=social" alt="X">
|
| 105 |
+
</a>
|
| 106 |
+
</div>
|
| 107 |
"""
|
| 108 |
|
| 109 |
+
# ββββββββββ Gradio UI ββββββββββ
|
| 110 |
+
with gr.Blocks(css=css, title="ZenCtrl Playground") 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.**
|
| 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 |
+
ref_img = gr.Image(type="pil", label="Background / Mask image", tool="sketch", brush_color="#00FF00", sources=["upload", "clipboard"])
|
| 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)
|
| 127 |
+
run_btn = gr.Button("Generate", variant="primary")
|
| 128 |
+
|
| 129 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 130 |
+
bgprompt = gr.Textbox(label="Background Prompt", value="Scandinavian living room β¦")
|
| 131 |
+
bg_btn = gr.Button("Generate BG")
|
| 132 |
+
|
| 133 |
+
with gr.Column(scale=2):
|
| 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 |
+
["examples/subject1.png", "examples/bg1.png", "Make the toy sit on a marble table", "examples/out1.png"],
|
| 140 |
+
["examples/subject2.png", "examples/bg2.png", "Turn the flowers into sunflowers", "examples/out2.png"],
|
| 141 |
+
["examples/subject3.png", "examples/bg3.png", "Make this monster ride a skateboard on the beach", "examples/out3.png"],
|
| 142 |
+
["examples/subject4.png", "examples/bg4.png", "Make this cat happy", "examples/out4.png"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
],
|
| 144 |
+
inputs=[subj_img, ref_img, promptbox],
|
| 145 |
+
outputs=[gallery],
|
| 146 |
+
fn=process_image_and_text,
|
| 147 |
+
examples_per_page="all",
|
| 148 |
+
label="Presets (Input Β· Background Β· Prompt Β· Output)",
|
| 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(lambda v: gr.update(visible=v), inputs=use_detect_ck, outputs=detect_box)
|
| 160 |
+
|
| 161 |
+
# ββββββββββ Launch ββββββββββ
|
| 162 |
+
demo.launch(show_api=False, share=True)
|