File size: 9,346 Bytes
ddea0a0
 
 
 
 
 
 
 
 
 
 
 
 
ace0b06
 
 
 
 
 
 
 
 
 
 
ddea0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
856c348
 
 
 
6625c7b
 
ace0b06
 
6625c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddea0a0
6625c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

from __future__ import annotations

import os
import random
from typing import Tuple, Optional

import gradio as gr
from huggingface_hub import HfApi

from inf import InferencePipeline


TITLE = '# Implicit Style-Content Separation using B-LoRA'


DESC = '''<center>
    This is a demo for our <a href="https://arxiv.org/abs/2403.14572">paper</a>: <b>''Implicit Style-Content Separation using B-LoRA''</b>.
    <br>
    Project page and code is available <a href="https://b-lora.github.io/B-LoRA/">here</a>.
</center>'''


SAMPLE_MODEL_IDS = [
    'lora-library/B-LoRA-teddybear',
    'lora-library/B-LoRA-bull',
    'lora-library/B-LoRA-wolf_plushie',
    'lora-library/B-LoRA-pen_sketch',
    'lora-library/B-LoRA-cartoon_line',
    'lora-library/B-LoRA-multi-dog2',
]
css = """
body {
    font-size: 30px;
}
.gr-image {
    width: 512px;
    height: 512px;
    object-fit: contain;
    margin: auto;
}

.lora-column {
    display: flex;
    flex-direction: column;
    align-items: center; /* Center align content vertically in columns */
    justify-content: center; /* Center content horizontally in columns */
}
.gr-row {
    align-items: center;
    justify-content: center;
    margin-top: 5px;
}
"""

def get_choices(hf_token):
    api = HfApi(token=hf_token)
    choices = [
        info.modelId for info in api.list_models(author='lora-library')
    ]
    models_list = ['None'] + SAMPLE_MODEL_IDS + choices
    return models_list


def get_image_from_card(card, model_id) -> Optional[str]:
    try:
        card_path = f"https://huggingface.co/{model_id}/resolve/main/"
        widget = card.data.get('widget')
        if widget is not None or len(widget) > 0:
            output = widget[0].get('output')
            if output is not None:
                url = output.get('url')
                if url is not None:
                    return card_path + url
        return None
    except Exception:
        return None


def demo_init():
    try:
        choices = get_choices(app.hf_token)
        content_blora = random.choice(SAMPLE_MODEL_IDS)
        style_blora = random.choice(SAMPLE_MODEL_IDS)
        content_blora_prompt, content_blora_image = app.load_model_info(content_blora)
        style_blora_prompt, style_blora_image = app.load_model_info(style_blora)

        content_lora_model_id = gr.update(choices=choices, value=content_blora)
        content_prompt = gr.update(value=content_blora_prompt)
        content_image = gr.update(value=content_blora_image)

        style_lora_model_id = gr.update(choices=choices, value=style_blora)
        style_prompt = gr.update(value=style_blora_prompt)
        style_image = gr.update(value=style_blora_image)

        prompt = gr.update(
            value=f'{content_blora_prompt} in {style_blora_prompt[0].lower() + style_blora_prompt[1:]} style')

        return content_lora_model_id, content_prompt, content_image, style_lora_model_id, style_prompt, style_image, prompt

    except Exception as e:
        raise type(e)(f'failed to demo_init, due to: {e}')


def toggle_column(is_checked):
    try:
        return 'None' if is_checked else random.choice(SAMPLE_MODEL_IDS)
    except Exception as e:
        raise type(e)(f'failed to toggle_column, due to: {e}')


class InferenceUtil:
    def __init__(self, hf_token: str | None):
        self.hf_token = hf_token

    def load_model_info(self, lora_model_id: str) -> Tuple[str, Optional[str]]:
        try:
            try:
                card = InferencePipeline.get_model_card(lora_model_id,
                                                        self.hf_token)
            except Exception:
                return '', None
            instance_prompt = getattr(card.data, 'instance_prompt', '')
            image_url = get_image_from_card(card, lora_model_id)
            return instance_prompt, image_url
        except Exception as e:
            raise type(e)(f'failed to load_model_info, due to: {e}')

    def update_model_info(self, model_source: str):
        try:
            if model_source == 'None':
                return '', None
            else:
                model_info = self.load_model_info(model_source)
                new_prompt, new_image = model_info[0], model_info[1]
            return new_prompt, new_image
        except Exception as e:
            raise type(e)(f'failed to update_model_info, due to: {e}')


hf_token = os.getenv('HF_TOKEN')
pipe = InferencePipeline(hf_token)
app = InferenceUtil(hf_token)


with gr.Blocks(css=css) as demo:
    gr.Markdown(TITLE)
    gr.Markdown(DESC)
    with gr.Row(elem_classes="gr-row"):
        with gr.Column():
            with gr.Group(elem_classes="lora-column"):
                gr.Markdown('## Content B-LoRA')
                content_checkbox = gr.Checkbox(label='Use Content Only', value=False)
                content_lora_model_id = gr.Dropdown(label='Model ID', choices=[])
                content_prompt = gr.Text(label='Content instance prompt', interactive=False, max_lines=1)
                content_image = gr.Image(label='Content Image', elem_classes="gr-image")
        with gr.Column():
            with gr.Group(elem_classes="lora-column"):
                gr.Markdown('## Style B-LoRA')
                style_checkbox = gr.Checkbox(label='Use Style Only', value=False)
                style_lora_model_id = gr.Dropdown(label='Model ID', choices=[])
                style_prompt = gr.Text(label='Style instance prompt', interactive=False, max_lines=1)
                style_image = gr.Image(label='Style Image', elem_classes="gr-image")
    with gr.Row(elem_classes="gr-row"):
        with gr.Column():
            with gr.Group():
                prompt = gr.Textbox(
                    label='Prompt',
                    max_lines=1,
                    placeholder='Example: "A [c] in [s] style"'
                )
                result = gr.Image(label='Result')
                with gr.Accordion('Other Parameters', open=False, elem_classes="gr-accordion"):
                    content_alpha = gr.Slider(label='Content B-LoRA alpha',
                                              minimum=0,
                                              maximum=2,
                                              step=0.05,
                                              value=1)
                    style_alpha = gr.Slider(label='Style B-LoRA alpha',
                                            minimum=0,
                                            maximum=2,
                                            step=0.05,
                                            value=1)
                    seed = gr.Slider(label='Seed',
                                     minimum=0,
                                     maximum=100000,
                                     step=1,
                                     value=8888)
                    num_steps = gr.Slider(label='Number of Steps',
                                          minimum=0,
                                          maximum=100,
                                          step=1,
                                          value=50)
                    guidance_scale = gr.Slider(label='CFG Scale',
                                               minimum=0,
                                               maximum=50,
                                               step=0.1,
                                               value=7.5)
                run_button = gr.Button('Generate')
    demo.load(demo_init, inputs=[],
              outputs=[content_lora_model_id, content_prompt, content_image, style_lora_model_id, style_prompt,
                       style_image, prompt], queue=False, show_progress="hidden")
    content_lora_model_id.change(
        fn=app.update_model_info,
        inputs=content_lora_model_id,
        outputs=[
            content_prompt,
            content_image,
        ])
    style_lora_model_id.change(
        fn=app.update_model_info,
        inputs=style_lora_model_id,
        outputs=[
            style_prompt,
            style_image,
        ])
    style_prompt.change(
        fn=lambda content_blora_prompt,
                  style_blora_prompt: f'{content_blora_prompt} in {style_blora_prompt[0].lower() + style_blora_prompt[1:]} style' if style_blora_prompt else content_blora_prompt,
        inputs=[content_prompt, style_prompt],
        outputs=prompt,
    )
    content_prompt.change(
        fn=lambda content_blora_prompt,
                  style_blora_prompt: f'{content_blora_prompt} in {style_blora_prompt[0].lower() + style_blora_prompt[1:]} style' if content_blora_prompt else style_blora_prompt,
        inputs=[content_prompt, style_prompt],
        outputs=prompt,
    )
    content_checkbox.change(toggle_column, inputs=[content_checkbox],
                            outputs=[style_lora_model_id])
    style_checkbox.change(toggle_column, inputs=[style_checkbox],
                          outputs=[content_lora_model_id])
    inputs = [
        content_lora_model_id,
        style_lora_model_id,
        prompt,
        content_alpha,
        style_alpha,
        seed,
        num_steps,
        guidance_scale,
    ]
    prompt.submit(fn=pipe.run, inputs=inputs, outputs=result)
    run_button.click(fn=pipe.run, inputs=inputs, outputs=result)

demo.queue(max_size=10).launch(share=False)