Spaces:
Runtime error
Runtime error
Update Room Cleaner instructions and optimize image loading
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
|
|
| 1 |
from typing import cast
|
| 2 |
from comfydeploy import ComfyDeploy
|
| 3 |
-
import asyncio
|
| 4 |
import os
|
| 5 |
import gradio as gr
|
| 6 |
from gradio.components.image_editor import EditorValue
|
|
@@ -19,10 +19,8 @@ API_KEY = os.environ.get("API_KEY")
|
|
| 19 |
DEPLOYMENT_ID = os.environ.get("DEPLOYMENT_ID", "DEPLOYMENT_ID_NOT_SET")
|
| 20 |
|
| 21 |
if not API_KEY:
|
| 22 |
-
raise ValueError(
|
| 23 |
-
|
| 24 |
-
)
|
| 25 |
-
if DEPLOYMENT_ID == "DEPLOYMENT_ID_NOT_SET":
|
| 26 |
raise ValueError("Please set DEPLOYMENT_ID in your environment variables")
|
| 27 |
|
| 28 |
|
|
@@ -35,12 +33,12 @@ def get_base64_from_image(image: Image.Image) -> str:
|
|
| 35 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 36 |
|
| 37 |
|
| 38 |
-
|
| 39 |
image: Image.Image | str | None,
|
| 40 |
mask: Image.Image | str | None,
|
| 41 |
progress: gr.Progress = gr.Progress(),
|
| 42 |
) -> Image.Image | None:
|
| 43 |
-
progress(0, desc="
|
| 44 |
if image is None or mask is None:
|
| 45 |
return None
|
| 46 |
|
|
@@ -100,7 +98,7 @@ async def process_image(
|
|
| 100 |
print("Processing failed")
|
| 101 |
return None
|
| 102 |
|
| 103 |
-
|
| 104 |
except Exception as e:
|
| 105 |
print(f"Error: {e}")
|
| 106 |
return None
|
|
@@ -141,7 +139,7 @@ def resize_image(img: Image.Image, min_side_length: int = 768) -> Image.Image:
|
|
| 141 |
return img.resize((new_width, min_side_length))
|
| 142 |
|
| 143 |
|
| 144 |
-
async def
|
| 145 |
image_and_mask: EditorValue | None,
|
| 146 |
progress: gr.Progress = gr.Progress(),
|
| 147 |
) -> tuple[Image.Image, Image.Image] | None:
|
|
@@ -172,7 +170,7 @@ async def run_async(
|
|
| 172 |
image = Image.fromarray(image_np)
|
| 173 |
image = resize_image(image)
|
| 174 |
|
| 175 |
-
output =
|
| 176 |
image, # type: ignore
|
| 177 |
mask, # type: ignore
|
| 178 |
progress,
|
|
@@ -181,32 +179,30 @@ async def run_async(
|
|
| 181 |
if output is None:
|
| 182 |
gr.Info("Processing failed")
|
| 183 |
return None
|
| 184 |
-
|
| 185 |
return image, output
|
| 186 |
|
| 187 |
|
| 188 |
-
def run_sync(*args):
|
| 189 |
-
return asyncio.run(run_async(*args))
|
| 190 |
-
|
| 191 |
-
|
| 192 |
with gr.Blocks() as demo:
|
| 193 |
gr.HTML("""
|
| 194 |
<div style="display: flex; justify-content: center; text-align:center; flex-direction: column;">
|
| 195 |
-
<h1>🧹 Room Cleaner</h1>
|
| 196 |
-
<div>
|
| 197 |
-
<p>Upload an image and use the pencil tool (✏️ icon at the bottom) to <b>mark the areas you want to remove</b>.</p>
|
| 198 |
-
<p
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
| 201 |
<br>
|
| 202 |
<video width="640" height="360" controls style="margin: 0 auto; border-radius: 10px;">
|
| 203 |
<source src="https://dropshare.blanchon.xyz/public/dropshare/room_cleaner_demo.mp4" type="video/mp4">
|
| 204 |
</video>
|
| 205 |
<br>
|
| 206 |
-
<p>Finally, click on the <b>"Run"</b> button to process the image.</p>
|
| 207 |
-
<p>Wait for the processing to complete and compare the original and processed images using the slider.</p>
|
| 208 |
|
| 209 |
-
<p>⚠️ Note that the images are compressed to reduce the workloads of the demo. </p>
|
| 210 |
</div>
|
| 211 |
<div style="margin-top: 20px; display: flex; justify-content: center; gap: 10px;">
|
| 212 |
<a href="https://x.com/JulienBlanchon">
|
|
@@ -246,13 +242,33 @@ with gr.Blocks() as demo:
|
|
| 246 |
size="lg",
|
| 247 |
components=[image_slider],
|
| 248 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
process_btn.click(
|
| 250 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
inputs=[
|
| 252 |
image_and_mask,
|
| 253 |
],
|
| 254 |
outputs=[image_slider],
|
| 255 |
api_name=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
)
|
| 257 |
|
| 258 |
example1 = make_example("./examples/ex1.jpg", "./examples/ex1_mask_only.png")
|
|
|
|
| 1 |
+
import time
|
| 2 |
from typing import cast
|
| 3 |
from comfydeploy import ComfyDeploy
|
|
|
|
| 4 |
import os
|
| 5 |
import gradio as gr
|
| 6 |
from gradio.components.image_editor import EditorValue
|
|
|
|
| 19 |
DEPLOYMENT_ID = os.environ.get("DEPLOYMENT_ID", "DEPLOYMENT_ID_NOT_SET")
|
| 20 |
|
| 21 |
if not API_KEY:
|
| 22 |
+
raise ValueError("Please set API_KEY in your environment variables")
|
| 23 |
+
if not DEPLOYMENT_ID or DEPLOYMENT_ID == "DEPLOYMENT_ID_NOT_SET":
|
|
|
|
|
|
|
| 24 |
raise ValueError("Please set DEPLOYMENT_ID in your environment variables")
|
| 25 |
|
| 26 |
|
|
|
|
| 33 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 34 |
|
| 35 |
|
| 36 |
+
def process_image(
|
| 37 |
image: Image.Image | str | None,
|
| 38 |
mask: Image.Image | str | None,
|
| 39 |
progress: gr.Progress = gr.Progress(),
|
| 40 |
) -> Image.Image | None:
|
| 41 |
+
progress(0, desc="Preparing inputs...")
|
| 42 |
if image is None or mask is None:
|
| 43 |
return None
|
| 44 |
|
|
|
|
| 98 |
print("Processing failed")
|
| 99 |
return None
|
| 100 |
|
| 101 |
+
time.sleep(1) # Wait for 1 second before checking the status again
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Error: {e}")
|
| 104 |
return None
|
|
|
|
| 139 |
return img.resize((new_width, min_side_length))
|
| 140 |
|
| 141 |
|
| 142 |
+
async def process(
|
| 143 |
image_and_mask: EditorValue | None,
|
| 144 |
progress: gr.Progress = gr.Progress(),
|
| 145 |
) -> tuple[Image.Image, Image.Image] | None:
|
|
|
|
| 170 |
image = Image.fromarray(image_np)
|
| 171 |
image = resize_image(image)
|
| 172 |
|
| 173 |
+
output = process_image(
|
| 174 |
image, # type: ignore
|
| 175 |
mask, # type: ignore
|
| 176 |
progress,
|
|
|
|
| 179 |
if output is None:
|
| 180 |
gr.Info("Processing failed")
|
| 181 |
return None
|
| 182 |
+
progress(100, desc="Processing completed")
|
| 183 |
return image, output
|
| 184 |
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
with gr.Blocks() as demo:
|
| 187 |
gr.HTML("""
|
| 188 |
<div style="display: flex; justify-content: center; text-align:center; flex-direction: column;">
|
| 189 |
+
<h1 style="color: #333;">🧹 Room Cleaner</h1>
|
| 190 |
+
<div style="max-width: 800px; margin: 0 auto;">
|
| 191 |
+
<p style="font-size: 16px;">Upload an image and use the pencil tool (✏️ icon at the bottom) to <b>mark the areas you want to remove</b>.</p>
|
| 192 |
+
<p style="font-size: 16px;">
|
| 193 |
+
For best results, include the shadows and reflections of the objects you want to remove.
|
| 194 |
+
You can remove multiple objects at once.
|
| 195 |
+
If you forget to mask some parts of your object, it's likely that the model will reconstruct them.
|
| 196 |
+
</p>
|
| 197 |
<br>
|
| 198 |
<video width="640" height="360" controls style="margin: 0 auto; border-radius: 10px;">
|
| 199 |
<source src="https://dropshare.blanchon.xyz/public/dropshare/room_cleaner_demo.mp4" type="video/mp4">
|
| 200 |
</video>
|
| 201 |
<br>
|
| 202 |
+
<p style="font-size: 16px;">Finally, click on the <b>"Run"</b> button to process the image.</p>
|
| 203 |
+
<p style="font-size: 16px;">Wait for the processing to complete and compare the original and processed images using the slider.</p>
|
| 204 |
|
| 205 |
+
<p style="font-size: 16px;">⚠️ Note that the images are compressed to reduce the workloads of the demo. </p>
|
| 206 |
</div>
|
| 207 |
<div style="margin-top: 20px; display: flex; justify-content: center; gap: 10px;">
|
| 208 |
<a href="https://x.com/JulienBlanchon">
|
|
|
|
| 242 |
size="lg",
|
| 243 |
components=[image_slider],
|
| 244 |
)
|
| 245 |
+
|
| 246 |
+
# image_slider.change(
|
| 247 |
+
# fn=on_change_prompt,
|
| 248 |
+
# inputs=[
|
| 249 |
+
# image_slider,
|
| 250 |
+
# ],
|
| 251 |
+
# outputs=[process_btn],
|
| 252 |
+
# api_name=False,
|
| 253 |
+
# )
|
| 254 |
+
|
| 255 |
process_btn.click(
|
| 256 |
+
fn=lambda _: gr.update(interactive=False, value="Processing..."),
|
| 257 |
+
inputs=[],
|
| 258 |
+
outputs=[process_btn],
|
| 259 |
+
api_name=False,
|
| 260 |
+
).then(
|
| 261 |
+
fn=process,
|
| 262 |
inputs=[
|
| 263 |
image_and_mask,
|
| 264 |
],
|
| 265 |
outputs=[image_slider],
|
| 266 |
api_name=False,
|
| 267 |
+
).then(
|
| 268 |
+
fn=lambda _: gr.update(interactive=True, value="Run"),
|
| 269 |
+
inputs=[],
|
| 270 |
+
outputs=[process_btn],
|
| 271 |
+
api_name=False,
|
| 272 |
)
|
| 273 |
|
| 274 |
example1 = make_example("./examples/ex1.jpg", "./examples/ex1_mask_only.png")
|