salso commited on
Commit
668b8e4
·
verified ·
1 Parent(s): 0e5a0f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -76
app.py CHANGED
@@ -8,20 +8,15 @@ from PIL import Image, ImageDraw
8
  import gradio as gr
9
 
10
  # ────────── Secrets & endpoints ──────────
11
- BASETEN_MODEL_URL = "https://app.baseten.co/models/YOUR_MODEL_ID/predict"
12
  BASETEN_API_KEY = os.getenv("BASETEN_API_KEY")
13
  REPLICATE_TOKEN = os.getenv("REPLICATE_API_TOKEN")
14
 
15
- from florence_sam.detect_and_segment import fill_detected_bboxes
16
-
17
  # ────────── Globals ──────────
18
- ADAPTER_NAME = "inpaint"
19
  ADAPTER_SIZE = 1024
20
- model_config = dict(union_cond_attn=True, add_cond_attn=False,
21
- latent_lora=False, independent_condition=False)
22
  css = "#col-container {margin:0 auto; max-width:960px;}"
23
 
24
- #Background prompt via Replicate
25
  def _gen_bg(prompt: str):
26
  url = replicate.run(
27
  "google/imagen-4-fast",
@@ -30,48 +25,43 @@ def _gen_bg(prompt: str):
30
  url = url[0] if isinstance(url, list) else url
31
  return Image.open(BytesIO(requests.get(url, timeout=120).content)).convert("RGB")
32
 
33
- #@spaces.GPU
34
- def process_image_and_text(subject_image, adapter_dict, prompt, use_detect, detect_prompt, size=ADAPTER_SIZE, rank=10.0):
35
  seed, guidance_scale, steps = 42, 2.5, 28
36
 
37
- if use_detect:
38
- base_img = adapter_dict["image"] if isinstance(adapter_dict, dict) else adapter_dict
39
- if base_img is None:
40
- raise gr.Error("Upload a background image first.")
41
- adapter_image, _ = fill_detected_bboxes(
42
- image=base_img, text=detect_prompt,
43
- inflate_pct=0.15, fill_color="#00FF00"
44
- )
45
- else:
46
- adapter_image = adapter_dict["image"] if isinstance(adapter_dict, dict) else adapter_dict
47
- if isinstance(adapter_dict, dict) and adapter_dict.get("mask") is not None:
48
- m = adapter_dict["mask"].convert("L").point(lambda p: 255 if p else 0)
49
- if bbox := m.getbbox():
50
- rect = Image.new("L", m.size, 0)
51
- ImageDraw.Draw(rect).rectangle(bbox, fill=255)
52
- m = rect
53
- green = Image.new("RGB", adapter_image.size, "#00FF00")
54
- adapter_image = Image.composite(green, adapter_image, m)
55
 
56
  def prep(img: Image.Image):
57
  w, h = img.size
58
  m = min(w, h)
59
- return img.crop(((w-m)//2, (h-m)//2, (w+m)//2, (h+m)//2)).resize((size, size), Image.LANCZOS)
60
 
61
  subj_proc = prep(subject_image)
62
  adap_proc = prep(adapter_image)
63
 
64
  def b64(img):
65
- buf = BytesIO(); img.save(buf, format="PNG")
 
66
  return base64.b64encode(buf.getvalue()).decode()
67
 
68
  payload = {
69
  "prompt": prompt,
70
  "subject_image": b64(subj_proc),
71
  "adapter_image": b64(adap_proc),
72
- "height": size, "width": size,
73
- "steps": steps, "seed": seed,
74
- "guidance_scale": guidance_scale, "rank": rank,
 
 
 
75
  }
76
 
77
  headers = {"Content-Type": "application/json"}
@@ -121,53 +111,30 @@ with gr.Blocks(css=css, title="ZenCtrl Inpainting") as demo:
121
  with gr.Row():
122
  with gr.Column(scale=2, elem_id="col-container"):
123
  subj_img = gr.Image(type="pil", label="Subject image")
 
 
124
 
125
- # Visible sketch component (what the user draws on)
126
- ref_img = gr.Image(
127
- type="pil", label="Background / Mask image",
128
- tool="sketch", brush_color="#00FF00"
129
- )
130
-
131
- # ⚑ Hidden plain-image component used ONLY for examples
132
- ref_img_ex = gr.Image(
133
- type="pil", visible=False
134
- )
135
-
136
- use_detect_ck = gr.Checkbox(False, label="Detect with Florence-SAM")
137
- detect_box = gr.Textbox(label="Detection prompt", value="person, chair", visible=False)
138
- promptbox = gr.Textbox(label="Generation prompt", value="furniture", lines=2)
139
- run_btn = gr.Button("Generate", variant="primary")
140
 
141
  with gr.Accordion("Advanced Settings", open=False):
142
  bgprompt = gr.Textbox(label="Background Prompt", value="Scandinavian living room …")
143
- bg_btn = gr.Button("Generate BG")
144
 
145
  with gr.Column(scale=2):
146
  gallery = gr.Gallery(columns=[1], rows=[1], object_fit="contain", height="auto")
147
- bg_img = gr.Image(label="Background", visible=False)
148
 
149
  # ---------- Example wrapper ---------------------------------
150
- #from PIL import Image
151
  def _run_example(subj, bg, prompt):
152
- # 1️⃣ Ensure we have PIL.Image objects
153
  if isinstance(subj, str):
154
  subj = Image.open(subj)
155
  if isinstance(bg, str):
156
  bg = Image.open(bg)
157
-
158
- # 2️⃣ Wrap background for pipeline (no mask)
159
  adapter_dict = {"image": bg, "mask": None}
160
-
161
- # 3️⃣ Call your real pipeline
162
- gallery_out, _ = process_image_and_text(
163
- subj,
164
- adapter_dict,
165
- prompt,
166
- use_detect=False,
167
- detect_prompt=""
168
- )
169
-
170
- # 4️⃣ Also show the bg in the visible sketch component so user can draw
171
  return gallery_out, gr.update(value=bg)
172
 
173
  # ---------- Examples ----------------------------------------
@@ -178,8 +145,8 @@ with gr.Blocks(css=css, title="ZenCtrl Inpainting") as demo:
178
  ["examples/subject1.png", "examples/subject1.png", "Make this monster ride a skateboard on the beach"],
179
  ["examples/subject1.png", "examples/subject1.png", "Make this cat happy"],
180
  ],
181
- inputs=[subj_img, ref_img_ex, promptbox], # <-- uses the hidden plain image
182
- outputs=[gallery, ref_img], # update gallery + visible sketch img
183
  fn=_run_example,
184
  examples_per_page="all",
185
  label="Presets (Input · Background · Prompt)",
@@ -189,19 +156,11 @@ with gr.Blocks(css=css, title="ZenCtrl Inpainting") as demo:
189
  # ---------- Buttons & interactions --------------------------
190
  run_btn.click(
191
  process_image_and_text,
192
- inputs=[subj_img, ref_img, promptbox, use_detect_ck, detect_box],
193
  outputs=[gallery, raw_state]
194
  )
195
  bg_btn.click(_gen_bg, inputs=[bgprompt], outputs=[bg_img])
196
- use_detect_ck.change(
197
- lambda v: gr.update(visible=v), inputs=use_detect_ck, outputs=detect_box
198
- )
199
 
200
  # ---------------- Launch ---------------------------------------
201
-
202
  if __name__ == "__main__":
203
- #init_pipeline()
204
- demo.launch(
205
- debug=True,
206
- share=True
207
- )
 
8
  import gradio as gr
9
 
10
  # ────────── Secrets & endpoints ──────────
11
+ BASETEN_MODEL_URL = "https://app.baseten.co/models/YOUR_MODEL_ID/predict"
12
  BASETEN_API_KEY = os.getenv("BASETEN_API_KEY")
13
  REPLICATE_TOKEN = os.getenv("REPLICATE_API_TOKEN")
14
 
 
 
15
  # ────────── Globals ──────────
 
16
  ADAPTER_SIZE = 1024
 
 
17
  css = "#col-container {margin:0 auto; max-width:960px;}"
18
 
19
+ # Background generation via Replicate
20
  def _gen_bg(prompt: str):
21
  url = replicate.run(
22
  "google/imagen-4-fast",
 
25
  url = url[0] if isinstance(url, list) else url
26
  return Image.open(BytesIO(requests.get(url, timeout=120).content)).convert("RGB")
27
 
28
+ # Main processing function
29
+ def process_image_and_text(subject_image, adapter_dict, prompt, _unused1, _unused2, size=ADAPTER_SIZE, rank=10.0):
30
  seed, guidance_scale, steps = 42, 2.5, 28
31
 
32
+ adapter_image = adapter_dict["image"] if isinstance(adapter_dict, dict) else adapter_dict
33
+ if isinstance(adapter_dict, dict) and adapter_dict.get("mask") is not None:
34
+ m = adapter_dict["mask"].convert("L").point(lambda p: 255 if p else 0)
35
+ if bbox := m.getbbox():
36
+ rect = Image.new("L", m.size, 0)
37
+ ImageDraw.Draw(rect).rectangle(bbox, fill=255)
38
+ m = rect
39
+ green = Image.new("RGB", adapter_image.size, "#00FF00")
40
+ adapter_image = Image.composite(green, adapter_image, m)
 
 
 
 
 
 
 
 
 
41
 
42
  def prep(img: Image.Image):
43
  w, h = img.size
44
  m = min(w, h)
45
+ return img.crop(((w - m) // 2, (h - m) // 2, (w + m) // 2, (h + m) // 2)).resize((size, size), Image.LANCZOS)
46
 
47
  subj_proc = prep(subject_image)
48
  adap_proc = prep(adapter_image)
49
 
50
  def b64(img):
51
+ buf = BytesIO()
52
+ img.save(buf, format="PNG")
53
  return base64.b64encode(buf.getvalue()).decode()
54
 
55
  payload = {
56
  "prompt": prompt,
57
  "subject_image": b64(subj_proc),
58
  "adapter_image": b64(adap_proc),
59
+ "height": size,
60
+ "width": size,
61
+ "steps": steps,
62
+ "seed": seed,
63
+ "guidance_scale": guidance_scale,
64
+ "rank": rank,
65
  }
66
 
67
  headers = {"Content-Type": "application/json"}
 
111
  with gr.Row():
112
  with gr.Column(scale=2, elem_id="col-container"):
113
  subj_img = gr.Image(type="pil", label="Subject image")
114
+ ref_img = gr.Image(type="pil", label="Background / Mask image", tool="sketch", brush_color="#00FF00")
115
+ ref_img_ex = gr.Image(type="pil", visible=False)
116
 
117
+ # Removed Florence-SAM
118
+ promptbox = gr.Textbox(label="Generation prompt", value="furniture", lines=2)
119
+ run_btn = gr.Button("Generate", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  with gr.Accordion("Advanced Settings", open=False):
122
  bgprompt = gr.Textbox(label="Background Prompt", value="Scandinavian living room …")
123
+ bg_btn = gr.Button("Generate BG")
124
 
125
  with gr.Column(scale=2):
126
  gallery = gr.Gallery(columns=[1], rows=[1], object_fit="contain", height="auto")
127
+ bg_img = gr.Image(label="Background", visible=False)
128
 
129
  # ---------- Example wrapper ---------------------------------
 
130
  def _run_example(subj, bg, prompt):
 
131
  if isinstance(subj, str):
132
  subj = Image.open(subj)
133
  if isinstance(bg, str):
134
  bg = Image.open(bg)
135
+
 
136
  adapter_dict = {"image": bg, "mask": None}
137
+ gallery_out, _ = process_image_and_text(subj, adapter_dict, prompt, False, "")
 
 
 
 
 
 
 
 
 
 
138
  return gallery_out, gr.update(value=bg)
139
 
140
  # ---------- Examples ----------------------------------------
 
145
  ["examples/subject1.png", "examples/subject1.png", "Make this monster ride a skateboard on the beach"],
146
  ["examples/subject1.png", "examples/subject1.png", "Make this cat happy"],
147
  ],
148
+ inputs=[subj_img, ref_img_ex, promptbox],
149
+ outputs=[gallery, ref_img],
150
  fn=_run_example,
151
  examples_per_page="all",
152
  label="Presets (Input · Background · Prompt)",
 
156
  # ---------- Buttons & interactions --------------------------
157
  run_btn.click(
158
  process_image_and_text,
159
+ inputs=[subj_img, ref_img, promptbox, gr.State(False), gr.State("")],
160
  outputs=[gallery, raw_state]
161
  )
162
  bg_btn.click(_gen_bg, inputs=[bgprompt], outputs=[bg_img])
 
 
 
163
 
164
  # ---------------- Launch ---------------------------------------
 
165
  if __name__ == "__main__":
166
+ demo.launch(debug=True, share=True)