File size: 4,906 Bytes
f357c72
 
 
 
 
 
 
 
 
ba9e1f1
9432c1a
f357c72
 
ba9e1f1
 
f357c72
 
 
 
23c73d4
f357c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23c73d4
f357c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba9e1f1
 
f357c72
ba9e1f1
 
f357c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d34aeeb
f357c72
 
 
d34aeeb
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
from PIL import Image

import torch

from transformers import (
    AutoImageProcessor,
    AutoModelForImageClassification,
)

import gradio as gr
import spaces  # ZERO GPU


MODEL_NAMES = ["p1atdev/wd-swinv2-tagger-v3-hf"]
MODEL_NAME = MODEL_NAMES[0]

model = AutoModelForImageClassification.from_pretrained(
    MODEL_NAME,
)
model.to("cuda" if torch.cuda.is_available() else "cpu")
processor = AutoImageProcessor.from_pretrained(MODEL_NAME, trust_remote_code=True)


def _people_tag(noun: str, minimum: int = 1, maximum: int = 5):
    return (
        [f"1{noun}"]
        + [f"{num}{noun}s" for num in range(minimum + 1, maximum + 1)]
        + [f"{maximum+1}+{noun}s"]
    )


PEOPLE_TAGS = (
    _people_tag("girl") + _people_tag("boy") + _people_tag("other") + ["no humans"]
)
RATING_MAP = {
    "general": "safe",
    "sensitive": "sensitive",
    "questionable": "nsfw",
    "explicit": "explicit, nsfw",
}

DESCRIPTION_MD = """
# WD Tagger with 🤗 transformers
Currently supports the following model(s):
- [p1atdev/wd-swinv2-tagger-v3-hf](https://huggingface.co/p1atdev/wd-swinv2-tagger-v3-hf)

""".strip()


def postprocess_results(
    results: dict[str, float], general_threshold: float, character_threshold: float
):
    results = {
        k: v for k, v in sorted(results.items(), key=lambda item: item[1], reverse=True)
    }

    rating = {}
    character = {}
    general = {}

    for k, v in results.items():
        if k.startswith("rating:"):
            rating[k.replace("rating:", "")] = v
            continue
        elif k.startswith("character:"):
            character[k.replace("character:", "")] = v
            continue

        general[k] = v

    character = {k: v for k, v in character.items() if v >= character_threshold}
    general = {k: v for k, v in general.items() if v >= general_threshold}

    return rating, character, general


def animagine_prompt(rating: list[str], character: list[str], general: list[str]):
    people_tags: list[str] = []
    other_tags: list[str] = []
    rating_tag = RATING_MAP[rating[0]]

    for tag in general:
        if tag in PEOPLE_TAGS:
            people_tags.append(tag)
        else:
            other_tags.append(tag)

    all_tags = people_tags + character + other_tags + [rating_tag]

    return ", ".join(all_tags)


@spaces.GPU(enable_queue=True)
def predict_tags(
    image: Image.Image, general_threshold: float = 0.3, character_threshold: float = 0.8
):
    inputs = processor.preprocess(image, return_tensors="pt")

    outputs = model(**inputs.to(model.device, model.dtype))
    logits = torch.sigmoid(outputs.logits[0])  # take the first logits

    # get probabilities
    results = {
        model.config.id2label[i]: float(logit.float()) for i, logit in enumerate(logits)
    }

    # rating, character, general
    rating, character, general = postprocess_results(
        results, general_threshold, character_threshold
    )

    prompt = animagine_prompt(
        list(rating.keys()), list(character.keys()), list(general.keys())
    )

    return rating, character, general, prompt


def demo():
    with gr.Blocks() as ui:
        gr.Markdown(DESCRIPTION_MD)

        with gr.Row():
            with gr.Column():
                input_image = gr.Image(label="Input image", type="pil")

                with gr.Group():
                    general_threshold = gr.Slider(
                        label="Threshold",
                        minimum=0.0,
                        maximum=1.0,
                        value=0.3,
                        step=0.01,
                        interactive=True,
                    )
                    character_threshold = gr.Slider(
                        label="Character threshold",
                        minimum=0.0,
                        maximum=1.0,
                        value=0.8,
                        step=0.01,
                        interactive=True,
                    )

                _model_radio = gr.Dropdown(
                    choices=MODEL_NAMES,
                    label="Model",
                    value=MODEL_NAMES[0],
                    interactive=True,
                )

                start_btn = gr.Button(value="Start", variant="primary")

            with gr.Column():
                prompt_text = gr.Text(label="Prompt")

                rating_tags_label = gr.Label(label="Rating tags")
                character_tags_label = gr.Label(label="Character tags")
                general_tags_label = gr.Label(label="General tags")

        start_btn.click(
            predict_tags,
            inputs=[input_image, general_threshold, character_threshold],
            outputs=[
                rating_tags_label,
                character_tags_label,
                general_tags_label,
                prompt_text,
            ],
        )

    return ui


if __name__ == "__main__":
    demo().queue().launch()