File size: 7,068 Bytes
48f4d16
 
 
80fefdb
 
48f4d16
 
 
 
 
 
 
80fefdb
 
 
 
 
 
 
 
 
 
 
 
 
48f4d16
80fefdb
48f4d16
 
 
 
 
80fefdb
 
 
 
 
48f4d16
80fefdb
 
 
 
 
 
48f4d16
80fefdb
 
 
 
 
 
48f4d16
 
 
 
80fefdb
 
 
 
 
 
 
 
 
 
 
48f4d16
 
 
 
 
80fefdb
 
48f4d16
 
 
 
 
 
 
80fefdb
 
48f4d16
 
 
 
 
80fefdb
48f4d16
 
80fefdb
 
 
 
 
 
 
 
 
 
 
 
 
 
48f4d16
 
 
 
80fefdb
48f4d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80fefdb
 
 
 
 
 
 
 
 
 
48f4d16
 
 
 
 
 
80fefdb
48f4d16
80fefdb
 
 
 
 
 
 
 
48f4d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch

from utils.exif import get_image_info
from utils.generator import generate_prompt
from utils.image2text import git_image2text, w14_image2text, clip_image2text
from utils.translate import en2zh as translate_en2zh
from utils.translate import zh2en as translate_zh2en

device = "cuda" if torch.cuda.is_available() else "cpu"


def text_generate_prompter(
        plain_text,
        model_name='microsoft',
        prompt_min_length=60,
        prompt_max_length=75,
        prompt_num_return_sequences=8,
):
    result = generate_prompt(
        plain_text=plain_text,
        model_name=model_name,
        min_length=prompt_min_length,
        max_length=prompt_max_length,
        num_return_sequences=prompt_num_return_sequences
    )
    return result, "\n".join(translate_en2zh(line) for line in result.split("\n") if len(line) > 0)


def image_generate_prompter(
        bclip_text,
        w14_text,
        model_name='microsoft',
        prompt_min_length=60,
        prompt_max_length=75,
        prompt_num_return_sequences=8,

):
    result = generate_prompt(
        plain_text=bclip_text,
        model_name=model_name,
        min_length=prompt_min_length,
        max_length=prompt_max_length,
        num_return_sequences=prompt_num_return_sequences
    )
    prompter_list = ["{},{}".format(line.strip(), w14_text.strip()) for line in result.split("\n") if len(line) > 0]
    prompter_zh_list = [
        "{},{}".format(translate_en2zh(line.strip()), translate_en2zh(w14_text.strip())) for line in
        result.split("\n") if len(line) > 0
    ]
    return "\n".join(prompter_list), "\n".join(prompter_zh_list)


with gr.Blocks(title="Prompt生成器") as block:
    with gr.Column():
        with gr.Tab('文本生成'):
            with gr.Row():
                input_text = gr.Textbox(lines=6, label='你的想法', placeholder='在此输入内容...')
                translate_output = gr.Textbox(lines=6, label='翻译结果(Prompt输入)')

            output = gr.Textbox(lines=6, label='优化的 Prompt')
            output_zh = gr.Textbox(lines=6, label='优化的 Prompt(zh)')
            with gr.Row():
                translate_btn = gr.Button('翻译')

                generate_prompter_btn = gr.Button('优化Prompt')

        with gr.Tab('从图片中生成'):
            with gr.Row():
                input_image = gr.Image(type='pil')
                exif_info = gr.HTML()
            output_blip_or_clip = gr.Textbox(label='生成的 Prompt', lines=4)
            output_w14 = gr.Textbox(label='W14的 Prompt', lines=4)

            with gr.Accordion('W14', open=False):
                w14_raw_output = gr.Textbox(label="Output (raw string)")
                w14_booru_output = gr.Textbox(label="Output (booru string)")
                w14_rating_output = gr.Label(label="Rating")
                w14_characters_output = gr.Label(label="Output (characters)")
                w14_tags_output = gr.Label(label="Output (tags)")
            output_img_prompter = gr.Textbox(lines=6, label='优化的 Prompt')
            output_img_prompter_zh = gr.Textbox(lines=6, label='优化的 Prompt(zh)')
            with gr.Row():
                img_exif_btn = gr.Button('EXIF')
                img_blip_btn = gr.Button('BLIP图片转描述')
                img_w14_btn = gr.Button('W14图片转描述')
                img_clip_btn = gr.Button('CLIP图片转描述')
                img_prompter_btn = gr.Button('优化Prompt')

        with gr.Tab('参数设置'):
            with gr.Accordion('Prompt优化参数', open=True):
                prompt_mode_name = gr.Radio(
                    [
                        'microsoft',
                        'mj',
                        'gpt2_650k',
                    ],
                    value='gpt2_650k',
                    label='model_name'
                )
                prompt_min_length = gr.Slider(1, 512, 100, label='min_length', step=1)
                prompt_max_length = gr.Slider(1, 512, 200, label='max_length', step=1)
                prompt_num_return_sequences = gr.Slider(1, 30, 6, label='num_return_sequences', step=1)

            with gr.Accordion('BLIP参数', open=True):
                blip_max_length = gr.Slider(1, 512, 100, label='max_length', step=1)
            with gr.Accordion('CLIP参数', open=True):
                clip_mode_type = gr.Radio(['best', 'classic', 'fast', 'negative'], value='best', label='mode_type')
                clip_model_name = gr.Radio(['vit_h_14', 'vit_l_14', ], value='vit_h_14', label='model_name')
            with gr.Accordion('WD14参数', open=True):
                image2text_model = gr.Radio(
                    [
                        "SwinV2",
                        "ConvNext",
                        "ConvNextV2",
                        "ViT",
                    ],
                    value="ConvNextV2",
                    label="Model"
                )
                general_threshold = gr.Slider(
                    0,
                    1,
                    step=0.05,
                    value=0.35,
                    label="General Tags Threshold",
                )
                character_threshold = gr.Slider(
                    0,
                    1,
                    step=0.05,
                    value=0.85,
                    label="Character Tags Threshold",
                )
    img_prompter_btn.click(
        fn=image_generate_prompter,
        inputs=[
            output_blip_or_clip,
            output_w14,
            prompt_mode_name,
            prompt_min_length,
            prompt_max_length,
            prompt_num_return_sequences,

        ],
        outputs=[output_img_prompter, output_img_prompter_zh]
    )
    translate_btn.click(
        fn=translate_zh2en,
        inputs=input_text,
        outputs=translate_output
    )

    generate_prompter_btn.click(
        fn=text_generate_prompter,
        inputs=[
            translate_output,
            prompt_mode_name,
            prompt_min_length,
            prompt_max_length,
            prompt_num_return_sequences,
        ],
        outputs=[output, output_zh]
    )
    img_w14_btn.click(
        fn=w14_image2text,
        inputs=[input_image, image2text_model, general_threshold, character_threshold],
        outputs=[
            output_w14,
            w14_raw_output,
            w14_booru_output,
            w14_rating_output,
            w14_characters_output,
            w14_tags_output
        ]
    )

    img_blip_btn.click(
        fn=git_image2text,
        inputs=[input_image, blip_max_length],
        outputs=output_blip_or_clip
    )
    img_clip_btn.click(
        fn=clip_image2text,
        inputs=[input_image, clip_mode_type, clip_model_name],
        outputs=output_blip_or_clip
    )

    img_exif_btn.click(
        fn=get_image_info,
        inputs=input_image,
        outputs=exif_info
    )
block.queue(max_size=64).launch(show_api=False, enable_queue=True, debug=True, share=False, server_name='0.0.0.0')