File size: 10,930 Bytes
2295066
f34769f
ab4fc1e
6a950b7
 
 
 
 
 
 
 
 
 
664cfb0
6a950b7
6e14af5
ab4fc1e
 
 
 
 
 
ad7bffa
 
 
 
 
ab4fc1e
 
eab6422
f34769f
6a950b7
664cfb0
6e14af5
483d77b
6e14af5
ab4fc1e
 
 
 
 
 
 
80593c9
 
 
 
 
 
 
 
 
 
483d77b
f34769f
80593c9
 
 
 
ab4fc1e
 
80593c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6026c0f
a7fad10
 
 
 
 
 
 
ad7bffa
ab4fc1e
9e35c8e
ab4fc1e
9e35c8e
 
 
ab4fc1e
 
21533df
082f86a
ab4fc1e
ad7bffa
 
 
 
ad7bc78
073dd5a
8f17729
95ef998
8f17729
95ef998
eab6422
 
6a950b7
 
664cfb0
 
6e14af5
 
8f17729
9e35c8e
8f17729
9e35c8e
8f17729
9e35c8e
8f17729
9e35c8e
ab4fc1e
9e35c8e
49247fd
ab4fc1e
 
80593c9
 
 
 
8f17729
9e35c8e
8f17729
9e35c8e
8f17729
9e35c8e
8f17729
9e35c8e
80593c9
8f17729
 
ab4fc1e
 
 
 
 
 
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
import spaces
import gradio as gr

from v2 import v2_upsampling_prompt, V2_ALL_MODELS
from utils import (gradio_copy_text, COPY_ACTION_JS, V2_ASPECT_RATIO_OPTIONS,
    V2_RATING_OPTIONS, V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
from tagger import (predict_tags_wd, convert_danbooru_to_e621_prompt,
    remove_specific_prompt, insert_recom_prompt, compose_prompt_to_copy,
    translate_prompt, sort_tags, select_random_character)
from z3de621conv import predict_tags_e621
from fl2sd3longcap import predict_tags_fl2_sd3
from fl2basepromptgen import predict_tags_fl2_base_prompt_gen
from fl2cog import predict_tags_fl2_cog
from fl2flux import predict_tags_fl2_flux
from wdtagger import predict_tags_wd_large, get_wdtagger_models, VIT_LARGE_MODEL_DSV3_REPO
from dgb import predict_tags_dgb

def description_ui():
    gr.Markdown(
        """

## Danbooru Tags Transformer V2 Demo with WD Tagger

(Image =>) Prompt => Upsampled longer prompt

"""
    )
def description_ui2():
    gr.Markdown(
        """

- Mod of p1atdev's [Danbooru Tags Transformer V2 Demo](https://huggingface.co/spaces/p1atdev/danbooru-tags-transformer-v2) and [WD Tagger with πŸ€— transformers](https://huggingface.co/spaces/p1atdev/wd-tagger-transformers).

- Models: p1atdev's [wd-swinv2-tagger-v3-hf](https://huggingface.co/p1atdev/wd-swinv2-tagger-v3-hf), [dart-v2-moe-sft](https://huggingface.co/p1atdev/dart-v2-moe-sft), [dart-v2-sft](https://huggingface.co/p1atdev/dart-v2-sft)\

, toynya's [Z3D-E621-Convnext](https://huggingface.co/toynya/Z3D-E621-Convnext), gokaygokay's [Florence-2-SD3-Captioner](https://huggingface.co/gokaygokay/Florence-2-SD3-Captioner),\

 MiaoshouAI's [Florence-2-base-PromptGen-v1.5](https://huggingface.co/MiaoshouAI/Florence-2-base-PromptGen-v1.5),\

 SmilingWolf's [wd-vit-large-tagger-v3](https://huggingface.co/SmilingWolf/wd-vit-large-tagger-v3),\

 thwri's [CogFlorence-2.1-Large](https://huggingface.co/thwri/CogFlorence-2.1-Large/),\

 gokaygokay's [Florence-2-Flux](https://huggingface.co/gokaygokay/Florence-2-Flux),\

 gokaygokay's [Florence-2-Flux-Large](https://huggingface.co/gokaygokay/Florence-2-Flux-Large),\

 v2ray's [deepgelbooru](https://huggingface.co/v2ray/deepgelbooru).

"""
    )


def main():
    with gr.Blocks() as ui:
        description_ui()
        with gr.Column():
            with gr.Group():
                input_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
                with gr.Accordion(label="Advanced options", open=False):
                    wdtagger_model = gr.Dropdown(label="Original WD Tagger Model", choices=get_wdtagger_models(), value=VIT_LARGE_MODEL_DSV3_REPO)
                    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)
                    e621_threshold = gr.Slider(label="Threshold (Z3D-E621-Convnext)", minimum=0.0, maximum=1.0, value=0.5, step=0.01, interactive=True)
                    input_tag_type = gr.Radio(label="Convert tags to", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
                    recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
                image_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Original WD Tagger", "Use Z3D-E621-Convnext", "Use Florence-2-SD3-Long-Captioner",
                    "Use Florence-2-base-PromptGen", "Use CogFlorence-2.1-Large", "Use Florence-2-Flux","Use Florence-2-Flux-Large", "Use DeepGelbooru"], label="Algorithms", value=["Use WD Tagger"])
                keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
                generate_from_image_btn = gr.Button(value="GENERATE TAGS FROM IMAGE", size="lg", variant="primary")
            with gr.Group():
                with gr.Row():
                    input_character = gr.Textbox(label="Character tags", placeholder="hatsune miku")
                    input_copyright = gr.Textbox(label="Copyright tags", placeholder="vocaloid")
                    random_character = gr.Button(value="Random character 🎲", size="sm")
                input_general = gr.TextArea(label="General tags", lines=4, placeholder="1girl, ...", value="")
                input_tags_to_copy = gr.Textbox(value="", visible=False)
                copy_input_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
                translate_input_prompt_button = gr.Button(value="Translate prompt to English", size="sm", variant="secondary")
                tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
                input_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="explicit")
                with gr.Accordion(label="Advanced options", open=False):
                    input_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square")
                    input_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="very_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(V2_IDENTITY_OPTIONS), value="lax")                    
                    input_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
                    model_name = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
                    dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
                    recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
                    recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
                generate_btn = gr.Button(value="GENERATE TAGS", size="lg", variant="primary")
            with gr.Row():
                with gr.Group():
                    output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
                    copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
                with gr.Group():
                    output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
                    copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
        description_ui2()

        random_character.click(select_random_character, [input_copyright, input_character], [input_copyright, input_character], queue=False)

        translate_input_prompt_button.click(translate_prompt, [input_general], [input_general], queue=False)
        translate_input_prompt_button.click(translate_prompt, [input_character], [input_character], queue=False)
        translate_input_prompt_button.click(translate_prompt, [input_copyright], [input_copyright], queue=False)

        generate_from_image_btn.click(
            lambda: ("", "", ""), None, [input_copyright, input_character, input_general], queue=False,
        ).success(
            predict_tags_wd,
            [input_image, input_general, image_algorithms, general_threshold, character_threshold, input_copyright, input_character],
            [input_copyright, input_character, input_general, copy_input_btn],
        ).success(
            predict_tags_wd_large,
            [input_image, input_general, image_algorithms, general_threshold, character_threshold, input_copyright, input_character, wdtagger_model],
            [input_copyright, input_character, input_general, copy_input_btn],
        ).success(
            predict_tags_e621, [input_image, input_general, image_algorithms, e621_threshold], [input_general],
        ).success(
            predict_tags_fl2_sd3, [input_image, input_general, image_algorithms], [input_general],
        ).success(
            predict_tags_fl2_base_prompt_gen, [input_image, input_general, image_algorithms], [input_general],
        ).success(
            predict_tags_fl2_cog, [input_image, input_general, image_algorithms], [input_general],
        ).success(
            predict_tags_fl2_flux, [input_image, input_general, image_algorithms], [input_general],
        ).success(
            predict_tags_dgb, [input_image, input_general, image_algorithms, general_threshold], [input_general],
        ).success(
            remove_specific_prompt, [input_general, keep_tags], [input_general], queue=False,
        ).success(
            convert_danbooru_to_e621_prompt, [input_general, input_tag_type], [input_general], queue=False,
        ).success(
            sort_tags, [input_general], [input_general], queue=False,
        ).success(
            insert_recom_prompt, [input_general, dummy_np, recom_prompt], [input_general, dummy_np], queue=False,
        )
        copy_input_btn.click(compose_prompt_to_copy, [input_character, input_copyright, input_general], [input_tags_to_copy], queue=False)\
            .success(gradio_copy_text, [input_tags_to_copy], js=COPY_ACTION_JS)

        generate_btn.click(
            v2_upsampling_prompt,
            [model_name, input_copyright, input_character, input_general,
            input_rating, input_aspect_ratio, input_length, input_identity, input_ban_tags],
            [output_text],
        ).success(
            sort_tags, [output_text], [output_text], queue=False,
        ).success(
            convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False,
        ).success(
            insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False,
        ).success(
            insert_recom_prompt, [output_text_pony, dummy_np, recom_pony], [output_text_pony, dummy_np], queue=False,
        ).success(lambda: (gr.update(interactive=True), gr.update(interactive=True)), None, [copy_btn, copy_btn_pony], queue=False)
        copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS)
        copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS)

    ui.launch()


if __name__ == "__main__":
    main()