File size: 8,721 Bytes
9d1a9fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fd22de
 
 
 
 
9d1a9fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc5a588
 
 
2ba904e
dc5a588
9d1a9fc
 
 
 
 
 
 
dc5a588
 
 
 
 
9d1a9fc
 
 
 
 
 
 
dc5a588
 
 
 
 
9d1a9fc
 
 
 
 
 
 
dc5a588
 
 
 
 
9d1a9fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from typing import cast
from comfydeploy import ComfyDeploy
import asyncio
import os
import gradio as gr
from gradio.components.image_editor import EditorValue
from PIL import Image
import requests
import dotenv
from gradio_imageslider import ImageSlider
from io import BytesIO
import base64
import glob
import numpy as np

dotenv.load_dotenv()


API_KEY = os.environ.get("API_KEY")
DEPLOYMENT_ID = os.environ.get("DEPLOYMENT_ID", "DEPLOYMENT_ID_NOT_SET")

if not API_KEY:
    raise ValueError(
        "Please set API_KEY and DEPLOYMENT_ID in your environment variables"
    )
if DEPLOYMENT_ID == "DEPLOYMENT_ID_NOT_SET":
    raise ValueError("Please set DEPLOYMENT_ID in your environment variables")


client = ComfyDeploy(bearer_auth=API_KEY)


def get_base64_from_image(image: Image.Image) -> str:
    buffered: BytesIO = BytesIO()
    image.save(buffered, format="PNG")
    return base64.b64encode(buffered.getvalue()).decode("utf-8")


async def process_image(
    image: Image.Image | str | None,
    mask: Image.Image | str | None,
    progress: gr.Progress = gr.Progress(),
) -> Image.Image | None:
    progress(0, desc="Starting...")
    if image is None or mask is None:
        return None

    if isinstance(mask, str):
        mask = Image.open(mask)
    if isinstance(image, str):
        image = Image.open(image)

    image_base64 = get_base64_from_image(image)
    mask_base64 = get_base64_from_image(mask)

    # Prepare inputs
    inputs: dict = {
        "image": f"data:image/png;base64,{image_base64}",
        "mask": f"data:image/png;base64,{mask_base64}",
    }

    # Call ComfyDeploy API
    try:
        result = client.run.create(
            request={"deployment_id": DEPLOYMENT_ID, "inputs": inputs}
        )

        if result and result.object:
            run_id: str = result.object.run_id
            progress(0, desc="Starting processing...")
            # Wait for the result
            while True:
                run_result = client.run.get(run_id=run_id)
                if not run_result.object:
                    continue

                progress_value = (
                    run_result.object.progress
                    if run_result.object.progress is not None
                    else 0
                )
                status = (
                    run_result.object.live_status
                    if run_result.object.live_status is not None
                    else "Cold starting..."
                )
                progress(progress_value, desc=f"Status: {status}")

                if run_result.object.status == "success":
                    for output in run_result.object.outputs or []:
                        if output.data and output.data.images:
                            image_url: str = output.data.images[0].url
                            # Download and return both the original and processed images
                            response: requests.Response = requests.get(image_url)
                            processed_image: Image.Image = Image.open(
                                BytesIO(response.content)
                            )
                            return processed_image
                    return None
                elif run_result.object.status == "failed":
                    print("Processing failed")
                    return None

                await asyncio.sleep(2)  # Wait for 2 seconds before checking again
    except Exception as e:
        print(f"Error: {e}")
        return None


def resize(image: Image.Image, shortest_side: int = 768) -> Image.Image:
    if image.width <= shortest_side and image.height <= shortest_side:
        return image
    if image.width < image.height:
        return image.resize(
            size=(shortest_side, int(shortest_side * image.height / image.width))
        )
    return image.resize(
        size=(int(shortest_side * image.width / image.height), shortest_side)
    )


async def run_async(
    image_and_mask: EditorValue | None,
    progress: gr.Progress = gr.Progress(),
) -> tuple[Image.Image, Image.Image] | None:
    if not image_and_mask:
        return None

    alpha_channel = image_and_mask["layers"][0]
    alpha_channel = cast(np.ndarray, alpha_channel)
    mask_np = np.where(alpha_channel[:, :, 3] == 0, 0, 255).astype(np.uint8)

    image_np = image_and_mask["background"]
    image_np = cast(np.ndarray, image_np)

    # Save mask to ./masks.png
    mask = Image.fromarray(mask_np)
    mask = resize(mask)
    # mask.save("mask.png")

    # Save image to ./images.png
    image = Image.fromarray(image_np)
    image = resize(image)
    # image.save("image.png")

    output = await process_image(
        image,  # type: ignore
        mask,  # type: ignore
        progress,
    )

    if output is None:
        return None

    return output, image


def run_sync(*args):
    return asyncio.run(run_async(*args))


with gr.Blocks() as demo:
    gr.Markdown("""
                # 🧹 Room Cleaner
                Upload an image and and use pen tool (pencil icon at the bottom) to mark the areas you want to remove.
                Click on the "Run" button to process the image and remove the marked areas.
                """)
    with gr.Row():
        with gr.Column():
            # The image overflow, fix
            image_and_mask = gr.ImageMask(
                label="Input Image and Mask",
                layers=False,
                show_fullscreen_button=False,
                sources=["upload"],
                show_download_button=False,
                interactive=True,
                height="full",
                width="full",
            )

        with gr.Column():
            image_slider = ImageSlider(
                label="Compare Original and Processed",
                interactive=False,
            )

            process_btn = gr.ClearButton(
                value="Run",
                variant="primary",
                size="lg",
                components=[image_slider],
            )
            process_btn.click(
                fn=run_sync,
                inputs=[
                    image_and_mask,
                ],
                outputs=[image_slider],
                api_name=False,
            )

    # Build examples
    images_examples = glob.glob("examples/*")
    mask_examples = [img.replace("inputs", "masks") for img in images_examples]
    output_examples = [img.replace("inputs", "outputs") for img in images_examples]
    # examples = [
    #     [
    #         img,
    #         mask,
    #         (img, out),
    #     ]
    #     for img, mask, out in zip(images_examples, mask_examples, output_examples)
    # ]
    examples = [
        [
            {
                "background": "./examples/ex1.jpg",
                "layers": [],
                "composite": "./examples/ex1_mask.png",
            },
            # ("./examples/ex1.jpg", "./examples/ex1_result.png"),
            (
                "https://dropshare.blanchon.xyz/public/dropshare/ex1.jpg",
                "https://dropshare.blanchon.xyz/public/dropshare/ex1_results.png",
            ),
        ],
        [
            {
                "background": "./examples/ex2.jpg",
                "layers": [],
                "composite": "./examples/ex2_mask.png",
            },
            # ("./examples/ex2.jpg", "./examples/ex2_result.png"),
            (
                "https://dropshare.blanchon.xyz/public/dropshare/ex2.jpg",
                "https://dropshare.blanchon.xyz/public/dropshare/ex2_result.png",
            ),
        ],
        [
            {
                "background": "./examples/ex3.jpg",
                "layers": [],
                "composite": "./examples/ex3_mask.png",
            },
            # ("./examples/ex3.jpg", "./examples/ex3_result.png"),
            (
                "https://dropshare.blanchon.xyz/public/dropshare/ex3.jpg",
                "https://dropshare.blanchon.xyz/public/dropshare/ex3_result.png",
            ),
        ],
        [
            {
                "background": "./examples/ex4.jpg",
                "layers": [],
                "composite": "./examples/ex4_mask.png",
            },
            # ("./examples/ex4.jpg", "./examples/ex4_result.png"),
            (
                "https://dropshare.blanchon.xyz/public/dropshare/ex4.jpg",
                "https://dropshare.blanchon.xyz/public/dropshare/ex4_result.png",
            ),
        ],
    ]

    # Update the gr.Examples call
    gr.Examples(
        examples=examples,
        inputs=[
            image_and_mask,
            image_slider,
        ],
        api_name=False,
    )

if __name__ == "__main__":
    demo.launch(debug=True, share=True)