File size: 6,065 Bytes
cf72c4b
08bd9a8
cf72c4b
a1db0e9
987024c
 
 
b783233
987024c
b783233
 
 
 
987024c
a1db0e9
 
 
cf72c4b
 
 
 
 
 
 
 
 
 
 
 
 
 
a1db0e9
cf72c4b
08bd9a8
 
cf72c4b
b783233
 
cf72c4b
a1db0e9
cf72c4b
b783233
 
 
 
 
cf72c4b
 
 
a1db0e9
 
08bd9a8
 
cf72c4b
 
 
 
 
 
 
b783233
 
 
cf72c4b
 
 
 
a1db0e9
 
cf72c4b
08bd9a8
cf72c4b
 
a1db0e9
 
 
 
 
 
 
08bd9a8
a1db0e9
cf72c4b
 
a1db0e9
cf72c4b
 
 
 
a1db0e9
 
cf72c4b
 
 
 
 
 
 
 
 
 
b783233
 
 
 
08bd9a8
cf72c4b
 
 
a1db0e9
cf72c4b
 
 
 
 
a1db0e9
 
 
cf72c4b
08bd9a8
cf72c4b
 
987024c
cf72c4b
 
 
 
 
 
 
 
 
 
 
 
 
08bd9a8
cf72c4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
987024c
cf72c4b
 
 
 
b783233
cf72c4b
 
a1db0e9
 
284b24d
a1db0e9
 
 
cf72c4b
 
9203920
b783233
cf72c4b
 
 
284b24d
51ce345
b783233
f937dbd
cf72c4b
 
08bd9a8
 
 
284b24d
08bd9a8
 
 
cf72c4b
 
 
 
 
 
 
 
 
 
 
 
a1db0e9
cf72c4b
 
08bd9a8
cf72c4b
 
 
 
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
import time
import os
import torch

from dartrs.v2 import (
    V2Model,
    MixtralModel,
    MistralModel,
    compose_prompt,
    LengthTag,
    AspectRatioTag,
    RatingTag,
    IdentityTag,
)
from dartrs.dartrs import DartTokenizer
from dartrs.utils import get_generation_config


import gradio as gr
from gradio.components import Component

try:
    import spaces
except ImportError:

    class spaces:
        def GPU(*args, **kwargs):
            return lambda x: x


from output import UpsamplingOutput
from utils import ASPECT_RATIO_OPTIONS, RATING_OPTIONS, LENGTH_OPTIONS, IDENTITY_OPTIONS

HF_TOKEN = os.getenv("HF_TOKEN", None)

ALL_MODELS = {
    "dart-v2-moe-sft": {
        "repo": "p1atdev/dart-v2-moe-sft",
        "type": "sft",
        "class": MixtralModel,
    },
    "dart-v2-sft": {
        "repo": "p1atdev/dart-v2-sft",
        "type": "sft",
        "class": MistralModel,
    },
}


def prepare_models(model_config: dict):
    model_name = model_config["repo"]
    tokenizer = DartTokenizer.from_pretrained(model_name, auth_token=HF_TOKEN)
    model = model_config["class"].from_pretrained(model_name, auth_token=HF_TOKEN)

    return {
        "tokenizer": tokenizer,
        "model": model,
    }


def normalize_tags(tokenizer: DartTokenizer, tags: str):
    """Just remove unk tokens."""
    return ", ".join([tag for tag in tokenizer.tokenize(tags) if tag != "<|unk|>"])


@torch.no_grad()
def generate_tags(
    model: V2Model,
    tokenizer: DartTokenizer,
    prompt: str,
    ban_token_ids: list[int],
):
    output = model.generate(
        get_generation_config(
            prompt,
            tokenizer=tokenizer,
            temperature=1,
            top_p=0.9,
            top_k=100,
            max_new_tokens=256,
            ban_token_ids=ban_token_ids,
        ),
    )

    return output


class V2UI:
    model_name: str | None = None
    model: V2Model
    tokenizer: DartTokenizer

    input_components: list[Component] = []
    generate_btn: gr.Button

    def on_generate(
        self,
        model_name: str,
        copyright_tags: str,
        character_tags: str,
        general_tags: str,
        rating_tag: RatingTag,
        aspect_ratio_tag: AspectRatioTag,
        length_tag: LengthTag,
        identity_tag: IdentityTag,
        ban_tags: str,
        *args,
    ) -> UpsamplingOutput:
        if self.model_name is None or self.model_name != model_name:
            models = prepare_models(ALL_MODELS[model_name])
            self.model = models["model"]
            self.tokenizer = models["tokenizer"]
            self.model_name = model_name

        # normalize tags
        # copyright_tags = normalize_tags(self.tokenizer, copyright_tags)
        # character_tags = normalize_tags(self.tokenizer, character_tags)
        # general_tags = normalize_tags(self.tokenizer, general_tags)

        ban_token_ids = self.tokenizer.encode(ban_tags.strip())

        prompt = compose_prompt(
            prompt=general_tags,
            copyright=copyright_tags,
            character=character_tags,
            rating=rating_tag,
            aspect_ratio=aspect_ratio_tag,
            length=length_tag,
            identity=identity_tag,
        )

        start = time.time()
        upsampled_tags = generate_tags(
            self.model,
            self.tokenizer,
            prompt,
            ban_token_ids,
        )
        elapsed_time = time.time() - start

        return UpsamplingOutput(
            upsampled_tags=upsampled_tags,
            copyright_tags=copyright_tags,
            character_tags=character_tags,
            general_tags=general_tags,
            rating_tag=rating_tag,
            aspect_ratio_tag=aspect_ratio_tag,
            length_tag=length_tag,
            identity_tag=identity_tag,
            elapsed_time=elapsed_time,
        )

    def ui(self):
        input_copyright = gr.Textbox(
            label="Copyright tags",
            placeholder="vocaloid",
        )
        input_character = gr.Textbox(
            label="Character tags",
            placeholder="hatsune miku",
        )
        input_general = gr.TextArea(
            label="General tags",
            lines=4,
            placeholder="1girl, ...",
            value="1girl, solo",
        )

        input_rating = gr.Radio(
            label="Rating",
            choices=list(RATING_OPTIONS),
            value="general",
        )
        input_aspect_ratio = gr.Radio(
            label="Aspect ratio",
            info="The aspect ratio of the image.",
            choices=["ultra_wide", "wide", "square", "tall", "ultra_tall"],
            value="tall",
        )
        input_length = gr.Radio(
            label="Length",
            info="The total length of the tags.",
            choices=list(LENGTH_OPTIONS),
            value="long",
        )
        input_identity = gr.Radio(
            label="Keep identity",
            info="How strictly to keep the identity of the character or subject. If you specify the detail of subject in the prompt, you should choose `strict`. Otherwise, choose `none` or `lax`. `none` is very creative but sometimes ignores the input prompt.",
            choices=list(IDENTITY_OPTIONS),
            value="none",
        )

        with gr.Accordion(label="Advanced options", open=False):
            input_ban_tags = gr.Textbox(
                label="Ban tags",
                info="Tags to ban from the output.",
                placeholder="alternate costumen, ...",
            )

        model_name = gr.Dropdown(
            label="Model",
            choices=list(ALL_MODELS.keys()),
            value=list(ALL_MODELS.keys())[0],
        )

        self.input_components = [
            model_name,
            input_copyright,
            input_character,
            input_general,
            input_rating,
            input_aspect_ratio,
            input_length,
            input_identity,
            input_ban_tags,
        ]

    def get_inputs(self) -> list[Component]:
        return self.input_components