File size: 7,747 Bytes
f85986b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio_imageslider import ImageSlider
import os
from comfydeploy import ComfyDeploy
import requests
from PIL import Image
from io import BytesIO
from dotenv import load_dotenv
import base64
from typing import Optional, Tuple, Union
import glob

load_dotenv()

# Initialize ComfyDeploy client
client: ComfyDeploy = ComfyDeploy(bearer_auth=os.environ.get("COMFY_DEPLOY_API_KEY"))
deployment_id: str = os.environ.get("COMFY_DEPLOYMENT_ID")

# Add these global variables at the top of the file, after imports
global_input_image = None
global_image_slider = None


def clear_output():
    return None


def process_image(
    image: Optional[Union[str, Image.Image]],
    denoise: float,
    steps: int,
    tile_size: int,
    downscale: float,
    upscale: float,
    color_match: float,
    controlnet_tile_end: float,
    controlnet_tile_strength: float,
) -> Tuple[Optional[Image.Image], Optional[Image.Image]]:
    # Convert image to base64
    if image is not None:
        if isinstance(image, str):
            with open(image, "rb") as img_file:
                image_base64: str = base64.b64encode(img_file.read()).decode("utf-8")
        else:
            buffered: BytesIO = BytesIO()
            image.save(buffered, format="PNG")
            image_base64: str = base64.b64encode(buffered.getvalue()).decode("utf-8")
    else:
        return None, None

    # Prepare inputs
    inputs: dict = {
        "image": f"data:image/png;base64,{image_base64}",
        "denoise": str(denoise),
        "steps": str(steps),
        "tile_size": str(tile_size),
        "downscale": str(downscale),
        "upscale": str(upscale),
        "color_match": str(color_match),
        "controlnet_tile_end": str(controlnet_tile_end),
        "controlnet_tile_strength": str(controlnet_tile_strength),
    }

    # 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
            # Wait for the result
            while True:
                run_result = client.run.get(run_id=run_id)
                if run_result.object.status == "success":
                    for output in run_result.object.outputs:
                        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 image, processed_image
                    return None, None
                elif run_result.object.status == "failed":
                    return None, None
    except Exception as e:
        print(f"Error: {e}")
        return None, None


def run(
    denoise,
    steps,
    tile_size,
    downscale,
    upscale,
    color_match,
    controlnet_tile_end,
    controlnet_tile_strength,
):
    global global_input_image
    global global_image_slider

    if not global_input_image:
        return None

    # Set image_slider to None before processing
    global_image_slider = None

    # Process the image
    original, processed = process_image(
        global_input_image,
        denoise,
        steps,
        tile_size,
        downscale,
        upscale,
        color_match,
        controlnet_tile_end,
        controlnet_tile_strength,
    )

    if original and processed:
        global_image_slider = [original, processed]

    return global_image_slider


# Function to load preset images
def load_preset_images():
    image_files = glob.glob("images/inputs/*")
    return [
        {"name": img, "image": Image.open(img)}
        for img in image_files
        if Image.open(img).format.lower()
        in ["png", "jpg", "jpeg", "gif", "bmp", "webp"]
    ]


def set_input_image(images, evt: gr.SelectData):
    global global_input_image
    global_input_image = images[evt.index][0]
    return global_input_image


# Define Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# 🌊 Creative Image Upscaler")
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(
                type="pil",
                label="Input Image",
                value=lambda: global_input_image,
                interactive=True,
            )

            # Add preset images
            gr.Markdown("### Preset Images")
            preset_images = load_preset_images()
            gallery = gr.Gallery(
                [img["image"] for img in preset_images],
                label="Preset Images",
                columns=5,
                height=130,
                allow_preview=False,
            )
            gallery.select(set_input_image, gallery, input_image)

            with gr.Accordion("Advanced Parameters", open=False):
                denoise: gr.Slider = gr.Slider(0, 1, value=0.4, label="Denoise")
                steps: gr.Slider = gr.Slider(1, 40, value=10, step=1, label="Steps")
                tile_size: gr.Slider = gr.Slider(
                    64, 2048, value=1024, step=8, label="Tile Size"
                )
                downscale: gr.Slider = gr.Slider(
                    1, 4, value=1, step=1, label="Downscale"
                )
                upscale: gr.Slider = gr.Slider(1, 4, value=4, step=0.1, label="Upscale")
                color_match: gr.Slider = gr.Slider(0, 1, value=0, label="Color Match")
                controlnet_tile_end: gr.Slider = gr.Slider(
                    0, 1, value=1, label="ControlNet Tile End"
                )
                controlnet_tile_strength: gr.Slider = gr.Slider(
                    0, 1, value=0.7, label="ControlNet Tile Strength"
                )

        with gr.Column():
            image_slider = ImageSlider(
                label="Compare Original and Processed",
                type="pil",
                value=lambda: global_image_slider,
                interactive=True,
            )

            process_btn: gr.Button = gr.Button("Run")
            process_btn.click(
                fn=run,
                inputs=[
                    denoise,
                    steps,
                    tile_size,
                    downscale,
                    upscale,
                    color_match,
                    controlnet_tile_end,
                    controlnet_tile_strength,
                ],
                outputs=[image_slider],
            )

    def build_example(input_image_path):
        output_image_path = input_image_path.replace("inputs", "outputs")
        return [
            input_image_path,
            0.4,  # denoise
            10,  # steps
            1024,  # tile_size
            1,  # downscale
            4,  # upscale
            0,  # color_match
            1,  # controlnet_tile_end
            0.7,  # controlnet_tile_strength
            (input_image_path, output_image_path),
        ]

    # Build examples
    input_images = glob.glob("images/inputs/*")
    examples = [build_example(img) for img in input_images]

    # Update the gr.Examples call
    gr.Examples(
        examples=examples,
        inputs=[
            input_image,
            denoise,
            steps,
            tile_size,
            downscale,
            upscale,
            color_match,
            controlnet_tile_end,
            controlnet_tile_strength,
            image_slider,
        ],
    )

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