File size: 10,255 Bytes
7d5de5f
35b0893
808dc9c
7d5de5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808dc9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d5de5f
 
 
808dc9c
7d5de5f
 
808dc9c
7d5de5f
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import gradio as gr
import spaces
from ominicontrol import generate_image, generate_image_with_prompt
import os

from huggingface_hub import login

USE_ZERO_GPU = os.environ.get("USE_ZERO_GPU", "0") == "1"

HF_TOKEN = os.environ.get("HF_TOKEN") 
if HF_TOKEN:
    login(token=HF_TOKEN)

css = """
.inputPanel {
    width: 320px;
    display: flex;
    align-items: center;
}
.outputPanel {
    display: flex;
    align-items: center;
}
.hint {
    font-size: 14px;
    color: #777;
    # border: 1px solid #ccc;
    padding: 4px;
    border-radius: 5px;
    # background-color: #efefef;
}
"""

header = """
# 🎨 OminiControl Art
<div style="text-align: center; display: flex; justify-content: left; gap: 5px;">
<a href="https://arxiv.org/abs/2411.15098"><img src="https://img.shields.io/badge/ariXv-Paper-A42C25.svg" alt="arXiv"></a>
<a href="https://huggingface.co/spaces/Yuanshi/OminiControl"><img src="https://img.shields.io/badge/🤗OminiControl-Demo-ffbd45.svg" alt="HuggingFace"></a>
<a href="https://github.com/Yuanshi9815/OminiControl"><img src="https://img.shields.io/badge/GitHub-Code-blue.svg?logo=github&" alt="GitHub"></a>
</div>

***OminiControl Art*** distills the artistic style of [GPT-4o](https://openai.com/index/introducing-4o-image-generation/) into the [FLUX.1](https://blackforestlabs.ai/) model, building on the foundation of [OminiControl](https://github.com/Yuanshi9815/OminiControl)✨.
Enjoy playing around! 🌈
"""


def style_transfer(image, style):
    return image


styles = [
    "Studio Ghibli",
    "Irasutoya Illustration",
    "The Simpsons",
    "Snoopy",
]


def gradio_interface():
    with gr.Blocks(css=css) as demo:
        gr.Markdown(header)
        with gr.Row(equal_height=False):
            with gr.Column(variant="panel", elem_classes="inputPanel"):
                original_image = gr.Image(
                    type="pil",
                    label="Condition Image",
                    width=400,
                    height=400,
                )
                style = gr.Radio(
                    styles,
                    label="🎨 Select Style",
                    value=styles[0],
                )
                # Advanced settings
                with gr.Accordion(
                    "⚙️ Advanced Settings", open=False
                ) as advanced_settings:
                    inference_mode = gr.Radio(
                        ["High Quality", "Fast"],
                        value="High Quality",
                        label="Generating Mode",
                    )
                    image_ratio = gr.Radio(
                        ["Auto", "Square(1:1)", "Portrait(2:3)", "Landscape(3:2)"],
                        label="Image Ratio",
                        value="Auto",
                    )
                    use_random_seed = gr.Checkbox(label="Use Random Seed", value=True)
                    seed = gr.Number(
                        label="Seed",
                        value=42,
                        visible=(not use_random_seed.value),
                    )
                    use_random_seed.change(
                        lambda x: gr.update(visible=(not x)),
                        use_random_seed,
                        seed,
                        show_progress="hidden",
                    )
                    image_guidance = gr.Slider(
                        label="Image Guidance",
                        minimum=1.1,
                        maximum=5,
                        value=1.5,
                        step=0.1,
                    )
                    steps = gr.Slider(
                        label="Steps",
                        minimum=10,
                        maximum=50,
                        value=20,
                        step=1,
                    )
                    inference_mode.change(
                        lambda x: gr.update(interactive=(x == "High Quality")),
                        inference_mode,
                        image_guidance,
                        show_progress="hidden",
                    )

                btn = gr.Button("Generate Image", variant="primary")

            with gr.Column(elem_classes="outputPanel"):
                output_image = gr.Image(
                    type="pil",
                    width=600,
                    height=600,
                    label="Output Image",
                    interactive=False,
                    sources=None,
                )

                hint = gr.Markdown(
                    """
                    <div style="text-align: center; width: 100%;">
                    <b>Note: The selected style is in beta testing.</b> Feel free to try a few more times to get a the better result.
                    </div>
                    """,
                    visible=False,
                )
                style.change(
                    lambda x: gr.update(visible=x in styles[1:]),
                    style,
                    hint,
                )

        # with gr.Row():
        btn.click(
            fn=infer,
            inputs=[
                style,
                original_image,
                inference_mode,
                image_guidance,
                image_ratio,
                use_random_seed,
                seed,
                steps,
            ],
            outputs=[
                output_image,
            ],
        )

    return demo


def infer(
    style,
    original_image,
    inference_mode,
    image_guidance,
    image_ratio,
    use_random_seed,
    seed,
    steps,
):
    print(
        f"Style: {style}, Inference Mode: {inference_mode}, Image Guidance: {image_guidance}, Image Ratio: {image_ratio}, Use Random Seed: {use_random_seed}, Seed: {seed}"
    )
    result_image = generate_image(
        image=original_image,
        style=style,
        inference_mode=inference_mode,
        image_guidance=image_guidance,
        image_ratio=image_ratio,
        use_random_seed=use_random_seed,
        seed=seed,
        steps=steps,
    )
    return result_image

def prompt_gradio_interface():
    with gr.Blocks(css=css) as demo:
        with gr.Row(equal_height=False):
            with gr.Column(variant="panel", elem_classes="inputPanel"):
                original_image = gr.Image(
                    type="pil",
                    label="Condition Image",
                    width=400,
                    height=400,
                )
                prompt = gr.Textbox(
                    label="Prompt",
                )
                # Advanced settings
                with gr.Accordion(
                    "⚙️ Advanced Settings", open=False
                ) as advanced_settings:
                    inference_mode = gr.Radio(
                        ["High Quality", "Fast"],
                        value="High Quality",
                        label="Generating Mode",
                    )
                    image_ratio = gr.Radio(
                        ["Auto", "Square(1:1)", "Portrait(2:3)", "Landscape(3:2)"],
                        label="Image Ratio",
                        value="Auto",
                    )
                    use_random_seed = gr.Checkbox(label="Use Random Seed", value=True)
                    seed = gr.Number(
                        label="Seed",
                        value=42,
                        visible=(not use_random_seed.value),
                    )
                    use_random_seed.change(
                        lambda x: gr.update(visible=(not x)),
                        use_random_seed,
                        seed,
                        show_progress="hidden",
                    )
                    image_guidance = gr.Slider(
                        label="Image Guidance",
                        minimum=1.1,
                        maximum=5,
                        value=1.5,
                        step=0.1,
                    )
                    steps = gr.Slider(
                        label="Steps",
                        minimum=10,
                        maximum=50,
                        value=20,
                        step=1,
                    )
                    inference_mode.change(
                        lambda x: gr.update(interactive=(x == "High Quality")),
                        inference_mode,
                        image_guidance,
                        show_progress="hidden",
                    )

                btn = gr.Button("Generate Image", variant="primary")

            with gr.Column(elem_classes="outputPanel"):
                output_image = gr.Image(
                    type="pil",
                    width=600,
                    height=600,
                    label="Output Image",
                    interactive=False,
                    sources=None,
                )

        # with gr.Row():
        btn.click(
            fn=prompt_infer,
            inputs=[
                original_image,
                prompt,
                inference_mode,
                image_guidance,
                image_ratio,
                use_random_seed,
                seed,
                steps,
            ],
            outputs=[
                output_image,
            ],
        )

    return demo

def prompt_infer(
    original_image,
    prompt,
    inference_mode,
    image_guidance,
    image_ratio,
    use_random_seed,
    seed,
    steps,
):
    result_image = generate_image_with_prompt(
        image=original_image,
        prompt=prompt,
        inference_mode=inference_mode,
        image_guidance=image_guidance,
        image_ratio=image_ratio,
        use_random_seed=use_random_seed,
        seed=seed,
        steps=steps,
    )
    return result_image

def multi_gradio_interface():
    with gr.Blocks(css="style.css") as demo:
        with gr.Tabs():
            with gr.Tab(label="Style"):
                gradio_interface()
            with gr.Tab(label="Prompt"):
                prompt_gradio_interface()

    return demo

if USE_ZERO_GPU:
    infer = spaces.GPU(infer)
    prompt_infer = spaces.GPU(prompt_infer)

if __name__ == "__main__":
    demo = multi_gradio_interface()
    demo.launch(server_name="0.0.0.0", ssr_mode=False)