Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -124,7 +124,7 @@ def _gpu_process(
|
|
124 |
def _process(
|
125 |
img: Image.Image,
|
126 |
prompt: str | BoundingBox | None,
|
127 |
-
) -> tuple[tuple[Image.Image, Image.Image],
|
128 |
# enforce max dimensions for pymatting performance reasons
|
129 |
if img.width > 2048 or img.height > 2048:
|
130 |
orig_res = max(img.width, img.height)
|
@@ -150,10 +150,11 @@ def _process(
|
|
150 |
to_dl.save(temp, format="PNG")
|
151 |
temp.close()
|
152 |
|
153 |
-
|
|
|
154 |
|
155 |
|
156 |
-
def process_bbox(prompts: dict[str, Any]) -> tuple[tuple[Image.Image, Image.Image],
|
157 |
assert isinstance(img := prompts["image"], Image.Image)
|
158 |
assert isinstance(boxes := prompts["boxes"], list)
|
159 |
if len(boxes) == 1:
|
@@ -169,7 +170,7 @@ def on_change_bbox(prompts: dict[str, Any] | None):
|
|
169 |
return gr.update(interactive=prompts is not None)
|
170 |
|
171 |
|
172 |
-
def process_prompt(img: Image.Image, prompt: str) -> tuple[tuple[Image.Image, Image.Image],
|
173 |
return _process(img, prompt)
|
174 |
|
175 |
|
@@ -177,6 +178,14 @@ def on_change_prompt(img: Image.Image | None, prompt: str | None):
|
|
177 |
return gr.update(interactive=bool(img and prompt))
|
178 |
|
179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
css = """
|
181 |
footer {
|
182 |
visibility: hidden;
|
@@ -194,20 +203,26 @@ with gr.Blocks(css=css) as demo:
|
|
194 |
btn = gr.Button("Cut Out Object", interactive=False)
|
195 |
with gr.Column():
|
196 |
oimg = ImageSlider(label="Before / After", show_download_button=False, interactive=False)
|
|
|
|
|
197 |
dlbt = gr.DownloadButton("Download Cutout", interactive=False)
|
198 |
|
199 |
-
# btn.add(oimg) <- 제거됨
|
200 |
-
|
201 |
for inp in [iimg, prompt]:
|
202 |
inp.change(
|
203 |
fn=on_change_prompt,
|
204 |
inputs=[iimg, prompt],
|
205 |
outputs=[btn],
|
206 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
btn.click(
|
208 |
-
fn=
|
209 |
inputs=[iimg, prompt],
|
210 |
-
outputs=[oimg, dlbt],
|
211 |
api_name=False,
|
212 |
)
|
213 |
|
@@ -230,12 +245,13 @@ with gr.Blocks(css=css) as demo:
|
|
230 |
],
|
231 |
]
|
232 |
|
|
|
233 |
ex = gr.Examples(
|
234 |
examples=examples,
|
235 |
inputs=[iimg, prompt],
|
236 |
-
outputs=[oimg,
|
237 |
fn=process_prompt,
|
238 |
-
cache_examples=
|
239 |
)
|
240 |
|
241 |
with gr.Tab("By bounding box", id="tab_bb"):
|
@@ -249,22 +265,27 @@ with gr.Blocks(css=css) as demo:
|
|
249 |
single_box=True,
|
250 |
label="Input",
|
251 |
)
|
252 |
-
|
253 |
with gr.Column():
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
# btn.add(oimg) <- 제거됨
|
258 |
|
259 |
annotator.change(
|
260 |
fn=on_change_bbox,
|
261 |
inputs=[annotator],
|
262 |
-
outputs=[
|
263 |
)
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
inputs=[annotator],
|
267 |
-
outputs=[
|
268 |
api_name=False,
|
269 |
)
|
270 |
|
@@ -287,14 +308,14 @@ with gr.Blocks(css=css) as demo:
|
|
287 |
},
|
288 |
]
|
289 |
|
290 |
-
|
291 |
examples=examples,
|
292 |
inputs=[annotator],
|
293 |
-
outputs=[
|
294 |
fn=process_bbox,
|
295 |
-
cache_examples=
|
296 |
)
|
297 |
|
298 |
|
299 |
demo.queue(max_size=30, api_open=False)
|
300 |
-
demo.launch(show_api=False)
|
|
|
124 |
def _process(
|
125 |
img: Image.Image,
|
126 |
prompt: str | BoundingBox | None,
|
127 |
+
) -> tuple[tuple[Image.Image, Image.Image], str]: # Changed return type
|
128 |
# enforce max dimensions for pymatting performance reasons
|
129 |
if img.width > 2048 or img.height > 2048:
|
130 |
orig_res = max(img.width, img.height)
|
|
|
150 |
to_dl.save(temp, format="PNG")
|
151 |
temp.close()
|
152 |
|
153 |
+
# Return the file path instead of a DownloadButton
|
154 |
+
return (img, masked_rgb), temp.name
|
155 |
|
156 |
|
157 |
+
def process_bbox(prompts: dict[str, Any]) -> tuple[tuple[Image.Image, Image.Image], str]:
|
158 |
assert isinstance(img := prompts["image"], Image.Image)
|
159 |
assert isinstance(boxes := prompts["boxes"], list)
|
160 |
if len(boxes) == 1:
|
|
|
170 |
return gr.update(interactive=prompts is not None)
|
171 |
|
172 |
|
173 |
+
def process_prompt(img: Image.Image, prompt: str) -> tuple[tuple[Image.Image, Image.Image], str]:
|
174 |
return _process(img, prompt)
|
175 |
|
176 |
|
|
|
178 |
return gr.update(interactive=bool(img and prompt))
|
179 |
|
180 |
|
181 |
+
# Function to update download button with file path
|
182 |
+
def update_download_button(file_path: str | None) -> gr.update:
|
183 |
+
if file_path:
|
184 |
+
return gr.update(value=file_path, interactive=True)
|
185 |
+
else:
|
186 |
+
return gr.update(value=None, interactive=False)
|
187 |
+
|
188 |
+
|
189 |
css = """
|
190 |
footer {
|
191 |
visibility: hidden;
|
|
|
203 |
btn = gr.Button("Cut Out Object", interactive=False)
|
204 |
with gr.Column():
|
205 |
oimg = ImageSlider(label="Before / After", show_download_button=False, interactive=False)
|
206 |
+
# Use a File component for download instead of DownloadButton
|
207 |
+
file_output = gr.File(label="Download Cutout", visible=False)
|
208 |
dlbt = gr.DownloadButton("Download Cutout", interactive=False)
|
209 |
|
|
|
|
|
210 |
for inp in [iimg, prompt]:
|
211 |
inp.change(
|
212 |
fn=on_change_prompt,
|
213 |
inputs=[iimg, prompt],
|
214 |
outputs=[btn],
|
215 |
)
|
216 |
+
|
217 |
+
# Process and update both outputs
|
218 |
+
def process_and_update_prompt(img, prompt):
|
219 |
+
result, file_path = process_prompt(img, prompt)
|
220 |
+
return result, file_path, update_download_button(file_path)
|
221 |
+
|
222 |
btn.click(
|
223 |
+
fn=process_and_update_prompt,
|
224 |
inputs=[iimg, prompt],
|
225 |
+
outputs=[oimg, file_output, dlbt],
|
226 |
api_name=False,
|
227 |
)
|
228 |
|
|
|
245 |
],
|
246 |
]
|
247 |
|
248 |
+
# Don't cache examples to avoid the error
|
249 |
ex = gr.Examples(
|
250 |
examples=examples,
|
251 |
inputs=[iimg, prompt],
|
252 |
+
outputs=[oimg, file_output], # Remove dlbt from outputs
|
253 |
fn=process_prompt,
|
254 |
+
cache_examples=False, # Disable caching
|
255 |
)
|
256 |
|
257 |
with gr.Tab("By bounding box", id="tab_bb"):
|
|
|
265 |
single_box=True,
|
266 |
label="Input",
|
267 |
)
|
268 |
+
btn2 = gr.Button("Cut Out Object", interactive=False)
|
269 |
with gr.Column():
|
270 |
+
oimg2 = ImageSlider(label="Before / After", show_download_button=False)
|
271 |
+
file_output2 = gr.File(label="Download Cutout", visible=False)
|
272 |
+
dlbt2 = gr.DownloadButton("Download Cutout", interactive=False)
|
|
|
273 |
|
274 |
annotator.change(
|
275 |
fn=on_change_bbox,
|
276 |
inputs=[annotator],
|
277 |
+
outputs=[btn2],
|
278 |
)
|
279 |
+
|
280 |
+
# Process and update both outputs
|
281 |
+
def process_and_update_bbox(prompts):
|
282 |
+
result, file_path = process_bbox(prompts)
|
283 |
+
return result, file_path, update_download_button(file_path)
|
284 |
+
|
285 |
+
btn2.click(
|
286 |
+
fn=process_and_update_bbox,
|
287 |
inputs=[annotator],
|
288 |
+
outputs=[oimg2, file_output2, dlbt2],
|
289 |
api_name=False,
|
290 |
)
|
291 |
|
|
|
308 |
},
|
309 |
]
|
310 |
|
311 |
+
ex2 = gr.Examples(
|
312 |
examples=examples,
|
313 |
inputs=[annotator],
|
314 |
+
outputs=[oimg2, file_output2], # Remove dlbt2 from outputs
|
315 |
fn=process_bbox,
|
316 |
+
cache_examples=False, # Disable caching
|
317 |
)
|
318 |
|
319 |
|
320 |
demo.queue(max_size=30, api_open=False)
|
321 |
+
demo.launch(show_api=False)
|