File size: 8,305 Bytes
8f873ac
 
130100d
 
8f873ac
 
130100d
8f873ac
 
3494400
 
 
 
 
 
 
74ca97e
130100d
 
8f873ac
 
 
 
 
 
 
3494400
8f873ac
 
 
376f4dc
8f873ac
 
 
 
74ca97e
3494400
8f873ac
 
 
 
 
 
 
 
376f4dc
8f873ac
 
 
 
130100d
 
 
 
858e084
2123cc0
 
130100d
 
 
 
 
 
 
 
 
858e084
2123cc0
 
130100d
 
 
 
 
 
 
 
2483fe6
858e084
2123cc0
 
130100d
 
 
 
 
 
2483fe6
858e084
2123cc0
 
130100d
 
 
 
 
 
2483fe6
858e084
2123cc0
 
130100d
 
 
 
 
 
2483fe6
858e084
2123cc0
 
130100d
 
 
 
 
 
2483fe6
858e084
2123cc0
 
130100d
 
 
5cb020b
 
 
 
 
 
 
 
 
 
e77b7f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130100d
 
8f873ac
 
 
 
 
 
2483fe6
8f873ac
 
 
246c984
26721d3
8f873ac
 
 
 
 
5df7af3
8f873ac
 
 
 
 
 
 
 
 
376f4dc
8f873ac
 
 
 
 
376f4dc
 
 
8f873ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130100d
 
 
 
 
 
858e084
2123cc0
 
130100d
 
 
 
 
 
 
 
8f873ac
 
 
 
 
a28ba9d
8f873ac
 
 
376f4dc
8f873ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
#!/usr/bin/env python

import os

import gradio as gr
import PIL.Image
from diffusers.utils import load_image

from model import ADAPTER_NAMES, Model
from utils import (
    DEFAULT_STYLE_NAME,
    MAX_SEED,
    STYLE_NAMES,
    apply_style,
    randomize_seed_fn,
)

CACHE_EXAMPLES = os.environ.get("CACHE_EXAMPLES") == "1"


def create_demo(model: Model) -> gr.Blocks:
    def run(
        image: PIL.Image.Image,
        prompt: str,
        negative_prompt: str,
        adapter_name: str,
        style_name: str = DEFAULT_STYLE_NAME,
        num_inference_steps: int = 30,
        guidance_scale: float = 5.0,
        adapter_conditioning_scale: float = 1.0,
        adapter_conditioning_factor: float = 1.0,
        seed: int = 0,
        apply_preprocess: bool = True,
        progress=gr.Progress(track_tqdm=True),
    ) -> list[PIL.Image.Image]:
        prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)

        return model.run(
            image=image,
            prompt=prompt,
            negative_prompt=negative_prompt,
            adapter_name=adapter_name,
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
            adapter_conditioning_scale=adapter_conditioning_scale,
            adapter_conditioning_factor=adapter_conditioning_factor,
            seed=seed,
            apply_preprocess=apply_preprocess,
        )

    def process_example(
        image_url: str,
        prompt: str,
        adapter_name: str,
        style_name: str,
        guidance_scale: float,
        adapter_conditioning_scale: float,
        seed: int,
        apply_preprocess: bool,
    ) -> list[PIL.Image.Image]:
        image = load_image(image_url)
        return run(
            image=image,
            prompt=prompt,
            negative_prompt="",
            adapter_name=adapter_name,
            style_name=style_name,
            guidance_scale=guidance_scale,
            adapter_conditioning_scale=adapter_conditioning_scale,
            seed=seed,
            apply_preprocess=apply_preprocess,
        )

    examples = [
        [
            "assets/org_canny.jpg",
            "Mystical fairy in real, magic, 4k picture, high quality",
            "canny",
            "Photographic",
            5.0,
            1.0,
            0,
            True,
        ],
        [
            "assets/org_sketch.png",
            "a robot, mount fuji in the background, 4k photo, highly detailed",
            "sketch",
            "Photographic",
            5.0,
            1.0,
            0,
            True,
        ],
        [
            "assets/org_lin.jpg",
            "Ice dragon roar, 4k photo",
            "lineart",
            "Cinematic",
            7.5,
            0.8,
            0,
            True,
        ],
        [
            "assets/org_mid.jpg",
            "A photo of a room, 4k photo, highly detailed",
            "depth-midas",
            "Photographic",
            5.0,
            1.0,
            0,
            True,
        ],
        [
            "assets/org_zoe.jpg",
            "A photo of a orchid, 4k photo, highly detailed",
            "depth-zoe",
            "Photographic",
            5.0,
            1.0,
            0,
            True,
        ],
        [
            "assets/people.jpg",
            "A couple, 4k photo, highly detailed",
            "openpose",
            "Photographic",
            5.0,
            1.0,
            0,
            True,
        ],
        [
            "assets/depth-midas-image.png",
            "stormtrooper lecture, 4k photo, highly detailed",
            "depth-midas",
            "Photographic",
            5.0,
            1.0,
            0,
            False,
        ],
        [
            "assets/openpose-image.png",
            "spiderman, 4k photo, highly detailed",
            "openpose",
            "Photographic",
            5.0,
            1.0,
            0,
            False,
        ],
    ]

    with gr.Blocks() as demo:
        with gr.Row():
            with gr.Column():
                with gr.Group():
                    image = gr.Image(label="Input image", type="pil", height=600)
                    prompt = gr.Textbox(label="Prompt")
                    adapter_name = gr.Dropdown(label="Adapter name", choices=ADAPTER_NAMES, value=ADAPTER_NAMES[0])
                    run_button = gr.Button("Run")
                with gr.Accordion("Advanced options", open=False):
                    apply_preprocess = gr.Checkbox(label="Apply preprocess", value=True)
                    style = gr.Dropdown(label="Style", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
                    negative_prompt = gr.Textbox(label="Negative prompt")
                    num_inference_steps = gr.Slider(
                        label="Number of steps",
                        minimum=1,
                        maximum=Model.MAX_NUM_INFERENCE_STEPS,
                        step=1,
                        value=25,
                    )
                    guidance_scale = gr.Slider(
                        label="Guidance scale",
                        minimum=0.1,
                        maximum=30.0,
                        step=0.1,
                        value=5.0,
                    )
                    adapter_conditioning_scale = gr.Slider(
                        label="Adapter conditioning scale",
                        minimum=0.5,
                        maximum=1,
                        step=0.1,
                        value=1.0,
                    )
                    adapter_conditioning_factor = gr.Slider(
                        label="Adapter conditioning factor",
                        info="Fraction of timesteps for which adapter should be applied",
                        minimum=0.5,
                        maximum=1.0,
                        step=0.1,
                        value=1.0,
                    )
                    seed = gr.Slider(
                        label="Seed",
                        minimum=0,
                        maximum=MAX_SEED,
                        step=1,
                        value=0,
                    )
                    randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
            with gr.Column():
                result = gr.Gallery(label="Result", columns=2, height=600, object_fit="scale-down", show_label=False)

        gr.Examples(
            examples=examples,
            inputs=[
                image,
                prompt,
                adapter_name,
                style,
                guidance_scale,
                adapter_conditioning_scale,
                seed,
                apply_preprocess,
            ],
            outputs=result,
            fn=process_example,
            cache_examples=CACHE_EXAMPLES,
        )

        inputs = [
            image,
            prompt,
            negative_prompt,
            adapter_name,
            style,
            num_inference_steps,
            guidance_scale,
            adapter_conditioning_scale,
            adapter_conditioning_factor,
            seed,
            apply_preprocess,
        ]
        prompt.submit(
            fn=randomize_seed_fn,
            inputs=[seed, randomize_seed],
            outputs=seed,
            queue=False,
            api_name=False,
        ).then(
            fn=run,
            inputs=inputs,
            outputs=result,
            api_name=False,
        )
        negative_prompt.submit(
            fn=randomize_seed_fn,
            inputs=[seed, randomize_seed],
            outputs=seed,
            queue=False,
            api_name=False,
        ).then(
            fn=run,
            inputs=inputs,
            outputs=result,
            api_name=False,
        )
        run_button.click(
            fn=randomize_seed_fn,
            inputs=[seed, randomize_seed],
            outputs=seed,
            queue=False,
            api_name=False,
        ).then(
            fn=run,
            inputs=inputs,
            outputs=result,
            api_name="run",
        )

    return demo


if __name__ == "__main__":
    model = Model(ADAPTER_NAMES[0])
    demo = create_demo(model)
    demo.queue(max_size=20).launch()