File size: 10,575 Bytes
0f74281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a859a
0f74281
 
 
 
 
 
 
 
 
 
 
 
 
 
af09165
 
 
0f74281
af09165
 
 
0f74281
f1f32cc
af09165
f1f32cc
af09165
 
 
 
 
 
 
 
0f74281
 
af09165
0f74281
 
 
 
 
 
 
 
 
 
 
 
 
09b05c1
0f74281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af09165
0f74281
 
af09165
0f74281
 
af09165
0f74281
 
af09165
0f74281
 
 
 
 
af09165
0f74281
 
af09165
0f74281
 
 
 
af09165
0f74281
 
 
 
af09165
0f74281
af09165
0f74281
 
af09165
0f74281
 
af09165
0f74281
 
af09165
0f74281
af09165
0f74281
af09165
0f74281
 
 
af09165
0f74281
af09165
0f74281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af09165
0f74281
 
 
 
 
 
 
 
 
 
 
 
 
 
af09165
f1f32cc
0f74281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a15f5f
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
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates. All rights reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import dataclasses
import json
import os
from pathlib import Path

import gradio as gr
import torch
import spaces

from uso.flux.pipeline import USOPipeline
from transformers import SiglipVisionModel, SiglipImageProcessor


with open("assets/uso_text.svg", "r", encoding="utf-8") as svg_file:
    text_content = svg_file.read()

with open("assets/uso_logo.svg", "r", encoding="utf-8") as svg_file:
    logo_content = svg_file.read()

title = f"""
<div style="display: flex; align-items: center; justify-content: center;">
    <span style="transform: scale(0.7);margin-right: -5px;">{text_content}</span>    
    <span style="font-size: 1.8em;margin-left: -10px;font-weight: bold; font-family: Gill Sans;">바이트댄스 USO 모델</span>
    <span style="margin-left: 0px; transform: scale(0.85); display: inline-block;">{logo_content}</span>
</div>
""".strip()

badges_text = r"""
<div style="text-align: center; display: flex; justify-content: center; gap: 5px;">
<a href="https://github.com/bytedance/USO"><img src="https://img.shields.io/static/v1?label=GitHub&message=Code&color=green&logo=github"></a>
<a href="https://bytedance.github.io/USO/"><img alt="Build" src="https://img.shields.io/badge/Project%20Page-USO-yellow"></a>
<a href="https://arxiv.org/abs/2504.02160"><img alt="Build" src="https://img.shields.io/badge/arXiv%20paper-USO-b31b1b.svg"></a>
<a href="https://huggingface.co/bytedance-research/USO"><img src="https://img.shields.io/static/v1?label=%F0%9F%A4%97%20Hugging%20Face&message=Model&color=orange"></a>
</div>
""".strip()

tips = """
**USO란 무엇인가요?**  🎨
USO는 통합 스타일-주제 최적화 커스터마이징 모델이며, UXO 패밀리(<a href='https://github.com/bytedance/USO' target='_blank'> USO</a>와 <a href='https://github.com/bytedance/UNO' target='_blank'> UNO</a>)의 최신 버전입니다. 
모든 주제를 모든 스타일과 모든 시나리오에서 자유롭게 결합할 수 있습니다.

**사용 방법**    💡
<a href='https://github.com/bytedance/USO' target='_blank'> Github 레포지토리</a>에서 단계별 지침을 제공합니다.
또한 아래 데모에서 제공되는 예제들을 시도해보시고, USO에 빠르게 익숙해지고 창의력을 발휘해보세요!

<details>
<summary style="cursor: pointer; color: #d34c0e; font-weight: 500;">이 모델은 1024x1024 해상도로 학습되었으며 3가지 유형의 사용을 지원합니다. 📌 팁:</summary>

* **콘텐츠 이미지만**: 다음 유형을 지원합니다:
  * 주제/아이덴티티 기반 (자연스러운 프롬프트 지원, 예: *탁자 위의 시계.* *바다 근처의 여성.*, **사실적인 초상화** 생성에 뛰어남)
  * 스타일 편집 (레이아웃 보존): *이미지를 지브리 스타일/픽셀 스타일/레트로 만화 스타일/수채화 스타일로 변환...*.
  * 스타일 편집 (레이아웃 변경): *지브리 스타일, 해변의 남자.*.
* **스타일 이미지만**: 입력 스타일을 참조하여 프롬프트에 따라 무엇이든 생성합니다. 이 분야에서 뛰어나며 다중 스타일 참조도 지원합니다 (베타).
* **콘텐츠 이미지 + 스타일 이미지**: 콘텐츠를 원하는 스타일로 배치합니다. 
  * 레이아웃 보존: 프롬프트를 **비워두세요**.
  * 레이아웃 변경: 자연스러운 프롬프트를 사용하세요.</details>"""

star = r"""
USO가 도움이 되셨다면, <a href='https://github.com/bytedance/USO' target='_blank'> Github 레포지토리</a>에 ⭐를 눌러주세요. 감사합니다!"""

def get_examples(examples_dir: str = "assets/examples") -> list:
    examples = Path(examples_dir)
    ans = []  
    for example in examples.iterdir():
        if not example.is_dir() or len(os.listdir(example)) == 0:
            continue
        with open(example / "config.json") as f:
            example_dict = json.load(f)


        example_list = []

        # example_list.append(example_dict["usage"])  # case for
        example_list.append(example_dict["prompt"])  # prompt

        for key in ["image_ref1", "image_ref2", "image_ref3"]:
            if key in example_dict:
                example_list.append(str(example / example_dict[key]))
            else:
                example_list.append(None)

        example_list.append(example_dict["seed"])
        ans.append(example_list)
    return ans


def create_demo(
    model_type: str,
    device: str = "cuda" if torch.cuda.is_available() else "cpu",
    offload: bool = False,
):
    pipeline = USOPipeline(
        model_type, device, offload, only_lora=True, lora_rank=128, hf_download=True
    )
    print("USOPipeline loaded successfully")

    siglip_processor = SiglipImageProcessor.from_pretrained(
        "google/siglip-so400m-patch14-384"
    )
    siglip_model = SiglipVisionModel.from_pretrained(
        "google/siglip-so400m-patch14-384"
    )
    siglip_model.eval()
    siglip_model.to(device)
    pipeline.model.vision_encoder = siglip_model
    pipeline.model.vision_encoder_processor = siglip_processor
    print("SigLIP model loaded successfully")

    pipeline.gradio_generate = spaces.GPU(duration=120)(pipeline.gradio_generate)
    with gr.Blocks() as demo:
        gr.Markdown(title)
        gr.Markdown(badges_text)
        gr.Markdown(tips)
        with gr.Row():
            with gr.Column():
                prompt = gr.Textbox(label="프롬프트", value="아름다운 여성.")
                with gr.Row():
                    image_prompt1 = gr.Image(
                        label="콘텐츠 참조 이미지", visible=True, interactive=True, type="pil"
                    )
                    image_prompt2 = gr.Image(
                        label="스타일 참조 이미지", visible=True, interactive=True, type="pil"
                    )
                    image_prompt3 = gr.Image(
                        label="추가 스타일 참조 이미지 (베타)", visible=True, interactive=True, type="pil"
                    )

                with gr.Row():
                    with gr.Row():
                        width = gr.Slider(
                            512, 1536, 1024, step=16, label="생성 너비"
                        )
                        height = gr.Slider(
                            512, 1536, 1024, step=16, label="생성 높이"
                        )
                with gr.Row():
                    with gr.Row():
                        keep_size = gr.Checkbox(
                            label="입력 크기 유지",
                            value=False,
                            interactive=True
                        )
                    with gr.Column():
                        gr.Markdown("스타일 편집만 필요하거나 레이아웃을 유지하려면 True로 설정하세요.")

                with gr.Accordion("고급 옵션", open=True):
                    with gr.Row():
                        num_steps = gr.Slider(
                            1, 50, 25, step=1, label="단계 수"
                        )
                        guidance = gr.Slider(
                            1.0, 5.0, 4.0, step=0.1, label="가이던스", interactive=True
                        )
                        content_long_size = gr.Slider(
                            0, 1024, 512, step=16, label="콘텐츠 참조 크기"
                        )                        
                        seed = gr.Number(-1, label="시드 (랜덤: -1)")

                generate_btn = gr.Button("생성")
                gr.Markdown(star)

            with gr.Column():
                output_image = gr.Image(label="생성된 이미지")
                download_btn = gr.File(
                    label="고해상도 다운로드", type="filepath", interactive=False
                )

            inputs = [
                prompt,
                image_prompt1,
                image_prompt2,
                image_prompt3,
                seed,                     
                width,
                height,
                guidance,
                num_steps,
                keep_size,
                content_long_size,
            ]
            generate_btn.click(
                fn=pipeline.gradio_generate,
                inputs=inputs,
                outputs=[output_image, download_btn],
            )   

        example_text = gr.Text("", visible=False, label="사용 사례:")
        examples = get_examples("./assets/gradio_examples")

        gr.Examples(
            examples=examples,
            inputs=[
                prompt,
                image_prompt1,
                image_prompt2,
                image_prompt3,
                seed,
            ],
            # cache_examples='lazy',
            outputs=[output_image, download_btn],
            fn=pipeline.gradio_generate,
            label='행 1-4: 아이덴티티/주제 기반; 행 5-7: 스타일-주제 기반; 행 8-9: 스타일 기반; 행 10-12: 다중 스타일 기반 작업; 행 13: 텍스트-이미지',
            examples_per_page=15
        )

    return demo


if __name__ == "__main__":
    from typing import Literal

    from transformers import HfArgumentParser

    @dataclasses.dataclass
    class AppArgs:
        name: Literal["flux-dev", "flux-dev-fp8", "flux-schnell", "flux-krea-dev"] = "flux-dev"
        device: Literal["cuda", "cpu"] = "cuda" if torch.cuda.is_available() else "cpu"
        offload: bool = dataclasses.field(
            default=False,
            metadata={
                "help": "If True, sequantial offload the models(ae, dit, text encoder) to CPU if not used."
            },
        )
        port: int = 7860

    parser = HfArgumentParser([AppArgs])
    args_tuple = parser.parse_args_into_dataclasses()  # type: tuple[AppArgs]
    args = args_tuple[0]

    demo = create_demo(args.name, args.device, args.offload)
    demo.launch(server_port=args.port, ssr_mode=False)